Initial import
[jpf-core.git] / src / tests / gov / nasa / jpf / test / mc / data / JSONTest.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18
19 package gov.nasa.jpf.test.mc.data;
20
21 import gov.nasa.jpf.util.test.TestJPF;
22 import gov.nasa.jpf.vm.Verify;
23
24 import org.junit.Test;
25
26
27 /**
28  * JPF regression test for JSON test object creation
29  * @author Ivan Mushketik
30  */
31 public class JSONTest extends TestJPF {
32   
33   class MySup {
34     int j;
35   }
36
37
38   @Test
39   public void testFillFromJSONSingleClass() {
40     if (verifyNoPropertyViolation()) {
41       MySup sup = Verify.createFromJSON(MySup.class, "{'j' : 123 }");
42
43       assert sup.j == 123;
44     }
45   }
46
47   class MyClass extends MySup {
48     int i;
49   }
50
51   @Test
52   public void testFillFromJSONInheritance() {
53     if (verifyNoPropertyViolation()) {
54       MyClass sup = Verify.createFromJSON(MyClass.class, "{'j':123, 'i':321 }");
55
56       assert sup.j == 123;
57       assert sup.i == 321;
58     }
59   }
60
61   class Primitives {
62     boolean z;
63     byte b;
64     short s;
65     int i;
66     long l;
67     float f;
68     double d;
69   }
70
71   @Test
72   public void testFillPrivimitivesFromJSON() {
73     if (verifyNoPropertyViolation()) {
74       String json = "{'z': true,'b': 10,'s': 1000,'i': 321, 'l': 123456,'f': 12.34,'d': 23.45}";
75       Primitives p = Verify.createFromJSON( Primitives.class, json);
76
77       assert p.z == true;
78       assert p.b == 10;
79       assert p.s == 1000;
80       assert p.i == 321;
81       assert p.l == 123456;
82       assertEquals(12.34, p.f, 0.001);
83       assertEquals(23.45, p.d, 0.001);
84     }
85   }
86
87   class IntArr {
88     int ints[];
89   }
90
91   @Test
92   public void testFillIntArrayFromJSON() {
93     if (verifyNoPropertyViolation()) {
94       IntArr ia = Verify.createFromJSON( IntArr.class, "{'ints': [1, 2, 3]}");
95
96       assert ia.ints[0] == 1;
97       assert ia.ints[1] == 2;
98       assert ia.ints[2] == 3;
99     }
100   }
101
102   class Boxed {
103     Boolean t;
104     Boolean f;
105     Byte b;
106     Short s;
107     Integer i;
108     Long l;
109     Float fl;
110     Double d;
111   }
112
113   @Test
114   public void testFillBoxedPrimitivesFromJSON() {
115     if (verifyNoPropertyViolation()) {
116       String json = "{'t':true, 'f':false, 'b':10, 's':1000,'i':321, 'l':123456, 'fl':12.34, 'd':23.45 }";
117       Boxed b = Verify.createFromJSON( Boxed.class, json);
118
119       assert b.t == true;
120       assert b.f == false;
121       assert b.b == 10;
122       assert b.s == 1000;
123       assert b.i == 321;
124       assert b.l == 123456;
125       assertEquals(12.34, b.fl, 0.001);
126       assertEquals(23.45, b.d, 0.001);
127     }
128   }
129
130   class PrimitiveArrays {
131     boolean bools[];
132     byte bytes[];
133     short shorts[];
134     int ints[];
135     long longs[];
136     float floats[];
137     double doubles[];
138   }
139
140   @Test
141   public void testFillPrimitiveArrays() {
142     if (verifyNoPropertyViolation()) {
143       String json = "{"
144               + "\"bools\" : [true, false, true],"
145               + "\"bytes\" : [-40, -30, -20],"
146               + "\"shorts\" : [2, 3, 4],"
147               + "\"ints\" : [1, 2, 3],"
148               + "\"longs\" : [1000, 2000, 3000],"
149               + "\"floats\" : [12.34, 23.45, 34.56],"
150               + "\"doubles\" : [-12.34, -23.45, -34.56]"
151               + "}";
152       PrimitiveArrays pa = Verify.createFromJSON( PrimitiveArrays.class, json);
153
154       assert pa.bools[0] == true;
155       assert pa.bools[1] == false;
156       assert pa.bools[2] == true;
157
158       assert pa.bytes[0] == -40;
159       assert pa.bytes[1] == -30;
160       assert pa.bytes[2] == -20;
161
162       assert pa.shorts[0] == 2;
163       assert pa.shorts[1] == 3;
164       assert pa.shorts[2] == 4;
165
166       assert pa.ints[0] == 1;
167       assert pa.ints[1] == 2;
168       assert pa.ints[2] == 3;
169
170       assert pa.longs[0] == 1000;
171       assert pa.longs[1] == 2000;
172       assert pa.longs[2] == 3000;
173
174       assertEquals(12.34, pa.floats[0], 0.0001);
175       assertEquals(23.45, pa.floats[1], 0.0001);
176       assertEquals(34.56, pa.floats[2], 0.0001);
177
178       assertEquals(-12.34, pa.doubles[0], 0.0001);
179       assertEquals(-23.45, pa.doubles[1], 0.0001);
180       assertEquals(-34.56, pa.doubles[2], 0.0001);
181     }
182   }
183
184   class InnerClass {
185     int i;
186   }
187
188   class OuterClass {
189     long l;
190     InnerClass ic;
191   }
192
193   @Test
194   public void testInnerClassFilling() {
195     if (verifyNoPropertyViolation()) {
196       String json =
197                 "{"
198               +   "'l' : 1234,"
199               +   "'ic' : {"
200               +      "'i' : 4321"
201               +   "}"
202               + "}";
203
204       OuterClass oc = Verify.createFromJSON( OuterClass.class, json);
205
206       assert oc.l == 1234;
207       assert oc.ic.i == 4321;
208     }
209   }
210
211   @Test
212   public void testFillingWhenInnerClassIsNull() {
213     if (verifyNoPropertyViolation()) {
214       String json = "{"
215               + "\"l\" : 1234,"
216               + "\"ic\" : null"
217               + "}";
218
219       OuterClass oc = Verify.createFromJSON( OuterClass.class, json);
220
221       assert oc.l == 1234;
222       assert oc.ic == null;
223     }
224   }
225
226   class MultiArray {
227     int intsInts[][];
228   }
229
230   @Test
231   public void testMultiArrayFilling() {
232     if (verifyNoPropertyViolation()) {
233       String json = "{"
234               + "\"intsInts\" : [[1, 2, 3], [4, 5, 6]]"
235               + "}";
236
237       MultiArray ma = Verify.createFromJSON( MultiArray.class, json);
238
239       assert ma.intsInts[0][0] == 1;
240       assert ma.intsInts[0][1] == 2;
241       assert ma.intsInts[0][2] == 3;
242
243       assert ma.intsInts[1][0] == 4;
244       assert ma.intsInts[1][1] == 5;
245       assert ma.intsInts[1][2] == 6;
246     }
247   }
248
249   class BoxIntsArr {
250     Integer ints[];
251   }
252
253   @Test
254   public void testBoxedTypesArrayFilling() {
255     if (verifyNoPropertyViolation()) {
256       String json = "{"
257               + "\"ints\" : [1, 2, 3]"
258               + "}";
259
260       BoxIntsArr bia = Verify.createFromJSON( BoxIntsArr.class, json);
261
262       assert bia.ints[0] == 1;
263       assert bia.ints[1] == 2;
264       assert bia.ints[2] == 3;
265     }
266   }
267
268   static class IC {
269     int i;
270   }
271
272   static class ArrayOfObjects {
273     IC cls[];
274   }
275
276   @Test
277   public void testArrayOfObjectsFilling() {
278     if (verifyNoPropertyViolation()) {
279       String json = "{"
280               + "\"cls\" : [{\"i\" : 1}, {\"i\" : 2}, {\"i\" : 3}]"
281               + "}";
282
283       ArrayOfObjects aoo = Verify.createFromJSON( ArrayOfObjects.class, json);
284
285       assert aoo.cls[0].i == 1;
286       assert aoo.cls[1].i == 2;
287       assert aoo.cls[2].i == 3;
288     }
289   }
290
291   class MultObjectsArr {
292     IC cls[][];
293   }
294
295   @Test
296   public void testFillingMultArrayOfObjects() {
297       if (verifyNoPropertyViolation()) {
298       String json = "{"
299               + "\"cls\" : ["
300               + "[{\"i\" : 1}, {\"i\" : 2}, {\"i\" : 3}],"
301               + "[{\"i\" : 4}, {\"i\" : 5}, {\"i\" : 6}],"
302               + "[{\"i\" : 7}, {\"i\" : 8}, {\"i\" : 9}]"
303               + "]"
304               + "}";
305
306       MultObjectsArr moa = Verify.createFromJSON( MultObjectsArr.class, json);
307
308       assert moa.cls[0][0].i == 1;
309       assert moa.cls[0][1].i == 2;
310       assert moa.cls[0][2].i == 3;
311
312       assert moa.cls[1][0].i == 4;
313       assert moa.cls[1][1].i == 5;
314       assert moa.cls[1][2].i == 6;
315
316       assert moa.cls[2][0].i == 7;
317       assert moa.cls[2][1].i == 8;
318       assert moa.cls[2][2].i == 9;
319     }
320   }
321
322   class ClassWithString {
323     String s1;
324     String s2;
325   }
326
327   @Test
328   public void testFillStringValue() {
329     if (verifyNoPropertyViolation()) {
330       String json = "{"
331               + "\"s1\" : \"val\","
332               + "\"s2\" : null"
333               + "}";
334
335       ClassWithString cws = Verify.createFromJSON( ClassWithString.class, json);
336
337       assert cws.s1.equals("val") == true;
338       assert cws.s2 == null;
339     }
340   }
341
342   // --- CG Tests
343
344   class Bool {
345     Bool(boolean b) {this.b = b;}
346     boolean b;
347
348     @Override
349         public boolean equals(Object o) {
350       Bool bool = (Bool) o;
351       return this.b == bool.b;
352     }
353   }
354
355   static void checkValue(Object[] expected, Object curVal) {
356     for (int i = 0; i < expected.length; i++) {
357       if (curVal.equals(expected[i])) {
358         Verify.setBitInBitSet(0, i, true);
359         break;
360       }
361     }
362
363     Verify.incrementCounter(0);
364
365     if (Verify.getCounter(0) == expected.length) {
366       for (int i = 0; i < expected.length; i++) {
367         assert Verify.getBitInBitSet(0, i) == true;
368       }
369     }
370   }
371
372   @Test
373   public void testSetBoolFromCG() {
374     if (verifyNoPropertyViolation()) {      
375       String json = "{"
376               + "'b' : TrueFalse()"
377               + "}";
378
379       Object[] expected = {
380         new Bool(true),
381         new Bool(false)
382       };
383       Bool bb = Verify.createFromJSON(Bool.class, json);      
384       checkValue(expected, bb);
385     }
386   }
387
388   class ByteShortIntLong {
389
390     public ByteShortIntLong(int b, int s, int i, long l) {
391       this.b = (byte) b; this.s = (short) s; this.i = i; this.l = l;
392     }
393
394     byte b; short s; int i; long l;
395
396     @Override
397         public boolean equals(Object o) {
398       ByteShortIntLong bs = (ByteShortIntLong) o;
399
400       return bs.b == b && bs.s == s && bs.i == i && bs.l == l;
401     }
402   }
403
404   @Test
405   public void testSetByteShortIntFromCG() {
406     if (verifyNoPropertyViolation()) {
407       String json = "{"
408               + "'b' : IntSet(1, 2),"
409               + "'s' : 2,"
410               + "'i' : IntSet(3, 4, 5),"
411               + "'l' : IntSet(8)"
412               + "}";
413
414       Object[] expected = {
415         new ByteShortIntLong(1, 2, 3, 8), new ByteShortIntLong(2, 2, 3, 8),
416         new ByteShortIntLong(1, 2, 4, 8), new ByteShortIntLong(2, 2, 4, 8),
417         new ByteShortIntLong(1, 2, 5, 8), new ByteShortIntLong(2, 2, 5, 8),
418       };
419       ByteShortIntLong bsil = Verify.createFromJSON(ByteShortIntLong.class, json);
420       checkValue(expected, bsil);
421     }
422   }
423
424   @Test
425   public void testFillWithIntevalCG() {
426     if (verifyNoPropertyViolation()) {
427       String json = "{"
428               + "'b' : 1,"
429               + "'s' : IntInterval(1, 3),"
430               + "'i' : 1,"
431               + "'l' : IntInterval(8, 10)"
432               + "}";
433
434       Object[] expected = {
435         new ByteShortIntLong(1, 1, 1, 8), new ByteShortIntLong(1, 2, 1, 8), new ByteShortIntLong(1, 3, 1, 8),
436         new ByteShortIntLong(1, 1, 1, 9), new ByteShortIntLong(1, 2, 1, 9), new ByteShortIntLong(1, 3, 1, 9),
437         new ByteShortIntLong(1, 1, 1, 10), new ByteShortIntLong(1, 2, 1, 10), new ByteShortIntLong(1, 3, 1, 10),};
438       ByteShortIntLong bsil = Verify.createFromJSON(ByteShortIntLong.class, json);
439       checkValue(expected, bsil);
440     }
441   }
442
443   class I {
444     int i;
445   }
446
447   class O {
448     I inner;
449     public O(int i) {
450       inner = new I();
451       inner.i = i;
452     }
453
454     @Override
455         public boolean equals(Object o) {
456       O outer = (O) o;
457
458       return outer.inner.i == this.inner.i;
459     }
460   }
461
462   @Test
463   public void testFillInnerClassCG() {
464     if (verifyNoPropertyViolation()) {
465       String json = "{"
466               + "'inner' : {"
467                 + "'i' : IntSet(3, 4, 5)"
468                 + "}"
469               + "}";
470
471       Object[] expected = {
472         new O(3), new O(4), new O(5),
473       };
474       O bsil = Verify.createFromJSON(O.class, json);
475       checkValue(expected, bsil);
476     }
477   }
478
479   class ArrI {
480     I[] arr;
481
482     ArrI(int... ints) {
483       arr = new I[ints.length];
484       for (int i = 0; i < ints.length; i++) {arr[i] = new I(); arr[i].i = ints[i];}
485     }
486
487     @Override
488         public boolean equals(Object o) {
489       ArrI other = (ArrI) o;
490
491       if (other.arr.length != this.arr.length) {
492         return false;
493       }
494       for (int i = 0; i < this.arr.length; i++) {
495         if (this.arr[i].i != other.arr[i].i) {
496           return false;
497         }
498       }
499       return true;
500     }
501   }
502
503   @Test
504   public void testFillingObjectInArrayWithCG() {
505     if (verifyNoPropertyViolation()) {
506       String json = "{"
507               + "'arr' : [ {'i' : IntSet(1, 2, 3)}, {'i' : IntSet(4, 5, 6)}]"
508               + "}";
509
510       Object[] expected = {
511         new ArrI(1, 4), new ArrI(2, 4), new ArrI(3, 4),
512         new ArrI(1, 5), new ArrI(2, 5), new ArrI(3, 5),
513         new ArrI(1, 6), new ArrI(2, 6), new ArrI(3, 6),
514       };
515       ArrI arri = Verify.createFromJSON(ArrI.class, json);
516       checkValue(expected, arri);
517     }
518   }
519
520   class BoxedInteger {
521     Integer bi;
522
523     BoxedInteger(Integer newI) {
524       bi = newI;
525     }
526
527     @Override
528     public boolean equals(Object obj) {
529       BoxedInteger bic = (BoxedInteger) obj;
530       return this.bi.equals(bic.bi);
531     }
532   }
533
534   @Test
535   public void testObjectFromCG() {
536     if (verifyNoPropertyViolation()) {
537       String json = "{"
538               + "'bi' : IntSet(1, 2, 3)"
539               + "}";
540
541       Object[] expected = {
542         new BoxedInteger(1), new BoxedInteger(2), new BoxedInteger(3),
543       };
544       BoxedInteger bi = Verify.createFromJSON(BoxedInteger.class, json);
545       checkValue(expected, bi);
546     }
547   }
548
549   class BoxedDouble {
550     Double d;
551
552     public BoxedDouble(Double d) {
553       this.d = d;
554     }
555
556     @Override
557         public boolean equals(Object o) {
558       BoxedDouble bd = (BoxedDouble) o;
559
560       return doublesEqual(bd.d, this.d);
561     }
562
563     boolean doublesEqual(double d1, double d2) {
564       double diff = 0.001;
565
566       return Math.abs(d1 - d2) <= diff;
567     }
568   }
569
570   @Test
571   public void testBoxedDoubleFromCG() {
572     if (verifyNoPropertyViolation()) {
573       String json = "{"
574               + "'d' : DoubleSet(1.1, 2.2, 3.3)"
575               + "}";
576
577       Object[] expected = {
578         new BoxedDouble(1.1), new BoxedDouble(2.2), new BoxedDouble(3.3),
579       };
580       BoxedDouble bd = Verify.createFromJSON(BoxedDouble.class, json);
581       checkValue(expected, bd);
582     }
583   }
584
585   
586 }
587
588