|
| 1 | +-- Tests for vigenere_cipher.lua |
| 2 | +local vigenere = require 'vigenere_cipher' |
| 3 | + |
| 4 | +local total, pass = 0, 0 |
| 5 | + |
| 6 | +local function dec(str, len) |
| 7 | + return #str < len |
| 8 | + and str .. (('.'):rep(len-#str)) |
| 9 | + or str:sub(1,len) |
| 10 | +end |
| 11 | + |
| 12 | +local function run(message, f) |
| 13 | + total = total + 1 |
| 14 | + local ok, err = pcall(f) |
| 15 | + if ok then pass = pass + 1 end |
| 16 | + local status = ok and 'PASSED' or 'FAILED' |
| 17 | + print(('%02d. %68s: %s'):format(total, dec(message,68), status)) |
| 18 | +end |
| 19 | + |
| 20 | +run('Ciphering test', function() |
| 21 | + assert(caesar.cipher('abcd',1) == 'bcde') |
| 22 | + assert(caesar.cipher('WXYZ',2) == 'YZAB') |
| 23 | + assert(caesar.cipher('abcdefghijklmnopqrstuvwxyz',3) == 'defghijklmnopqrstuvwxyzabc') |
| 24 | + assert(caesar.cipher('ABCDEFGHIJKLMNOPQRSTUVWXYZ',4) == 'EFGHIJKLMNOPQRSTUVWXYZABCD') |
| 25 | +end) |
| 26 | + |
| 27 | +run('Deciphering test', function() |
| 28 | + assert(caesar.decipher('bcde',1) == 'abcd') |
| 29 | + assert(caesar.decipher('YZAB',2) == 'WXYZ') |
| 30 | + assert(caesar.decipher('defghijklmnopqrstuvwxyzabc',3) == 'abcdefghijklmnopqrstuvwxyz') |
| 31 | + assert(caesar.decipher('EFGHIJKLMNOPQRSTUVWXYZABCD',4) == 'ABCDEFGHIJKLMNOPQRSTUVWXYZ') |
| 32 | +end) |
| 33 | + |
| 34 | +print(('-'):rep(80)) |
| 35 | +print(('Total : %02d: Pass: %02d - Failed : %02d - Success: %.2f %%') |
| 36 | + :format(total, pass, total-pass, (pass*100/total))) |
0 commit comments