summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/binaryprefix/BinaryScaledAmountTestCase.java
blob: bfb36ec80ce00a6e5a012f9f70db90a960f023a4 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.binaryprefix;

import junit.framework.TestCase;

/**
 * @author tonytv
 */
public class BinaryScaledAmountTestCase extends TestCase {
    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));
    }

    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);
    }
}