3239 |
11 Apr 07 |
nicklas |
1 |
/* |
3239 |
11 Apr 07 |
nicklas |
$Id$ |
3239 |
11 Apr 07 |
nicklas |
3 |
|
3239 |
11 Apr 07 |
nicklas |
BioArray Software Environment (BASE) - http://base.thep.lu.se/ |
3675 |
16 Aug 07 |
jari |
Copyright (C) 2007 Johan Enell, Nicklas Nordborg |
3239 |
11 Apr 07 |
nicklas |
6 |
|
3239 |
11 Apr 07 |
nicklas |
This file is part of BASE. |
3239 |
11 Apr 07 |
nicklas |
8 |
|
3239 |
11 Apr 07 |
nicklas |
BASE is free software; you can redistribute it and/or |
3239 |
11 Apr 07 |
nicklas |
modify it under the terms of the GNU General Public License |
4480 |
05 Sep 08 |
jari |
as published by the Free Software Foundation; either version 3 |
3239 |
11 Apr 07 |
nicklas |
of the License, or (at your option) any later version. |
3239 |
11 Apr 07 |
nicklas |
13 |
|
3239 |
11 Apr 07 |
nicklas |
BASE is distributed in the hope that it will be useful, |
3239 |
11 Apr 07 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
3239 |
11 Apr 07 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3239 |
11 Apr 07 |
nicklas |
GNU General Public License for more details. |
3239 |
11 Apr 07 |
nicklas |
18 |
|
3239 |
11 Apr 07 |
nicklas |
You should have received a copy of the GNU General Public License |
4514 |
11 Sep 08 |
jari |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
3239 |
11 Apr 07 |
nicklas |
21 |
*/ |
3239 |
11 Apr 07 |
nicklas |
22 |
import net.sf.basedb.core.Type; |
5776 |
30 Sep 11 |
nicklas |
23 |
import net.sf.basedb.util.EqualsHelper; |
3239 |
11 Apr 07 |
nicklas |
24 |
import net.sf.basedb.util.NumberFormatUtil; |
3239 |
11 Apr 07 |
nicklas |
25 |
|
3239 |
11 Apr 07 |
nicklas |
26 |
public class TestNumberFormat |
3239 |
11 Apr 07 |
nicklas |
27 |
{ |
3239 |
11 Apr 07 |
nicklas |
28 |
|
3239 |
11 Apr 07 |
nicklas |
29 |
static boolean ok = true; |
3239 |
11 Apr 07 |
nicklas |
30 |
public static void main(String[] args) |
3239 |
11 Apr 07 |
nicklas |
31 |
{ |
3239 |
11 Apr 07 |
nicklas |
32 |
TestUtil.checkArgs(args); |
3239 |
11 Apr 07 |
nicklas |
33 |
TestUtil.begin(); |
3239 |
11 Apr 07 |
nicklas |
34 |
ok = test_all(); |
3239 |
11 Apr 07 |
nicklas |
35 |
TestUtil.stop(); |
3239 |
11 Apr 07 |
nicklas |
36 |
} |
3239 |
11 Apr 07 |
nicklas |
37 |
|
3239 |
11 Apr 07 |
nicklas |
38 |
static boolean test_all() |
3239 |
11 Apr 07 |
nicklas |
39 |
{ |
3239 |
11 Apr 07 |
nicklas |
40 |
write("++Testing Number parser"); |
3239 |
11 Apr 07 |
nicklas |
41 |
|
3239 |
11 Apr 07 |
nicklas |
42 |
java.text.NumberFormat dotFormat = NumberFormatUtil.getNumberFormat('.', ';'); |
5458 |
29 Oct 10 |
nicklas |
43 |
float resultPositive = 123.456f; |
5458 |
29 Oct 10 |
nicklas |
44 |
float resultNegative = -123.456f; |
3239 |
11 Apr 07 |
nicklas |
45 |
|
5458 |
29 Oct 10 |
nicklas |
// Positive numbers |
5460 |
29 Oct 10 |
nicklas |
47 |
final boolean SHOULD_FAIL = true; |
5460 |
29 Oct 10 |
nicklas |
48 |
final boolean SHOULD_PASS = false; |
5460 |
29 Oct 10 |
nicklas |
49 |
test_parse_number(dotFormat, "123.456", resultPositive, SHOULD_PASS); |
5460 |
29 Oct 10 |
nicklas |
50 |
test_parse_number(dotFormat, "1.23456E2", resultPositive, SHOULD_PASS); |
5460 |
29 Oct 10 |
nicklas |
51 |
test_parse_number(dotFormat, "1.23456E+2", resultPositive, SHOULD_PASS); |
5460 |
29 Oct 10 |
nicklas |
52 |
test_parse_number(dotFormat, "1234.56E-1", resultPositive, SHOULD_PASS); |
5460 |
29 Oct 10 |
nicklas |
53 |
test_parse_number(dotFormat, "1.23456e2", resultPositive, SHOULD_PASS); |
5460 |
29 Oct 10 |
nicklas |
54 |
test_parse_number(dotFormat, "1.23456e+2", resultPositive, SHOULD_PASS); |
5460 |
29 Oct 10 |
nicklas |
55 |
test_parse_number(dotFormat, "1234.56e-1", resultPositive, SHOULD_PASS); |
5460 |
29 Oct 10 |
nicklas |
56 |
test_parse_number(dotFormat, "123.456E0", resultPositive, SHOULD_PASS); |
5460 |
29 Oct 10 |
nicklas |
57 |
test_parse_number(dotFormat, "123.456E+0", resultPositive, SHOULD_PASS); |
5460 |
29 Oct 10 |
nicklas |
58 |
test_parse_number(dotFormat, "123.456E-0", resultPositive, SHOULD_PASS); |
5460 |
29 Oct 10 |
nicklas |
59 |
test_parse_number(dotFormat, "123.456e0", resultPositive, SHOULD_PASS); |
5460 |
29 Oct 10 |
nicklas |
60 |
test_parse_number(dotFormat, "123.456e+0", resultPositive, SHOULD_PASS); |
5460 |
29 Oct 10 |
nicklas |
61 |
test_parse_number(dotFormat, "123.456e-0", resultPositive, SHOULD_PASS); |
5458 |
29 Oct 10 |
nicklas |
62 |
|
5458 |
29 Oct 10 |
nicklas |
// Negative numbers |
5460 |
29 Oct 10 |
nicklas |
64 |
test_parse_number(dotFormat, "-123.456", resultNegative, SHOULD_PASS); |
5460 |
29 Oct 10 |
nicklas |
65 |
test_parse_number(dotFormat, "-1.23456E2", resultNegative, SHOULD_PASS); |
5460 |
29 Oct 10 |
nicklas |
66 |
test_parse_number(dotFormat, "-1.23456E+2", resultNegative, SHOULD_PASS); |
5460 |
29 Oct 10 |
nicklas |
67 |
test_parse_number(dotFormat, "-1234.56E-1", resultNegative, SHOULD_PASS); |
5460 |
29 Oct 10 |
nicklas |
68 |
test_parse_number(dotFormat, "-1.23456e2", resultNegative, SHOULD_PASS); |
5460 |
29 Oct 10 |
nicklas |
69 |
test_parse_number(dotFormat, "-1.23456e+2", resultNegative, SHOULD_PASS); |
5460 |
29 Oct 10 |
nicklas |
70 |
test_parse_number(dotFormat, "-1234.56e-1", resultNegative, SHOULD_PASS); |
5460 |
29 Oct 10 |
nicklas |
71 |
test_parse_number(dotFormat, "-123.456E0", resultNegative, SHOULD_PASS); |
5460 |
29 Oct 10 |
nicklas |
72 |
test_parse_number(dotFormat, "-123.456E+0", resultNegative, SHOULD_PASS); |
5460 |
29 Oct 10 |
nicklas |
73 |
test_parse_number(dotFormat, "-123.456E-0", resultNegative, SHOULD_PASS); |
5460 |
29 Oct 10 |
nicklas |
74 |
test_parse_number(dotFormat, "-123.456e0", resultNegative, SHOULD_PASS); |
5460 |
29 Oct 10 |
nicklas |
75 |
test_parse_number(dotFormat, "-123.456e+0", resultNegative, SHOULD_PASS); |
5460 |
29 Oct 10 |
nicklas |
76 |
test_parse_number(dotFormat, "-123.456e-0", resultNegative, SHOULD_PASS); |
5458 |
29 Oct 10 |
nicklas |
77 |
|
5458 |
29 Oct 10 |
nicklas |
// Invalid decimal separator |
5776 |
30 Sep 11 |
nicklas |
79 |
test_parse_number(dotFormat, "123,456", null, SHOULD_FAIL); |
5458 |
29 Oct 10 |
nicklas |
80 |
|
3239 |
11 Apr 07 |
nicklas |
81 |
java.text.NumberFormat decFormat = NumberFormatUtil.getNumberFormat(',', ';'); |
5460 |
29 Oct 10 |
nicklas |
82 |
test_parse_number(decFormat, "123,456", resultPositive, SHOULD_PASS); |
5460 |
29 Oct 10 |
nicklas |
83 |
test_parse_number(decFormat, "1,23456E2", resultPositive, SHOULD_PASS); |
5460 |
29 Oct 10 |
nicklas |
84 |
test_parse_number(decFormat, "1234,56e-1", resultPositive, SHOULD_PASS); |
5776 |
30 Sep 11 |
nicklas |
85 |
test_parse_number(decFormat, "123.456", null, SHOULD_FAIL); |
3239 |
11 Apr 07 |
nicklas |
86 |
|
5776 |
30 Sep 11 |
nicklas |
// Special values |
5776 |
30 Sep 11 |
nicklas |
88 |
test_parse_number(dotFormat, "NaN", null, SHOULD_PASS); |
5776 |
30 Sep 11 |
nicklas |
89 |
|
3239 |
11 Apr 07 |
nicklas |
90 |
write("++Testing Number parser "+(ok ? "OK" : "Failed")+"\n"); |
3239 |
11 Apr 07 |
nicklas |
91 |
return ok; |
3239 |
11 Apr 07 |
nicklas |
92 |
} |
3239 |
11 Apr 07 |
nicklas |
93 |
|
3239 |
11 Apr 07 |
nicklas |
94 |
static void write(String message) |
3239 |
11 Apr 07 |
nicklas |
95 |
{ |
3239 |
11 Apr 07 |
nicklas |
96 |
System.out.println(message); |
3239 |
11 Apr 07 |
nicklas |
97 |
} |
3239 |
11 Apr 07 |
nicklas |
98 |
|
5776 |
30 Sep 11 |
nicklas |
99 |
static void test_parse_number(java.text.NumberFormat format, String value, Float expectedResult, boolean shouldFail) |
3239 |
11 Apr 07 |
nicklas |
100 |
{ |
3239 |
11 Apr 07 |
nicklas |
101 |
try |
3239 |
11 Apr 07 |
nicklas |
102 |
{ |
3472 |
11 Jun 07 |
nicklas |
103 |
Float fValue = (Float)Type.FLOAT.parseString(value, format, false); |
3239 |
11 Apr 07 |
nicklas |
104 |
if (shouldFail) |
3239 |
11 Apr 07 |
nicklas |
105 |
{ |
5460 |
29 Oct 10 |
nicklas |
106 |
throw new Exception("Parsing '" + value + "' should fail; got: " + fValue + "f"); |
3239 |
11 Apr 07 |
nicklas |
107 |
} |
5776 |
30 Sep 11 |
nicklas |
108 |
if (!EqualsHelper.equals(expectedResult, fValue)) |
5458 |
29 Oct 10 |
nicklas |
109 |
{ |
5460 |
29 Oct 10 |
nicklas |
110 |
throw new Exception("Parsing '" + value + "' --> " + fValue + "f <> " + expectedResult + " (expected)"); |
5458 |
29 Oct 10 |
nicklas |
111 |
} |
5460 |
29 Oct 10 |
nicklas |
112 |
write("--Parse string '" + value + "' --> " + fValue + "f OK"); |
3239 |
11 Apr 07 |
nicklas |
113 |
} |
3239 |
11 Apr 07 |
nicklas |
114 |
catch (NumberFormatException ex) |
3239 |
11 Apr 07 |
nicklas |
115 |
{ |
3239 |
11 Apr 07 |
nicklas |
116 |
if (!shouldFail) |
3239 |
11 Apr 07 |
nicklas |
117 |
{ |
5460 |
29 Oct 10 |
nicklas |
118 |
write("--Parse string '" + value + "' --> " + expectedResult + "f FAILED"); |
3239 |
11 Apr 07 |
nicklas |
119 |
ex.printStackTrace(); |
3239 |
11 Apr 07 |
nicklas |
120 |
ok = false; |
3239 |
11 Apr 07 |
nicklas |
121 |
} |
3239 |
11 Apr 07 |
nicklas |
122 |
else |
3239 |
11 Apr 07 |
nicklas |
123 |
{ |
5460 |
29 Oct 10 |
nicklas |
124 |
write("--Parse string '" + value + "' failed conversion as expected: " + ex.getMessage()); |
3239 |
11 Apr 07 |
nicklas |
125 |
} |
3239 |
11 Apr 07 |
nicklas |
126 |
} |
3239 |
11 Apr 07 |
nicklas |
127 |
catch (Throwable t) |
3239 |
11 Apr 07 |
nicklas |
128 |
{ |
5460 |
29 Oct 10 |
nicklas |
129 |
write("--Parse string '" + value + "' --> " + expectedResult + "f FAILED"); |
3239 |
11 Apr 07 |
nicklas |
130 |
t.printStackTrace(); |
3239 |
11 Apr 07 |
nicklas |
131 |
ok = false; |
3239 |
11 Apr 07 |
nicklas |
132 |
} |
3239 |
11 Apr 07 |
nicklas |
133 |
} |
3239 |
11 Apr 07 |
nicklas |
134 |
|
3239 |
11 Apr 07 |
nicklas |
135 |
} |
3239 |
11 Apr 07 |
nicklas |
136 |
|