forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharithmetic.lcb
More file actions
248 lines (195 loc) · 7.13 KB
/
arithmetic.lcb
File metadata and controls
248 lines (195 loc) · 7.13 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
Copyright (C) 2015-2016 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
module com.livecode.arithmetic.tests
public handler TestUnarySigns()
variable tNum
put 10 into tNum
test "unary−" when -tNum is -10
put -10 into tNum
test "unary+" when +tNum is -10
end handler
public handler TestAdd()
variable tInt as Integer
put 10 into tInt
add 2 to tInt
test "add (syntax, int)" when tInt = 12
test "add (operator, int)" when tInt + 1 = 13
add 1.9 to tInt
test diagnostic "Expecting an error"
broken test "add (syntax, int+real)" when tInt = 13 because "bug 14592"
variable tReal as Real
put 10 into tReal
add 1.9 to tReal
test "add (syntax, real)" when tReal = 11.9
test "add (operator, real)" when tReal + 0.2 = 12.1
variable tNumber
put 10 into tNumber
add -1.9 to tNumber
test "add (syntax, number)" when tNumber = 8.1
test "add (operator, number)" when tNumber + -1 = 7.1
end handler
public handler TestSubtract()
variable tInt as Integer
put 10 into tInt
subtract 2 from tInt
test "subtract (syntax, int)" when tInt = 8
test "subtract (operator, int)" when tInt - -2 = 10
subtract 1.9 from tInt
test diagnostic "Expecting an error"
broken test "subtract (syntax, int-real)" when tInt = 6 because "bug 14592"
variable tReal as Real
put 10 into tReal
subtract 1.9 from tReal
test "subtract (syntax, real)" when tReal = 8.1
test "subtract (operator, real)" when tReal - 0.25 = 7.85
variable tNumber
put 10 into tNumber
subtract -1.9 from tNumber
test "subtract (syntax, number)" when tNumber = 11.9
test "subtract (operator, number)" when tNumber - 1 = 10.9
end handler
public handler TestMultiply()
variable tInt as Integer
put 10 into tInt
multiply tInt by 2
test "multiply (syntax, int)" when tInt = 20
test "multiply (operator, int)" when tInt * 2 = 40
variable tReal as Real
put 10 into tReal
multiply tReal by -0.5
test "multiply (syntax, real)" when tReal = -5
test "multiply (operator, real)" when tReal * 3.5 = -17.5
variable tNumber
put 10 into tNumber
multiply tNumber by 0.5
test "multiply (syntax, number)" when tNumber = 5
test "multiply (operator, number)" when tNumber * 3.5 = 17.5
end handler
public handler TestDivide()
variable tInt as Integer
put 10 into tInt
divide tInt by 2
test "divide (syntax, int)" when tInt = 5
test "divide (operator, int)" when 30 / tInt = 6
divide tInt by 2
test diagnostic "Expecting trunc"
broken test "divide (syntax, int)" when tInt = 2 because "bug 14592"
variable tReal as Real
put 10 into tReal
divide tReal by -0.5
test "divide (syntax, real)" when tReal = -20
test "divide (operator, real)" when 50 / tReal = -2.5
variable tNumber
put 10 into tNumber
divide tNumber by 0.5
test "divide (syntax, number)" when tNumber = 20
test "divide (operator, number)" when tNumber / 8 = 2.5
end handler
private handler ModInvariantDiagnostic(in pX, in pY, in pZ, in pR)
test diagnostic pX formatted as string && "mod" && pY formatted as string
test diagnostic pY formatted as string && "*" && \
pZ formatted as string && "+" && \
pR formatted as string && "=" && \
(pY * pZ + pR) formatted as string
end handler
private handler ModInvariantInteger(in pX as Integer, in pY as Integer) \
returns Boolean
variable tZ as Integer
variable tR as Integer
put (pX / pY) into tZ
put (pX mod pY) into tR
ModInvariantDiagnostic(pX, pY, tZ, tR)
return (pY * tZ + tR) is pX
end handler
private handler ModInvariantReal(in pX as Real, in pY as Real) \
returns Boolean
variable tZ as Real
variable tR as Real
put the trunc of (pX / pY) into tZ -- because there is no "div" operator
put (pX mod pY) into tR
ModInvariantDiagnostic(pX, pY, tZ, tR)
return (pY * tZ + tR) is pX
end handler
public handler TestModInteger()
broken test "mod int (+ve, +ve)" when ModInvariantInteger(10, 3) because "bug 14592"
broken test "mod (-ve, +ve)" when ModInvariantInteger(-10, 3) because "bug 14592"
broken test "mod int (+ve, -ve)" when ModInvariantInteger(10, -3) because "bug 14592"
broken test "mod int (-ve, -ve)" when ModInvariantInteger(-10, -3) because "bug 14592"
test "mod real (+ve, +ve)" when ModInvariantReal(16, 3.75)
test "mod real (-ve, +ve)" when ModInvariantReal(-16, 3.75)
test "mod real (+ve, -ve)" when ModInvariantReal(16, -3.75)
test "mod real (-ve, -ve)" when ModInvariantReal(-16, -3.75)
end handler
public handler TestWrap()
test "wrap (+ve, +ve)" when 5 wrap 3 = 2
test "wrap (+ve, -ve)" when 5 wrap -3 = 2
test "wrap (-ve, -ve)" when -5 wrap -3 = -2
test "wrap (-ve, +ve)" when -5 wrap 3 = -2
end handler
public handler TestGreaterThan()
test "> (int, real)" when 5.1 > 5
test "> (real, int)" when -5 > -5.1
test "> (equal)" when not 5.0 > 5
test "not > (int, real)" when not 5.1 > 6
test "not > (real, int)" when not -6 > -5.1
end handler
public handler TestGreaterThanEqual()
test ">= (int, real)" when 5.1 >= 5
test ">= (real, int)" when -5 >= -5.1
test ">= (equal)" when 5.0 >= 5
test "not >= (int, real)" when not 5.1 >= 6
test "not >= (real, int)" when not -6 >= -5.1
end handler
public handler TestLessThan()
test "not < (int, real)" when not 5.1 < 5
test "not < (real, int)" when not -5 < -5.1
test "< (equal)" when not 5.0 < 5
test "< (int, real)" when 5.1 < 6
test "< (real, int)" when -6 < -5.1
end handler
public handler TestLessThanEqual()
test "not <= (int, real)" when not 5.1 <= 5
test "not <= (real, int)" when not -5 <= -5.1
test "<= (equal)" when 5.0 <= 5
test "<= (int, real)" when 5.1 <= 6
test "<= (real, int)" when -6 <= -5.1
end handler
public handler TestFormatString()
test "formatted as string (int)" when (-1) formatted as string is "-1"
test "formatted as string (real)" when (-1.1) formatted as string is "-1.1"
variable tString as String
format -1 as string into tString
test "format as string (int)" when tString is "-1"
format -1.1 as string into tString
test "format as string (real)" when tString is "-1.1"
end handler
public handler TestParseString()
test "parsed (int)" when "-1" parsed as number = -1
test "parsed (real)" when "+1.1" parsed as number = 1.1
test "parsed (nothing)" when "x" parsed as number is nothing
parse "-1" as number
test "parse (int)" when the result is -1
parse "+1.1" as number
test "parse (real)" when the result is 1.1
parse "x" as number
test "parse (nothing)" when the result is nothing
end handler
public handler TestParseStringList()
test "parsed list" when ["-1", "+2.2"] parsed as list of number is [-1, 2.2]
variable tList
parse ["-1", "+2.2", "x"] as list of number
put the result into tList
test "parse list (defined)" when element 1 to 2 of tList is [-1, 2.2]
test "parse list (nothing)" when element 3 of tList is nothing
end handler
end module