66
77import java .io .ByteArrayInputStream ;
88import java .io .IOException ;
9+ //import java.math.BigDecimal;
10+ //import java.math.BigInteger;
911
1012public class TestInteger extends TestCase {
1113
@@ -21,25 +23,29 @@ public void test_char() throws IOException {
2123 }
2224
2325 public void test_positive_negative_int () throws IOException {
26+ assertEquals (0 , parseInt ("0" ));
2427 assertEquals (4321 , parseInt ("4321" ));
2528 assertEquals (54321 , parseInt ("54321" ));
2629 assertEquals (654321 , parseInt ("654321" ));
2730 assertEquals (7654321 , parseInt ("7654321" ));
2831 assertEquals (87654321 , parseInt ("87654321" ));
2932 assertEquals (987654321 , parseInt ("987654321" ));
33+ assertEquals (2147483647 , parseInt ("2147483647" ));
3034 assertEquals (-4321 , parseInt ("-4321" ));
35+ assertEquals (-2147483648 , parseInt ("-2147483648" ));
3136 }
3237
3338 public void test_positive_negative_long () throws IOException {
3439 assertEquals (0L , parseLong ("0" ));
35- assertEquals (1L , parseLong ("01" ));
3640 assertEquals (4321L , parseLong ("4321" ));
3741 assertEquals (54321L , parseLong ("54321" ));
3842 assertEquals (654321L , parseLong ("654321" ));
3943 assertEquals (7654321L , parseLong ("7654321" ));
4044 assertEquals (87654321L , parseLong ("87654321" ));
4145 assertEquals (987654321L , parseLong ("987654321" ));
46+ assertEquals (9223372036854775807L , parseLong ("9223372036854775807" ));
4247 assertEquals (-4321L , parseLong ("-4321" ));
48+ assertEquals (-9223372036854775808L , parseLong ("-9223372036854775808" ));
4349 }
4450
4551 public void test_max_min_int () throws IOException {
@@ -88,20 +94,47 @@ public void test_streaming() throws IOException {
8894
8995 public void test_leading_zero () throws IOException {
9096 try {
91- JsonIterator .deserialize ("001 " , int .class );
97+ JsonIterator .deserialize ("01 " , int .class );
9298 fail ();
9399 } catch (JsonException e ) {
94100 }
95101 try {
96- JsonIterator .deserialize ("001 " , long .class );
102+ JsonIterator .deserialize ("02147483647 " , int .class );
97103 fail ();
98104 } catch (JsonException e ) {
99105 }
100106 try {
101- JsonIterator .deserialize ("001" );
107+ JsonIterator .deserialize ("01" , long . class );
102108 fail ();
103109 } catch (JsonException e ) {
104110 }
111+ try {
112+ JsonIterator .deserialize ("09223372036854775807" , long .class );
113+ fail ();
114+ } catch (JsonException e ) {
115+ }
116+ /* FIXME if we should fail on parsing of leading zeroes for other numbers
117+ try {
118+ JsonIterator.deserialize("01", double.class);
119+ fail();
120+ } catch (JsonException e) {
121+ }
122+ try {
123+ JsonIterator.deserialize("01", float.class);
124+ fail();
125+ } catch (JsonException e) {
126+ }
127+ try {
128+ JsonIterator.deserialize("01", BigInteger.class);
129+ fail();
130+ } catch (JsonException e) {
131+ }
132+ try {
133+ JsonIterator.deserialize("01", BigDecimal.class);
134+ fail();
135+ } catch (JsonException e) {
136+ }
137+ */
105138 }
106139
107140 public void test_max_int () throws IOException {
@@ -116,7 +149,9 @@ private int parseInt(String input) throws IOException {
116149 return iter .readInt ();
117150 } else {
118151 JsonIterator iter = JsonIterator .parse (input );
119- return iter .readInt ();
152+ int v = iter .readInt ();
153+ assertEquals (input .length (), iter .head ); // iterator head should point on next non-parsed byte
154+ return v ;
120155 }
121156 }
122157
@@ -126,7 +161,9 @@ private long parseLong(String input) throws IOException {
126161 return iter .readLong ();
127162 } else {
128163 JsonIterator iter = JsonIterator .parse (input );
129- return iter .readLong ();
164+ long v = iter .readLong ();
165+ assertEquals (input .length (), iter .head ); // iterator head should point on next non-parsed byte
166+ return v ;
130167 }
131168 }
132169}
0 commit comments