code wiki / (root) / nx_econ_game_test.nx

nx_econ_game_test.nx source

↩ module page · 58 lines · 3063 B

1// nx_econ_game_test.nx -- KAT gate for the build-and-play tycoon emitter. 2// Verifies the economy machinery (produce/sell/upkeep/win/bankrupt/affordability) 3// + the board; saves /tmp/econ_game.html. expect_exit: 0. 4 5import "nx_syscalls.nx" 6import "nx_econ_game.nx" 7 8func w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 9func wn(v: i64) -> i64 { 10 let b: *u8 = sys_mmap(28); var m: i64 = v 11 if m < 0 { m = 0 - m; sys_write(1, "-" as *u8, 1) } 12 let t: *u8 = sys_mmap(28); var k: i64 = 0 13 if m == 0 { t[0] = 48 as u8; k = 1 } 14 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 15 var i: i64 = 0; while i < k { b[i] = t[k - 1 - i]; i = i + 1 } 16 sys_write(1, b, k); return 0 17} 18func contains(hay: *u8, hn: i64, ndl: *u8) -> i64 { 19 var m: i64 = 0; while ndl[m] != (0 as u8) { m = m + 1 } 20 if m == 0 { return 1 } 21 var i: i64 = 0 22 while i + m <= hn { var j: i64 = 0; var ok: i64 = 1; while j < m { if hay[i + j] != ndl[j] { ok = 0; j = m } else { j = j + 1 } } if ok == 1 { return 1 } i = i + 1 } 23 return 0 24} 25func chk(cond: i64, pass: *i64, fail: *i64, label: *u8) -> i64 { 26 if cond == 1 { pass[0] = pass[0] + 1; w(" ok " as *u8); w(label); w("\n" as *u8) } 27 else { fail[0] = fail[0] + 1; w(" XX " as *u8); w(label); w("\n" as *u8) } 28 return 0 29} 30 31func main() -> i64 { 32 let pass: *i64 = (sys_mmap(8)) as *i64 33 let fail: *i64 = (sys_mmap(8)) as *i64 34 pass[0] = 0 35 fail[0] = 0 36 w("=== nx_econ_game KAT (build-and-play tycoon) ===\n" as *u8) 37 let out: *u8 = sys_mmap(65536) 38 let len: i64 = nx_econ_game_page(out, 65536) 39 w(" page bytes=" as *u8); wn(len); w("\n" as *u8) 40 chk(contains(out, len, "<canvas id=c" as *u8) == 1, pass, fail, "canvas board" as *u8) 41 chk(contains(out, len, "setInterval(tick,1000)" as *u8) == 1, pass, fail, "monthly tick loop" as *u8) 42 chk(contains(out, len, "inv+=nf*10" as *u8) == 1, pass, fail, "factories produce" as *u8) 43 chk(contains(out, len, "Math.min(inv,ns*8)" as *u8) == 1, pass, fail, "stores sell (capacity)" as *u8) 44 chk(contains(out, len, "sold*5-(nf+ns)*5" as *u8) == 1, pass, fail, "revenue minus upkeep" as *u8) 45 chk(contains(out, len, "money>=200" as *u8) == 1, pass, fail, "must afford to build" as *u8) 46 chk(contains(out, len, "if(money>=5000)over=1" as *u8) == 1, pass, fail, "win at $5000" as *u8) 47 chk(contains(out, len, "if(money<0)over=2" as *u8) == 1, pass, fail, "bankrupt if money<0" as *u8) 48 chk(contains(out, len, "function dFactory" as *u8) == 1, pass, fail, "factory sprite" as *u8) 49 chk(contains(out, len, "YOU WIN" as *u8) == 1, pass, fail, "win state shown" as *u8) 50 51 let wr: i64 = nx_econ_game_save("/tmp/econ_game.html" as *u8) 52 w(" wrote /tmp/econ_game.html bytes=" as *u8); wn(wr); w("\n" as *u8) 53 chk(wr == len, pass, fail, "/tmp/econ_game.html saved (playable!)" as *u8) 54 55 w("=== VERDICT pass=" as *u8); wn(pass[0]); w(" fail=" as *u8); wn(fail[0]); w(" ===\n" as *u8) 56 if fail[0] == 0 { return 0 } 57 return 1 58}