|
| 1 | +script "TestCiphers" |
| 2 | + |
| 3 | +on TestSetup |
| 4 | + local tSourceFile |
| 5 | + put the filename of me into tSourceFile |
| 6 | + set the itemdelimiter to slash |
| 7 | + put "src/PuzzleTools.livecodescript" into item -3 to -1 of tSourceFile |
| 8 | + start using stack tSourceFile |
| 9 | +end TestSetup |
| 10 | + |
| 11 | +private command TestAssertUserThrows pDesc, pHandler, pError |
| 12 | + local tError |
| 13 | + try |
| 14 | + dispatch pHandler to me |
| 15 | + catch tError |
| 16 | + end try |
| 17 | + |
| 18 | + TestDiagnostic tError |
| 19 | + |
| 20 | + TestAssert pDesc, tError is pError |
| 21 | +end TestAssertUserThrows |
| 22 | + |
| 23 | +on LetterToNumberThrow |
| 24 | + get toNumber("'") |
| 25 | +end LetterToNumberThrow |
| 26 | + |
| 27 | +on TestLetterToNumber |
| 28 | + local tIndex |
| 29 | + put 0 into tIndex |
| 30 | + repeat for each char tChar in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 31 | + add 1 to tIndex |
| 32 | + TestAssert "uppercase letter" && tChar && "to number correct", \ |
| 33 | + toNumber(tChar) is tIndex |
| 34 | + end repeat |
| 35 | + |
| 36 | + put 0 into tIndex |
| 37 | + repeat for each char tChar in "abcdefghijklmnopqrstuvwxyz" |
| 38 | + add 1 to tIndex |
| 39 | + TestAssert "lowercase letter" && tChar && "to number correct", \ |
| 40 | + toNumber(tChar) is tIndex |
| 41 | + end repeat |
| 42 | + |
| 43 | + TestAssertUserThrows "non alphabetic input to toNumber function throws", \ |
| 44 | + "LetterToNumberThrow", "char not alphabetical" |
| 45 | +end TestLetterToNumber |
| 46 | + |
| 47 | +on TestLetterFromNumber |
| 48 | + local tIndex |
| 49 | + put 0 into tIndex |
| 50 | + repeat for each char tChar in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 51 | + add 1 to tIndex |
| 52 | + TestAssert "uppercase letter" && tChar && "from number correct", \ |
| 53 | + fromNumber(tIndex) is tChar |
| 54 | + end repeat |
| 55 | + |
| 56 | + put 0 into tIndex |
| 57 | + repeat for each char tChar in "abcdefghijklmnopqrstuvwxyz" |
| 58 | + add 1 to tIndex |
| 59 | + TestAssert "lowercase letter" && tChar && "from number correct", \ |
| 60 | + fromNumber(tIndex) is tChar |
| 61 | + end repeat |
| 62 | +end TestLetterFromNumber |
| 63 | + |
| 64 | +on TestCaesarCipher |
| 65 | + TestAssert "abjurer <-> nowhere (13)", caesarShift("abjurer", 13) is "nowhere" |
| 66 | + TestAssert "inkier <-> purply (7)", caesarShift("inkier", 7) is "purply" |
| 67 | + TestAssert "fusion <-> layout (6)", caesarShift("fusion", 6) is "layout" |
| 68 | + TestAssert "manful <-> thumbs (7)", caesarShift("manful", 7) is "thumbs" |
| 69 | + TestAssert "primero <-> sulphur (3)", caesarShift("primero", 3) is "sulphur" |
| 70 | + TestAssert "steeds <-> tuffet (1)", caesarShift("steeds", 1) is "tuffet" |
| 71 | +end TestCaesarCipher |
| 72 | + |
| 73 | +on TestVigenereCipher |
| 74 | + TestAssert "LIVECODE encoded using vigenere cipher with key TEST is EMNXVSVX", \ |
| 75 | + vigenereShift("LIVECODE","TEST") is "EMNXVSVX" |
| 76 | +end TestVigenereCipher |
| 77 | + |
0 commit comments