1+ package processing .data ;
2+
3+ import org .junit .Before ;
4+ import org .junit .Test ;
5+
6+ import static org .junit .Assert .*;
7+
8+ public class JSONObjectTest {
9+
10+ private JSONObject obj ;
11+
12+ @ Before
13+ public void setUp () {
14+ obj = new JSONObject ();
15+ }
16+
17+ // Verify set and get long methods work as intended
18+ @ Test
19+ public void testSetAndGetLong () {
20+ long value = 9223372036854775807L ;
21+ long defaultValue = 1234567890123456789L ;
22+
23+ obj .setLong ("long_key" , value );
24+
25+ assertEquals (value , obj .getLong ("long_key" ));
26+ assertEquals (defaultValue , obj .getLong ("long_key_not_present" , defaultValue ));
27+ }
28+
29+ // Verify set and get float methods work as intended
30+ @ Test
31+ public void testSetAndGetFloat () {
32+ float value = 3.14159f ;
33+ float defaultValue = 1234567890.123456789f ;
34+
35+ obj .setFloat ("float_key" , value );
36+
37+ assertEquals (value , obj .getFloat ("float_key" ), 0.0001f );
38+ assertEquals (defaultValue , obj .getFloat ("float_key_not_present" , defaultValue ), 0.0001f );
39+ }
40+
41+ // Verify set and get double methods work as intended
42+ @ Test
43+ public void testSetAndGetDouble () {
44+ double value = 3.14159265359 ;
45+ double defaultValue = 1234567890.123456789 ;
46+
47+ obj .setDouble ("double_key" , value );
48+
49+ assertEquals (value , obj .getDouble ("double_key" ), 0.0000001 );
50+ assertEquals (defaultValue , obj .getDouble ("double_key_not-present" , defaultValue ), 0.0000001 );
51+ }
52+
53+ // Verify set and get int methods work as intended
54+ @ Test
55+ public void testSetAndGetInt () {
56+ int value = 12 ;
57+ int defaultValue = 42 ;
58+
59+ obj .setInt ("int_key" , value );
60+
61+ assertEquals (value , obj .getInt ("int_key" ));
62+ assertEquals (defaultValue , obj .getInt ("int_key_not-present" , defaultValue ));
63+ }
64+
65+ // Verify set and get bool methods work as intended
66+ @ Test
67+ public void testSetAndGetBool () {
68+ boolean value = true ;
69+ boolean defaultValue = false ;
70+
71+ obj .setBoolean ("bool_key" , value );
72+
73+ assertEquals (value , obj .getBoolean ("bool_key" ));
74+ assertEquals (defaultValue , obj .getBoolean ("bool_key_not-present" , defaultValue ));
75+ }
76+
77+ // Verify set and get string methods work as intended
78+ @ Test
79+ public void testSetAndGetString () {
80+ String value = "Some string" ;
81+ String defaultValue = "Some default string" ;
82+
83+ obj .setString ("string_key" , value );
84+
85+ assertEquals (value , obj .getString ("string_key" ));
86+ assertEquals (defaultValue , obj .getString ("string_key_not-present" , defaultValue ));
87+ }
88+
89+ // Verify getInt method with non-existent key throws exception
90+ @ Test (expected = RuntimeException .class )
91+ public void testGetNonExistentKeyInt () {
92+ obj .getInt ("nonexistent" );
93+ }
94+
95+ // Verify getLong method with non-existent key throws exception
96+ @ Test (expected = RuntimeException .class )
97+ public void testGetNonExistentKeyLong () {
98+ obj .getLong ("nonexistent" );
99+ }
100+
101+ // Verify getFloat method with non-existent key throws exception
102+ @ Test (expected = RuntimeException .class )
103+ public void testGetNonExistentKeyFloat () {
104+ obj .getFloat ("nonexistent" );
105+ }
106+
107+ // Verify getDouble method with non-existent key throws exception
108+ @ Test (expected = RuntimeException .class )
109+ public void testGetNonExistentKeyDouble () {
110+ obj .getDouble ("nonexistent" );
111+ }
112+
113+ // Verify getBoolean method with non-existent key throws exception
114+ @ Test (expected = RuntimeException .class )
115+ public void testGetNonExistentKeyBoolean () {
116+ obj .getBoolean ("nonexistent" );
117+ }
118+
119+ // Verify getString method with non-existent returns null
120+ @ Test
121+ public void testGetNonExistentKeyString () {
122+ assertNull (obj .getString ("nonexistent" ));
123+ }
124+
125+ // Verify stringToValue with empty string works as intended
126+ @ Test
127+ public void testStringToValueEmptyString () {
128+ assertEquals ("" , JSONObject .stringToValue ("" ));
129+ }
130+
131+ // Verify stringToValue with boolean strings work as intended
132+ @ Test
133+ public void testStringToValueBoolean () {
134+ assertEquals (Boolean .TRUE , JSONObject .stringToValue ("true" ));
135+ assertEquals (Boolean .FALSE , JSONObject .stringToValue ("FALSE" ));
136+ assertEquals (Boolean .TRUE , JSONObject .stringToValue ("True" ));
137+ }
138+
139+ // Verify stringToValue with 'null' strings work as intended
140+ @ Test
141+ public void testStringToValueNull () {
142+ assertEquals (JSONObject .NULL , JSONObject .stringToValue ("null" ));
143+ assertEquals (JSONObject .NULL , JSONObject .stringToValue ("NULL" ));
144+ assertEquals (JSONObject .NULL , JSONObject .stringToValue ("Null" ));
145+ }
146+
147+ // Verify stringToValue with int strings work as intended
148+ @ Test
149+ public void testStringToValueInteger () {
150+ assertEquals (42 , JSONObject .stringToValue ("42" ));
151+ assertEquals (-17 , JSONObject .stringToValue ("-17" ));
152+ assertEquals (42 , JSONObject .stringToValue ("+42" ));
153+ assertEquals (Integer .MAX_VALUE , JSONObject .stringToValue ("2147483647" ));
154+ assertEquals (Integer .MIN_VALUE , JSONObject .stringToValue ("-2147483648" ));
155+ }
156+
157+ // Verify stringToValue with long strings work as intended
158+ @ Test
159+ public void testStringToValueLong () {
160+ assertEquals (2147483648L , JSONObject .stringToValue ("2147483648" ));
161+ assertEquals (-2147483649L , JSONObject .stringToValue ("-2147483649" ));
162+ assertEquals (Long .MAX_VALUE , JSONObject .stringToValue ("9223372036854775807" ));
163+ assertEquals (Long .MIN_VALUE , JSONObject .stringToValue ("-9223372036854775808" ));
164+ }
165+
166+ // Verify stringToValue with double strings work as intended
167+ @ Test
168+ public void testStringToValueDouble () {
169+ assertEquals (3.14 , JSONObject .stringToValue ("3.14" ));
170+ assertEquals (-0.5 , JSONObject .stringToValue ("-0.5" ));
171+ assertEquals (0.5 , JSONObject .stringToValue (".5" ));
172+ assertEquals (-0.5 , JSONObject .stringToValue ("-.5" ));
173+ assertEquals (0.5 , JSONObject .stringToValue ("+.5" ));
174+ }
175+
176+ // Verify stringToValue with scientific notation strings work as intended
177+ @ Test
178+ public void testStringToValueScientificNotation () {
179+ assertEquals (100000.0 , JSONObject .stringToValue ("1e5" ));
180+ assertEquals (100000.0 , JSONObject .stringToValue ("1E5" ));
181+ assertEquals (0.00001 , JSONObject .stringToValue ("1e-5" ));
182+ assertEquals (0.00001 , JSONObject .stringToValue ("1E-5" ));
183+ assertEquals (-100000.0 , JSONObject .stringToValue ("-1e5" ));
184+ assertEquals (100000.0 , JSONObject .stringToValue ("+1e5" ));
185+ }
186+
187+ // Verify stringToValue with special numbers work as intended
188+ @ Test
189+ public void testStringToValueSpecialNumbers () {
190+ assertEquals ("NaN" , JSONObject .stringToValue ("NaN" ));
191+ assertEquals ("Infinity" , JSONObject .stringToValue ("Infinity" ));
192+ assertEquals ("-Infinity" , JSONObject .stringToValue ("-Infinity" ));
193+ }
194+
195+ // Verify stringToValue with invalid numbers work as intended
196+ @ Test
197+ public void testStringToValueInvalidNumbers () {
198+ assertEquals ("0x123" , JSONObject .stringToValue ("0x123" )); // Hexadecimal
199+ assertEquals ("0b101" , JSONObject .stringToValue ("0b101" )); // Binary
200+ assertEquals ("1.2.3" , JSONObject .stringToValue ("1.2.3" )); // Multiple dots
201+ assertEquals ("1e2e3" , JSONObject .stringToValue ("1e2e3" )); // Multiple exponents
202+ assertEquals ("--1" , JSONObject .stringToValue ("--1" )); // Multiple signs
203+ assertEquals ("++1" , JSONObject .stringToValue ("++1" )); // Multiple signs
204+ assertEquals ("1e" , JSONObject .stringToValue ("1e" )); // Incomplete exponent
205+ assertEquals ("e1" , JSONObject .stringToValue ("e1" )); // Missing base
206+ assertEquals (".e1" , JSONObject .stringToValue (".e1" )); // Missing base with dot
207+ }
208+
209+ // Verify stringToValue with regular strings work as intended
210+ @ Test
211+ public void testStringToValueRegularStrings () {
212+ assertEquals ("hello" , JSONObject .stringToValue ("hello" ));
213+ assertEquals ("123abc" , JSONObject .stringToValue ("123abc" ));
214+ assertEquals ("true_false" , JSONObject .stringToValue ("true_false" ));
215+ assertEquals ("null_value" , JSONObject .stringToValue ("null_value" ));
216+ }
217+
218+ // Verify valueToString with null values work as intended
219+ @ Test
220+ public void testValueToStringNull () {
221+ assertEquals ("null" , JSONObject .valueToString (null ));
222+ assertEquals ("null" , JSONObject .valueToString (JSONObject .NULL ));
223+ }
224+
225+ // Verify valueToString with numbers work as intended
226+ @ Test
227+ public void testValueToStringNumbers () {
228+ assertEquals ("42" , JSONObject .valueToString (42 ));
229+ assertEquals ("-17" , JSONObject .valueToString (-17 ));
230+ assertEquals ("3.14159" , JSONObject .valueToString (3.14159 ));
231+ assertEquals ("1.23E-4" , JSONObject .valueToString (0.000123 ));
232+ assertEquals ("9223372036854775807" , JSONObject .valueToString (Long .MAX_VALUE ));
233+ }
234+
235+ // Verify valueToString with booleans work as intended
236+ @ Test
237+ public void testValueToStringBoolean () {
238+ assertEquals ("true" , JSONObject .valueToString (true ));
239+ assertEquals ("false" , JSONObject .valueToString (false ));
240+ assertEquals ("true" , JSONObject .valueToString (Boolean .TRUE ));
241+ assertEquals ("false" , JSONObject .valueToString (Boolean .FALSE ));
242+ }
243+
244+ // Verify valueToString with JSONArray objects work as intended
245+ @ Test
246+ public void testValueToStringJSONArray () {
247+ JSONArray arr = new JSONArray ();
248+ arr .append (1 );
249+ arr .append ("test" );
250+ assertEquals ("[\n 1,\n \" test\" \n ]" , JSONObject .valueToString (arr ));
251+ }
252+
253+ // Verify valueToString with array objects work as intended
254+ @ Test
255+ public void testValueToStringArray () {
256+ String [] arr = new String []{"test1" , "test2" };
257+ assertEquals ("[\n \" test1\" ,\n \" test2\" \n ]" , JSONObject .valueToString (arr ));
258+
259+ int [] intArr = new int []{1 , 2 , 3 };
260+ assertEquals ("[\n 1,\n 2,\n 3\n ]" , JSONObject .valueToString (intArr ));
261+ }
262+
263+ // Verify valueToString with escaped characters work as intended
264+ @ Test
265+ public void testValueToStringRegularObject () {
266+ assertEquals ("\" hello\" " , JSONObject .valueToString ("hello" ));
267+ assertEquals ("\" special\\ \" quote\\ \" \" " ,
268+ JSONObject .valueToString ("special\" quote\" " ));
269+ assertEquals ("\" tab\\ tand\\ nnewline\" " ,
270+ JSONObject .valueToString ("tab\t and\n newline" ));
271+ }
272+
273+ // Verify valueToString with invalid numbers throw exception
274+ @ Test (expected = RuntimeException .class )
275+ public void testValueToStringInvalidNumber () {
276+ JSONObject .valueToString (Double .POSITIVE_INFINITY );
277+ }
278+
279+ // Verify valueToString with NaN throws exception
280+ @ Test (expected = RuntimeException .class )
281+ public void testValueToStringNaN () {
282+ JSONObject .valueToString (Double .NaN );
283+ }
284+ }
0 commit comments