Transpiled version (24621L) is out of date.
1 | scope test_BStack |
2 | |
3 | /* Goal: Java code with backtracking, like this, to return 4 different values: |
4 | |
5 | S color = "black" or "white"; |
6 | S shape = "circle" or "square"; |
7 | return "A " + color + " " + shape; |
8 | */ |
9 | |
10 | sclass #OneBranch extends VStackComputableWithStep<S> {
|
11 | S color; |
12 | |
13 | void step(VStack stack) {
|
14 | cast stack to IStackWithOptions; |
15 | |
16 | if (step == 0) {
|
17 | step = 1; |
18 | stack.options(this, |
19 | ivf1WithToString("black", f -> f.color = "black"),
|
20 | ivf1WithToString("white", f -> f.color = "white"));
|
21 | } else |
22 | stack.return("Color is " + color);
|
23 | } |
24 | } |
25 | |
26 | sclass #OneBranch3Options extends VStackComputableWithStep<S> {
|
27 | S color; |
28 | |
29 | void step(VStack stack) {
|
30 | cast stack to IStackWithOptions; |
31 | |
32 | if (step == 0) {
|
33 | step = 1; |
34 | stack.options(this, |
35 | ivf1WithToString("black", f -> f.color = "black"),
|
36 | ivf1WithToString("white", f -> f.color = "white"),
|
37 | ivf1WithToString("gray", f -> f.color = "gray"));
|
38 | } else |
39 | stack.return("Color is " + color);
|
40 | } |
41 | } |
42 | |
43 | sclass #NoOptions extends VStackComputableWithStep<S> {
|
44 | void step(VStack stack) {
|
45 | cast stack to IStackWithOptions; |
46 | |
47 | stack.options(this, ll()); // No options should cause error |
48 | } |
49 | } |
50 | |
51 | sclass #TwoBranches extends VStackComputableWithStep<S> {
|
52 | S color, shape; |
53 | |
54 | void step(VStack stack) {
|
55 | cast stack to IStackWithOptions; |
56 | |
57 | if (step == 0) {
|
58 | step = 1; |
59 | print("Creating options at step 0 (black/white)");
|
60 | stack.options(this, |
61 | ivf1WithToString("black", f -> f.color = "black"),
|
62 | ivf1WithToString("white", f -> f.color = "white"));
|
63 | } else if (step == 1) {
|
64 | step = 2; |
65 | print("Creating options at step 1 (circle/square)");
|
66 | stack.options(this, |
67 | ivf1WithToString("circle", f -> f.shape = "circle"),
|
68 | ivf1WithToString("square", f -> f.shape = "square"));
|
69 | } else |
70 | stack.return("A " + color + " " + shape);
|
71 | } |
72 | } |
73 | |
74 | sclass #TwoBranches3Options extends VStackComputableWithStep<S> {
|
75 | S color, shape; |
76 | |
77 | void step(VStack stack) {
|
78 | cast stack to IStackWithOptions; |
79 | |
80 | if (step == 0) {
|
81 | step = 1; |
82 | print("Creating options at step 0 (black/white/gray)");
|
83 | stack.options(this, |
84 | ivf1WithToString("black", f -> f.color = "black"),
|
85 | ivf1WithToString("white", f -> f.color = "white"),
|
86 | ivf1WithToString("white", f -> f.color = "gray"));
|
87 | } else if (step == 1) {
|
88 | step = 2; |
89 | print("Creating options at step 1 (circle/square/triangle)");
|
90 | stack.options(this, |
91 | ivf1WithToString("circle", f -> f.shape = "circle"),
|
92 | ivf1WithToString("square", f -> f.shape = "square"),
|
93 | ivf1WithToString("square", f -> f.shape = "triangle"));
|
94 | } else |
95 | stack.return("A " + color + " " + shape);
|
96 | } |
97 | } |
98 | |
99 | srecord noeq #TwoBranchesWithUndos(LS things) extends VStackComputableWithStep<S> {
|
100 | S color, shape; |
101 | |
102 | void step(VStack stack) {
|
103 | cast stack to IBStack<?>; |
104 | |
105 | if (step == 0) {
|
106 | step = 1; |
107 | print("Creating options at step 0 (black/white)");
|
108 | stack.options(this, |
109 | ivf1WithToString("black", f -> f.color = "black"),
|
110 | ivf1WithToString("white", f -> f.color = "white"));
|
111 | } else if (step == 1) {
|
112 | stack.temp(tempAdd(things, color)); |
113 | step = 2; |
114 | } else if (step == 2) {
|
115 | step = 3; |
116 | print("Creating options at step 2 (circle/square)");
|
117 | stack.options(this, |
118 | ivf1WithToString("circle", f -> f.shape = "circle"),
|
119 | ivf1WithToString("square", f -> f.shape = "square"));
|
120 | } else {
|
121 | stack.temp(tempAdd(things, shape)); |
122 | stack.return("A " + color + " " + shape);
|
123 | } |
124 | } |
125 | } |
126 | |
127 | svoid test_BStack() {
|
128 | test_BStack_oneBranch(); |
129 | test_BStack_twoBranches(); |
130 | test_BStack_undos(); |
131 | test_BStack_oneBranch3Options(); |
132 | test_BStack_twoBranches3Options(); |
133 | test_BStack_noOptions(); |
134 | } |
135 | |
136 | svoid test_BStack_oneBranch() {
|
137 | assertEqualsVerbose( |
138 | ll("Color is black", "Color is white"),
|
139 | new BStackComputeAllWithPrintStruct(new OneBranch)!); |
140 | } |
141 | |
142 | svoid test_BStack_oneBranch3Options() {
|
143 | assertEqualsVerbose( |
144 | ll("Color is black", "Color is white", "Color is gray"),
|
145 | new BStackComputeAllWithPrintStruct(new OneBranch3Options)!); |
146 | } |
147 | |
148 | svoid test_BStack_twoBranches() {
|
149 | assertEqualsVerbose( |
150 | ll("A black circle", "A black square",
|
151 | "A white circle", "A white square"), |
152 | new BStackComputeAllWithPrintStruct(new TwoBranches)!); |
153 | } |
154 | |
155 | svoid test_BStack_twoBranches3Options() {
|
156 | assertEqualsVerbose( |
157 | ll("A black circle", "A black square", "A black triangle",
|
158 | "A white circle", "A white square", "A white triangle", |
159 | "A gray circle", "A gray square", "A gray triangle"), |
160 | new BStackComputeAllWithPrintStruct(new TwoBranches3Options)!); |
161 | } |
162 | |
163 | svoid test_BStack_undos() {
|
164 | new LS things; // list that is changed during execution to test the undos |
165 | |
166 | var stack = new BStack<>(new TwoBranchesWithUndos(things)); |
167 | for (S color : ll("black", "white"))
|
168 | for (S shape : ll("circle", "square")) {
|
169 | assertEqualsVerbose("A \*color*/ \*shape*/", stack.nextResultWithPrintStruct(10));
|
170 | assertEqualsVerbose(ll(color, shape), things); |
171 | stack = stack.backtrack(); |
172 | } |
173 | assertNull(stack); |
174 | } |
175 | |
176 | svoid test_BStack_noOptions() {
|
177 | assertInnerExceptionOfTypeVerbose(BStack.NoOptionsException.class, |
178 | -> new BStackComputeAllWithPrintStruct(new NoOptions)!); |
179 | } |
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
| Snippet ID: | #1035387 |
| Snippet name: | test_BStack - OK! |
| Eternal ID of this version: | #1035387/32 |
| Text MD5: | 3efa67027fd0a3780bdf29711ce67749 |
| Author: | stefan |
| Category: | javax |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2022-05-10 18:34:10 |
| Source code size: | 5498 bytes / 179 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 414 / 680 |
| Version history: | 31 change(s) |
| Referenced in: | [show references] |