Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.
C calculatorcalculator-gamescasiochesscrossy-roaddrive-madfx-cg50gamesgeometry-dashgraphing-calculatorndlessnumworksnumworks-appsnumworks-calculatornumworks-gamenumworks-gamestetristi-84-plus-ceti-nspire
Cat Ninja: tests
Mason36373c56282e360
diff --git a/games/catninja/tests/play.c b/games/catninja/tests/play.cindex 6ce76ee2..5bfd67ee 100644--- a/games/catninja/tests/play.c+++ b/games/catninja/tests/play.c@@ -7,6 +7,7 @@#include "../src/main.c"#undef main#include <stdio.h>+#include <math.h>#include <stdlib.h>/* ---------------------------------------------------------------- the calculator, on a computer */@@ -202,7 +203,268 @@ static void at_level(int lv) {test_goto(lv);}++/* ---------------------------------------------------------------- the tests */+static double trunc20(double v) { return (double)(int)(v * 20) / 20; } /* a display object's x: twips */+extern void game_debug_goto(int lv);+extern void mine_create(int x, int y, int g);+static void press(int k) { key_press(k); }+static void release(int k) { key_release(k); }+static void steps(int n) {+ for (int i = 0; i < n; i++) game_step();+}+static void settle(void) { /* standing still on the ground */+ key_left = key_right = key_up = key_down = false;+ for (int i = 0; i < 120 && (i < 10 || !(P.onground && P.speedx == 0)); i++) game_step();+}++/* spec/physics.md: the run, the jumps, the roll, the wall jump, the spring */+static void test_physics(void) {+ /* running: each frame the speed is held to the last frame's top (6), gains 1, loses a tenth; the cat moves by it,+ its place kept in twentieths of a pixel (twips, truncated) */+ game_new();+ game_debug_goto(1); /* level 2's first floor */+ settle();+ double x = P.x, s = P.speedx;+ int top = P.maxspeedx;+ press(GK_RIGHT);+ for (int f = 0; f < 40; f++) {+ game_step();+ if (s >= top) s = top;+ top = 6;+ s = (s + 1) * 0.9;+ x = trunc20(x + s);+ CHECK(P.x == x && P.speedx == s, "run, frame %d: x %.2f (spec %.2f), speed %.4f (spec %.4f)", f, P.x, x, P.speedx, s);+ }+ CHECK(fabs(P.speedx - 6.3) < 1e-9, "running speed %.4f, spec 6.3", P.speedx);+ release(GK_RIGHT);+ settle();+ CHECK(P.speedx == 0 && P.onground, "stops: speed %.3f", P.speedx);++ /* jumping: up sets -8; each frame 0.5 more; a jump rises 60 pixels (three tiles) and lands where it began */+ game_new();+ game_debug_goto(-1); /* the title map: open above */+ settle();+ double y0 = P.y, ymin = P.y;+ press(GK_UP);+ release(GK_UP);+ CHECK(P.gravity == -8, "jump: gravity %.2f, spec -8", P.gravity);+ int f = 0;+ for (; f < 100; f++) {+ game_step();+ if (P.y < ymin) ymin = P.y;+ if (P.onground) break;+ }+ CHECK(y0 - ymin == 60, "jump height %.2f, spec 60", y0 - ymin);+ CHECK(P.y == y0, "lands at %.2f, began at %.2f", P.y, y0);++ /* the double jump: again in the air, once gravity is -6 or more: -8 again (120 pixels in all from the top) */+ settle();+ y0 = ymin = P.y;+ press(GK_UP), release(GK_UP);+ for (f = 0; f < 16; f++) game_step(); /* (the top of the first jump) */+ press(GK_UP), release(GK_UP);+ CHECK(P.gravity == -8 && P.doublefalling, "double jump: gravity %.2f", P.gravity);+ press(GK_UP), release(GK_UP);+ CHECK(P.gravity == -8 && !P.doublejump, "only one double jump");+ for (f = 0; f < 100; f++) {+ game_step();+ if (P.y < ymin) ymin = P.y;+ if (P.onground) break;+ }+ CHECK(y0 - ymin == 120, "double jump height %.2f, spec 120", y0 - ymin);++ /* rolling: Down held while running: top speed 10, friction 0.94: (10 + 1) x 0.94 = 10.34 */+ game_new();+ game_debug_goto(1);+ settle();+ press(GK_DOWN), press(GK_RIGHT);+ steps(60);+ CHECK(fabs(P.speedx - 10.34) < 1e-9 && P.rolling, "roll speed %.4f, spec 10.34", P.speedx);+ release(GK_DOWN), release(GK_RIGHT);++ /* the wall jump: up against a wall of ground in the air: 12 away from it, -12 up */+ game_new();+ game_debug_goto(1);+ P.x = 36 * 20 + OFFX - 1, P.y = 25 * 20 + OFFY, P.speedx = 0, P.gravity = 2, P.onground = false, P.doublejump = false;+ press(GK_UP), release(GK_UP);+ CHECK(P.speedx == 12 && P.gravity == -12, "wall jump: speed %.2f, gravity %.2f, spec 12 and -12", P.speedx, P.gravity);+ /* sliding down it: holding towards it while falling: gravity 2 */+ P.x = 36 * 20 + OFFX - 1, P.y = 25 * 20 + OFFY, P.speedx = -1, P.gravity = 3, P.onground = false;+ press(GK_LEFT);+ game_step();+ CHECK(P.gravity == 2 && P.hitwall, "wall slide: gravity %.2f, spec 2", P.gravity);+ release(GK_LEFT);++ /* the spring: -20 */+ game_new();+ game_debug_goto(-1);+ settle();+ change_tile((int)(P.x / 20), (int)(P.y / 20), 62, true, false, true, true);+ game_step();+ CHECK(P.gravity == -20, "spring: gravity %.2f, spec -20", P.gravity);++ /* a mine: set off, it blinks at 10, 8, 6, 4, 2 frames, then goes off (30 frames) */+ game_new();+ game_debug_goto(-1);+ settle();+ int m = n_mines;+ mine_create((int)P.x - 10, (int)P.y - 10, 0);+ for (f = 1; f <= 40 && !mines[m].off; f++) game_step();+ CHECK(f - 1 == 30, "mine: off after %d frames, spec 30", f - 1);++ /* the laser of level 9's second script: on from the start, switched every second (60 frames): off at 2 s */+ game_new();+ game_debug_goto(8);+ int lx = 85, ly = 27, t0 = grid[ly * gw + lx] & 0x7F;+ steps(61);+ int t1 = grid[ly * gw + lx] & 0x7F;+ steps(60);+ int t2 = grid[ly * gw + lx] & 0x7F;+ steps(60);+ int t3 = grid[ly * gw + lx] & 0x7F;+ CHECK(t0 == 15 && t1 == 15 && t2 == 0 && t3 == 15, "laser: %d %d %d %d, spec 15 15 0 15", t0, t1, t2, t3);++ /* dying: the message, then the cat back at its checkpoint 1.5 s (90 frames) later */+ game_new();+ game_debug_goto(1);+ settle();+ int sx = P.spawnx, sy = P.spawny, d0 = pdeaths;+ press(GK_R), release(GK_R);+ CHECK(P.dead && pdeaths == d0 + 1, "R kills the cat");+ for (f = 1; f <= 120 && P.dead; f++) game_step();+ CHECK(f - 1 == 90 && P.x == sx + OFFX && P.y == sy + OFFY, "back after %d frames at %.0f, %.0f (spec 90)", f - 1, P.x,+ P.y);+}++/* the first levels, played: from the title map through level 1 and level 2, to level 3 */+static void test_run(void) {+ game_new();+ bot_dir = 1, bot_wait = 0, bot_climb = false, bot_held = 0;+ int reached = -2, at = 0;+ for (int f = 0; f < 1400 && reached < 2; f++) {+ bot_frame();+ if (level > reached) reached = level, at = f;+ }+ CHECK(reached == 2, "the run reached level %d", reached + 1);+ CHECK(pdeaths == 0, "the run died %d times", pdeaths);+ printf(" title map to level 3 in %d frames (%.1f s), %d crystals\n", at, at / 60.0, collected);+}++/* every level: loaded, its scripts run, ten seconds of random keys each (twice), nothing out of the map */+static void test_levels(void) {+ for (int round = 0; round < 2; round++)+ for (int lv = -1; lv < 30; lv++) {+ game_new();+ game_debug_goto(lv);+ uint32_t held = 0;+ for (int f = 0; f < 600; f++) {+ uint32_t g = lcg() >> 7 & 63, on = g & ~held, off = held & ~g;+ if (lcg() % 4) g = held, on = off = 0;+ for (int i = 0; i < 6; i++) {+ if (off >> i & 1) key_release(i);+ if (on >> i & 1) key_press(i);+ }+ held = g;+ game_step();+ if (f % 3 == 0) {+ draw_frame();+ gfx_render(scene, NULL, 0, 240);+ }+ CHECK(level >= -1 && level < 30 && P.x == P.x && P.y == P.y, "level %d: lost (level %d)", lv + 1, level);+ }+ }+}++/* the whole app on random keys: pauses, menus, the How to play page, quitting and coming back */+static uint32_t fuzz_keys(void) {+ static uint32_t k;+ if (lcg() % 6 == 0) {+ static const uint32_t pick[] = {PK_LEFT, PK_RIGHT, PK_UP, PK_DOWN, PK_OK, PK_EXE, PK_BACK, PK_DEL, PK_SHIFT, 0};+ k = pick[lcg() % 10] | (lcg() % 3 ? 0 : pick[lcg() % 10]);+ if (k & PK_BACK && lcg() % 4) k &= ~PK_BACK;+ }+ return k;+}+static void test_fuzz(void) {+ for (int run = 0; run < 12; run++) {+ if (run % 4 == 0) files_clear();+ lcg_s = 1000 + run;+ frame_no = 0, quit_at = 2500 + lcg() % 1500;+ script = fuzz_keys;+ catninja_main();+ uint32_t n;+ const uint8_t *d = file_of(SAVE_NAME, &n);+ save_t t;+ CHECK(d && n == sizeof t, "fuzz %d: no save", run);+ if (d && n == sizeof t) {+ memcpy(&t, d, sizeof t);+ CHECK(save_ok(&t), "fuzz %d: a bad save", run);+ }+ }+ script = 0, quit_at = 0xFFFFFFFFu;+}++/* the saves: kept, damaged, lost, from the copy; and the game back where it was */+static void test_saves(void) {+ files_clear();+ /* play into level 2 with the bot, then quit */+ game_new();+ bot_dir = 1, bot_wait = 0, bot_climb = false, bot_held = 0;+ for (int f = 0; f < 600; f++) bot_frame();+ CHECK(level == 1, "the bot is in level 2 (%d)", level + 1);+ int lv = level;+ save();+ uint32_t n;+ CHECK(plat_load(SAVE_NAME, &n) && n == sizeof(save_t), "the save is written");+ save_t kept = sv;+ /* back: the title map, the way right leads to level 2, where the cat was */+ memset(&sv, 0, sizeof sv);+ load();+ CHECK(!memcmp(&sv, &kept, sizeof sv), "the save reads back");+ start_game();+ CHECK(level == -1 && playerpoints[5] == lv, "back on the title map, its way right to level %d", playerpoints[5] + 1);+ /* lost: from the copy */+ file_drop(SAVE_NAME);+ memset(&sv, 0, sizeof sv);+ load();+ CHECK(!memcmp(&sv, &kept, sizeof sv), "the save comes back from the copy");+ /* damaged: from the copy */+ save();+ uint8_t *d = file_of(SAVE_NAME, &n);+ d[10] ^= 0x55;+ memset(&sv, 0, sizeof sv);+ load();+ CHECK(sv.level == kept.level && sv.x == kept.x && save_ok(&sv), "a damaged save comes back from the copy");+ /* both gone: a new game */+ files_clear();+ memset(&sv, 0x77, sizeof sv);+ load();+ CHECK(sv.level == -1 && sv.deaths == 0, "no save: a new game");+ /* a damaged copy alone: a new game, no crash */+ files_clear();+ plat_save(COPY_NAME, "\0# Cat Ninja's saves: keep this file\n#>catninja.sav:AAAA!!\n", 60);+ load();+ CHECK(sv.level == -1, "a damaged copy: a new game");+}+int main(int argc, char **argv) {+ if (argc >= 2 && !strcmp(argv[1], "test")) {+ gfx_buf = strip;+ draw_init();+ printf("physics\n");+ test_physics();+ printf("the first levels\n");+ test_run();+ printf("every level, random keys\n");+ test_levels();+ printf("saves\n");+ test_saves();+ printf("the app, random keys\n");+ test_fuzz();+ printf("%s\n", fails ? "FAILED" : "ok");+ return fails ? 1 : 0;+ }if (argc >= 4 && (!strcmp(argv[1], "shot") || !strcmp(argv[1], "film"))) {shot_dir = argv[2];parse_keys(argv[3]);