-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLayoutBap.java
More file actions
765 lines (666 loc) · 32.4 KB
/
LayoutBap.java
File metadata and controls
765 lines (666 loc) · 32.4 KB
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
//------------------------------------------------------------------------------
// Basic Array Machine with layout and execution
// Philip R Brenan at appaapps dot com, Appa Apps Ltd Inc., 2025
//------------------------------------------------------------------------------
package com.AppaApps.Silicon; // Btree in a block on the surface of a silicon chip.
import java.util.*;
abstract class LayoutBap extends Test // Layout the array used by the basic array machine
{int size = 0; // The size of the memory used by the basic array machine
final Stack <Array> order = new Stack<>(); // The order in which the arrays were defined
final TreeMap<String,Array> arrays = new TreeMap<>(); // A number of different sized arrays concatenated to make one array
final int[]memory; // The concatenation of all the arrays
final int[]save; // A save area so we can save and restore before printing
final Stack<I> code = new Stack<>(); // Code of the program
final int maxTime = 1000; // Maximum numner of steps permitted while running the program
int step = 0; // Execution step
int time = 0; // Execution time
boolean running = false; // Executing if true
Stack<Label> labels = new Stack<>(); // Labels for some instructions
LayoutBap() // Create a layout
{load(); // Load array definitions
memory = new int[size]; // Create the memory for the basic array machine
save = new int[size]; // Create the save area
}
void load() {} // Override this method to define the layout
void save() // Save the memory
{final int N = memory.length;
for (int i = 0; i < N; i++) save[i] = memory[i];
}
void restore() // Restore the memory
{final int N = memory.length;
for (int i = 0; i < N; i++) memory[i] = save[i];
}
class Array
{final String name;
final int[]dimensions;
int base; // Base position of this array
final int length; // Total number of elements in this array
Array(String Name, int...Dimensions) // Array definition within the basic array machine
{name = Name;
final int D = Dimensions.length;
if (D > 2) stop("Too many dimensions:", D); // Too many dimensions
dimensions = new int[D];
for (int i = 0; i < D; i++) dimensions[D-1-i] = Dimensions[i]; // Save the dimensions. A zero dimension array is a field.
base = size;
size += length = switch(D) // Number of elements in this array
{case 0 -> 1;
case 1 -> dimensions[0];
default -> dimensions[1] * dimensions[0];
};
order.push(this); // The definition order
if (arrays.containsKey(name))
{stop("Name :", name, "has already been defined");
}
arrays.put(name, this); // Make the array accessible by name
}
}
Array array(String Name, int...Dimensions) // Array definition within the basic array machine
{return new Array(Name, Dimensions);
}
Array overlay(String Name, int...Dimensions) // Overlay this definition over the previous one
{if (order.size() == 0) stop("No previous array to overlay"); // Nothing to overlay
final Array p = order.lastElement(); // Previous array which we will overlay
final Array q = array(Name, Dimensions); // New array
q.base = p.base; // Overlay on previous array
size = size - p.length + max(p.length, q.length); // Account for overall size
return q; // Return overlay
}
private Array getArray(String Name) // Get an array by name or complain if the name is invalid
{final Array A = arrays.get(Name);
if (A == null) stop("No such array as", Name);
return A;
}
int lookUpIndex(Array Array, int Dimension, String Index) // Look up an index which will be used in the specified dimension of the specified array
{int v; // The value of the index
try
{v = Integer.parseInt(Index); // The integer has been supplied directly as a string that we can parse
}
catch (NumberFormatException e) // The index is a reference to an array
{final Array I = getArray(Index); // Hopefully the index is a field - a zero dimensional array
if (I.dimensions.length > 0) // Complain if the index is not a field
{stop("Index:", Index, "cannot itself be be an indexed array element");
}
v = memory[I.base]; // The value of the index
}
if (v < 0) stop("Index:", Index, "is negative");
if (v >= Array.dimensions[Dimension]) stop("Index:", Index, "value", v, "is greater than dimension["+Dimension+"] with extent:", Array.dimensions[Dimension], "in array:", Array.name);
return v;
}
void set(int Value, String target, String...Indices) // Set the value of an array element
{final Array t = getArray(target);
final String[]i = Indices;
final int I = i.length;
final int T = t.dimensions.length;
if (T != I) stop("Wrong number of dimensions:", T, "!=", I, "for array:", target);
final int v = switch(T)
{case 0 -> memory[t.base] = Value;
case 1 -> memory[t.base+lookUpIndex(t, 0, i[0])] = Value;
default -> memory[t.base+lookUpIndex(t, 1, i[0])*t.dimensions[0]+lookUpIndex(t, 0, i[1])] = Value;
};
}
void set(String target, int...values) // Load an array
{final Array t = getArray(target);
final int N = values.length;
if (N > t.length) stop("Too many initializers for array:", t.name, "with length", t.length);
for (int i = 0; i < N; i++) memory[t.base+i] = values[i];
}
int get(String source, String...Indices) // Get the value of an array element
{final Array s = getArray(source);
final String[]i = Indices;
final int I = i.length;
final int S = s.dimensions.length;
if (S != I) stop("Wrong number of dimensions:", S, "!=", I, "for array:", source);
return switch(S)
{case 0 -> memory[s.base];
case 1 -> memory[s.base+lookUpIndex(s, 0, i[0])];
default -> memory[s.base+lookUpIndex(s, 1, i[0])*s.dimensions[0]+lookUpIndex(s, 0, i[1])];
};
}
void clear(int Value, String target, String...Indices) // Clear the indicated part of the specified array to the the specified value presumed to be a constant, typically -1 or 0
{final Array t = getArray(target);
final String[]i = Indices;
final int I = i.length;
final int T = t.dimensions.length;
if (I > T) stop("Too many dimensions:", I, ">", T, "for array:", target);
if (I == 0) // Clear entire array
{for (int j = 0; j < t.length; j++)
{final int J = j;
new I() {void a() {memory[t.base + J] = Value;}};
}
return;
}
if ((I == 1 && T == 1) || (I == 2 && T == 2)) // Clear single element of one or two dimensional array
{set(Value, target, Indices);
return;
}
if (I == 1 && T == 2) // Clear indicated sub array
{final int l = t.dimensions[0]; // Size of sub array
final int p = l * lookUpIndex(t, 1, i[0]); // Start of sub array
for (int j = 0; j < l; j++) memory[t.base+ p + j] = Value; // Clear sub array in two dimensional array
return;
}
}
void zero(String source, String...Indices) {clear( 0, source, Indices);} // Zero the indicated part of the specified array
void ones(String source, String...Indices) {clear(-1, source, Indices);} // Set the indicated part of the specified array to all ones
void move(String target, String source, String...Indices) // Copy the indexed source field to the indexed target fields
{final Array t = getArray(target), s = getArray(source);
final int I = Indices.length;
final int T = t.dimensions.length, S = s.dimensions.length;
if (T + S != I) stop("Wrong number of dimensions:", T, "+", S, "!=", I, "for arrays:", target+",", source);
final String[]ti = new String[T]; // Assign indices
final String[]si = new String[S];
for (int i = 0; i < T; i++) ti[i] = Indices[i];
for (int i = T; i < I; i++) si[i-T] = Indices[i];
set(get(source, si), target, ti);
}
int compareImmediate(int immediate, String source, String...Indices) // Compare the indexed source field to the immediate value returning -1 if source is less that target, 0 if equal otherwise +1
{final Array s = getArray(source);
final int I = Indices.length;
final int S = s.dimensions.length;
if (S != I) stop("Wrong number of dimensions:", S, "!=", I, "for array:", source);
final int a = immediate;
final int b = get(source, Indices);
return Integer.valueOf(b).compareTo(Integer.valueOf(a));
}
int compare(String target, String source, String...Indices) // Compare the indexed source field to the indexed target field returning -1 if source is less that target, 0 if equal otherwise +1
{final Array t = getArray(target), s = getArray(source);
final int I = Indices.length;
final int T = t.dimensions.length, S = s.dimensions.length;
if (T + S != I) stop("Wrong number of dimensions:", T, "+", S, "!=", I); // Check we have the right number of indices
final String[]ti = new String[T]; // Assign indices
final String[]si = new String[S];
for (int i = 0; i < T; i++) ti[i] = Indices[i];
for (int i = T; i < I; i++) si[i-T] = Indices[i];
final int a = get(source, si);
final int b = get(target, ti);
return Integer.valueOf(b).compareTo(Integer.valueOf(a));
}
void compareImmediateAndSet // Compare the indexed source field to the immediate value storing -1 in the target if the source is less than the immediate value, 0 if equal otherwise +1
(String Target, int immediate, String Source, String...Indices)
{final Array s = getArray(Source);
final Array t = getArray(Target);
final int I = Indices.length;
final int S = s.dimensions.length;
final int T = t.dimensions.length;
if (S != I) stop("Wrong number of dimensions:", S, "!=", I, "for array:", Source);
if (T > 0) stop("A field (an array with zero dimansions) is required for the target:", Target);
final int a = immediate;
final int b = get(Source, Indices);
final int r = Integer.valueOf(b).compareTo(Integer.valueOf(a));
set(r, Target);
}
void compareAndSet // Compare the indexed source fields and write -1 into the target if the first is less than the second, 0 if equal otherwise +1
(String Target, String S1, String S2, String...Indices)
{final Array s1 = getArray(S1);
final Array s2 = getArray(S2);
final Array t = getArray(Target);
final int I = Indices.length;
final int d1 = s1.dimensions.length;
final int d2 = s2.dimensions.length;
if (d1 + d2 != I) stop("Wrong number of dimensions:", d1, "+", d2, "!=", I, "for arrays:", S1+",", S2);
final int T = t.dimensions.length;
if (T > 0) stop("A field (an array with zero dimansions) is required for the target:", Target);
final String[]i1 = new String[d1];
final String[]i2 = new String[d2];
for (int i = 0; i < d1; i++) i1[i] = Indices[i];
for (int i = d1; i < I; i++) i2[i-d1] = Indices[i];
final int a = get(S1, i1);
final int b = get(S2, i2);
final int r = Integer.valueOf(a).compareTo(Integer.valueOf(b));
set(r, Target);
}
void gtZero(String Target) {set(get(Target) > 0 ? 1 : 0, Target);} // Set the target to one if it is currently greater than zero else zero
void geZero(String Target) {set(get(Target) >= 0 ? 1 : 0, Target);} // Set the target to one if it is currently greater than or equal to zero else zero
void ltZero(String Target) {set(get(Target) < 0 ? 1 : 0, Target);} // Set the target to one if it is currently less than zero else zero
void leZero(String Target) {set(get(Target) <= 0 ? 1 : 0, Target);} // Set the target to one if it is currently less than or equal to zero else zero
void eqZero(String Target) {set(get(Target) == 0 ? 1 : 0, Target);} // Set the target to one if it is currently equal to zero else zero
void neZero(String Target) {set(get(Target) != 0 ? 1 : 0, Target);} // Set the target to one if it is currently less not equal to zero else zero
void addImmediate(int immediate, String source, String...Indices) // Add a constant value to the source field
{final Array s = getArray(source);
final int I = Indices.length;
final int S = s.dimensions.length;
if (S != I) stop("Wrong number of dimensions:", S, "!=", I, "for array:", source);
final int a = get(source, Indices);
set(immediate + a, source, Indices);
}
void add(String target, String source, String...Indices) // Add the source to the target replacing the target
{final Array t = getArray(target), s = getArray(source);
final int I = Indices.length;
final int T = t.dimensions.length, S = s.dimensions.length;
if (T + S != I) stop("Wrong number of dimensions:", T, "+", S, "!=", I); // Check we have the right number of indices
final String[]ti = new String[T]; // Assign indices
final String[]si = new String[S];
for (int i = 0; i < T; i++) ti[i] = Indices[i];
for (int i = T; i < I; i++) si[i-T] = Indices[i];
final int a = get(source, si);
final int b = get(target, ti);
set(a+b, target, ti);
}
public String toString() // Print layout
{final StringBuilder s = new StringBuilder();
int l = 0;
for (Array a: order)
{if (a.dimensions.length == 0)
{ s.append(String.format("%4d %4d %4d %s %s\n", l++, a.base, memory[a.base], a.name, "-"));
}
else if (a.dimensions.length == 1)
{for (int i = 0; i < a.dimensions[0]; i++)
{ s.append(String.format("%4d %4d %4d %4d %s %s\n", l++, a.base, i, memory[a.base+i], a.name, i == 0 ? "-" : "|"));
}
}
else if (a.dimensions.length == 2)
{for (int j = 0; j < a.dimensions[1]; j++)
{for (int i = 0; i < a.dimensions[0]; i++)
{s.append(String.format("%4d %4d %4d %4d %4d %s %s\n", l++, a.base, j, i, memory[a.base+j*a.dimensions[0]+i], a.name, i == 0 && j == 0 ? "-" : i == 0 ? "+" : "|"));
}
}
}
}
return ""+s;
}
String print(String Source, String...Indices) // Print the indicated part of the array
{final Array s = getArray(Source);
final String[]i = Indices;
final StringBuilder t = new StringBuilder();
final int I = i.length;
final int S = s.dimensions.length;
if (I > S) stop("Too many dimensions:", I, ">", S, "for array:", Source);
if (I == 0) // Print entire array
{if (s.dimensions.length == 0) return ""+get(Source);
if (s.dimensions.length == 1)
{for (int j = 0; j < s.length; j++) t.append(""+memory[s.base + j]+", ");
t.setLength(t.length()-2);
return ""+t;
}
for (int k = 0; k < s.dimensions[1]; k++)
{t.append("[");
for (int j = 0; j < s.dimensions[0]; j++)
{t.append(""+memory[s.base + k*s.dimensions[0]+ j]+", ");
}
t.setLength(t.length()-2);
t.append("],\n");
}
t.setLength(t.length()-2);
return ""+t+"\n";
}
if ((I == 1 && S == 1) || (I == 2 && S == 2)) // Print single element of one or two dimensional array
{t.append(get(Source, Indices));
return ""+get(Source, Indices);
}
if (I == 1 && S == 2) // Print indicated sub array
{final int l = s.dimensions[0]; // Size of sub array
final int p = l * lookUpIndex(s, 1, i[0]); // Start of sub array
for (int j = 0; j < l; j++) t.append(""+memory[s.base+ p + j]+", "); // Clear sub array in two dimensional array
t.setLength(t.length()-2);
return ""+t;
}
return "";
}
class Label // Label definition
{int instruction; // The instruction to which this labels applies
Label() {set(); labels.push(this);} // A label assigned to an instruction
void set() {instruction = code.size();} // Reassign the label to an instruction
}
class I // Instruction definition
{int instructionNumber; // Instruction number
final String definition = traceBack(); // Location of code that defined this instruction
I() // Define an instruction
{if (running) stop("Cannot define instructions during program execution",
definition);
instructionNumber = code.size(); code.push(this);
}
void a() {} // Action performed by instruction
void i() {} // Initialization for instruction
String n() {return "instruction";} // Instruction name
}
//D1 Execute // Execute the program
void run() // Run the program
{z();
running = true;
final int N = code.size();
for (step = 0, time = 0; step < N && time < maxTime; step++, time++)
{z(); code.elementAt(step).a();
}
running = false;
}
void stop(String em) // Stop everything with an explanatory message
{z();
new I() {void a() {Test.stop(em);}};
}
void clear() {z(); code.clear(); running = false;} // Clear the program code
//D1 Blocks // Blocks of code used to implement if statements and for loops
void Goto(Label label) {z(); step = label.instruction-1;} // The program execution for loop will increment first
void GoOn(Label label, MemoryLayout.At condition) // Go to a specified label if a memory location is on, i.e. not zero
{z(); if (condition.setOff().getInt() > 0) Goto(label);
}
void GoOff(Label label, MemoryLayout.At condition) // Go to a specified label if a memory location is off, i.e. zero
{z(); if (condition.setOff().getInt() == 0) Goto(label);
}
abstract class Block // A block that can be continued or exited
{final Label start = new Label(), end = new Label(); // Labels at start and end of block to facilitate continuing or exiting
Block()
{code();
end.set();
}
abstract void code();
}
//D0 Tests // Testing
static LayoutBap test_layout() // A test layout
{return new LayoutBap()
{void load()
{array("i");
array("j");
array("k");
array("a", 4);
array("b", 6);
array("A", 2, 4);
array("B", 4, 3);
}
};
}
static LayoutBap test_set()
{LayoutBap l = test_layout();
l.set(1, "i");
l.set(2, "j");
l.set(3, "k");
l.set(4, "a", "i");
l.set(5, "a", "j");
l.set(6, "A", "i", "k"); // 1,3
l.set(7, "A", "i", "j"); // 1,2
l.set(8, "B", "k", "i"); // 3,1
l.set(9, "B", "j", "j"); // 2,2
//stop(l);
ok(""+l, """
0 0 1 i -
1 1 2 j -
2 2 3 k -
3 3 0 0 a -
4 3 1 4 a |
5 3 2 5 a |
6 3 3 0 a |
7 7 0 0 b -
8 7 1 0 b |
9 7 2 0 b |
10 7 3 0 b |
11 7 4 0 b |
12 7 5 0 b |
13 13 0 0 0 A -
14 13 0 1 0 A |
15 13 0 2 0 A |
16 13 0 3 0 A |
17 13 1 0 0 A +
18 13 1 1 0 A |
19 13 1 2 7 A |
20 13 1 3 6 A |
21 21 0 0 0 B -
22 21 0 1 0 B |
23 21 0 2 0 B |
24 21 1 0 0 B +
25 21 1 1 0 B |
26 21 1 2 0 B |
27 21 2 0 0 B +
28 21 2 1 0 B |
29 21 2 2 9 B |
30 21 3 0 0 B +
31 21 3 1 8 B |
32 21 3 2 0 B |
""");
return l;
}
static void test_get()
{LayoutBap l = test_set();
ok(l.get("B", "k", "i"), 8);
ok(l.get("B", "j", "j"), 9);
}
static LayoutBap test_move()
{LayoutBap l = test_set();
l.move("B", "a", "k", "i", "i");
l.move("B", "a", "k", "j", "j");
//stop(l);
ok(""+l, """
0 0 1 i -
1 1 2 j -
2 2 3 k -
3 3 0 0 a -
4 3 1 4 a |
5 3 2 5 a |
6 3 3 0 a |
7 7 0 0 b -
8 7 1 0 b |
9 7 2 0 b |
10 7 3 0 b |
11 7 4 0 b |
12 7 5 0 b |
13 13 0 0 0 A -
14 13 0 1 0 A |
15 13 0 2 0 A |
16 13 0 3 0 A |
17 13 1 0 0 A +
18 13 1 1 0 A |
19 13 1 2 7 A |
20 13 1 3 6 A |
21 21 0 0 0 B -
22 21 0 1 0 B |
23 21 0 2 0 B |
24 21 1 0 0 B +
25 21 1 1 0 B |
26 21 1 2 0 B |
27 21 2 0 0 B +
28 21 2 1 0 B |
29 21 2 2 9 B |
30 21 3 0 0 B +
31 21 3 1 4 B |
32 21 3 2 5 B |
""");
return l;
}
static void test_compare()
{LayoutBap l = test_move();
ok(l.compareImmediate(4, "B", "k", "j"), +1);
ok(l.compareImmediate(5, "B", "k", "j"), 0);
ok(l.compareImmediate(6, "B", "k", "j"), -1);
ok(l.compare("B", "B", "k", "j", "k", "i"), +1);
ok(l.compare("B", "B", "k", "j", "j", "j"), -1);
ok(l.compare("B", "a", "k", "j", "j"), 0);
}
static void test_compare_int() // Encode some indices directly instead of indirectly
{LayoutBap l = test_move();
ok(l.compareImmediate(4, "B", "3", "j"), +1);
ok(l.compareImmediate(5, "B", "3", "j"), 0);
ok(l.compareImmediate(6, "B", "3", "j"), -1);
ok(l.compare("B", "B", "3", "j", "3", "i"), +1);
ok(l.compare("B", "B", "3", "j", "j", "j"), -1);
ok(l.compare("B", "a", "3", "j", "j"), 0);
}
// 4 3 1 4 a |
// 5 3 2 5 a |
static void test_compare_and_set()
{LayoutBap l = test_move();
l.compareImmediateAndSet("j", 3, "a", "1"); ok(l.get("j"), +1);
l.compareImmediateAndSet("j", 5, "a", "2"); ok(l.get("j"), 0);
l.compareImmediateAndSet("j", 6, "a", "1"); ok(l.get("j"), -1);
l.compareAndSet("k", "a", "a", "1", "2"); ok(l.get("k"), -1);
l.compareAndSet("k", "a", "a", "1", "1"); ok(l.get("k"), 0);
l.compareAndSet("k", "a", "a", "2", "1"); ok(l.get("k"), +1);
}
static void test_comparisons()
{LayoutBap l = test_move();
l.compareImmediateAndSet("j", 3, "a", "1"); l.eqZero("j"); ok(l.get("j"), 0);
l.compareImmediateAndSet("j", 3, "a", "1"); l.neZero("j"); ok(l.get("j"), 1);
l.compareImmediateAndSet("j", 3, "a", "1"); l.ltZero("j"); ok(l.get("j"), 0);
l.compareImmediateAndSet("j", 3, "a", "1"); l.leZero("j"); ok(l.get("j"), 0);
l.compareImmediateAndSet("j", 3, "a", "1"); l.gtZero("j"); ok(l.get("j"), 1);
l.compareImmediateAndSet("j", 3, "a", "1"); l.geZero("j"); ok(l.get("j"), 1);
l.compareImmediateAndSet("j", 5, "a", "2"); l.eqZero("j"); ok(l.get("j"), 1);
l.compareImmediateAndSet("j", 5, "a", "2"); l.neZero("j"); ok(l.get("j"), 0);
l.compareImmediateAndSet("j", 5, "a", "2"); l.ltZero("j"); ok(l.get("j"), 0);
l.compareImmediateAndSet("j", 5, "a", "2"); l.leZero("j"); ok(l.get("j"), 1);
l.compareImmediateAndSet("j", 5, "a", "2"); l.gtZero("j"); ok(l.get("j"), 0);
l.compareImmediateAndSet("j", 5, "a", "2"); l.geZero("j"); ok(l.get("j"), 1);
l.compareImmediateAndSet("j", 6, "a", "1"); l.eqZero("j"); ok(l.get("j"), 0);
l.compareImmediateAndSet("j", 6, "a", "1"); l.neZero("j"); ok(l.get("j"), 1);
l.compareImmediateAndSet("j", 6, "a", "1"); l.ltZero("j"); ok(l.get("j"), 1);
l.compareImmediateAndSet("j", 6, "a", "1"); l.leZero("j"); ok(l.get("j"), 1);
l.compareImmediateAndSet("j", 6, "a", "1"); l.gtZero("j"); ok(l.get("j"), 0);
l.compareImmediateAndSet("j", 6, "a", "1"); l.geZero("j"); ok(l.get("j"), 0);
}
static void test_add()
{LayoutBap l = test_move();
l.addImmediate(1, "B", "k", "i");
ok(l.get("B", "k", "i"), 5);
l.add ("B", "B", "k", "j", "k", "i");
ok(l.get("B", "k", "j"), 10);
}
static LayoutBap test_overlay() // A test layout with overlays
{final int M = 4, N = 3;
final LayoutBap l = new LayoutBap()
{void load()
{array("i");
array("j");
array("k");
array("a", M*N);
overlay("b", M, N);
}
};
for (int i = 0; i < M*N; i++)
{l.set(i, "i");
l.set(i, "a", "i");
}
for (int i = 0; i < M; i++)
{for (int j = 0; j < N; j++)
{l.set(i, "i");
l.set(j, "j");
ok(l.get("b", "i", "j"), i*N+j);
}
}
return l;
}
static void test_clear()
{final int M = 4, N = 3;
final LayoutBap l = new LayoutBap()
{void load()
{array("i");
array("j");
array("k");
array("a", M);
array("b", M, N);
}
};
for (int i = 0; i < M; i++)
{l.set(i, "i");
l.set(i, "a", "i");
}
for (int i = 0; i < M; i++)
{for (int j = 0; j < N; j++)
{l.set(i, "i");
l.set(j, "j");
l.set(i*N+j, "b", "i", "j");
}
}
//stop(l);
l.clear(3, "j");
l.clear(4, "a", "j");
l.clear(2, "i");
l.clear(5, "b", "i");
//stop(l);
ok(""+l, """
0 0 2 i -
1 1 3 j -
2 2 0 k -
3 3 0 0 a -
4 3 1 1 a |
5 3 2 2 a |
6 3 3 0 a |
7 7 0 0 0 b -
8 7 0 1 1 b |
9 7 0 2 2 b |
10 7 1 0 3 b +
11 7 1 1 4 b |
12 7 1 2 5 b |
13 7 2 0 0 b +
14 7 2 1 0 b |
15 7 2 2 0 b |
16 7 3 0 9 b +
17 7 3 1 10 b |
18 7 3 2 11 b |
""");
}
static LayoutBap test_initialize()
{final LayoutBap l = new LayoutBap()
{void load()
{array("a");
array("b", 2);
array("c", 2, 3);
}
};
l.set("a", 1);
l.set("b", 1, 2);
l.set("c", 11, 12, 13, 21, 22, 23);
//stop(l);
ok(""+l, """
0 0 1 a -
1 1 0 1 b -
2 1 1 2 b |
3 3 0 0 11 c -
4 3 0 1 12 c |
5 3 0 2 13 c |
6 3 1 0 21 c +
7 3 1 1 22 c |
8 3 1 2 23 c |
""");
return l;
}
static void test_print()
{final LayoutBap l = test_initialize();
ok(l.print("a"), "1");
ok(l.print("b"), "1, 2");
ok(l.print("c"), """
[11, 12, 13],
[21, 22, 23]
""");
l.set("b", 1, 2);
l.set("c", 11, 12, 13, 21, 22, 23);
}
static void test_save_and_restore()
{final LayoutBap l = test_initialize();
ok(l.print("a"), "1");
l.save();
l.set(0, "a");
ok(l.print("a"), "0");
l.restore();
ok(l.print("a"), "1");
}
static void oldTests() // Tests thought to be in good shape
{test_set();
test_get();
test_move();
test_compare();
test_compare_int();
test_compare_and_set();
test_comparisons();
test_add();
test_overlay();
test_clear();
test_initialize();
test_print();
test_save_and_restore();
}
static void newTests() // Tests being worked on
{oldTests();
}
public static void main(String[] args) // Test if called as a program
{try // Get a traceback in a format clickable in Geany if something goes wrong to speed up debugging.
{if (github_actions) oldTests(); else newTests(); // Tests to run
if (github_actions) // Coverage analysis
{coverageAnalysis(sourceFileName(), 12);
}
testSummary(); // Summarize test results
System.exit(testsFailed);
}
catch(Exception e) // Get a traceback in a format clickable in Geany
{System.err.println(e);
System.err.println(fullTraceBack(e));
System.exit(1);
}
}
}