aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/anarres/cpp/NumericValueTest.java
blob: d4ea432d57b2d520d1f10abe90c4bc3fb2b59328 (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package org.anarres.cpp;

import java.io.IOException;
import org.junit.Test;
import static org.anarres.cpp.Token.*;
import static org.junit.Assert.*;

/**
 *
 * @author shevek
 */
public class NumericValueTest {

    private Token testNumericValue(String in) throws IOException, LexerException {
        StringLexerSource s = new StringLexerSource(in);

        Token tok = s.token();
        System.out.println("Token is " + tok);
        assertEquals(NUMBER, tok.getType());

        Token eof = s.token();
        assertEquals("Didn't get EOF, but " + tok, EOF, eof.getType());

        return tok;
    }

    private void testNumericValue(String in, double out) throws IOException, LexerException {
        System.out.println("Testing '" + in + "' -> " + out);
        Token tok = testNumericValue(in);
        assertEquals(in, tok.getText());
        NumericValue value = (NumericValue) tok.getValue();
        assertEquals("Double mismatch", out, value.doubleValue(), 0.01d);
        assertEquals("Float mismatch", (float) out, value.floatValue(), 0.01f);
        assertEquals("Long mismatch", (long) out, value.longValue());
        assertEquals("Integer mismatch", (int) out, value.intValue());
    }

    @Test
    public void testNumericValue() throws Exception {

        // Zero
        testNumericValue("0", 0);

        // Decimal
        testNumericValue("1", 1);
        testNumericValue("1L", 1);
        testNumericValue("12", 12);
        testNumericValue("12L", 12);

        // Hex
        testNumericValue("0xf", 0xf);
        testNumericValue("0xfL", 0xf);
        testNumericValue("0x12", 0x12);
        testNumericValue("0x12L", 0x12);

        // Negative
        // testNumericValue("-0", 0);
        // testNumericValue("-1", -1);
        // Negative hex
        // testNumericValue("-0x56", -0x56);
        // testNumericValue("-0x102", -0x102);
        // Octal and negative octal
        testNumericValue("0673", Integer.parseInt("673", 8));
        // testNumericValue("-0673", Integer.parseInt("-673", 8));

        // Floating point
        testNumericValue(".0", 0);
        testNumericValue(".00", 0);
        testNumericValue("0.", 0);
        testNumericValue("0.0", 0);
        testNumericValue("00.0", 0);
        testNumericValue("00.", 0);

        // Sign on exponents
        testNumericValue("1e1", 1e1);
        // testNumericValue("-1e1", -1e1);
        testNumericValue("1e-1", 1e-1);

        // Hex numbers with decimal exponents
        testNumericValue("0x12e3", 0x12e3);
        testNumericValue("0x12p3", 0x12p3);

        // Octal numbers with decimal exponents
        testNumericValue("012e3", 012e3);    // Fails
        testNumericValue("067e4", 067e4);    // Fails

        // Issues a warning.
        try {
            testNumericValue("097", 97);
            fail("No warning.");
        } catch (LexerException e) {
        }

    }
}