Skip to content

Commit 2e6d68b

Browse files
committed
Better json tests
1 parent 791399d commit 2e6d68b

9 files changed

Lines changed: 241 additions & 684 deletions

File tree

utils/json/json-lib/src/test/java/com/syntaxphoenix/syntaxapi/test/json/JsonIOTest.java

Lines changed: 0 additions & 130 deletions
This file was deleted.

utils/json/json-lib/src/test/java/com/syntaxphoenix/syntaxapi/test/json/JsonObjectTest.java

Lines changed: 0 additions & 84 deletions
This file was deleted.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.syntaxphoenix.syntaxapi.test.json;
2+
3+
import java.io.File;
4+
import java.math.BigDecimal;
5+
import java.math.BigInteger;
6+
import java.util.HashMap;
7+
import java.util.function.Function;
8+
9+
import com.syntaxphoenix.syntaxapi.json.JsonArray;
10+
import com.syntaxphoenix.syntaxapi.json.JsonObject;
11+
import com.syntaxphoenix.syntaxapi.json.JsonValue;
12+
import com.syntaxphoenix.syntaxapi.json.ValueType;
13+
import com.syntaxphoenix.syntaxapi.json.value.*;
14+
import com.syntaxphoenix.syntaxapi.random.Keys;
15+
import com.syntaxphoenix.syntaxapi.random.NumberGeneratorType;
16+
import com.syntaxphoenix.syntaxapi.random.RandomNumberGenerator;
17+
18+
public final class JsonTestConfiguration {
19+
20+
private JsonTestConfiguration() {
21+
throw new UnsupportedOperationException("Utility class");
22+
}
23+
24+
public static final File TEST_DIRECTORY = new File("testData");
25+
26+
public static final long SEED = 5908375382894239029L;
27+
private static final RandomNumberGenerator SEED_RANDOM = NumberGeneratorType.MURMUR.create(SEED);
28+
29+
public static final int TEST_AMOUNT = 20;
30+
public static final boolean TEST_PLAIN = true;
31+
public static final boolean TEST_PRETTY = true;
32+
33+
public static final int OBJECT_VALUE_AMOUNT = 10;
34+
35+
private static final HashMap<ValueType, Function<RandomNumberGenerator, JsonValue<?>>> BUILDERS = new HashMap<>();
36+
37+
private static final BigInteger MAX_INT = BigInteger.valueOf(Long.MAX_VALUE);
38+
private static final BigDecimal DECI_LONG = BigDecimal.valueOf(Long.MAX_VALUE);
39+
private static final BigDecimal MAX_DECI = BigDecimal.valueOf(Double.MAX_VALUE).multiply(DECI_LONG).multiply(DECI_LONG);
40+
41+
static {
42+
BUILDERS.put(ValueType.NULL, random -> JsonNull.get());
43+
BUILDERS.put(ValueType.STRING, random -> {
44+
return new JsonString(new Keys(random).makeKey(32));
45+
});
46+
BUILDERS.put(ValueType.BOOLEAN, random -> {
47+
return new JsonBoolean(random.nextBoolean());
48+
});
49+
BUILDERS.put(ValueType.BYTE, random -> {
50+
return new JsonByte((byte) random.nextShort(Byte.MIN_VALUE, Byte.MAX_VALUE));
51+
});
52+
BUILDERS.put(ValueType.SHORT, random -> {
53+
return new JsonShort(random.nextShort());
54+
});
55+
BUILDERS.put(ValueType.INTEGER, random -> {
56+
return new JsonInteger(random.nextInt());
57+
});
58+
BUILDERS.put(ValueType.FLOAT, random -> {
59+
return new JsonFloat(random.nextFloat(Float.MAX_VALUE));
60+
});
61+
BUILDERS.put(ValueType.LONG, random -> {
62+
return new JsonLong(random.nextLong());
63+
});
64+
BUILDERS.put(ValueType.DOUBLE, random -> {
65+
return new JsonDouble(random.nextDouble(Double.MAX_VALUE));
66+
});
67+
BUILDERS.put(ValueType.BIG_INTEGER, random -> {
68+
return new JsonBigInteger(BigInteger.valueOf(random.nextLong() * (random.nextShort() >> 8)).multiply(MAX_INT));
69+
});
70+
BUILDERS.put(ValueType.BIG_DECIMAL, random -> {
71+
return new JsonBigDecimal(BigDecimal.valueOf(random.nextDouble() * (random.nextShort() >> 8)).multiply(MAX_DECI));
72+
});
73+
BUILDERS.put(ValueType.ARRAY, random -> {
74+
JsonArray array = new JsonArray();
75+
for (ValueType type : ValueType.values()) {
76+
if (type.isJson() || type == ValueType.NUMBER) {
77+
continue;
78+
}
79+
array.add(build(random, type));
80+
}
81+
return array;
82+
});
83+
BUILDERS.put(ValueType.OBJECT, random -> {
84+
JsonObject object = new JsonObject();
85+
for (ValueType type : ValueType.values()) {
86+
if (type.isJson() || type == ValueType.NUMBER) {
87+
continue;
88+
}
89+
JsonArray array = new JsonArray();
90+
for (int i = 0; i < OBJECT_VALUE_AMOUNT; i++) {
91+
array.add(build(random, type));
92+
}
93+
object.set(type.name(), array);
94+
}
95+
return object;
96+
});
97+
}
98+
99+
public static RandomNumberGenerator newGenerator() {
100+
return NumberGeneratorType.MURMUR.create(SEED_RANDOM.nextLong(Long.MAX_VALUE));
101+
}
102+
103+
public static void skipGenerators() {
104+
for (int i = 0; i < TEST_AMOUNT; i++) {
105+
SEED_RANDOM.nextLong(Long.MAX_VALUE);
106+
}
107+
}
108+
109+
public static JsonValue<?> build(RandomNumberGenerator generator, ValueType type) {
110+
if (type == ValueType.JSON || type == ValueType.NUMBER) {
111+
return null;
112+
}
113+
return BUILDERS.get(type).apply(generator);
114+
}
115+
116+
}

0 commit comments

Comments
 (0)