Warning: session_start(): open(/var/lib/php/sessions/sess_9svrvrd0odjdnupkuoqt4tfpnf, O_RDWR) failed: No space left on device (28) in /var/www/tb-usercake/models/config.php on line 51
Warning: session_start(): Failed to read session data: files (path: /var/lib/php/sessions) in /var/www/tb-usercake/models/config.php on line 51
scope test_BStack
/* Goal: Java code with backtracking, like this, to return 4 different values:
S color = "black" or "white";
S shape = "circle" or "square";
return "A " + color + " " + shape;
*/
sclass #OneBranch extends VStackComputableWithStep {
S color;
void step(VStack stack) {
cast stack to BStack>;
if (step == 0) {
step = 1;
stack.options(this,
ivf1WithToString("black", f -> f.color = "black"),
ivf1WithToString("white", f -> f.color = "white"));
} else
stack.return("Color is " + color);
}
}
sclass #TwoBranches extends VStackComputableWithStep {
S color, shape;
void step(VStack stack) {
cast stack to BStack>;
if (step == 0) {
step = 1;
print("Creating options at step 0 (black/white)");
stack.options(this,
ivf1WithToString("black", f -> f.color = "black"),
ivf1WithToString("white", f -> f.color = "white"));
} else if (step == 1) {
step = 2;
print("Creating options at step 1 (circle/square)");
stack.options(this,
ivf1WithToString("circle", f -> f.shape = "circle"),
ivf1WithToString("square", f -> f.shape = "square"));
} else
stack.return("A " + color + " " + shape);
}
}
srecord noeq #TwoBranchesWithUndos(LS colors) extends VStackComputableWithStep {
S color, shape;
void step(VStack stack) {
cast stack to BStack>;
if (step == 0) {
step = 1;
print("Creating options at step 0 (black/white)");
stack.options(this,
ivf1WithToString("black", f -> f.color = "black"),
ivf1WithToString("white", f -> f.color = "white"));
} else if (step == 1) {
stack.temp(tempAdd(colors, color));
step = 2;
} else if (step == 2) {
step = 3;
print("Creating options at step 2 (circle/square)");
stack.options(this,
ivf1WithToString("circle", f -> f.shape = "circle"),
ivf1WithToString("square", f -> f.shape = "square"));
} else
stack.return("A " + color + " " + shape);
}
}
svoid test_BStack() {
test_BStack_oneBranch();
test_BStack_twoBranches();
test_BStack_undos();
}
svoid test_BStack_oneBranch() {
assertEqualsVerbose(
ll("Color is black", "Color is white"),
new BStackComputeAllWithPrintStruct(new OneBranch)!);
}
svoid test_BStack_twoBranches() {
assertEqualsVerbose(
ll("A black circle", "A black square",
"A white circle", "A white square"),
new BStackComputeAllWithPrintStruct(new TwoBranches)!);
}
svoid test_BStack_undos() {
new LS colors; // list that is changed during execution to test the undos
var stack = new BStack<>(new TwoBranchesWithUndos(colors));
for (S color : ll("black", "white"))
for (S shape : ll("circle", "square")) {
assertEqualsVerbose("A \*color*/ \*shape*/", stack.nextResultWithPrintStruct(10));
assertEqualsVerbose(ll(color), colors);
stack = stack.backtrack();
}
assertNull(stack);
}