aboutsummaryrefslogtreecommitdiff
path: root/spec/outputs/test/operators_spec.lua
blob: a17ff036df40f16d7fbd04e881a21a4221b990d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
return describe("operators", function()
	it("should support addition", function()
		return assert.same(1 + 2, 3)
	end)
	it("should support subtraction", function()
		return assert.same(5 - 3, 2)
	end)
	it("should support multiplication", function()
		return assert.same(4 * 3, 12)
	end)
	it("should support division", function()
		return assert.same(10 / 2, 5)
	end)
	it("should support modulo", function()
		return assert.same(10 % 3, 1)
	end)
	it("should support exponentiation", function()
		return assert.same(2 ^ 3, 8)
	end)
	it("should support unary minus", function()
		return assert.same(-5, -5)
	end)
	it("should support equality comparison", function()
		assert.is_true(1 == 1)
		return assert.is_false(1 == 2)
	end)
	it("should support inequality comparison", function()
		assert.is_true(1 ~= 2)
		return assert.is_false(1 ~= 1)
	end)
	it("should support less than", function()
		assert.is_true(1 < 2)
		return assert.is_false(2 < 1)
	end)
	it("should support greater than", function()
		assert.is_true(2 > 1)
		return assert.is_false(1 > 2)
	end)
	it("should support less than or equal", function()
		assert.is_true(1 <= 2)
		assert.is_true(2 <= 2)
		return assert.is_false(3 <= 2)
	end)
	it("should support greater than or equal", function()
		assert.is_true(2 >= 1)
		assert.is_true(2 >= 2)
		return assert.is_false(1 >= 2)
	end)
	it("should support logical and", function()
		assert.same(true and false, false)
		assert.same(true and true, true)
		return assert.same(false and true, false)
	end)
	it("should support logical or", function()
		assert.same(true or false, true)
		assert.same(false or true, true)
		return assert.same(false or false, false)
	end)
	it("should support logical not", function()
		assert.same(not true, false)
		assert.same(not false, true)
		return assert.same(not nil, true)
	end)
	it("should support bitwise and", function()
		return assert.same(5 & 3, 1)
	end)
	it("should support bitwise or", function()
		return assert.same(5 | 3, 7)
	end)
	it("should support bitwise xor", function()
		return assert.same(5 ~ 3, 6)
	end)
	it("should support left shift", function()
		return assert.same(2 << 3, 16)
	end)
	it("should support right shift", function()
		return assert.same(16 >> 2, 4)
	end)
	it("should support string concatenation", function()
		return assert.same("hello" .. " world", "hello world")
	end)
	it("should support length operator", function()
		assert.same(#"hello", 5)
		return assert.same(#{
			1,
			2,
			3
		}, 3)
	end)
	it("should respect operator precedence", function()
		assert.same(1 + 2 * 3, 7)
		return assert.same((1 + 2) * 3, 9)
	end)
	it("should support compound assignment", function()
		local x = 10
		x = x + 5
		return assert.same(x, 15)
	end)
	it("should support compound subtraction", function()
		local x = 10
		x = x - 3
		return assert.same(x, 7)
	end)
	it("should support compound multiplication", function()
		local x = 5
		x = x * 2
		return assert.same(x, 10)
	end)
	it("should support compound division", function()
		local x = 20
		x = x / 4
		return assert.same(x, 5)
	end)
	it("should handle division by zero", function()
		local result = pcall(function()
			local x = 10 / 0
		end)
		return assert.is_true(result)
	end)
	it("should handle very large numbers", function()
		local big = 1e100
		return assert.is_true(big > 0)
	end)
	it("should handle very small numbers", function()
		local small = 1e-100
		return assert.is_true(small > 0)
	end)
	it("should support negation", function()
		assert.same(-10, -10)
		return assert.same
	end)
	it("should work with complex expressions", function()
		local result = (1 + 2) * (3 + 4) / 2
		return assert.same(result, 10.5)
	end)
	it("should support power with decimal", function()
		return assert.same(4 ^ 0.5, 2)
	end)
	return it("should handle modulo with negative numbers", function()
		return assert.same(-10 % 3, 2)
	end)
end)