|
| 1 | +/* |
| 2 | +Copyright (C) 2015 Runtime Revolution Ltd. |
| 3 | + |
| 4 | +This file is part of LiveCode. |
| 5 | + |
| 6 | +LiveCode is free software; you can redistribute it and/or modify it under |
| 7 | +the terms of the GNU General Public License v3 as published by the Free |
| 8 | +Software Foundation. |
| 9 | + |
| 10 | +LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 | +WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | +for more details. |
| 14 | + |
| 15 | +You should have received a copy of the GNU General Public License |
| 16 | +along with LiveCode. If not see <http://www.gnu.org/licenses/>. */ |
| 17 | + |
| 18 | +module com.livecode.sort.tests |
| 19 | + |
| 20 | +use com.livecode.sort |
| 21 | + |
| 22 | +---------------------------------------------------------------- |
| 23 | +-- Utilities |
| 24 | +---------------------------------------------------------------- |
| 25 | + |
| 26 | +handler RandomNumericList() as list |
| 27 | + variable tList |
| 28 | + put [] into tList |
| 29 | + |
| 30 | + variable tCount |
| 31 | + repeat with tCount from 1 up to 100 |
| 32 | + push any number onto tList |
| 33 | + end repeat |
| 34 | + |
| 35 | + return tList |
| 36 | +end handler |
| 37 | + |
| 38 | +-- Create a list containing 100 random strings of 1-10 characters |
| 39 | +handler RandomStringList() as list |
| 40 | + variable tDict |
| 41 | + -- TODO allow generation of *any* codepoint |
| 42 | + put "abcdefghijklmnopqrstuvwxyz01234567890!£$%^^&*(){}[]?+/=@'<,>.~#|\\�\u{0}-_" into tDict |
| 43 | + |
| 44 | + variable tList |
| 45 | + put [] into tList |
| 46 | + |
| 47 | + variable tCount |
| 48 | + repeat with tCount from 1 up to 100 |
| 49 | + variable tString |
| 50 | + put "" into tString |
| 51 | + |
| 52 | + variable tIdx |
| 53 | + repeat with tIdx from 1 up to the floor of (any number * 10) + 1 |
| 54 | + variable tCharIdx |
| 55 | + put the floor of (any number * (the number of chars in tDict) + 1) into tCharIdx |
| 56 | + variable tChar |
| 57 | + put char tCharIdx of tDict into tChar |
| 58 | + |
| 59 | + if any number > 0.5 then |
| 60 | + put the upper of tChar after tString |
| 61 | + else |
| 62 | + put the lower of tChar after tString |
| 63 | + end if |
| 64 | + end repeat |
| 65 | + push tString onto tList |
| 66 | + end repeat |
| 67 | + |
| 68 | + return tList |
| 69 | +end handler |
| 70 | + |
| 71 | +handler IsSorted(in pList as list, in pDescending as boolean) as boolean |
| 72 | + if pList is empty then |
| 73 | + return true |
| 74 | + end if |
| 75 | + |
| 76 | + variable tLast |
| 77 | + pop front of pList into tLast |
| 78 | + |
| 79 | + variable tSorted |
| 80 | + put true into tSorted |
| 81 | + |
| 82 | + variable tIter |
| 83 | + repeat for each element tIter in pList |
| 84 | + if (pDescending and tIter > tLast) or (not pDescending and tIter < tLast) then |
| 85 | + put false into tSorted |
| 86 | + exit repeat |
| 87 | + end if |
| 88 | + put tIter into tLast |
| 89 | + end repeat |
| 90 | + return tSorted |
| 91 | +end handler |
| 92 | + |
| 93 | +---------------------------------------------------------------- |
| 94 | +-- Tests |
| 95 | +---------------------------------------------------------------- |
| 96 | + |
| 97 | +public handler TestAscendingNumeric() -- RANDOMIZED |
| 98 | + variable tRandom |
| 99 | + repeat forever |
| 100 | + put RandomNumericList() into tRandom |
| 101 | + if not IsSorted(tRandom, false) then |
| 102 | + exit repeat |
| 103 | + end if |
| 104 | + end repeat |
| 105 | + |
| 106 | + variable tList |
| 107 | + |
| 108 | + -- Test sorting |
| 109 | + put tRandom into tList |
| 110 | + sort tList in ascending numeric order |
| 111 | + test "sort ascending numeric" when IsSorted(tList, false) |
| 112 | + |
| 113 | + put tRandom into tList |
| 114 | + sort tList in ascending order |
| 115 | + test "sort ascending generic (numeric)" when IsSorted(tList, false) |
| 116 | + |
| 117 | + -- Test sorting stability |
| 118 | + variable tStable |
| 119 | + put tList into tStable |
| 120 | + sort tList in ascending numeric order |
| 121 | + test "sort ascending numeric is stable" when tList is tStable |
| 122 | +end handler |
| 123 | + |
| 124 | +public handler TestDescendingNumeric() -- RANDOMIZED |
| 125 | + variable tRandom |
| 126 | + repeat forever |
| 127 | + put RandomNumericList() into tRandom |
| 128 | + if not IsSorted(tRandom, true) then |
| 129 | + exit repeat |
| 130 | + end if |
| 131 | + end repeat |
| 132 | + |
| 133 | + variable tList |
| 134 | + |
| 135 | + -- Test sorting |
| 136 | + put tRandom into tList |
| 137 | + sort tList in descending numeric order |
| 138 | + test "sort descending numeric" when IsSorted(tList, true) |
| 139 | + |
| 140 | + put tRandom into tList |
| 141 | + sort tList in descending order |
| 142 | + test "sort descending generic (numeric)" when IsSorted(tList, true) |
| 143 | + |
| 144 | + -- Test sorting stability |
| 145 | + variable tStable |
| 146 | + put tList into tStable |
| 147 | + sort tList in descending numeric order |
| 148 | + test "sort descending numeric is stable" when tList is tStable |
| 149 | +end handler |
| 150 | + |
| 151 | +public handler TestAscendingText() -- RANDOMIZED |
| 152 | + variable tRandom |
| 153 | + repeat forever |
| 154 | + put RandomStringList() into tRandom |
| 155 | + if not IsSorted(tRandom, false) then |
| 156 | + exit repeat |
| 157 | + end if |
| 158 | + end repeat |
| 159 | + |
| 160 | + variable tList |
| 161 | + |
| 162 | + -- Test sorting |
| 163 | + put tRandom into tList |
| 164 | + sort tList in ascending text order |
| 165 | + broken test "sort ascending text" when IsSorted(tList, false) because "bug 14599" |
| 166 | + |
| 167 | + put tRandom into tList |
| 168 | + sort tList in ascending order |
| 169 | + broken test "sort ascending generic (text)" when IsSorted(tList, false) because "bug 14599" |
| 170 | + |
| 171 | + -- Test sorting stability |
| 172 | + variable tStable |
| 173 | + put tList into tStable |
| 174 | + sort tList in ascending text order |
| 175 | + test "sort ascending text is stable" when tList is tStable |
| 176 | +end handler |
| 177 | + |
| 178 | +public handler TestDescendingText() -- RANDOMIZED |
| 179 | + variable tRandom |
| 180 | + repeat forever |
| 181 | + put RandomStringList() into tRandom |
| 182 | + if not IsSorted(tRandom, true) then |
| 183 | + exit repeat |
| 184 | + end if |
| 185 | + end repeat |
| 186 | + |
| 187 | + variable tList |
| 188 | + |
| 189 | + -- Test sorting |
| 190 | + put tRandom into tList |
| 191 | + sort tList in descending text order |
| 192 | + broken test "sort descending text" when IsSorted(tList, true) because "bug 14599" |
| 193 | + |
| 194 | + put tRandom into tList |
| 195 | + sort tList in descending order |
| 196 | + broken test "sort descending generic (text)" when IsSorted(tList, true) because "bug 14599" |
| 197 | + |
| 198 | + -- Test sorting stability |
| 199 | + variable tStable |
| 200 | + put tList into tStable |
| 201 | + sort tList in descending text order |
| 202 | + test "sort descending text is stable" when tList is tStable |
| 203 | +end handler |
| 204 | + |
| 205 | +end module |
0 commit comments