forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.lcb
More file actions
384 lines (308 loc) · 9.39 KB
/
sort.lcb
File metadata and controls
384 lines (308 loc) · 9.39 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
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.sort.tests
use com.livecode.sort
use com.livecode.__INTERNAL._testlib
----------------------------------------------------------------
-- Utilities
----------------------------------------------------------------
handler RandomNumericList() returns List
variable tList
put [] into tList
variable tCount
repeat with tCount from 1 up to 100
push any number onto tList
end repeat
return tList
end handler
-- Create a list containing 100 random strings of 1-10 characters
handler RandomStringList() returns List
variable tDict
-- TODO allow generation of *any* codepoint
put "abcdefghijklmnopqrstuvwxyz01234567890!£$%^^&*(){}[]?+/=@'<,>.~#|\\�\u{0}-_" into tDict
variable tList
put [] into tList
variable tCount
repeat with tCount from 1 up to 100
variable tString
put "" into tString
variable tIdx
repeat with tIdx from 1 up to the floor of (any number * 10) + 1
variable tCharIdx
put the floor of (any number * (the number of chars in tDict) + 1) into tCharIdx
variable tChar
put char tCharIdx of tDict into tChar
if any number > 0.5 then
put the upper of tChar after tString
else
put the lower of tChar after tString
end if
end repeat
push tString onto tList
end repeat
return tList
end handler
-- Create a list containing 100 random strings of 1-10 characters, or numbers from 1 to 100
handler RandomMixedList() returns List
variable tStringList as List
variable tNumericList as List
put RandomStringList() into tStringList
put RandomNumericList() into tNumericList
variable tList
put [] into tList
variable tCount
repeat with tCount from 1 up to 100
if any number > 0.5 then
push tStringList[tCount] onto tList
else
push tNumericList[tCount] onto tList
end if
end repeat
return tList
end handler
handler IsSorted(in pList as List, in pDescending as Boolean) returns Boolean
if pList is empty then
return true
end if
variable tLast
pop front of pList into tLast
variable tSorted
put true into tSorted
variable tIter
repeat for each element tIter in pList
if (pDescending and tIter > tLast) or (not pDescending and tIter < tLast) then
put false into tSorted
exit repeat
end if
put tIter into tLast
end repeat
return tSorted
end handler
handler CompareNumericAscending(in pLeft as any, in pRight as any) returns Integer
return CompareNumeric(pLeft, pRight, false)
end handler
handler CompareNumericDescending(in pLeft as any, in pRight as any) returns Integer
return CompareNumeric(pLeft, pRight, true)
end handler
-- Sorts numerically, converting strings to numbers where possible
handler CompareNumeric(in pLeft as any, in pRight as any, in pDescending as Boolean) returns Integer
variable tLeft as optional Number
variable tRight as optional Number
if pLeft is a number then
put pLeft into tLeft
else
put pLeft parsed as number into tLeft
end if
if pRight is a number then
put pRight into tRight
else
put pRight parsed as number into tRight
end if
if tRight is tLeft then
return 0
end if
if tRight is nothing then
return -1
end if
if tLeft is nothing then
return 1
end if
if tLeft < tRight then
if pDescending then
return 1
else
return -1
end if
end if
if pDescending then
return -1
else
return 1
end if
end handler
handler IsSortedUsingHandler(in pList as List, in pHandler as SortCompare) returns Boolean
if pList is empty then
return true
end if
variable tLast
pop front of pList into tLast
variable tSorted
put true into tSorted
variable tIter
repeat for each element tIter in pList
if pHandler(tLast, tIter) > 0 then
put false into tSorted
exit repeat
end if
put tIter into tLast
end repeat
return tSorted
end handler
----------------------------------------------------------------
-- Tests
----------------------------------------------------------------
public handler TestAscendingNumeric() -- RANDOMIZED
variable tRandom
repeat forever
put RandomNumericList() into tRandom
if not IsSorted(tRandom, false) then
exit repeat
end if
end repeat
variable tList
-- Test sorting
put tRandom into tList
sort tList in ascending numeric order
test "sort ascending numeric" when IsSorted(tList, false)
put tRandom into tList
sort tList in ascending order
test "sort ascending generic (numeric)" when IsSorted(tList, false)
-- Test sorting stability
variable tStable
put tList into tStable
sort tList in ascending numeric order
test "sort ascending numeric is stable" when tList is tStable
end handler
public handler TestDescendingNumeric() -- RANDOMIZED
variable tRandom
repeat forever
put RandomNumericList() into tRandom
if not IsSorted(tRandom, true) then
exit repeat
end if
end repeat
variable tList
-- Test sorting
put tRandom into tList
sort tList in descending numeric order
test "sort descending numeric" when IsSorted(tList, true)
put tRandom into tList
sort tList in descending order
test "sort descending generic (numeric)" when IsSorted(tList, true)
-- Test sorting stability
variable tStable
put tList into tStable
sort tList in descending numeric order
test "sort descending numeric is stable" when tList is tStable
end handler
public handler TestAscendingText() -- RANDOMIZED
variable tRandom
repeat forever
put RandomStringList() into tRandom
if not IsSorted(tRandom, false) then
exit repeat
end if
end repeat
variable tList
-- Test sorting
put tRandom into tList
sort tList in ascending text order
test "sort ascending text" when IsSorted(tList, false)
put tRandom into tList
sort tList in ascending order
test "sort ascending generic (text)" when IsSorted(tList, false)
-- Test sorting stability
variable tStable
put tList into tStable
sort tList in ascending text order
test "sort ascending text is stable" when tList is tStable
end handler
public handler TestDescendingText() -- RANDOMIZED
variable tRandom
repeat forever
put RandomStringList() into tRandom
if not IsSorted(tRandom, true) then
exit repeat
end if
end repeat
variable tList
-- Test sorting
put tRandom into tList
sort tList in descending text order
test "sort descending text" when IsSorted(tList, true)
put tRandom into tList
sort tList in descending order
test "sort descending generic (text)" when IsSorted(tList, true)
-- Test sorting stability
variable tStable
put tList into tStable
sort tList in descending text order
test "sort descending text is stable" when tList is tStable
end handler
public handler TestAscendingNumericMixed() -- RANDOMIZED
variable tRandom
repeat forever
put RandomMixedList() into tRandom
if not IsSortedUsingHandler(tRandom, CompareNumericAscending) then
exit repeat
end if
end repeat
variable tList
-- Test sorting
put tRandom into tList
sort tList using handler CompareNumericAscending
test "sort using handler ascending numeric" when IsSortedUsingHandler(tList, CompareNumericAscending)
-- Test sorting stability
variable tStable
put tList into tStable
sort tList using handler CompareNumericAscending
test "sort using handler ascending numeric is stable" when tList is tStable
end handler
public handler TestDescendingNumericMixed() -- RANDOMIZED
variable tRandom
repeat forever
put RandomMixedList() into tRandom
if not IsSortedUsingHandler(tRandom, CompareNumericDescending) then
exit repeat
end if
end repeat
variable tList
-- Test sorting
put tRandom into tList
sort tList using handler CompareNumericDescending
test "sort using handler descending numeric" when IsSortedUsingHandler(tList, CompareNumericDescending)
-- Test sorting stability
variable tStable
put tList into tStable
sort tList using handler CompareNumericDescending
test "sort using handler descending numeric is stable" when tList is tStable
end handler
handler InvalidSortHandler_Args(in pValue as any) returns Integer
return 1
end handler
handler InvalidSortHandler_ReturnType(in pLeft as any, in pRight as any) returns String
return ""
end handler
handler InvalidSortHandler_MissingReturn(in pLeft as any, in pRight as any) returns Integer
end handler
handler TestSortUsingHandler_InvalidArgs()
variable tList as List
put [1,2,3,4] into tList
sort tList using handler InvalidSortHandler_Args
end handler
handler TestSortUsingHandler_InvalidReturnType()
variable tList as List
put [1,2,3,4] into tList
sort tList using handler InvalidSortHandler_ReturnType
end handler
handler TestSortUsingHandler_MissingReturn()
variable tList as List
put [1,2,3,4] into tList
sort tList using handler InvalidSortHandler_MissingReturn
end handler
public handler TestUsingHandlerErrors()
MCUnitTestHandlerThrows(TestSortUsingHandler_InvalidArgs, "handler invalid number of args")
MCUnitTestHandlerThrows(TestSortUsingHandler_InvalidReturnType, "handler invalid return type")
MCUnitTestHandlerThrows(TestSortUsingHandler_MissingReturn, "handler missing return")
end handler
end module