aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/text/DoubleParserTestCase.java
blob: a48a1b6a9434e013f532a3eaa32ea2a74c1ae497 (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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.text;

import org.junit.Test;
import static org.junit.Assert.assertEquals;

@SuppressWarnings("deprecation")
/**
 * @author arnej27959
 */
public class DoubleParserTestCase {

    @Test
    public void testZero() {
        String[] zeros = {
            "0",
            "0.",
            ".0",
            "0.0",
            "0.0e0",
            "0.0e99",
            "0.0e+300",
            "0.0e-42"
        };
        for (String s : zeros) {
            double d = DoubleParser.parse(s);
            assertEquals(0.0, d, 0);
        }
    }

    @Test
    public void testOne() {
        String[] ones = {
            "1",
            "1.",
            "1.0",
            "+1",
            "10.0e-1",
            "0.1e1",
            "1000.0e-3",
            ".001e+3",
        };
        for (String s : ones) {
            System.out.println("parsing: '"+s+"' now");
            double d = DoubleParser.parse(s);
            System.out.println("expected: 1.0");
            System.out.println("actual: "+d);
            assertEquals(1.0, d, 0);
        }
    }

    @Test
    public void testMinusOne() {
        String[] numbers = {
            "-1",
            "-1.0",
            "-1.",
            "-1e0",
            "-10e-1",
        };
        for (String s : numbers) {
            System.out.println("parsing: '"+s+"' now");
            double d = DoubleParser.parse(s);
            System.out.println("expected: -1.0");
            System.out.println("actual: "+d);
            assertEquals(-1.0, d, 0);
        }
    }

    @Test
    public void testNanInf() {
        String[] numbers = {
            "NaN",
            "Infinity",
            "-Infinity",
            "+Infinity",
            "+NaN",
            "-NaN"
        };
        for (String s : numbers) {
            System.out.println("parsing: '"+s+"' now");
            double d1 = Double.parseDouble(s);
            double d2 = DoubleParser.parse(s);
            long lb1 = Double.doubleToRawLongBits(d1);
            long lb2 = Double.doubleToRawLongBits(d2);
            assertEquals(lb1, lb2, 0);
        }
    }

    @Test
    public void testSeven() {
        String[] sevens = {
            "7",
            "7.",
            "7.0",
            "70.0e-1",
            "0.7e1",
            "7000.0e-3",
            ".007e+3",
        };
        for (String s : sevens) {
            System.out.println("parsing: '"+s+"' now");
            double d = DoubleParser.parse(s);
            System.out.println("expected: 7.0");
            System.out.println("actual: "+d);
            assertEquals(7.0, d, 0);
        }
    }

    @Test
    public void testVerySmallNumbers() {
        String[] numbers = {
            "1.e-320",
            "-1.e-320",
            "1.0013378241589014e-303"
        };
        for (String s : numbers) {
            System.out.println("parsing: '"+s+"' now");
            double d1 = Double.parseDouble(s);
            double d2 = DoubleParser.parse(s);
            System.out.println("expected: "+d1);
            System.out.println("actual: "+d2);
            assertEquals(d1, d2, 0);
        }
    }

    @Test
    public void testRandomNumbers() {
        java.util.Random rgen = new java.util.Random(0xCafeBabe);
        for (int i = 0; i < 123456; i++) {
            double d = rgen.nextDouble();
            int exp = rgen.nextInt();
            d *= Math.pow(1.0000006, exp);
            String s = Double.toString(d);
            double d2 = Double.parseDouble(s);
            double d3 = DoubleParser.parse(s);

            if (d != d2) {
                System.out.println("WARNING: value ["+d+"] parses as ["+d2+"] by Java");
            }
            double allow = 1.0e-14 * d2;
            if (allow < 0) {
                allow = -allow;
            }
            if (d2 != d3) {
                long lb2 = Double.doubleToRawLongBits(d2);
                long lb3 = Double.doubleToRawLongBits(d3);
                if (lb2 - lb3 > 15 || lb3 - lb2 > 15) {
                    System.out.println("WARNING: string '"+s+"' parses as");
                    System.out.println("["+d2+"] by Java, ["+d3+"] by our method");
                    System.out.println("["+Long.toHexString(lb2)+"] bits vs ["+Long.toHexString(lb3)+"]");
                    System.out.println("==> "+(lb2 - lb3)+" <== diff value");
                }
            }
            assertEquals(d2, d3, allow);
        }
    }

}