MCPcopy Index your code
hub / github.com/EdwardRaff/JSAT / testParseDouble

Method testParseDouble

JSAT/test/jsat/utils/StringUtilsTest.java:83–150  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

81 }
82
83 @Test
84 public void testParseDouble()
85 {
86 System.out.println("parseDouble");
87 Random rand = new Random(42);
88
89 String[] signOps = new String[]{"+", "-", ""};
90 String[] Es = new String[]{"e", "E"};
91 String[] zeros = new String[]{"","", "", "0", "00", "000", "0000"};
92
93 double truth, attempt;
94 String toTest;
95
96 for (int trials = 0; trials < 10000; trials++)
97 {
98 String preFix = "";
99 String postFix = "";
100
101 int prefixSize = 0;//rand.nextInt(3);
102 int postFixSize = 0;//rand.nextInt(3);
103 for (int i = 0; i < prefixSize; i++)
104 preFix += Character.toString((char) rand.nextInt(128));
105 for (int i = 0; i < postFixSize; i++)
106 postFix += Character.toString((char) rand.nextInt(128));
107
108 //easy cases that should all be exact
109
110 //[sign][val]
111 toTest = signOps[rand.nextInt(3)] + zeros[rand.nextInt(zeros.length)] + rand.nextInt((int) Math.round(Math.pow(10, rand.nextInt(6)))) + "" + rand.nextInt((int) Math.round(Math.pow(10, rand.nextInt(6))));
112 truth = Double.parseDouble(toTest);
113 attempt = StringUtils.parseDouble(toTest, 0, toTest.length());
114 assertRelativeEquals(truth, attempt);
115
116 //[sign][val].[val]
117 toTest = signOps[rand.nextInt(3)] + zeros[rand.nextInt(zeros.length)] + rand.nextInt((int) Math.round(Math.pow(10, rand.nextInt(6)))) + "." + zeros[rand.nextInt(zeros.length)] + rand.nextInt((int) Math.round(Math.pow(10, rand.nextInt(6))));
118 truth = Double.parseDouble(toTest);
119 attempt = StringUtils.parseDouble(toTest, 0, toTest.length());
120 assertRelativeEquals(truth, attempt);
121
122 //[sign][val][eE][sign][val]
123 toTest = signOps[rand.nextInt(3)] + zeros[rand.nextInt(zeros.length)] + rand.nextInt((int) Math.round(Math.pow(10, rand.nextInt(6)))) + Es[rand.nextInt(2)] + signOps[rand.nextInt(3)] + zeros[rand.nextInt(zeros.length)] + rand.nextInt((int) Math.round(Math.pow(10, rand.nextInt(2))));
124 truth = Double.parseDouble(toTest);
125 attempt = StringUtils.parseDouble(toTest, 0, toTest.length());
126 assertRelativeEquals(truth, attempt);
127
128 //harder cases, may have nonsense values (over flow to Inf/NegInf, underflows to 0)
129
130 //[sign][val]
131 //XXX This code generates a random signed integer and then computes the absolute value of that random integer. If the number returned by the random number generator is Integer.MIN_VALUE, then the result will be negative as well (since Math.abs(Integer.MIN_VALUE) == Integer.MIN_VALUE). (Same problem arised for long values as well).
132 toTest = signOps[rand.nextInt(3)].replace("-", "") + zeros[rand.nextInt(zeros.length)] + Math.max(Math.abs(rand.nextLong()), 0);
133 truth = Double.parseDouble(toTest);
134 attempt = StringUtils.parseDouble(toTest, 0, toTest.length());
135 assertRelativeEquals(truth, attempt);
136
137 //[sign][val].[val]
138 toTest = signOps[rand.nextInt(3)] + rand.nextInt(Integer.MAX_VALUE) + zeros[rand.nextInt(zeros.length)] + "." + zeros[rand.nextInt(zeros.length)] + rand.nextInt(Integer.MAX_VALUE) ;
139 truth = Double.parseDouble(toTest);
140 attempt = StringUtils.parseDouble(toTest, 0, toTest.length());

Callers

nothing calls this directly

Calls 7

parseDoubleMethod · 0.95
assertRelativeEqualsMethod · 0.95
toStringMethod · 0.65
powMethod · 0.45
lengthMethod · 0.45
maxMethod · 0.45
nextLongMethod · 0.45

Tested by

no test coverage detected