Skip to content

Commit 20e0983

Browse files
committed
Add more tests
1 parent bdea358 commit 20e0983

1 file changed

Lines changed: 95 additions & 27 deletions

File tree

tests/init.lua

Lines changed: 95 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,44 +14,101 @@ local MathParser = require("MathParser")
1414
-- Create an instance of MathParser
1515
local myMathParser = MathParser:new()
1616

17+
local errorMessages = {}
18+
local successMessages = {}
19+
1720
local function runUnitTest(expression, expected, testName)
1821
local result = myMathParser:solve(expression)
19-
assert(result == expected, testName .. " failed. Expected: " .. expected .. ", Actual: " .. result)
20-
print("Successfully passed unit test: '" .. testName .. "' With result: " .. result)
22+
if result ~= expected then
23+
table.insert(errorMessages, testName .. " failed. Expected: " .. expected .. ", Actual: " .. result .. ", Expression: " .. expression)
24+
end
25+
table.insert(successMessages, "Successfully passed unit test: '" .. testName .. "' With result: " .. result)
26+
end
27+
local function displaySuccessMessages()
28+
if #successMessages > 0 then
29+
print(table.concat(successMessages, "\n") .. "")
30+
end
31+
end
32+
local function displayErrorMessages()
33+
if #errorMessages > 0 then
34+
error(table.concat(errorMessages, "\n") .. "")
35+
end
2136
end
2237

2338
-- Add some variables
2439
myMathParser:addVariable("test", 10)
2540
myMathParser:addVariable("a", 5)
2641

42+
-- Register the variables as locals so we can use it in Lua too
43+
local test = 10
44+
local a = 5
45+
2746
-- Add some functions
2847
myMathParser:addFunction("testFunction", function(a, b)
2948
return a + b
3049
end)
3150

32-
-- Unit tests
33-
runUnitTest("2 + 3", 5, "Addition")
34-
runUnitTest("5 - 2", 3, "Subtraction")
35-
runUnitTest("2 * 3", 6, "Multiplication")
36-
runUnitTest("6 / 2", 3, "Division")
37-
runUnitTest("2 ^ 3", 8, "Exponentiation")
38-
runUnitTest("2 + 3 * 4", 14, "Operator precedence (multiplication)")
39-
runUnitTest("2 ^ 3 ^ 2", 512, "Exponentiation (right-associative)")
40-
runUnitTest("a + test", 15, "Variable substitution")
41-
runUnitTest("(2 + 3) * 4 - 5", 15, "Complex expression")
42-
runUnitTest("1.5 + 2.5", 4, "Floating point numbers")
43-
runUnitTest(".5 + .5", 1, "Floating point numbers (leading decimal point)")
44-
runUnitTest("2e3 + 2e+3", 4000, "Scientific notation (positive exponent)")
45-
runUnitTest("2e3 - 2e-3", 1999.998, "Scientific notation (negative exponent)")
46-
runUnitTest(".5e3 + .5e-3", 500.0005, "Scientific notation (leading decimal point)")
47-
runUnitTest("1.10e+5", 110000, "Scientific notation (leading decimal point and positive exponent)")
48-
runUnitTest("0xFF + 0xFF", 510, "Hexadecimal numbers")
51+
52+
--// UNIT TESTS //--
53+
54+
-- Basic tests
55+
runUnitTest("1 + 1", 1+1, "Addition")
56+
runUnitTest("2 * 2", 2*2, "Subtraction")
57+
runUnitTest("1 * 2", 1*2, "Multiplication")
58+
runUnitTest("2 / 1", 2/1, "Division")
59+
runUnitTest("1 ^ 2", 1^2, "Exponentiation")
60+
runUnitTest("1 + 1 * 2", 1+1*2, "Operator precedence (multiplication)")
61+
runUnitTest("1 ^ 1 ^ 2", 1^1^2, "Exponentiation (right-associative)")
62+
runUnitTest("a + test", a+test, "Variable substitution")
63+
runUnitTest("(1 + 1) * 2 - 1", (1+1)*2-1, "Complex expression")
64+
runUnitTest("1.0 + 1.5", 1.0+1.5, "Floating point numbers")
65+
runUnitTest(".5 + .5", .5+.5, "Floating point numbers (leading decimal point)")
66+
runUnitTest("1e1 + 1e+1", 1e1+1e+1, "Scientific notation (positive exponent)")
67+
runUnitTest("1e1 - 1e-1", 1e1-1e-1, "Scientific notation (negative exponent)")
68+
runUnitTest(".5e1 + .5e-1", .5e1+.5e-1, "Scientific notation (leading decimal point)")
69+
runUnitTest("1.10e+1", 1.10e+1, "Scientific notation (leading decimal point and positive exponent)")
70+
runUnitTest(".5e1 + 1.5e+1 + 2.5e-1", .5e1+1.5e+1+2.5e-1, "Advanced scientific notation (leading decimal point)")
71+
runUnitTest("0xF + 0xF", 0xF+0xF, "Hexadecimal numbers")
4972
runUnitTest("sin(1)", math.sin(1), "Function call (sin)")
5073
runUnitTest("-sin(1)", -math.sin(1), "Function call (sin) with unary operator")
5174
runUnitTest("sin(sin(1))", math.sin(math.sin(1)), "Function call (sin) with nested function call")
52-
runUnitTest("log(10, 100)", math.log(10, 100), "Function call with multiple arguments (log)")
75+
runUnitTest("log(1, 10)", math.log(1, 10), "Function call with multiple arguments (log)")
5376
runUnitTest("log(sin(1), cos(1))", math.log(math.sin(1), math.cos(1)), "Function call (log) with multiple arguments-function-calls (sin, cos)")
54-
runUnitTest("testFunction(2, 3)", 5, "Custom function call (testFunction)")
77+
runUnitTest("testFunction(1, 1)", 1+1, "Custom function call (testFunction)")
78+
79+
-- Operator Precedence Tests
80+
runUnitTest("2+3*4", 2+3*4, "Operator precedence (multiplication before addition)")
81+
runUnitTest("2*3+4", 2*3+4, "Operator precedence (multiplication before addition)")
82+
runUnitTest("2-3*4", 2-3*4, "Operator precedence (multiplication before subtraction)")
83+
runUnitTest("2*3-4", 2*3-4, "Operator precedence (multiplication before subtraction)")
84+
runUnitTest("2+3/4", 2+3/4, "Operator precedence (division before addition)")
85+
runUnitTest("2/3+4", 2/3+4, "Operator precedence (division before addition)")
86+
runUnitTest("2-3/4", 2-3/4, "Operator precedence (division before subtraction)")
87+
runUnitTest("2/3-4", 2/3-4, "Operator precedence (division before subtraction)")
88+
runUnitTest("2+3^4", 2+3^4, "Operator precedence (exponentiation before addition)")
89+
runUnitTest("2^3+4", 2^3+4, "Operator precedence (exponentiation before addition)")
90+
runUnitTest("2-3^4", 2-3^4, "Operator precedence (exponentiation before subtraction)")
91+
runUnitTest("2^3-4", 2^3-4, "Operator precedence (exponentiation before subtraction)")
92+
runUnitTest("2*3^4", 2*3^4, "Operator precedence (exponentiation before multiplication)")
93+
runUnitTest("2^3*4", 2^3*4, "Operator precedence (exponentiation before multiplication)")
94+
runUnitTest("2/3^4", 2/3^4, "Operator precedence (exponentiation before division)")
95+
runUnitTest("2^3/4", 2^3/4, "Operator precedence (exponentiation before division)")
96+
runUnitTest("(2+3)*4", (2+3)*4, "Parentheses change precedence")
97+
runUnitTest("-2", -2, "Unary minus operator")
98+
runUnitTest("-2^3", -2^3, "Unary minus operator precedence with exponentiation")
99+
runUnitTest("-(2^3)", -(2^3), "Unary minus operator precedence with parentheses and exponentiation")
100+
runUnitTest("-2*3", -2*3, "Unary minus operator precedence with multiplication")
101+
runUnitTest("-(2*3)", -(2*3), "Unary minus operator precedence with parentheses and multiplication")
102+
runUnitTest("-2/3", -2/3, "Unary minus operator precedence with division")
103+
runUnitTest("-(2/3)", -(2/3), "Unary minus operator precedence with parentheses and division")
104+
runUnitTest("-2+3", -2+3, "Unary minus operator precedence with addition")
105+
runUnitTest("-(2+3)", -(2+3), "Unary minus operator precedence with parentheses and addition")
106+
runUnitTest("-2-3", -2-3, "Unary minus operator precedence with subtraction")
107+
runUnitTest("-(2-3)", -(2-3), "Unary minus operator precedence with parentheses and subtraction")
108+
runUnitTest("- -2", - -2, "Double unary minus operator")
109+
runUnitTest("-2+3*4^5-6/7", -2+3*4^5-6/7, "Complex expression with all operators and no parentheses")
110+
runUnitTest("-2+(3*4)^(5- -6)/7", -2+(3*4)^(5- -6)/7, "Complex expression with all operators and parentheses")
111+
55112

56113
-- Advanced tests
57114
local CUSTOM_OPERATOR_PRECEDENCE_LEVELS = {
@@ -95,32 +152,43 @@ end
95152

96153
local invalidExpressions = {
97154
-- Lexer errors
98-
"~2", -- Unknown character
155+
"~2", -- Unknown character before number
99156
"2~", -- Unknown character after number
157+
".", -- Missing digits after decimal point
158+
"1.", -- Missing digits after decimal point
159+
"1e", -- Missing exponent
160+
"1e+", -- Missing exponent value
161+
"0x", -- Missing hexadecimal digits
100162

101163
-- Parser errors
102164
"+ 2", -- Missing left operand
103165
"2 +", -- Missing right operand
104166
"2 + 3 +", -- Missing right operand after binary operator
105167
"-2-", -- Missing operand after unary operator
106168
"--", -- Missing operand after unary operator
169+
"2 + 3)", -- Missing left parenthesis
170+
"(2 + 3", -- Missing right parenthesis
171+
-- "sin" is one of default functions
172+
"sin(,)", -- Missing arguments in function call
173+
"sin(1,,1)", -- Missing argument in-between in function call
174+
"sin(1,)", -- Missing second argument in function call
175+
"sin(,1)", -- Missing first argument in function call
176+
"sin(1,2", -- Missing right parenthesis in function call
177+
"sin(1,", -- Missing right parenthesis in function call
107178

108179
-- Evaluator errors
109180
"1 + unknownVariable",
110-
"1 + 2 + unknownVariable",
111181
"-unknownVariable",
112182
"sin(unknownVariable)",
113183
"unknownFunction(1)",
114-
"sin(1",
115-
"sin(1 + 2",
116-
"sin(1,)",
117-
"sin(1,"
118184
}
119185

120186
for _, expression in ipairs(invalidExpressions) do
121187
runErrorHandlingTest(expression, "Error handling")
122188
end
123189

190+
displayErrorMessages()
191+
displaySuccessMessages()
124192
print("All unit tests passed!")
125193

126194
return true

0 commit comments

Comments
 (0)