Internal Test: Implements a `ShortCircuitOr` function to prove that the second argument is not evaluated if the first is true, using side effects (counters) for verification.
ID: 1231
using uCalcSoftware;
var uc = new uCalc();
static void ShortCircuitOr(uCalc.Callback cb) {
var arg1 = cb.ArgExpr(1);
var arg2 = cb.ArgExpr(2);
// Evaluate the first argument
var result1 = arg1.EvaluateBool();
// If the first is true, return immediately without touching the second
if (result1) {
cb.ReturnBool(true);
} else {
// Otherwise, evaluate and return the second argument's result
cb.ReturnBool(arg2.EvaluateBool());
}
}
static void FuncA(uCalc.Callback cb) {
cb.uCalc.Eval("countA = countA + 1");
cb.ReturnBool(true);
}
static void FuncB(uCalc.Callback cb) {
cb.uCalc.Eval("countB = countB + 1");
cb.ReturnBool(true);
}
uc.DefineVariable("countA = 0");
uc.DefineVariable("countB = 0");
uc.DefineFunction("FuncA() As Bool", FuncA);
uc.DefineFunction("FuncB() As Bool", FuncB);
uc.DefineFunction("SC_Or(ByExpr a, ByExpr b) As Bool", ShortCircuitOr);
Console.WriteLine("Calling SC_Or(FuncA(), FuncB())...");
uc.Eval("SC_Or(FuncA(), FuncB())");
Console.WriteLine($"FuncA was called {uc.Eval("countA")} time(s).");
Console.WriteLine($"FuncB was called {uc.Eval("countB")} time(s)."); // Should be 0
Calling SC_Or(FuncA(), FuncB())...
FuncA was called 1 time(s).
FuncB was called 0 time(s). using uCalcSoftware; var uc = new uCalc(); static void ShortCircuitOr(uCalc.Callback cb) { var arg1 = cb.ArgExpr(1); var arg2 = cb.ArgExpr(2); // Evaluate the first argument var result1 = arg1.EvaluateBool(); // If the first is true, return immediately without touching the second if (result1) { cb.ReturnBool(true); } else { // Otherwise, evaluate and return the second argument's result cb.ReturnBool(arg2.EvaluateBool()); } } static void FuncA(uCalc.Callback cb) { cb.uCalc.Eval("countA = countA + 1"); cb.ReturnBool(true); } static void FuncB(uCalc.Callback cb) { cb.uCalc.Eval("countB = countB + 1"); cb.ReturnBool(true); } uc.DefineVariable("countA = 0"); uc.DefineVariable("countB = 0"); uc.DefineFunction("FuncA() As Bool", FuncA); uc.DefineFunction("FuncB() As Bool", FuncB); uc.DefineFunction("SC_Or(ByExpr a, ByExpr b) As Bool", ShortCircuitOr); Console.WriteLine("Calling SC_Or(FuncA(), FuncB())..."); uc.Eval("SC_Or(FuncA(), FuncB())"); Console.WriteLine($"FuncA was called {uc.Eval("countA")} time(s)."); Console.WriteLine($"FuncB was called {uc.Eval("countB")} time(s)."); // Should be 0
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
void ucalc_call ShortCircuitOr(uCalcBase::Callback cb) {
auto arg1 = cb.ArgExpr(1);
auto arg2 = cb.ArgExpr(2);
// Evaluate the first argument
auto result1 = arg1.EvaluateBool();
// If the first is true, return immediately without touching the second
if (result1) {
cb.ReturnBool(true);
} else {
// Otherwise, evaluate and return the second argument's result
cb.ReturnBool(arg2.EvaluateBool());
}
}
void ucalc_call FuncA(uCalcBase::Callback cb) {
cb.uCalc().Eval("countA = countA + 1");
cb.ReturnBool(true);
}
void ucalc_call FuncB(uCalcBase::Callback cb) {
cb.uCalc().Eval("countB = countB + 1");
cb.ReturnBool(true);
}
int main() {
uCalc uc;
uc.DefineVariable("countA = 0");
uc.DefineVariable("countB = 0");
uc.DefineFunction("FuncA() As Bool", FuncA);
uc.DefineFunction("FuncB() As Bool", FuncB);
uc.DefineFunction("SC_Or(ByExpr a, ByExpr b) As Bool", ShortCircuitOr);
cout << "Calling SC_Or(FuncA(), FuncB())..." << endl;
uc.Eval("SC_Or(FuncA(), FuncB())");
cout << "FuncA was called " << uc.Eval("countA") << " time(s)." << endl;
cout << "FuncB was called " << uc.Eval("countB") << " time(s)." << endl; // Should be 0
}
Calling SC_Or(FuncA(), FuncB())...
FuncA was called 1 time(s).
FuncB was called 0 time(s). #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; void ucalc_call ShortCircuitOr(uCalcBase::Callback cb) { auto arg1 = cb.ArgExpr(1); auto arg2 = cb.ArgExpr(2); // Evaluate the first argument auto result1 = arg1.EvaluateBool(); // If the first is true, return immediately without touching the second if (result1) { cb.ReturnBool(true); } else { // Otherwise, evaluate and return the second argument's result cb.ReturnBool(arg2.EvaluateBool()); } } void ucalc_call FuncA(uCalcBase::Callback cb) { cb.uCalc().Eval("countA = countA + 1"); cb.ReturnBool(true); } void ucalc_call FuncB(uCalcBase::Callback cb) { cb.uCalc().Eval("countB = countB + 1"); cb.ReturnBool(true); } int main() { uCalc uc; uc.DefineVariable("countA = 0"); uc.DefineVariable("countB = 0"); uc.DefineFunction("FuncA() As Bool", FuncA); uc.DefineFunction("FuncB() As Bool", FuncB); uc.DefineFunction("SC_Or(ByExpr a, ByExpr b) As Bool", ShortCircuitOr); cout << "Calling SC_Or(FuncA(), FuncB())..." << endl; uc.Eval("SC_Or(FuncA(), FuncB())"); cout << "FuncA was called " << uc.Eval("countA") << " time(s)." << endl; cout << "FuncB was called " << uc.Eval("countB") << " time(s)." << endl; // Should be 0 }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub ShortCircuitOr(ByVal cb As uCalc.Callback)
Dim arg1 = cb.ArgExpr(1)
Dim arg2 = cb.ArgExpr(2)
'// Evaluate the first argument
Dim result1 = arg1.EvaluateBool()
'// If the first is true, return immediately without touching the second
If result1 Then
cb.ReturnBool(true)
Else
'// Otherwise, evaluate and return the second argument's result
cb.ReturnBool(arg2.EvaluateBool())
End If
End Sub
Public Sub FuncA(ByVal cb As uCalc.Callback)
cb.uCalc.Eval("countA = countA + 1")
cb.ReturnBool(true)
End Sub
Public Sub FuncB(ByVal cb As uCalc.Callback)
cb.uCalc.Eval("countB = countB + 1")
cb.ReturnBool(true)
End Sub
Public Sub Main()
Dim uc As New uCalc()
uc.DefineVariable("countA = 0")
uc.DefineVariable("countB = 0")
uc.DefineFunction("FuncA() As Bool", AddressOf FuncA)
uc.DefineFunction("FuncB() As Bool", AddressOf FuncB)
uc.DefineFunction("SC_Or(ByExpr a, ByExpr b) As Bool", AddressOf ShortCircuitOr)
Console.WriteLine("Calling SC_Or(FuncA(), FuncB())...")
uc.Eval("SC_Or(FuncA(), FuncB())")
Console.WriteLine($"FuncA was called {uc.Eval("countA")} time(s).")
Console.WriteLine($"FuncB was called {uc.Eval("countB")} time(s).") '// Should be 0
End Sub
End Module
Calling SC_Or(FuncA(), FuncB())...
FuncA was called 1 time(s).
FuncB was called 0 time(s). Imports System Imports uCalcSoftware Public Module Program Public Sub ShortCircuitOr(ByVal cb As uCalc.Callback) Dim arg1 = cb.ArgExpr(1) Dim arg2 = cb.ArgExpr(2) '// Evaluate the first argument Dim result1 = arg1.EvaluateBool() '// If the first is true, return immediately without touching the second If result1 Then cb.ReturnBool(true) Else '// Otherwise, evaluate and return the second argument's result cb.ReturnBool(arg2.EvaluateBool()) End If End Sub Public Sub FuncA(ByVal cb As uCalc.Callback) cb.uCalc.Eval("countA = countA + 1") cb.ReturnBool(true) End Sub Public Sub FuncB(ByVal cb As uCalc.Callback) cb.uCalc.Eval("countB = countB + 1") cb.ReturnBool(true) End Sub Public Sub Main() Dim uc As New uCalc() uc.DefineVariable("countA = 0") uc.DefineVariable("countB = 0") uc.DefineFunction("FuncA() As Bool", AddressOf FuncA) uc.DefineFunction("FuncB() As Bool", AddressOf FuncB) uc.DefineFunction("SC_Or(ByExpr a, ByExpr b) As Bool", AddressOf ShortCircuitOr) Console.WriteLine("Calling SC_Or(FuncA(), FuncB())...") uc.Eval("SC_Or(FuncA(), FuncB())") Console.WriteLine($"FuncA was called {uc.Eval("countA")} time(s).") Console.WriteLine($"FuncB was called {uc.Eval("countB")} time(s).") '// Should be 0 End Sub End Module