Internal Test: A custom `IIf` implementation using `ByExpr` to test lazy evaluation. The branches containing division by zero are never executed, preventing errors.
ID: 1188
using uCalcSoftware;
var uc = new uCalc();
static void CustomIIf(uCalc.Callback cb) {
bool condition = cb.ArgBool(1);
var truePart = cb.ArgExpr(2);
var falsePart = cb.ArgExpr(3);
if (condition) {
cb.Return(truePart.Evaluate());
} else {
cb.Return(falsePart.Evaluate());
}
}
uc.DefineFunction("MyIIf(condition As Bool, ByExpr thenExpr, ByExpr elseExpr)", CustomIIf);
// The 'else' branch contains a division by zero, but it should NOT be evaluated
// because the condition (1 < 2) is true.
var result = uc.Eval("MyIIf(1 < 2, 100, 1/0)");
Console.Write("Result 1: ");
Console.WriteLine(result);
// Now test the false branch. The 'then' branch with the error is skipped.
result = uc.Eval("MyIIf(1 > 2, 1/0, 200)");
Console.Write("Result 2: ");
Console.WriteLine(result);
Result 1: 100
Result 2: 200 using uCalcSoftware; var uc = new uCalc(); static void CustomIIf(uCalc.Callback cb) { bool condition = cb.ArgBool(1); var truePart = cb.ArgExpr(2); var falsePart = cb.ArgExpr(3); if (condition) { cb.Return(truePart.Evaluate()); } else { cb.Return(falsePart.Evaluate()); } } uc.DefineFunction("MyIIf(condition As Bool, ByExpr thenExpr, ByExpr elseExpr)", CustomIIf); // The 'else' branch contains a division by zero, but it should NOT be evaluated // because the condition (1 < 2) is true. var result = uc.Eval("MyIIf(1 < 2, 100, 1/0)"); Console.Write("Result 1: "); Console.WriteLine(result); // Now test the false branch. The 'then' branch with the error is skipped. result = uc.Eval("MyIIf(1 > 2, 1/0, 200)"); Console.Write("Result 2: "); Console.WriteLine(result);
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
void ucalc_call CustomIIf(uCalcBase::Callback cb) {
bool condition = cb.ArgBool(1);
auto truePart = cb.ArgExpr(2);
auto falsePart = cb.ArgExpr(3);
if (condition) {
cb.Return(truePart.Evaluate());
} else {
cb.Return(falsePart.Evaluate());
}
}
int main() {
uCalc uc;
uc.DefineFunction("MyIIf(condition As Bool, ByExpr thenExpr, ByExpr elseExpr)", CustomIIf);
// The 'else' branch contains a division by zero, but it should NOT be evaluated
// because the condition (1 < 2) is true.
auto result = uc.Eval("MyIIf(1 < 2, 100, 1/0)");
cout << "Result 1: ";
cout << result << endl;
// Now test the false branch. The 'then' branch with the error is skipped.
result = uc.Eval("MyIIf(1 > 2, 1/0, 200)");
cout << "Result 2: ";
cout << result << endl;
}
Result 1: 100
Result 2: 200 #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; void ucalc_call CustomIIf(uCalcBase::Callback cb) { bool condition = cb.ArgBool(1); auto truePart = cb.ArgExpr(2); auto falsePart = cb.ArgExpr(3); if (condition) { cb.Return(truePart.Evaluate()); } else { cb.Return(falsePart.Evaluate()); } } int main() { uCalc uc; uc.DefineFunction("MyIIf(condition As Bool, ByExpr thenExpr, ByExpr elseExpr)", CustomIIf); // The 'else' branch contains a division by zero, but it should NOT be evaluated // because the condition (1 < 2) is true. auto result = uc.Eval("MyIIf(1 < 2, 100, 1/0)"); cout << "Result 1: "; cout << result << endl; // Now test the false branch. The 'then' branch with the error is skipped. result = uc.Eval("MyIIf(1 > 2, 1/0, 200)"); cout << "Result 2: "; cout << result << endl; }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub CustomIIf(ByVal cb As uCalc.Callback)
Dim condition As Boolean = cb.ArgBool(1)
Dim truePart = cb.ArgExpr(2)
Dim falsePart = cb.ArgExpr(3)
If condition Then
cb.Return(truePart.Evaluate())
Else
cb.Return(falsePart.Evaluate())
End If
End Sub
Public Sub Main()
Dim uc As New uCalc()
uc.DefineFunction("MyIIf(condition As Bool, ByExpr thenExpr, ByExpr elseExpr)", AddressOf CustomIIf)
'// The 'else' branch contains a division by zero, but it should NOT be evaluated
'// because the condition (1 < 2) is true.
Dim result = uc.Eval("MyIIf(1 < 2, 100, 1/0)")
Console.Write("Result 1: ")
Console.WriteLine(result)
'// Now test the false branch. The 'then' branch with the error is skipped.
result = uc.Eval("MyIIf(1 > 2, 1/0, 200)")
Console.Write("Result 2: ")
Console.WriteLine(result)
End Sub
End Module
Result 1: 100
Result 2: 200 Imports System Imports uCalcSoftware Public Module Program Public Sub CustomIIf(ByVal cb As uCalc.Callback) Dim condition As Boolean = cb.ArgBool(1) Dim truePart = cb.ArgExpr(2) Dim falsePart = cb.ArgExpr(3) If condition Then cb.Return(truePart.Evaluate()) Else cb.Return(falsePart.Evaluate()) End If End Sub Public Sub Main() Dim uc As New uCalc() uc.DefineFunction("MyIIf(condition As Bool, ByExpr thenExpr, ByExpr elseExpr)", AddressOf CustomIIf) '// The 'else' branch contains a division by zero, but it should NOT be evaluated '// because the condition (1 < 2) is true. Dim result = uc.Eval("MyIIf(1 < 2, 100, 1/0)") Console.Write("Result 1: ") Console.WriteLine(result) '// Now test the false branch. The 'then' branch with the error is skipped. result = uc.Eval("MyIIf(1 > 2, 1/0, 200)") Console.Write("Result 2: ") Console.WriteLine(result) End Sub End Module