aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/binaryprefix/BinaryScaledAmountTestCase.java
blob: 58f08518a217115e08bc78b01969172b8c4b2c59 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.binaryprefix;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
 * @author Tony Vaagenes
 */
public class BinaryScaledAmountTestCase {

    @Test
    public void testConversion() {
        BinaryScaledAmount oneMeg = new BinaryScaledAmount(1024, BinaryPrefix.kilo);

        assertEquals(1, oneMeg.as(BinaryPrefix.mega));
        assertEquals(1024, oneMeg.as(BinaryPrefix.kilo));
        assertEquals(1024*1024, oneMeg.as(BinaryPrefix.unit));
        assertEquals(1 << 20, oneMeg.hashCode());

        Object v = this;
        assertEquals(false, oneMeg.equals(v));
        v = new BinaryScaledAmount(1, BinaryPrefix.mega);
        assertEquals(true, oneMeg.equals(v));
    }

    @Test
    public void testSymbols() {
        BinaryScaledAmount oneMeg = new BinaryScaledAmount(1024, BinaryPrefix.kilo);

        assertEquals(1, oneMeg.as(BinaryPrefix.fromSymbol('M')));
        assertEquals(1024, oneMeg.as(BinaryPrefix.fromSymbol('K')));

        boolean ex = false;
        try {
            BinaryPrefix invalid = BinaryPrefix.fromSymbol('q');
        } catch (RuntimeException e) {
            ex = true;
        }
        assertEquals(true, ex);
    }

}