|
| 1 | +-- Tests for sor.lua |
| 2 | +local sor = require 'sor' |
| 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 | +local function fuzzy_eq(a, b, eps) |
| 21 | + eps = eps or 1e-6 |
| 22 | + return math.abs(a - b) < eps |
| 23 | +end |
| 24 | + |
| 25 | +run('Testing SOR', function() |
| 26 | + local m = { |
| 27 | + {-4,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,-11}, |
| 28 | + {1,-4,1,0,0,1,0,0,0,0,0,0,0,0,0,0,-03}, |
| 29 | + {0,1,-4,1,0,0,1,0,0,0,0,0,0,0,0,0,-03}, |
| 30 | + {0,0,1,-4,0,0,0,1,0,0,0,0,0,0,0,0,-11}, |
| 31 | + {1,0,0,0,-4,1,0,0,1,0,0,0,0,0,0,0,-08}, |
| 32 | + {0,1,0,0,1,-4,1,0,0,1,0,0,0,0,0,0, 00}, |
| 33 | + {0,0,1,0,0,1,-4,1,0,0,1,0,0,0,0,0, 00}, |
| 34 | + {0,0,0,1,0,0,1,-4,0,0,0,1,0,0,0,0,-08}, |
| 35 | + {0,0,0,0,1,0,0,0,-4,1,0,0,1,0,0,0,-08}, |
| 36 | + {0,0,0,0,0,1,0,0,1,-4,1,0,0,1,0,0, 00}, |
| 37 | + {0,0,0,0,0,0,1,0,0,1,-4,1,0,0,1,0, 00}, |
| 38 | + {0,0,0,0,0,0,0,1,0,0,1,-4,0,0,0,1,-08}, |
| 39 | + {0,0,0,0,0,0,0,0,1,0,0,0,-4,1,0,0,-10}, |
| 40 | + {0,0,0,0,0,0,0,0,0,1,0,0,1,-4,1,0,-02}, |
| 41 | + {0,0,0,0,0,0,0,0,0,0,1,0,0,1,-4,1,-02}, |
| 42 | + {0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,-4,-10}, |
| 43 | + } |
| 44 | + |
| 45 | + local x = sor(m) |
| 46 | + assert(fuzzy_eq(x[1], 5.454459)) |
| 47 | + assert(fuzzy_eq(x[2], 4.594688)) |
| 48 | + assert(fuzzy_eq(x[3], 4.594679)) |
| 49 | + assert(fuzzy_eq(x[4], 5.454531)) |
| 50 | + assert(fuzzy_eq(x[5], 6.223469)) |
| 51 | + assert(fuzzy_eq(x[6], 5.329580)) |
| 52 | + assert(fuzzy_eq(x[7], 5.329561)) |
| 53 | + assert(fuzzy_eq(x[8], 6.223491)) |
| 54 | + assert(fuzzy_eq(x[9], 6.109833)) |
| 55 | + assert(fuzzy_eq(x[10], 5.170488)) |
| 56 | + assert(fuzzy_eq(x[11], 5.170455)) |
| 57 | + assert(fuzzy_eq(x[12], 6.109845)) |
| 58 | + assert(fuzzy_eq(x[13], 5.045460)) |
| 59 | + assert(fuzzy_eq(x[14], 4.071980)) |
| 60 | + assert(fuzzy_eq(x[15], 4.071964)) |
| 61 | + assert(fuzzy_eq(x[16], 5.045457)) |
| 62 | +end) |
| 63 | + |
| 64 | +print(('-'):rep(80)) |
| 65 | +print(('Total : %02d: Pass: %02d - Failed : %02d - Success: %.2f %%') |
| 66 | + :format(total, pass, total-pass, (pass*100/total))) |
0 commit comments