Skip to content

Commit dc57ad0

Browse files
committed
#37 added uint into dsl builder and test
1 parent 4fd6404 commit dc57ad0

File tree

2 files changed

+117
-44
lines changed

2 files changed

+117
-44
lines changed

jbbp/src/main/java/com/igormaznitsa/jbbp/utils/JBBPDslBuilder.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,16 @@ public JBBPDslBuilder Double() {
12871287
return this.Double(null);
12881288
}
12891289

1290+
/**
1291+
* Add anonymous unsigned int field.
1292+
*
1293+
* @return the builder instance, must not be null
1294+
* @since 2.0.4
1295+
*/
1296+
public JBBPDslBuilder UInt() {
1297+
return this.UInt(null);
1298+
}
1299+
12901300
/**
12911301
* Add named double field.
12921302
*
@@ -1299,6 +1309,19 @@ public JBBPDslBuilder Double(final String name) {
12991309
return this;
13001310
}
13011311

1312+
/**
1313+
* Add named unsigned int field.
1314+
*
1315+
* @param name name of the field, can be null for anonymous
1316+
* @return the builder instance, must not be null
1317+
* @since 2.0.4
1318+
*/
1319+
public JBBPDslBuilder UInt(final String name) {
1320+
final Item item = new Item(BinType.UINT, name, this.byteOrder);
1321+
this.addItem(item);
1322+
return this;
1323+
}
1324+
13021325
/**
13031326
* Add comment, in case that a field followed by the comment, the comment will be placed on the same line as field definition.
13041327
*
@@ -1342,6 +1365,17 @@ public JBBPDslBuilder DoubleArray(final int size) {
13421365
return this.DoubleArray(null, arraySizeToString(size));
13431366
}
13441367

1368+
/**
1369+
* Add anonymous fixed size unsigned int array field.
1370+
*
1371+
* @param size size of the array, if negative then read till end of stream
1372+
* @return the builder instance, must not be null
1373+
* @since 2.0.4
1374+
*/
1375+
public JBBPDslBuilder UIntArray(final int size) {
1376+
return this.UIntArray(null, arraySizeToString(size));
1377+
}
1378+
13451379
/**
13461380
* Add named fixed size double array field.
13471381
*
@@ -1353,6 +1387,21 @@ public JBBPDslBuilder DoubleArray(final String name, final int size) {
13531387
return this.DoubleArray(name, arraySizeToString(size));
13541388
}
13551389

1390+
/**
1391+
* Add named unsigned integer array field which size calculated trough expression.
1392+
*
1393+
* @param name name of the field, can be null for anonymous
1394+
* @param sizeExpression expression to be used to calculate array size, must not be null
1395+
* @return the builder instance, must not be null
1396+
* @since 2.0.4
1397+
*/
1398+
public JBBPDslBuilder UIntArray(final String name, final String sizeExpression) {
1399+
final Item item = new Item(BinType.UINT_ARRAY, name, this.byteOrder);
1400+
item.sizeExpression = assertExpressionChars(sizeExpression);
1401+
this.addItem(item);
1402+
return this;
1403+
}
1404+
13561405
/**
13571406
* Add named double array field which size calculated trough expression.
13581407
*
@@ -1367,6 +1416,18 @@ public JBBPDslBuilder DoubleArray(final String name, final String sizeExpression
13671416
return this;
13681417
}
13691418

1419+
/**
1420+
* Add named fixed size unsigned int array field.
1421+
*
1422+
* @param name ame of the field, can be null for anonymous
1423+
* @param size size of the array, if negative then read till end of stream
1424+
* @return the builder instance, must not be null
1425+
* @since 2.0.4
1426+
*/
1427+
public JBBPDslBuilder UIntArray(final String name, final int size) {
1428+
return this.UIntArray(name, arraySizeToString(size));
1429+
}
1430+
13701431
/**
13711432
* Add anonymous string field.
13721433
*

jbbp/src/test/java/com/igormaznitsa/jbbp/compiler/conversion/RandomAutoTest.java

Lines changed: 56 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertTrue;
55

6-
76
import com.igormaznitsa.jbbp.io.JBBPBitNumber;
87
import com.igormaznitsa.jbbp.io.JBBPByteOrder;
98
import com.igormaznitsa.jbbp.testaux.AbstractJBBPToJavaConverterTest;
@@ -58,6 +57,43 @@ String genRandomString(final int length) {
5857
return builder.toString();
5958
}
6059

60+
@Test
61+
public void testCompileParseAndWriteArray() throws Exception {
62+
int testIndex = 1;
63+
64+
long generatedFields = 0L;
65+
66+
for (int i = 5; i < 500; i += 3) {
67+
Result result;
68+
do {
69+
result = generate(i, true);
70+
} while (result.bitLength > 10000000);
71+
72+
generatedFields |= result.typeFlags;
73+
74+
System.out.println(String
75+
.format("Test %d, data bit length = %d, fields = %d, sructs = %d", testIndex,
76+
result.bitLength, result.fieldsNumber, result.structNumber));
77+
78+
final byte[] testData = makeRandomDataArray(result.bitLength);
79+
final Object clazzInstance = compileAndMakeInstance(result.script);
80+
callRead(clazzInstance, testData);
81+
assertEquals(testData.length, callWrite(clazzInstance).length, result.script);
82+
83+
testIndex++;
84+
}
85+
86+
assertEquals(0x1FFFFFFFL, generatedFields, "All field types must be presented");
87+
}
88+
89+
private byte[] makeRandomDataArray(final int bitLength) {
90+
final int bytelen = (bitLength / 8) + ((bitLength & 7) != 0 ? 1 : 0);
91+
assertTrue(bytelen > 0, "Bit length : " + bitLength);
92+
final byte[] result = new byte[bytelen];
93+
RND.nextBytes(result);
94+
return result;
95+
}
96+
6197
Result generate(final int items, final boolean generateNames) {
6298
final JBBPDslBuilder builder = JBBPDslBuilder.Begin();
6399

@@ -87,7 +123,7 @@ Result generate(final int items, final boolean generateNames) {
87123
final StructLen len = counterStack.remove(0);
88124
counterStack.get(0).add(len.make());
89125
} else {
90-
final int rndType = RND.nextInt(27);
126+
final int rndType = RND.nextInt(29);
91127
typeFlags |= (1 << rndType);
92128
switch (rndType) {
93129
case 0: { // STRUCT
@@ -244,20 +280,33 @@ Result generate(final int items, final boolean generateNames) {
244280
fieldsTotal++;
245281
}
246282
break;
247-
case 22: { // DOUBLE
283+
case 22: { // UINT
284+
builder.UInt(generateNames ? makeRndName() : null);
285+
counterStack.get(0).add(32);
286+
fieldsTotal++;
287+
}
288+
break;
289+
case 23: { // UINT_ARRAY
290+
final int arrayLen = makeArrayLengthNumber();
291+
builder.UIntArray(generateNames ? makeRndName() : null, String.valueOf(arrayLen));
292+
counterStack.get(0).add(arrayLen * 32);
293+
fieldsTotal++;
294+
}
295+
break;
296+
case 24: { // DOUBLE
248297
builder.Double(generateNames ? makeRndName() : null);
249298
counterStack.get(0).add(64);
250299
fieldsTotal++;
251300
}
252301
break;
253-
case 23: { // DOUBLE_ARRAY
302+
case 25: { // DOUBLE_ARRAY
254303
final int arrayLen = makeArrayLengthNumber();
255304
builder.DoubleArray(generateNames ? makeRndName() : null, String.valueOf(arrayLen));
256305
counterStack.get(0).add(arrayLen * 64);
257306
fieldsTotal++;
258307
}
259308
break;
260-
case 24: { // STRUCT END
309+
case 26: { // STRUCT END
261310
if (activeStructCounter > 0) {
262311
i--;
263312
activeStructCounter--;
@@ -267,11 +316,11 @@ Result generate(final int items, final boolean generateNames) {
267316
}
268317
}
269318
break;
270-
case 25: { // COMMENT
319+
case 27: { // COMMENT
271320
builder.Comment(genRandomString(this.RND.nextInt(32)));
272321
}
273322
break;
274-
case 26: { // COMMENT NEW LINE
323+
case 28: { // COMMENT NEW LINE
275324
builder.NewLineComment(genRandomString(this.RND.nextInt(32)));
276325
}
277326
break;
@@ -290,43 +339,6 @@ Result generate(final int items, final boolean generateNames) {
290339
booleanDataItems, typeFlags);
291340
}
292341

293-
private byte[] makeRandomDataArray(final int bitLength) {
294-
final int bytelen = (bitLength / 8) + ((bitLength & 7) != 0 ? 1 : 0);
295-
assertTrue(bytelen > 0, "Bit length : " + bitLength);
296-
final byte[] result = new byte[bytelen];
297-
RND.nextBytes(result);
298-
return result;
299-
}
300-
301-
@Test
302-
public void testCompileParseAndWriteArray() throws Exception {
303-
int testIndex = 1;
304-
305-
long generatedFields = 0L;
306-
307-
for (int i = 5; i < 500; i += 3) {
308-
Result result;
309-
do {
310-
result = generate(i, true);
311-
} while (result.bitLength > 10000000);
312-
313-
generatedFields |= result.typeFlags;
314-
315-
System.out.println(String
316-
.format("Test %d, data bit length = %d, fields = %d, sructs = %d", testIndex,
317-
result.bitLength, result.fieldsNumber, result.structNumber));
318-
319-
final byte[] testData = makeRandomDataArray(result.bitLength);
320-
final Object clazzInstance = compileAndMakeInstance(result.script);
321-
callRead(clazzInstance, testData);
322-
assertEquals(testData.length, callWrite(clazzInstance).length, result.script);
323-
324-
testIndex++;
325-
}
326-
327-
assertEquals(0x7FFFFFFL, generatedFields, "All field types must be presented");
328-
}
329-
330342
private static class StructLen {
331343
final int arrayLength;
332344

0 commit comments

Comments
 (0)