2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
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
10 * http://www.apache.org/licenses/LICENSE-2.0.
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.
19 package gov.nasa.jpf.test.mc.data;
21 import gov.nasa.jpf.util.test.TestJPF;
22 import gov.nasa.jpf.vm.Verify;
24 import org.junit.Test;
28 * JPF regression test for JSON test object creation
29 * @author Ivan Mushketik
31 public class JSONTest extends TestJPF {
39 public void testFillFromJSONSingleClass() {
40 if (verifyNoPropertyViolation()) {
41 MySup sup = Verify.createFromJSON(MySup.class, "{'j' : 123 }");
47 class MyClass extends MySup {
52 public void testFillFromJSONInheritance() {
53 if (verifyNoPropertyViolation()) {
54 MyClass sup = Verify.createFromJSON(MyClass.class, "{'j':123, 'i':321 }");
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);
82 assertEquals(12.34, p.f, 0.001);
83 assertEquals(23.45, p.d, 0.001);
92 public void testFillIntArrayFromJSON() {
93 if (verifyNoPropertyViolation()) {
94 IntArr ia = Verify.createFromJSON( IntArr.class, "{'ints': [1, 2, 3]}");
96 assert ia.ints[0] == 1;
97 assert ia.ints[1] == 2;
98 assert ia.ints[2] == 3;
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);
124 assert b.l == 123456;
125 assertEquals(12.34, b.fl, 0.001);
126 assertEquals(23.45, b.d, 0.001);
130 class PrimitiveArrays {
141 public void testFillPrimitiveArrays() {
142 if (verifyNoPropertyViolation()) {
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]"
152 PrimitiveArrays pa = Verify.createFromJSON( PrimitiveArrays.class, json);
154 assert pa.bools[0] == true;
155 assert pa.bools[1] == false;
156 assert pa.bools[2] == true;
158 assert pa.bytes[0] == -40;
159 assert pa.bytes[1] == -30;
160 assert pa.bytes[2] == -20;
162 assert pa.shorts[0] == 2;
163 assert pa.shorts[1] == 3;
164 assert pa.shorts[2] == 4;
166 assert pa.ints[0] == 1;
167 assert pa.ints[1] == 2;
168 assert pa.ints[2] == 3;
170 assert pa.longs[0] == 1000;
171 assert pa.longs[1] == 2000;
172 assert pa.longs[2] == 3000;
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);
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);
194 public void testInnerClassFilling() {
195 if (verifyNoPropertyViolation()) {
204 OuterClass oc = Verify.createFromJSON( OuterClass.class, json);
207 assert oc.ic.i == 4321;
212 public void testFillingWhenInnerClassIsNull() {
213 if (verifyNoPropertyViolation()) {
219 OuterClass oc = Verify.createFromJSON( OuterClass.class, json);
222 assert oc.ic == null;
231 public void testMultiArrayFilling() {
232 if (verifyNoPropertyViolation()) {
234 + "\"intsInts\" : [[1, 2, 3], [4, 5, 6]]"
237 MultiArray ma = Verify.createFromJSON( MultiArray.class, json);
239 assert ma.intsInts[0][0] == 1;
240 assert ma.intsInts[0][1] == 2;
241 assert ma.intsInts[0][2] == 3;
243 assert ma.intsInts[1][0] == 4;
244 assert ma.intsInts[1][1] == 5;
245 assert ma.intsInts[1][2] == 6;
254 public void testBoxedTypesArrayFilling() {
255 if (verifyNoPropertyViolation()) {
257 + "\"ints\" : [1, 2, 3]"
260 BoxIntsArr bia = Verify.createFromJSON( BoxIntsArr.class, json);
262 assert bia.ints[0] == 1;
263 assert bia.ints[1] == 2;
264 assert bia.ints[2] == 3;
272 static class ArrayOfObjects {
277 public void testArrayOfObjectsFilling() {
278 if (verifyNoPropertyViolation()) {
280 + "\"cls\" : [{\"i\" : 1}, {\"i\" : 2}, {\"i\" : 3}]"
283 ArrayOfObjects aoo = Verify.createFromJSON( ArrayOfObjects.class, json);
285 assert aoo.cls[0].i == 1;
286 assert aoo.cls[1].i == 2;
287 assert aoo.cls[2].i == 3;
291 class MultObjectsArr {
296 public void testFillingMultArrayOfObjects() {
297 if (verifyNoPropertyViolation()) {
300 + "[{\"i\" : 1}, {\"i\" : 2}, {\"i\" : 3}],"
301 + "[{\"i\" : 4}, {\"i\" : 5}, {\"i\" : 6}],"
302 + "[{\"i\" : 7}, {\"i\" : 8}, {\"i\" : 9}]"
306 MultObjectsArr moa = Verify.createFromJSON( MultObjectsArr.class, json);
308 assert moa.cls[0][0].i == 1;
309 assert moa.cls[0][1].i == 2;
310 assert moa.cls[0][2].i == 3;
312 assert moa.cls[1][0].i == 4;
313 assert moa.cls[1][1].i == 5;
314 assert moa.cls[1][2].i == 6;
316 assert moa.cls[2][0].i == 7;
317 assert moa.cls[2][1].i == 8;
318 assert moa.cls[2][2].i == 9;
322 class ClassWithString {
328 public void testFillStringValue() {
329 if (verifyNoPropertyViolation()) {
331 + "\"s1\" : \"val\","
335 ClassWithString cws = Verify.createFromJSON( ClassWithString.class, json);
337 assert cws.s1.equals("val") == true;
338 assert cws.s2 == null;
345 Bool(boolean b) {this.b = b;}
349 public boolean equals(Object o) {
350 Bool bool = (Bool) o;
351 return this.b == bool.b;
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);
363 Verify.incrementCounter(0);
365 if (Verify.getCounter(0) == expected.length) {
366 for (int i = 0; i < expected.length; i++) {
367 assert Verify.getBitInBitSet(0, i) == true;
373 public void testSetBoolFromCG() {
374 if (verifyNoPropertyViolation()) {
376 + "'b' : TrueFalse()"
379 Object[] expected = {
383 Bool bb = Verify.createFromJSON(Bool.class, json);
384 checkValue(expected, bb);
388 class ByteShortIntLong {
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;
394 byte b; short s; int i; long l;
397 public boolean equals(Object o) {
398 ByteShortIntLong bs = (ByteShortIntLong) o;
400 return bs.b == b && bs.s == s && bs.i == i && bs.l == l;
405 public void testSetByteShortIntFromCG() {
406 if (verifyNoPropertyViolation()) {
408 + "'b' : IntSet(1, 2),"
410 + "'i' : IntSet(3, 4, 5),"
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),
419 ByteShortIntLong bsil = Verify.createFromJSON(ByteShortIntLong.class, json);
420 checkValue(expected, bsil);
425 public void testFillWithIntevalCG() {
426 if (verifyNoPropertyViolation()) {
429 + "'s' : IntInterval(1, 3),"
431 + "'l' : IntInterval(8, 10)"
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);
455 public boolean equals(Object o) {
458 return outer.inner.i == this.inner.i;
463 public void testFillInnerClassCG() {
464 if (verifyNoPropertyViolation()) {
467 + "'i' : IntSet(3, 4, 5)"
471 Object[] expected = {
472 new O(3), new O(4), new O(5),
474 O bsil = Verify.createFromJSON(O.class, json);
475 checkValue(expected, bsil);
483 arr = new I[ints.length];
484 for (int i = 0; i < ints.length; i++) {arr[i] = new I(); arr[i].i = ints[i];}
488 public boolean equals(Object o) {
489 ArrI other = (ArrI) o;
491 if (other.arr.length != this.arr.length) {
494 for (int i = 0; i < this.arr.length; i++) {
495 if (this.arr[i].i != other.arr[i].i) {
504 public void testFillingObjectInArrayWithCG() {
505 if (verifyNoPropertyViolation()) {
507 + "'arr' : [ {'i' : IntSet(1, 2, 3)}, {'i' : IntSet(4, 5, 6)}]"
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),
515 ArrI arri = Verify.createFromJSON(ArrI.class, json);
516 checkValue(expected, arri);
523 BoxedInteger(Integer newI) {
528 public boolean equals(Object obj) {
529 BoxedInteger bic = (BoxedInteger) obj;
530 return this.bi.equals(bic.bi);
535 public void testObjectFromCG() {
536 if (verifyNoPropertyViolation()) {
538 + "'bi' : IntSet(1, 2, 3)"
541 Object[] expected = {
542 new BoxedInteger(1), new BoxedInteger(2), new BoxedInteger(3),
544 BoxedInteger bi = Verify.createFromJSON(BoxedInteger.class, json);
545 checkValue(expected, bi);
552 public BoxedDouble(Double d) {
557 public boolean equals(Object o) {
558 BoxedDouble bd = (BoxedDouble) o;
560 return doublesEqual(bd.d, this.d);
563 boolean doublesEqual(double d1, double d2) {
566 return Math.abs(d1 - d2) <= diff;
571 public void testBoxedDoubleFromCG() {
572 if (verifyNoPropertyViolation()) {
574 + "'d' : DoubleSet(1.1, 2.2, 3.3)"
577 Object[] expected = {
578 new BoxedDouble(1.1), new BoxedDouble(2.2), new BoxedDouble(3.3),
580 BoxedDouble bd = Verify.createFromJSON(BoxedDouble.class, json);
581 checkValue(expected, bd);