@cobol-ts/size 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,759 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const storage_size_expression_canonicalise_1 = require("./storage.size.expression.canonicalise");
4
+ const storage_size_expression_evaluate_1 = require("./storage.size.expression.evaluate");
5
+ const noLookup = (_path) => {
6
+ throw new Error("Fixed-size copybook must not read an OCCURS count");
7
+ };
8
+ /*
9
+ * 01 CUSTOMER-RECORD.
10
+ * 05 AGE PIC 99.
11
+ * 05 NAME PIC X(30).
12
+ * 05 POSTCODE PIC X(8).
13
+ */
14
+ describe("canonicalise CUSTOMER-RECORD: consecutive fixed fields", () => {
15
+ const expression = { kind: "sum", terms: [16, { kind: "sum", terms: [240, 64] }] };
16
+ test("flattens nested groups and produces the constant record size", () => {
17
+ expect((0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(expression)).toBe(320);
18
+ expect((0, storage_size_expression_evaluate_1.evaluateStorageSize)((0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(expression), noLookup)).toBe(320);
19
+ });
20
+ });
21
+ /*
22
+ * 01 WAREHOUSE-RECORD.
23
+ * 05 SHELVES OCCURS 2 TIMES.
24
+ * 10 BINS OCCURS 3 TIMES.
25
+ * 15 CODE PIC X(4).
26
+ * 15 QUANTITY PIC 9(3).
27
+ */
28
+ describe("canonicalise WAREHOUSE-RECORD: nested fixed OCCURS", () => {
29
+ const expression = {
30
+ kind: "occurs", count: 2,
31
+ expression: { kind: "occurs", count: 3, expression: { kind: "sum", terms: [32, 24] } },
32
+ };
33
+ test("collapses to the constant size when each occurrence is invariant", () => {
34
+ expect((0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(expression)).toBe(336);
35
+ expect((0, storage_size_expression_evaluate_1.evaluateStorageSize)(expression, noLookup)).toBe(336);
36
+ });
37
+ });
38
+ /*
39
+ * 01 MESSAGE-RECORD.
40
+ * 05 RAW-TEXT PIC X(12).
41
+ * 05 STRUCTURED REDEFINES RAW-TEXT.
42
+ * 10 HEADER PIC X(2).
43
+ * 10 CONTENT PIC X(10).
44
+ */
45
+ describe("canonicalise MESSAGE-RECORD: REDEFINES", () => {
46
+ test("collapses nested maxima to their largest fixed extent", () => {
47
+ const expression = { kind: "max", terms: [96, { kind: "max", terms: [16, 80] }] };
48
+ expect((0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(expression)).toBe(96);
49
+ });
50
+ });
51
+ /*
52
+ * 01 ORDER-RECORD.
53
+ * 05 ITEM-COUNT PIC 9.
54
+ * 05 ITEMS OCCURS 0 TO 9 TIMES DEPENDING ON ITEM-COUNT.
55
+ * 10 ITEM PIC X(10).
56
+ */
57
+ describe("canonicalise ORDER-RECORD: variable OCCURS", () => {
58
+ const expression = {
59
+ kind: "sum", terms: [8, {
60
+ kind: "depending-on", path: ["ORDER-RECORD", "ITEM-COUNT"],
61
+ expression: { kind: "sum", terms: [40, 40] },
62
+ }],
63
+ };
64
+ test("simplifies the body without discarding the record dependency", () => {
65
+ const canonical = (0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(expression);
66
+ expect(canonical).toEqual({ kind: "sum", terms: [
67
+ { kind: "depending-on", path: ["ORDER-RECORD", "ITEM-COUNT"], expression: 80 },
68
+ 8,
69
+ ] });
70
+ for (const count of [0, 1, 4]) {
71
+ const lookup = () => count;
72
+ expect((0, storage_size_expression_evaluate_1.evaluateStorageSize)(canonical, lookup)).toBe((0, storage_size_expression_evaluate_1.evaluateStorageSize)(expression, lookup));
73
+ }
74
+ });
75
+ });
76
+ /*
77
+ * 01 BATCH-RECORD.
78
+ * 05 GROUPS OCCURS 3 TIMES.
79
+ * 10 ITEM-COUNT PIC 9.
80
+ * 10 ITEMS OCCURS 0 TO 9 TIMES DEPENDING ON ITEM-COUNT.
81
+ * 15 ITEM PIC X(10).
82
+ */
83
+ describe("canonicalise BATCH-RECORD: fixed OCCURS containing a dependency", () => {
84
+ const expression = {
85
+ kind: "occurs", count: 3,
86
+ expression: { kind: "sum", terms: [8, {
87
+ kind: "depending-on", path: ["BATCH-RECORD", "GROUPS", 0, "ITEM-COUNT"],
88
+ expression: { kind: "sum", terms: [40, 40] },
89
+ }] },
90
+ };
91
+ test("preserves indexed iteration and simplifies constant subexpressions", () => {
92
+ const canonical = (0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(expression);
93
+ expect(canonical).toEqual({ kind: "occurs", count: 3, expression: {
94
+ kind: "sum", terms: [{ kind: "depending-on",
95
+ path: ["BATCH-RECORD", "GROUPS", 0, "ITEM-COUNT"], expression: 80 }, 8],
96
+ } });
97
+ const counts = [2, 5, 1];
98
+ const lookup = (path) => counts[path[2]];
99
+ expect((0, storage_size_expression_evaluate_1.evaluateStorageSize)(expression, lookup)).toBe(664);
100
+ expect((0, storage_size_expression_evaluate_1.evaluateStorageSize)(canonical, lookup)).toBe(664);
101
+ });
102
+ });
103
+ /*
104
+ * 01 SINGLE-GROUP.
105
+ * 05 GROUPS OCCURS 1 TIMES.
106
+ * 10 COUNT-FIELD PIC 9.
107
+ * 10 VALUES OCCURS 0 TO 9 TIMES DEPENDING ON COUNT-FIELD.
108
+ * 15 VALUE PIC X.
109
+ */
110
+ describe("canonicalise SINGLE-GROUP: preserve a needed index binding", () => {
111
+ test("does not unwrap OCCURS 1 when its child refers to depth zero", () => {
112
+ const expression = { kind: "occurs", count: 1, expression: {
113
+ kind: "sum", terms: [8, { kind: "depending-on",
114
+ path: ["SINGLE-GROUP", "GROUPS", 0, "COUNT-FIELD"], expression: 8,
115
+ }],
116
+ } };
117
+ const canonical = (0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(expression);
118
+ expect(typeof canonical).not.toBe("number");
119
+ expect(canonical).toMatchObject({ kind: "occurs", count: 1 });
120
+ const lookup = (path) => {
121
+ expect(path).toEqual(["SINGLE-GROUP", "GROUPS", 0, "COUNT-FIELD"]);
122
+ return 3;
123
+ };
124
+ expect((0, storage_size_expression_evaluate_1.evaluateStorageSize)(canonical, lookup)).toBe(32);
125
+ });
126
+ });
127
+ describe("canonicalise: algebraic identities", () => {
128
+ test("handles empty sums/maxima and zero fixed occurrences", () => {
129
+ expect((0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)({ kind: "sum", terms: [] })).toBe(0);
130
+ expect((0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)({ kind: "max", terms: [] })).toBe(0);
131
+ expect((0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)({ kind: "occurs", count: 0, expression: 80 })).toBe(0);
132
+ });
133
+ test("is idempotent and leaves its input intact", () => {
134
+ const original = { kind: "sum", terms: [0, { kind: "sum", terms: [8, 16] }] };
135
+ const canonical = (0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(original);
136
+ expect((0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(canonical)).toEqual(canonical);
137
+ expect(original.terms[1]).toEqual({ kind: "sum", terms: [8, 16] });
138
+ });
139
+ });
140
+ /*
141
+ * 01 BATCH-RECORD.
142
+ * 05 GROUP-COUNT PIC 9.
143
+ * 05 GROUPS OCCURS 0 TO 9 TIMES DEPENDING ON GROUP-COUNT.
144
+ * 10 ITEM-COUNT PIC 9.
145
+ * 10 ITEMS OCCURS 0 TO 9 TIMES DEPENDING ON ITEM-COUNT.
146
+ * 15 ITEM-CODE PIC X(2).
147
+ * 15 ITEM-VALUE PIC 9(2).
148
+ *
149
+ * Two nested DEPENDING ON declarations. The inner count is different
150
+ * for each group, so neither repetition can be replaced by a fixed
151
+ * multiplication. The fixed-size ITEM body can still be folded.
152
+ */
153
+ describe("canonicalise BATCH-RECORD: two nested DEPENDING ON", () => {
154
+ const expression = {
155
+ kind: "sum",
156
+ terms: [8, {
157
+ kind: "depending-on",
158
+ path: ["BATCH-RECORD", "GROUP-COUNT"],
159
+ expression: {
160
+ kind: "sum",
161
+ terms: [8, {
162
+ kind: "depending-on",
163
+ path: ["BATCH-RECORD", "GROUPS", 0, "ITEM-COUNT"],
164
+ expression: { kind: "sum", terms: [16, 16] },
165
+ }],
166
+ },
167
+ }],
168
+ };
169
+ test("folds each ITEM body but preserves both dependent repetitions", () => {
170
+ const canonical = (0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(expression);
171
+ expect(canonical).toEqual({
172
+ kind: "sum", terms: [{
173
+ kind: "depending-on",
174
+ path: ["BATCH-RECORD", "GROUP-COUNT"],
175
+ expression: {
176
+ kind: "sum", terms: [{
177
+ kind: "depending-on",
178
+ path: ["BATCH-RECORD", "GROUPS", 0, "ITEM-COUNT"],
179
+ expression: 32,
180
+ }, 8],
181
+ },
182
+ }, 8],
183
+ });
184
+ });
185
+ test.each([
186
+ { counts: [], expected: 8 },
187
+ { counts: [0], expected: 16 },
188
+ { counts: [2, 3], expected: 184 },
189
+ { counts: [3, 0, 1], expected: 160 },
190
+ ])("preserves per-group counts $counts and size $expected", ({ counts, expected }) => {
191
+ const seen = [];
192
+ const lookup = (path) => {
193
+ seen.push([...path]);
194
+ if (path.length === 2)
195
+ return counts.length;
196
+ return counts[path[2]];
197
+ };
198
+ const canonical = (0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(expression);
199
+ expect((0, storage_size_expression_evaluate_1.evaluateStorageSize)(expression, lookup)).toBe(expected);
200
+ expect((0, storage_size_expression_evaluate_1.evaluateStorageSize)(canonical, lookup)).toBe(expected);
201
+ expect((0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(canonical)).toEqual(canonical);
202
+ // Both evaluations only ask for the count fields that actually exist.
203
+ expect(seen.slice(0, counts.length + 1)).toEqual([
204
+ ["BATCH-RECORD", "GROUP-COUNT"],
205
+ ...counts.map((_, index) => ["BATCH-RECORD", "GROUPS", index, "ITEM-COUNT"]),
206
+ ]);
207
+ });
208
+ });
209
+ /*
210
+ * 01 DEEP-RECORD.
211
+ * 05 GROUP-COUNT PIC 9.
212
+ * 05 GROUPS OCCURS 0 TO 9 TIMES DEPENDING ON GROUP-COUNT.
213
+ * 10 ITEM-COUNT PIC 9.
214
+ * 10 ITEMS OCCURS 0 TO 9 TIMES DEPENDING ON ITEM-COUNT.
215
+ * 15 DETAIL-COUNT PIC 9.
216
+ * 15 DETAILS OCCURS 0 TO 9 TIMES DEPENDING ON DETAIL-COUNT.
217
+ * 20 DETAIL-CODE PIC X(2).
218
+ * 20 DETAIL-VALUE PIC X(2).
219
+ *
220
+ * The innermost count path uses TWO different enclosing indices:
221
+ * depth 1 for GROUPS and depth 0 for ITEMS. Canonicalisation must not
222
+ * remove either binding, even when it simplifies the fixed DETAIL body.
223
+ */
224
+ describe("canonicalise DEEP-RECORD: three nested DEPENDING ON", () => {
225
+ const expression = {
226
+ kind: "sum", terms: [8, {
227
+ kind: "depending-on", path: ["DEEP-RECORD", "GROUP-COUNT"],
228
+ expression: {
229
+ kind: "sum", terms: [8, {
230
+ kind: "depending-on", path: ["DEEP-RECORD", "GROUPS", 0, "ITEM-COUNT"],
231
+ expression: {
232
+ kind: "sum", terms: [8, {
233
+ kind: "depending-on",
234
+ path: ["DEEP-RECORD", "GROUPS", 1, "ITEMS", 0, "DETAIL-COUNT"],
235
+ expression: { kind: "sum", terms: [16, 16] },
236
+ }],
237
+ },
238
+ }],
239
+ },
240
+ }],
241
+ };
242
+ test("preserves all three dependencies and both relative path depths", () => {
243
+ const canonical = (0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(expression);
244
+ expect(canonical).toEqual({
245
+ kind: "sum", terms: [{
246
+ kind: "depending-on", path: ["DEEP-RECORD", "GROUP-COUNT"],
247
+ expression: {
248
+ kind: "sum", terms: [{
249
+ kind: "depending-on", path: ["DEEP-RECORD", "GROUPS", 0, "ITEM-COUNT"],
250
+ expression: {
251
+ kind: "sum", terms: [{
252
+ kind: "depending-on",
253
+ path: ["DEEP-RECORD", "GROUPS", 1, "ITEMS", 0, "DETAIL-COUNT"],
254
+ expression: 32,
255
+ }, 8],
256
+ },
257
+ }, 8],
258
+ },
259
+ }, 8],
260
+ });
261
+ });
262
+ test("evaluates different detail counts for different items and groups", () => {
263
+ const details = [[1, 3], [2]];
264
+ const seen = [];
265
+ const lookup = (path) => {
266
+ seen.push([...path]);
267
+ if (path.length === 2)
268
+ return details.length;
269
+ const group = path[2];
270
+ if (path[3] === "ITEM-COUNT")
271
+ return details[group].length;
272
+ return details[group][path[4]];
273
+ };
274
+ const canonical = (0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(expression);
275
+ expect((0, storage_size_expression_evaluate_1.evaluateStorageSize)(expression, lookup)).toBe(240);
276
+ expect(seen).toEqual([
277
+ ["DEEP-RECORD", "GROUP-COUNT"],
278
+ ["DEEP-RECORD", "GROUPS", 0, "ITEM-COUNT"],
279
+ ["DEEP-RECORD", "GROUPS", 0, "ITEMS", 0, "DETAIL-COUNT"],
280
+ ["DEEP-RECORD", "GROUPS", 0, "ITEMS", 1, "DETAIL-COUNT"],
281
+ ["DEEP-RECORD", "GROUPS", 1, "ITEM-COUNT"],
282
+ ["DEEP-RECORD", "GROUPS", 1, "ITEMS", 0, "DETAIL-COUNT"],
283
+ ]);
284
+ seen.length = 0;
285
+ expect((0, storage_size_expression_evaluate_1.evaluateStorageSize)(canonical, lookup)).toBe(240);
286
+ expect(seen).toHaveLength(6);
287
+ expect((0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(canonical)).toEqual(canonical);
288
+ });
289
+ test("does not resolve nonexistent descendants when an inner count is zero", () => {
290
+ const seen = [];
291
+ const lookup = (path) => {
292
+ seen.push([...path]);
293
+ if (path.length === 2)
294
+ return 2;
295
+ if (path[3] === "ITEM-COUNT")
296
+ return path[2] === 0 ? 0 : 1;
297
+ return 0;
298
+ };
299
+ // GROUP-COUNT(8) + GROUP[0](8) + GROUP[1](8)
300
+ // + ITEM[0] in GROUP[1] (DETAIL-COUNT 8, zero details).
301
+ expect((0, storage_size_expression_evaluate_1.evaluateStorageSize)((0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(expression), lookup)).toBe(32);
302
+ expect(seen).toEqual([
303
+ ["DEEP-RECORD", "GROUP-COUNT"],
304
+ ["DEEP-RECORD", "GROUPS", 0, "ITEM-COUNT"],
305
+ ["DEEP-RECORD", "GROUPS", 1, "ITEM-COUNT"],
306
+ ["DEEP-RECORD", "GROUPS", 1, "ITEMS", 0, "DETAIL-COUNT"],
307
+ ]);
308
+ });
309
+ });
310
+ /*
311
+ * 01 OVERLAY-RECORD.
312
+ * 05 ITEM-COUNT PIC 9.
313
+ * 05 RAW-AREA PIC X(8).
314
+ * 05 STRUCTURED-AREA REDEFINES RAW-AREA.
315
+ * 10 ITEMS OCCURS 0 TO 9 TIMES DEPENDING ON ITEM-COUNT.
316
+ * 15 CODE PIC X(4).
317
+ *
318
+ * This models a variable-sized overlay mathematically. Whether a dialect
319
+ * permits this particular REDEFINES/ODO combination is a separate check.
320
+ * The shared extent is the maximum of the alternative representations.
321
+ */
322
+ describe("canonicalise OVERLAY-RECORD: REDEFINES containing DEPENDING ON", () => {
323
+ const expression = {
324
+ kind: "sum", terms: [8, {
325
+ kind: "max", terms: [64, {
326
+ kind: "depending-on", path: ["OVERLAY-RECORD", "ITEM-COUNT"],
327
+ expression: { kind: "sum", terms: [16, 16] },
328
+ }],
329
+ }],
330
+ };
331
+ test("preserves the dependent alternative instead of folding the maximum", () => {
332
+ expect((0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(expression)).toEqual({
333
+ kind: "sum", terms: [{
334
+ kind: "max", terms: [{
335
+ kind: "depending-on", path: ["OVERLAY-RECORD", "ITEM-COUNT"], expression: 32,
336
+ }, 64],
337
+ }, 8],
338
+ });
339
+ });
340
+ test.each([
341
+ [0, 72], [1, 72], [2, 72], [3, 104], [9, 296],
342
+ ])("preserves the maximum for ITEM-COUNT %i (%i bits)", (count, expected) => {
343
+ const lookup = jest.fn((path) => {
344
+ expect(path).toEqual(["OVERLAY-RECORD", "ITEM-COUNT"]);
345
+ return count;
346
+ });
347
+ expect((0, storage_size_expression_evaluate_1.evaluateStorageSize)(expression, lookup)).toBe(expected);
348
+ expect((0, storage_size_expression_evaluate_1.evaluateStorageSize)((0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(expression), lookup)).toBe(expected);
349
+ });
350
+ });
351
+ /*
352
+ * 01 MIXED-RECORD.
353
+ * 05 ROW-COUNT PIC 9.
354
+ * 05 ROWS OCCURS 0 TO 9 TIMES DEPENDING ON ROW-COUNT.
355
+ * 10 ROW-CODE PIC X.
356
+ * 10 SLOTS OCCURS 2 TIMES.
357
+ * 15 DETAIL-COUNT PIC 9.
358
+ * 15 DETAILS OCCURS 0 TO 9 TIMES DEPENDING ON DETAIL-COUNT.
359
+ * 20 DETAIL PIC X(2).
360
+ *
361
+ * Both kinds of OCCURS bind an index. In DETAIL-COUNT's symbolic path,
362
+ * depth 1 denotes ROWS and depth 0 denotes SLOTS.
363
+ */
364
+ describe("canonicalise MIXED-RECORD: variable, fixed, variable nesting", () => {
365
+ const expression = {
366
+ kind: "sum", terms: [8, {
367
+ kind: "depending-on", path: ["MIXED-RECORD", "ROW-COUNT"],
368
+ expression: { kind: "sum", terms: [8, {
369
+ kind: "occurs", count: 2, expression: { kind: "sum", terms: [8, {
370
+ kind: "depending-on",
371
+ path: ["MIXED-RECORD", "ROWS", 1, "SLOTS", 0, "DETAIL-COUNT"],
372
+ expression: { kind: "sum", terms: [8, 8] },
373
+ }] },
374
+ }] },
375
+ }],
376
+ };
377
+ test("retains all index-producing expressions while simplifying detail size", () => {
378
+ const canonical = (0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(expression);
379
+ expect(JSON.stringify(canonical)).toContain('"count":2');
380
+ expect(JSON.stringify(canonical)).toContain('"ROWS",1,"SLOTS",0');
381
+ expect((0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(canonical)).toEqual(canonical);
382
+ });
383
+ test.each([
384
+ { details: [], expected: 8 },
385
+ { details: [[0, 0]], expected: 32 },
386
+ { details: [[1, 2], [0, 3]], expected: 152 },
387
+ { details: [[9, 0], [0, 0], [1, 1]], expected: 8 + 3 * 24 + 16 * 11 },
388
+ ])("preserves all nested counts for $details", ({ details, expected }) => {
389
+ const seen = [];
390
+ const lookup = (path) => {
391
+ seen.push([...path]);
392
+ if (path.length === 2)
393
+ return details.length;
394
+ return details[path[2]][path[4]];
395
+ };
396
+ const canonical = (0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(expression);
397
+ expect((0, storage_size_expression_evaluate_1.evaluateStorageSize)(expression, lookup)).toBe(expected);
398
+ expect((0, storage_size_expression_evaluate_1.evaluateStorageSize)(canonical, lookup)).toBe(expected);
399
+ expect(seen.filter(path => path.length === 6)).toHaveLength(details.length * 4);
400
+ });
401
+ });
402
+ /*
403
+ * 01 TWO-LISTS.
404
+ * 05 LEFT-COUNT PIC 9.
405
+ * 05 LEFT-ITEMS OCCURS 0 TO 9 TIMES DEPENDING ON LEFT-COUNT.
406
+ * 10 LEFT-CODE PIC X(2).
407
+ * 05 RIGHT-COUNT PIC 9.
408
+ * 05 RIGHT-ITEMS OCCURS 0 TO 9 TIMES DEPENDING ON RIGHT-COUNT.
409
+ * 10 RIGHT-CODE PIC X(3).
410
+ *
411
+ * Sibling repetitions must not inherit one another's occurrence indices.
412
+ */
413
+ describe("canonicalise TWO-LISTS: sibling dependencies", () => {
414
+ const expression = {
415
+ kind: "sum", terms: [8, {
416
+ kind: "depending-on", path: ["TWO-LISTS", "LEFT-COUNT"],
417
+ expression: { kind: "sum", terms: [8, 8] },
418
+ }, 8, {
419
+ kind: "depending-on", path: ["TWO-LISTS", "RIGHT-COUNT"],
420
+ expression: { kind: "sum", terms: [16, 8] },
421
+ }],
422
+ };
423
+ test.each([
424
+ [0, 0], [1, 0], [0, 2], [3, 4], [9, 1],
425
+ ])("keeps LEFT=%i and RIGHT=%i independent", (left, right) => {
426
+ const seen = [];
427
+ const lookup = (path) => {
428
+ seen.push([...path]);
429
+ return path[1] === "LEFT-COUNT" ? left : right;
430
+ };
431
+ const expected = 16 + 16 * left + 24 * right;
432
+ expect((0, storage_size_expression_evaluate_1.evaluateStorageSize)(expression, lookup)).toBe(expected);
433
+ expect((0, storage_size_expression_evaluate_1.evaluateStorageSize)((0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(expression), lookup)).toBe(expected);
434
+ expect(seen).toEqual([
435
+ ["TWO-LISTS", "LEFT-COUNT"], ["TWO-LISTS", "RIGHT-COUNT"],
436
+ ["TWO-LISTS", "LEFT-COUNT"], ["TWO-LISTS", "RIGHT-COUNT"],
437
+ ]);
438
+ });
439
+ });
440
+ /*
441
+ * 01 EDGE-RECORD.
442
+ * 05 EMPTY-TABLE OCCURS 0 TIMES.
443
+ * 10 UNREACHABLE-COUNT PIC 9.
444
+ * 10 UNREACHABLE OCCURS 0 TO 9 TIMES DEPENDING ON UNREACHABLE-COUNT.
445
+ * 15 VALUE PIC X.
446
+ * 05 ONE-TABLE OCCURS 1 TIMES.
447
+ * 10 COUNT-FIELD PIC 9.
448
+ * 10 VALUES OCCURS 0 TO 9 TIMES DEPENDING ON COUNT-FIELD.
449
+ * 15 VALUE PIC X.
450
+ *
451
+ * The zero table is unreachable. The one table must keep its binding.
452
+ */
453
+ describe("canonicalise EDGE-RECORD: zero and one occurrences", () => {
454
+ const expression = {
455
+ kind: "sum", terms: [
456
+ { kind: "occurs", count: 0, expression: {
457
+ kind: "depending-on", path: ["EDGE-RECORD", "EMPTY-TABLE", 0, "UNREACHABLE-COUNT"],
458
+ expression: 8,
459
+ } },
460
+ { kind: "occurs", count: 1, expression: { kind: "sum", terms: [8, {
461
+ kind: "depending-on", path: ["EDGE-RECORD", "ONE-TABLE", 0, "COUNT-FIELD"],
462
+ expression: 8,
463
+ }] } },
464
+ ],
465
+ };
466
+ test("removes the zero branch without unbinding the one branch", () => {
467
+ const canonical = (0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(expression);
468
+ expect(canonical).toMatchObject({ kind: "occurs", count: 1 });
469
+ const lookup = jest.fn((path) => {
470
+ expect(path).toEqual(["EDGE-RECORD", "ONE-TABLE", 0, "COUNT-FIELD"]);
471
+ return 4;
472
+ });
473
+ expect((0, storage_size_expression_evaluate_1.evaluateStorageSize)(expression, lookup)).toBe(40);
474
+ expect((0, storage_size_expression_evaluate_1.evaluateStorageSize)(canonical, lookup)).toBe(40);
475
+ expect(lookup).toHaveBeenCalledTimes(2);
476
+ });
477
+ });
478
+ /*
479
+ * 01 FIXED-DEEP.
480
+ * 05 REGIONS OCCURS 2 TIMES.
481
+ * 10 AISLES OCCURS 3 TIMES.
482
+ * 15 BINS OCCURS 4 TIMES.
483
+ * 20 LABEL PIC X(2).
484
+ * 20 QUANTITY PIC 9(3).
485
+ * 05 SUMMARY PIC X(5).
486
+ *
487
+ * Every extent is fixed: 2 * 3 * 4 * (16 + 24) + 40 = 1000 bits.
488
+ */
489
+ describe("canonicalise FIXED-DEEP: entirely constant nested layout", () => {
490
+ const expression = { kind: "sum", terms: [
491
+ { kind: "occurs", count: 2, expression: {
492
+ kind: "occurs", count: 3, expression: {
493
+ kind: "occurs", count: 4, expression: {
494
+ kind: "sum", terms: [16, { kind: "sum", terms: [8, 16] }],
495
+ },
496
+ },
497
+ } },
498
+ 40,
499
+ ] };
500
+ test("collapses the whole record to a single number", () => {
501
+ expect((0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(expression)).toBe(1000);
502
+ expect((0, storage_size_expression_evaluate_1.evaluateStorageSize)(expression, noLookup)).toBe(1000);
503
+ expect((0, storage_size_expression_evaluate_1.evaluateStorageSize)((0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(expression), noLookup)).toBe(1000);
504
+ });
505
+ });
506
+ /*
507
+ * 01 MANY-RECORDS.
508
+ * 05 GROUPS OCCURS 3 TIMES.
509
+ * 10 ITEM-COUNT PIC 9.
510
+ * 10 ITEMS OCCURS 0 TO 9 TIMES DEPENDING ON ITEM-COUNT.
511
+ * 15 ITEM PIC X(10).
512
+ *
513
+ * Varying the record's counts checks the canonicalisation for more
514
+ * than one favourable data set.
515
+ */
516
+ describe("canonicalise MANY-RECORDS: different count configurations", () => {
517
+ const expression = {
518
+ kind: "occurs", count: 3, expression: { kind: "sum", terms: [8, {
519
+ kind: "depending-on", path: ["MANY-RECORDS", "GROUPS", 0, "ITEM-COUNT"],
520
+ expression: { kind: "sum", terms: [40, 40] },
521
+ }] },
522
+ };
523
+ test.each([
524
+ [0, 0, 0], [2, 5, 1], [9, 0, 3], [1, 1, 1], [0, 9, 9], [9, 9, 9],
525
+ ])("preserves the size for counts %i, %i, %i", (a, b, c) => {
526
+ const counts = [a, b, c];
527
+ const resolved = [];
528
+ const lookup = (path) => {
529
+ resolved.push([...path]);
530
+ return counts[path[2]];
531
+ };
532
+ const canonical = (0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(expression);
533
+ const expected = 24 + 80 * (a + b + c);
534
+ expect((0, storage_size_expression_evaluate_1.evaluateStorageSize)(expression, lookup)).toBe(expected);
535
+ expect((0, storage_size_expression_evaluate_1.evaluateStorageSize)(canonical, lookup)).toBe(expected);
536
+ expect(resolved).toEqual([
537
+ ...[0, 1, 2].map(i => ["MANY-RECORDS", "GROUPS", i, "ITEM-COUNT"]),
538
+ ...[0, 1, 2].map(i => ["MANY-RECORDS", "GROUPS", i, "ITEM-COUNT"]),
539
+ ]);
540
+ expect((0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(canonical)).toEqual(canonical);
541
+ });
542
+ });
543
+ /*
544
+ * 01 ALIGNED-RECORD.
545
+ * 05 PREFIX PIC X.
546
+ * 05 AMOUNT PIC S9(9) COMP SYNC.
547
+ *
548
+ * AMOUNT needs a four-byte boundary. PREFIX occupies one byte,
549
+ * so the compiler introduces three bytes of padding.
550
+ */
551
+ describe("canonicalise ALIGNED-RECORD: fixed alignment", () => {
552
+ test.each([
553
+ [0, 0],
554
+ [8, 24],
555
+ [16, 16],
556
+ [24, 8],
557
+ [32, 0],
558
+ [40, 24],
559
+ [64, 0],
560
+ ])("aligns offset %i to 32 bits with %i bits of padding", (size, expected) => {
561
+ const expression = {
562
+ kind: "align", size, boundary: 32
563
+ };
564
+ expect((0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(expression)).toBe(expected);
565
+ expect((0, storage_size_expression_evaluate_1.evaluateStorageSize)(expression, noLookup)).toBe(expected);
566
+ });
567
+ test("canonicalises the alignment input before calculating padding", () => {
568
+ const expression = {
569
+ kind: "align",
570
+ size: { kind: "sum", terms: [8, { kind: "sum", terms: [8, 8] }] },
571
+ boundary: 32
572
+ };
573
+ expect((0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(expression)).toBe(8);
574
+ });
575
+ test("supports halfword and doubleword boundaries", () => {
576
+ expect((0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)({
577
+ kind: "align", size: 8, boundary: 16
578
+ })).toBe(8);
579
+ expect((0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)({
580
+ kind: "align", size: 8, boundary: 64
581
+ })).toBe(56);
582
+ });
583
+ });
584
+ /*
585
+ * 01 VARIABLE-RECORD.
586
+ * 05 ITEM-COUNT PIC 9.
587
+ * 05 ITEMS OCCURS 0 TO 9 TIMES DEPENDING ON ITEM-COUNT.
588
+ * 10 CODE PIC X(3).
589
+ * 05 AMOUNT PIC S9(9) COMP SYNC.
590
+ *
591
+ * AMOUNT's position is 8 + 24 * ITEM-COUNT bits.
592
+ * Its alignment padding depends on the actual count.
593
+ */
594
+ describe("canonicalise VARIABLE-RECORD: alignment after ODO", () => {
595
+ const expression = {
596
+ kind: "align",
597
+ size: {
598
+ kind: "sum",
599
+ terms: [
600
+ 8,
601
+ {
602
+ kind: "depending-on",
603
+ path: ["VARIABLE-RECORD", "ITEM-COUNT"],
604
+ expression: { kind: "sum", terms: [8, 16] }
605
+ }
606
+ ]
607
+ },
608
+ boundary: 32
609
+ };
610
+ test("simplifies the occurrence size but retains symbolic alignment", () => {
611
+ expect((0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(expression)).toEqual({
612
+ kind: "align",
613
+ size: {
614
+ kind: "sum",
615
+ terms: [{
616
+ kind: "depending-on",
617
+ path: ["VARIABLE-RECORD", "ITEM-COUNT"],
618
+ expression: 24
619
+ }, 8]
620
+ },
621
+ boundary: 32
622
+ });
623
+ });
624
+ test.each([
625
+ [0, 24],
626
+ [1, 0],
627
+ [2, 8],
628
+ [3, 16],
629
+ [4, 24],
630
+ [5, 0],
631
+ ])("calculates padding for ITEM-COUNT %i", (count, expected) => {
632
+ const lookup = (path) => {
633
+ expect(path).toEqual(["VARIABLE-RECORD", "ITEM-COUNT"]);
634
+ return count;
635
+ };
636
+ expect((0, storage_size_expression_evaluate_1.evaluateStorageSize)(expression, lookup)).toBe(expected);
637
+ expect((0, storage_size_expression_evaluate_1.evaluateStorageSize)((0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(expression), lookup))
638
+ .toBe(expected);
639
+ });
640
+ test("is idempotent and does not mutate the original expression", () => {
641
+ const original = JSON.stringify(expression);
642
+ const canonical = (0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(expression);
643
+ expect((0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(canonical)).toEqual(canonical);
644
+ expect(JSON.stringify(expression)).toBe(original);
645
+ });
646
+ });
647
+ /*
648
+ * 01 GROUP-RECORD.
649
+ * 05 GROUPS OCCURS 3 TIMES.
650
+ * 10 ITEM-COUNT PIC 9.
651
+ * 10 ITEMS OCCURS 0 TO 9 TIMES DEPENDING ON ITEM-COUNT.
652
+ * 15 CODE PIC X(3).
653
+ * 10 AMOUNT PIC S9(9) COMP SYNC.
654
+ *
655
+ * Each GROUPS occurrence can have a different ITEM-COUNT and
656
+ * therefore a different amount of padding before AMOUNT.
657
+ *
658
+ * The fixed OCCURS binding must survive canonicalisation.
659
+ */
660
+ describe("canonicalise GROUP-RECORD: alignment inside fixed OCCURS", () => {
661
+ const expression = {
662
+ kind: "occurs",
663
+ count: 3,
664
+ expression: {
665
+ kind: "align",
666
+ size: {
667
+ kind: "sum",
668
+ terms: [
669
+ 8,
670
+ {
671
+ kind: "depending-on",
672
+ path: ["GROUP-RECORD", "GROUPS", 0, "ITEM-COUNT"],
673
+ expression: 24
674
+ }
675
+ ]
676
+ },
677
+ boundary: 32
678
+ }
679
+ };
680
+ test("preserves the OCCURS binding around symbolic alignment", () => {
681
+ const canonical = (0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(expression);
682
+ expect(canonical).toMatchObject({
683
+ kind: "occurs",
684
+ count: 3,
685
+ expression: {
686
+ kind: "align",
687
+ boundary: 32
688
+ }
689
+ });
690
+ });
691
+ test("evaluates a different padding amount for each occurrence", () => {
692
+ const counts = [0, 1, 2];
693
+ const seen = [];
694
+ const lookup = (path) => {
695
+ seen.push([...path]);
696
+ return counts[path[2]];
697
+ };
698
+ const canonical = (0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(expression);
699
+ // COUNT 0 -> 24 bits, COUNT 1 -> 0 bits, COUNT 2 -> 8 bits.
700
+ expect((0, storage_size_expression_evaluate_1.evaluateStorageSize)(expression, lookup)).toBe(32);
701
+ expect((0, storage_size_expression_evaluate_1.evaluateStorageSize)(canonical, lookup)).toBe(32);
702
+ expect(seen).toEqual([
703
+ ...[0, 1, 2].map(i => ["GROUP-RECORD", "GROUPS", i, "ITEM-COUNT"]),
704
+ ...[0, 1, 2].map(i => ["GROUP-RECORD", "GROUPS", i, "ITEM-COUNT"])
705
+ ]);
706
+ });
707
+ });
708
+ /*
709
+ * 01 NESTED-ALIGNMENT.
710
+ * 05 PREFIX PIC X.
711
+ * 05 AMOUNT PIC S9(9) COMP SYNC.
712
+ *
713
+ * Aligning the position after adding the first padding amount
714
+ * must require no further padding at the same boundary.
715
+ */
716
+ describe("canonicalise NESTED-ALIGNMENT: nested alignment expressions", () => {
717
+ test("folds nested alignment when its inputs are constant", () => {
718
+ const expression = {
719
+ kind: "align",
720
+ size: {
721
+ kind: "sum",
722
+ terms: [
723
+ 8,
724
+ { kind: "align", size: 8, boundary: 32 }
725
+ ]
726
+ },
727
+ boundary: 32
728
+ };
729
+ expect((0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(expression)).toBe(0);
730
+ expect((0, storage_size_expression_evaluate_1.evaluateStorageSize)(expression, noLookup)).toBe(0);
731
+ });
732
+ test("retains symbolic nested alignment without losing information", () => {
733
+ const variable = {
734
+ kind: "depending-on",
735
+ path: ["NESTED-ALIGNMENT", "COUNT"],
736
+ expression: 24
737
+ };
738
+ const expression = {
739
+ kind: "align",
740
+ size: {
741
+ kind: "sum",
742
+ terms: [
743
+ variable,
744
+ { kind: "align", size: variable, boundary: 32 }
745
+ ]
746
+ },
747
+ boundary: 32
748
+ };
749
+ const canonical = (0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(expression);
750
+ expect((0, storage_size_expression_canonicalise_1.canonicaliseStorageSize)(canonical)).toEqual(canonical);
751
+ for (const count of [0, 1, 2, 3, 4]) {
752
+ const lookup = () => count;
753
+ expect((0, storage_size_expression_evaluate_1.evaluateStorageSize)(canonical, lookup)).toBe(0);
754
+ expect((0, storage_size_expression_evaluate_1.evaluateStorageSize)(canonical, lookup))
755
+ .toBe((0, storage_size_expression_evaluate_1.evaluateStorageSize)(expression, lookup));
756
+ }
757
+ });
758
+ });
759
+ //# sourceMappingURL=storage.size.expression.canonicalise.spec.js.map