-
Notifications
You must be signed in to change notification settings - Fork 0
/
unparse.q
445 lines (391 loc) · 12.8 KB
/
unparse.q
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
\c 100000 100000
{
path:"/"sv -1_"/"vs ssr[;"\\";"/"]first -3#value .z.s;
system"l ",path,"/../qbinparse.q";
}[];
simpleRecordByte:.binp.compileSchema"
record point
field xpos byte
field ypos byte
end
";
if[not .binp.unparse[simpleRecordByte;`ypos`xpos!(0x0201);`point]~0x0102;'"failed"];
if[not .binp.unparse[simpleRecordByte;`xpos`ypos!(0x0102);`point]~0x0102;'"failed"];
if[not .[.binp.unparse;(simpleRecordByte;enlist[`xpos]!enlist 0x02;`point);::]~"missing field: ypos"];
simpleRecord:.binp.compileSchema"
record point
field xpos real
field ypos real
end
";
if[not .binp.unparse[simpleRecord;`xpos`ypos!(120e;130e);`point]~0x0000f04200000243;'"failed"];
mixedRecord:.binp.compileSchema"
record point
field xpos real
field ypos short
end
";
if[not .binp.unparse[mixedRecord;`xpos`ypos!(120e;-10000h);`point]~0x0000f042f0d8;'"failed"];
simpleArray:.binp.compileSchema"
record a
field ints array int x 4
end
";
if[not .binp.unparse[simpleArray;enlist[`ints]!enlist 1 2 3 4i;`a]~0x01000000020000000300000004000000;'"failed"];
if[not .binp.unparse[simpleArray;enlist[`ints]!enlist"abcd";`a]~0x61000000620000006300000064000000;'"failed"];
if[not .[.binp.unparse;(simpleArray;enlist[`ints]!enlist 1 2 3 4 5i;`a);::]~"a.ints: expected 4 items, got 5";'"failed"];
multipleSimpleArrays:.binp.compileSchema"
record a
field ints array int x 4
field shorts array short x 5
field bytes array byte x 4
field chars array char x 4
end
";
if[not .binp.unparse[multipleSimpleArrays;`ints`shorts`bytes`chars!(1 2 3 4i;5 6 7 8 9h;10 11 12 13;14 15 16 17);`a]~
0x01000000020000000300000004000000050006000700080009000a0b0c0d0e0f1011; '"failed"];
arrayOfRecord:.binp.compileSchema"
record c
field num int
end
record b
field cnt array byte x 2
field cnt2 record c
end
record a
field bs array record b x 2
end
";
if[not .binp.unparse[arrayOfRecord;enlist[`bs]!enlist([]cnt:(0x0102;0x0304);cnt2:([]num:5 6));`a]
~0x010205000000030406000000; '"failed"];
arrayOfRecord2:.binp.compileSchema" //spanning test, don't change
record rec
field f int
end
record main
field recsL short
field recs array record rec xv recsL
end
";
if[not .binp.unparse[arrayOfRecord2;`recsL`recs!(4h;([]a:0 0 0 0;f:1 1 1 1));`main]~0x040001000000010000000100000001000000; '"failed"];
arrayOfRecord3:.binp.compileSchema" //spanning test, don't change
record rec
field f int
end
record main
field recsL short
field recs array record rec xv recsL
end
";
if[not .binp.unparse[arrayOfRecord3;`recsL`a`recs!(4h;5i;([]f:4#1 1 1 1i));`main]~0x040001000000010000000100000001000000; '"failed"];
varLengthArray:.binp.compileSchema"
record a
field intsL short
field ints array int xv intsL
end
";
if[not .binp.unparse[varLengthArray;`intsL`ints!(3h;1 2 3i);`a]~0x0300010000000200000003000000;'"failed"];
recArray:.binp.compileSchema"
record point
field xpos int
field ypos int
end
record a
field points array record point x 2
end
";
if[not .[.binp.unparse;(recArray;enlist[`points]!enlist(`xpos`ypos!(1i;2i);`xpos`ypos!(3i;4i);`xpos`ypos!(5i;6i));`a);::]~"a.points: expected 2 items, got 3";'"failed"];
if[not .[.binp.unparse;(recArray;enlist[`points]!enlist(`xpos`ypos!(1i;2i);`xpos`ypos!(3i;4i);`xpos`zpos!(5i;6i));`a);::]~"a.points: expected 2 items, got 3";'"failed"];
if[not .[.binp.unparse;(recArray;enlist[`points]!enlist(`xpos`zpos!(1i;2i);`xpos`zpos!(3i;4i));`a);::]~"a.points: missing field: ypos";'"failed"];
if[not .binp.unparse[recArray;enlist[`points]!enlist(`xpos`ypos!(1i;2i);`xpos`ypos!(3i;4i));`a]~0x01000000020000000300000004000000;'"failed"];
if[not .binp.unparse[recArray;enlist[`points]!enlist(`xpos`ypos!(1i;2i);`xpos`ypos`zpos!(3i;4i;5i));`a]~0x01000000020000000300000004000000;'"failed"];
varLengthRecArray:.binp.compileSchema"
record point
field xpos int
field ypos int
end
record a
field pointsL short
field points array record point xv pointsL
end
";
if[not .binp.unparse[varLengthRecArray;`pointsL`points!(2h;(`xpos`ypos!(1i;2i);`xpos`ypos!(3i;4i)));`a]~0x020001000000020000000300000004000000;'"failed"];
byteGuardAtomicArray:.binp.compileSchema"
record a
field items array short tpb 3
field guard byte
field other byte
end
";
if[not .binp.unparse[byteGuardAtomicArray;`items`guard`other!(513 1541 2055 2826h;0x03;0x04);`a]~0x0102050607080a0b0304;'"failed"];
shortGuardAtomicArray:.binp.compileSchema"
record a
field items array short tps 3
field guard short
field other byte
end
";
if[not .binp.unparse[shortGuardAtomicArray;`items`guard`other!(513 1027 1541 2055h;3h;0x0a);`a]~0x010203040506070803000a;'"failed"];
byteGuardRecordArray:.binp.compileSchema"
record point
field xpos int
field ypos int
end
record a
field items array record point tpb 255
field guard byte
field other byte
end
";
if[not .binp.unparse[byteGuardRecordArray;`items`guard`other!((`xpos`ypos!1 2i;`xpos`ypos!3 4i);0xff;0x04);`a]~0x01000000020000000300000004000000ff04;'"failed"];
recordWithCase:.binp.compileSchema"
record caseA
field contInt int
end
record caseB
field contShort short
end
record tagged
field tag byte
field data case tag
0 caseA
1 caseB
end
end
record main
field tags array record tagged tpb 2
field guard byte
end
";
if[not .binp.unparse[recordWithCase;
`tags`guard!((`tag`data!(0x00;enlist[`contInt]!enlist 1i);
`tag`data!(0x01;enlist[`contShort]!enlist 2h);
`tag`data!(0x00;enlist[`contInt]!enlist 3i);
`tag`data!(0x01;enlist[`contShort]!enlist 4h));
0x02);
`main]~0x0001000000010200000300000001040002;'"failed"];
recordWithCase2:.binp.compileSchema"
record caseA
field contInt int
end
record caseB
field contShort short
end
record tagged
field tag long
field data case tag
0 caseA
1 caseB
end
end
record main
field tags array record tagged tpb 2
field guard byte
end
";
if[not .binp.unparse[recordWithCase2;
`tags`guard!((`tag`data!(0j;enlist[`contInt]!enlist 1i);
`tag`data!(1j;enlist[`contShort]!enlist 2h);
`tag`data!(0j;enlist[`contInt]!enlist 3i);
`tag`data!(1j;enlist[`contShort]!enlist 4h))
;0x02);
`main]~0x000000000000000001000000010000000000000002000000000000000000030000000100000000000000040002;'"failed"];
recordWithCaseDefault:.binp.compileSchema"
record caseA
field contInt int
end
record caseB
field contShort short
end
record tagged
field tag byte
field data case tag
0 caseA
default caseB
end
end
record main
field tags array record tagged tpb 2
field guard byte
end
";
if[not .binp.unparse[recordWithCaseDefault;
`tags`guard!((`tag`data!(0x00;enlist[`contInt]!enlist 1i);
`tag`data!(0x38;enlist[`contShort]!enlist 2h);
`tag`data!(0x00;enlist[`contInt]!enlist 3i);
`tag`data!(0x4f;enlist[`contShort]!enlist 4h))
;0x02);`main]~(0x000100000038020000030000004f040002);'"failed"];
recordWithCase3:.binp.compileSchema"
record caseNo
end
record caseYes
field cont array byte x 4
end
record main
field tag int
field data
case tag
0 caseNo
default caseYes
end
end
";
if[not .binp.unparse[recordWithCase3;`tag`data!(1i;enlist[`cont]!enlist 0x02020202);`main]~0x0100000002020202;'"failed"];
if[not .binp.unparse[recordWithCase3;`tag`data!(0i;(`$())!());`main]~0x00000000;'"failed"];
bigEndian:.binp.compileSchema"
record main
field f1 be short
field f2 be int
field f3 be ushort
field f4 be uint
end
";
if[not .binp.unparse[bigEndian;`f1`f2`f3`f4!(1h;1i;32768i;2147483648j);`main]~0x000100000001800080000000;'"failed"];
repeatingAtom:.binp.compileSchema"
record main
field f1 array int repeat
end
";
if[not .binp.unparse[repeatingAtom;enlist[`f1]!enlist 1 2 3i;`main]~0x010000000200000003000000;'"failed"];
repeatingRecord:.binp.compileSchema"
record item
field a int
field b int
end
record main
field f1 array record item repeat
end
";
if[not .binp.unparse[repeatingRecord;enlist[`f1]!enlist(`a`b!1 2i;`a`b!3 4i);`main]~0x01000000020000000300000004000000;'"failed"];
emptyRecordList:.binp.compileSchema"
record item
field a int
field b int
end
record main
field f1 int
field f2 array record item x 0
field f3 int
end
";
if[not .binp.unparse[emptyRecordList;`f1`f2`f3!(1i;();2i);`main]~0x0100000002000000;'"failed"];
sizeTaggedRecord:.binp.compileSchema"
record item
field a byte
field b byte
end
record main
field f1C byte
field f1 parsedArray xv f1C array record item repeat
field f2 byte
end
";
if[not .[.binp.unparse;(sizeTaggedRecord;`f1C`f1`f2!(0x04;(`a`b!0x0102;`a`b!0x0304;`a`b!0x0506);0xff);`main);::]~"main.f1: can't fit parsed data: max(4)<actual(6)";'"failed"];
if[not .binp.unparse[sizeTaggedRecord;`f1C`f1`f2!(0x04;(`a`b!0x0102;`a`b!0x0304);0xff);`main]~0x0401020304ff;'"failed"];
if[not .binp.unparse[sizeTaggedRecord;`f1C`f1`f2!(0x04;enlist`a`b!0x0102;0xff);`main]~0x0401020000ff;'"failed"]; //zero-fill
emptyRecArray:.binp.compileSchema"
record rEmpty
end
record main
field f1 byte
field f2 array record rEmpty x 1
field f3 int
end
";
if[not .binp.unparse[emptyRecArray;`f1`f2`f3!(0x00;enlist(`symbol$())!();0i);`main]~0x0000000000;'"failed"];
longArraySize:.binp.compileSchema"
record r1
field id short
end
record main
field f1 array record r1 x 16
end
";
if[not .binp.unparse[longArraySize;enlist[`f1]!enlist([]id:16#1h);`main]~32#0x0100;'"failed"];
emptyArrayLast:.binp.compileSchema"
record main
field f1L byte
field f1 array short xv f1L
end
";
if[not .binp.unparse[emptyArrayLast;`f1L`f1!(0x00;`short$());`main]~enlist 0x00;'"failed"];
varlengths:.binp.compileSchema"
record main
field f1 dotnetVarLengthInt
field f2 dotnetVarLengthInt
end
";
if[not .binp.unparse[varlengths;`f1`f2!1 188i;`main]~0x01bc01; '"failed"];
unsigned:.binp.compileSchema"
record main
field f1 int
field f2 short
field f3 uint
field f4 ushort
end
";
if[not .binp.unparse[unsigned;`f1`f2`f3`f4!(-1i;-1h;4294967295j;65535i);`main]~0xffffffffffffffffffffffff;'"failed"];
unsignedRecArr:.binp.compileSchema"
record r
field f1 uint
field f2 ushort
end
record main
field f array record r x 2
end
";
if[not .binp.unparse[unsignedRecArr;enlist[`f]!enlist([]f1:2#4294967295;f2:2#65535i);`main]~0xffffffffffffffffffffffff; '"failed"];
unsignedArr:.binp.compileSchema"
record main
field f1 array ushort x 2
field f2 array uint x 2
end
";
if[not .binp.unparse[unsignedArr;`f1`f2!(2#65535i;2#4294967295);`main]~0xffffffffffffffffffffffff; '"failed"];
stringArr:.binp.compileSchema"
record r
field f1 array char x 4
end
record main
field f array record r x 2
end
";
if[not .binp.unparse[stringArr;enlist[`f]!enlist([]f1:("abcd";"efgh"));`main]~0x6162636465666768; '"failed"];
nestedRecordMixed:.binp.compileSchema"
record point
field xpos int
field ypos int
end
record size
field width int
field height int
end
record main
field topLeft record point
field size record size
end
";
if[not .binp.unparse[nestedRecordMixed;`topLeft`size!(`xpos`ypos!1 2i;`width`height!3 4i);`main]~0x01000000020000000300000004000000; '"failed"];
nestedRecord:.binp.compileSchema"
record point
field xpos int
field ypos int
end
record main
field topLeft record point
field bottomRight record point
end
";
if[not .binp.unparse[nestedRecord;`topLeft`bottomRight!(`xpos`ypos!1 2i;`xpos`ypos!3 4i);`main]~0x01000000020000000300000004000000; '"failed"];
inlineSize:.binp.compileSchema"
record r
field f1 int
field f2 recSize int
field f3 array byte repeat
end
record main
field f1 record r
field f2 int
end
";
if[not .binp.unparse[inlineSize;`f1`f2!(`f1`f2`f3!(1i;13i;0x0202020202);3i);`main]~0x010000000d000000020202020203000000; '"failed"];