forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.lcb
More file actions
241 lines (196 loc) · 7.03 KB
/
math.lcb
File metadata and controls
241 lines (196 loc) · 7.03 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
/*
Copyright (C) 2015 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.math.tests
use com.livecode.math
use com.livecode.foreign
use com.livecode.__INTERNAL._testlib
-- Empirically-chosen epsilon for floating-point comparisons
constant EPSILON is 0.00000000001
public handler TestFloor()
test "Floor (+ve)" when the floor of 5.5 is 5
test "Floor (-ve)" when the floor of -5.5 is -6
end handler
public handler TestCeil()
test "Ceil (+ve)" when the ceiling of 5.5 is 6
test "Ceil (-ve)" when the ceiling of -5.5 is -5
end handler
public handler TestRound()
variable tNum as Number
put 5.1 into tNum
round tNum
test "Round (in-place)" when tNum is 5
test "Round (+ve)" when the rounded of 5.1 is 5
test "Round (-ve)" when the rounded of -5.1 is -5
test "Round (+ve, .5)" when the rounded of 4.5 is 5
test "Round (-ve, .5)" when the rounded of -4.5 is -5
end handler
public handler TestPow()
test "pow (-ve, -ve)" when (-2) ^ -2 is 0.25
test "pow (-ve, +ve)" when (-2) ^ 2 is 4
test "pow (-ve, +ve)" when (-2) ^ 3 is -8
test "pow (+ve, -ve)" when 2 ^ -2 is 0.25
test "pow (+ve, +ve)" when 2 ^ 2 is 4
test "pow (0, 1)" when 0 ^ 1 is 0
test "pow (+ve, 0)" when 2 ^ 0 is 1
test "pow (-ve, 0)" when (-2) ^ 0 is 1
end handler
handler TestPowDomain_Imag()
return (-1) ^ 0.5
end handler
public handler TestPowDomain()
-- bug 14679
MCUnitTestHandlerThrows(TestPowDomain_Imag, "pow (imaginary)")
end handler
public handler TestPowPrecedence()
test "pow vs. -" when 0-1^2 is -1
test "pow vs. -" when -1^2 is -1
test "pow vs. -" when 0-(1^2) is -1
test "pow vs. -" when -(1^2) is -1
test "pow vs. -" when 0-(1^2) is -1
test "pow vs. -" when -(1)^2 is -1
test "pow vs. -" when 0-(1)^2 is -1
test "pow vs. -" when (-1)^2 is 1
end handler
public handler TestLog10()
test "log10 (function, +ve)" when log10(1000) is 3
test "log10 (syntax, +ve)" when the log of 1000 is 3
end handler
handler TestLog10Domain_Negative() returns nothing
variable tVar
put the log of -1 into tVar
end handler
handler TestLog10Domain_Zero() returns nothing
variable tVar
put the log of 0 into tVar
end handler
public handler TestLog10Domain()
-- bug 14678
MCUnitTestHandlerThrows(TestLog10Domain_Negative, "log10 (syntax, -ve)")
MCUnitTestHandlerThrowsBroken(TestLog10Domain_Zero, "log10 (syntax, 0)", "bug 14678")
end handler
public handler TestSin()
test "sin (function)" when sin(pi / 2) is 1
test "sin (syntax)" when the sine of (pi / 2) is 1
end handler
public handler TestCos()
test "cos (function)" when cos(pi) is -1
test "cos (syntax)" when the cosine of pi is -1
end handler
public handler TestTan()
test "tan (function)" when tan(pi/4) - 1 < EPSILON
test "tan (syntax)" when the tangent of (pi/4) - 1 < EPSILON
end handler
public handler TestArcsin()
test "arcsin (function)" when asin(1) is pi / 2
test "arcsin (syntax)" when the arcsine of 1 is pi / 2
end handler
public handler TestArccos()
test "arccos (function)" when acos(-1) is pi
test "arccos (syntax)" when the arccosine of -1 is pi
end handler
public handler TestArctan()
test "arctan (function)" when atan(1) is pi / 4
test "arctan (syntax)" when the arctangent of 1 is pi / 4
test "arctan2 (function)" when atan2(-1,-1) is -3 * pi / 4
test "arctan2 (function)" when the binary arctangent of -1 and -1 is -3 * pi / 4
end handler
handler TestTrigonometricDomain_Arcsin()
return asin((1 + any number) / any number)
end handler
handler TestTrigonometricDomain_Arccos()
return acos((1 + any number) / any number)
end handler
public handler TestTrigonometricDomain()
MCUnitTestHandlerThrows(TestTrigonometricDomain_Arcsin, "arcsin (> 1)")
MCUnitTestHandlerThrows(TestTrigonometricDomain_Arccos, "arccos (> 1)")
end handler
public handler TestExp()
variable tExp
put exp(3) into tExp
test "exp (function)" when tExp > 20 and tExp < 20.1
put the exponential of 3 into tExp
test "exp (syntax)" when tExp > 20 and tExp < 20.1
end handler
public handler TestLn()
variable tLn
put the natural log of 55 into tLn
test "ln (syntax) " when tLn > 4 and tLn < 4.01
put ln(55) into tLn
test "ln (function) " when tLn > 4 and tLn < 4.01
end handler
handler TestLnDomain_Negative() returns nothing
variable tVar
put the natural log of -1 into tVar
end handler
handler TestLnDomain_Zero() returns nothing
variable tVar
put the natural log of 0 into tVar
end handler
public handler TestLnDomain()
-- bug 14678
MCUnitTestHandlerThrows(TestLnDomain_Negative, "ln (syntax, -ve)")
MCUnitTestHandlerThrowsBroken(TestLnDomain_Zero, "ln (syntax, 0)", "bug 14678")
end handler
public handler TestExpLog()
test "exp-log" when the abs of the natural log of the exponential of 3 - 3 < EPSILON
test "log-exp" when the abs of the exponential of the natural log of 80 - 80 < EPSILON
end handler
public handler TestTrunc()
test "trunc (+ve)" when the trunc of 5.5 is 5
test "trunk (-ve)" when the trunc of -5.5 is -5
end handler
public handler TestAbs()
test "abs (+ve)" when the abs of 5.5 is 5.5
test "abs (-ve)" when the abs of -5.5 is 5.5
end handler
public handler TestMinBinary()
test "min (syntax)" when the minimum of 3 and 5 is 3
test "min (function)" when min(4, 5) is 4
end handler
handler TestMinList_Empty()
return the minimum value of []
end handler
public handler TestMinList()
test "min (list)" when the minimum value of [2, 1, 3] is 1
MCUnitTestHandlerThrows(TestMinList_Empty, "min (empty list)")
end handler
public handler TestMaxBinary()
test "max (syntax)" when the maximum of 3 and 5 is 5
test "max (function)" when max(4, 5) is 5
end handler
handler TestMaxList_Empty()
return the maximum value of []
end handler
public handler TestMaxList()
test "max (list)" when the maximum value of [2, 1, 3] is 3
MCUnitTestHandlerThrows(TestMaxList_Empty, "max (empty list)")
end handler
handler TestBaseConvert_InvalidFormat() returns LCUInt
return "0xf004" converted from base 16 -- bug 14889
end handler
public handler TestBaseConvertFrom()
test "convert from hex" when "6A" converted from base 16 is 106
test "convert from binary" when "1001010" converted from base 2 is 74
MCUnitTestHandlerThrows(TestBaseConvert_InvalidFormat, "convert from hex (0x)")
end handler
public handler TestBaseConvertTo()
test "convert to binary" when 7 converted to base 2 is "111"
test "convert to base 12" when 500 converted to base 12 is "358"
end handler
public handler TestBaseConvertBase()
test "convert from base to base" when "1122" converted from base 4 to base 14 is "66"
end handler
public handler TestRandomNumber()
test "Random number range" when any number < 1 and any number >= 0
end handler
end module