()
| 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()); |
nothing calls this directly
no test coverage detected