Files

260 lines
8.6 KiB
C++

// Minimal self-contained tests for tbg_core. Exit code = number of failures.
#include <cstdio>
#include "core/board.hpp"
#include "core/player.hpp"
#include "core/queue.hpp"
#include "check.hpp"
int g_failures = 0;
void run_match_tests();
void run_modern_tests();
void run_modern_cpu_tests();
namespace {
using namespace tbg;
PieceGenerator always(PieceType t, bool crystal = false) {
return [t, crystal] { return QueuedPiece{t, crystal}; };
}
Timing fast_timing() {
return Timing::instant();
}
void test_collision_walls_floor_hidden() {
Board b;
CHECK(!b.collides({PieceType::O, false, 0, 0, 0}));
CHECK(b.collides({PieceType::O, false, -2, 0, 0})); // O occupies box cols 1..2
CHECK(b.collides({PieceType::O, false, b.width() - 2, 0, 0}));
CHECK(b.collides({PieceType::O, false, 0, b.height() - 1, 0}));
b.set(1, 0, cell::kBlock); // hidden rows never block (81:CE6C)
CHECK(!b.collides({PieceType::O, false, 0, 0, 0}));
b.set(1, 2, cell::kBlock);
CHECK(b.collides({PieceType::O, false, 0, 1, 0}));
}
void test_line_clear_shifts_rows_and_counts_crystals() {
Board b;
const int bottom = b.height() - 1;
for (int x = 0; x < b.width(); ++x) b.set(x, bottom, cell::kBlock);
b.set(5, bottom, cell::kCrystal);
b.set(3, bottom - 1, cell::kStone);
for (int x = 0; x < b.width(); ++x) b.set(x, bottom - 2, cell::kBlock);
const ClearResult r = b.clear_full_rows();
CHECK(r.rows == 2);
CHECK(r.crystals == 1);
CHECK(b.at(3, bottom) == cell::kStone);
for (int x = 0; x < b.width(); ++x)
if (x != 3) CHECK(b.at(x, bottom) == cell::kEmpty);
CHECK(b.at(3, bottom - 1) == cell::kEmpty);
}
void test_hidden_rows_never_clear() {
Board b;
for (int x = 0; x < b.width(); ++x) b.set(x, 0, cell::kBlock);
CHECK(b.clear_full_rows().rows == 0);
}
void test_original_generator_rules() {
PieceGenerator g = make_original_generator(42, 5);
QueuedPiece prev = g();
int crystals = prev.crystal ? 1 : 0;
for (int i = 1; i < 1000; ++i) {
const QueuedPiece p = g();
CHECK(p.type != prev.type);
CHECK(static_cast<int>(p.type) < kPieceTypeCount);
CHECK(p.crystal == ((i + 1) % 5 == 0));
crystals += p.crystal ? 1 : 0;
prev = p;
}
CHECK(crystals == 200);
}
void test_contested_vs_mirrored_queue() {
int n = 0;
PieceGenerator counting = [&n] { return QueuedPiece{static_cast<PieceType>(n++ % kPieceTypeCount), false}; };
PieceQueue contested(counting, 2, QueueMode::Contested);
CHECK(contested.pop(0).type == PieceType::O);
CHECK(contested.pop(1).type == PieceType::L); // P2 gets the *next* piece
CHECK(contested.peek(0).type == contested.peek(1).type);
n = 0;
PieceQueue mirrored(counting, 2, QueueMode::Mirrored);
CHECK(mirrored.pop(0).type == PieceType::O);
CHECK(mirrored.pop(1).type == PieceType::O); // P2 gets its own copy
CHECK(mirrored.pop(0).type == PieceType::L);
}
void test_piece_falls_and_locks_at_spawn_column() {
PieceQueue q(always(PieceType::O), 1);
Player p(0, q, fast_timing());
FrameResult r;
int frames = 0;
while (!r.locked && frames < 10000) {
r = p.step({});
++frames;
}
CHECK(r.locked);
const int bottom = p.board().height() - 1;
CHECK(p.board().at(4, bottom) == cell::kBlock); // box at col 3, O cells at box cols 1..2
CHECK(p.board().at(5, bottom) == cell::kBlock);
// Level 0: 0.25 px/frame => 32 frames/row; O bottom starts at row 1, needs 22 rows + 16 grounded frames (DP $79).
CHECK(frames >= 22 * 32 + 16 && frames <= 22 * 32 + 20);
}
void test_crystal_cell_locks_as_crystal() {
PieceQueue q(always(PieceType::T, true), 1);
Timing t = fast_timing();
t.allow_hard_drop = true;
Player p(0, q, t);
p.step({});
Input drop;
drop.hard_drop = true;
CHECK(p.step(drop).locked);
int crystals = 0;
for (int y = 0; y < p.board().height(); ++y)
for (int x = 0; x < p.board().width(); ++x) crystals += p.board().at(x, y) == cell::kCrystal;
CHECK(crystals == 1);
}
void test_line_clear_fills_meter() {
PieceQueue q(always(PieceType::I, true), 1);
Timing t = fast_timing();
t.allow_hard_drop = true;
Player p(0, q, t);
const int bottom = p.board().height() - 1;
for (int x = 0; x < p.board().width(); ++x)
if (x < 3 || x > 6) p.board().set(x, bottom, cell::kBlock);
p.step({}); // spawn: I r0 lies in box row 2, cols 3..6
Input drop;
drop.hard_drop = true;
const FrameResult r = p.step(drop);
CHECK(r.locked);
CHECK(r.cleared.rows == 1);
CHECK(r.cleared.crystals == 1);
CHECK(p.meter() == 1);
}
void test_no_wall_kick() {
PieceQueue q(always(PieceType::I), 1);
Player p(0, q, fast_timing());
p.step({});
Input rot;
rot.rotate_cw = true;
p.step(rot); // vertical, occupies box col 2
CHECK(p.active()->rot == 1);
Input left;
left.left = true;
for (int i = 0; i < 40; ++i) {
p.step(left);
p.step({});
}
CHECK(p.active()->x == -2); // flush against the left wall
p.step(rot); // horizontal would poke out of the wall: rotation refused, no kick
CHECK(p.active()->rot == 1);
CHECK(p.active()->x == -2);
}
void test_top_out_after_lock_in_row_1() {
PieceQueue q(always(PieceType::O), 1);
Timing t = fast_timing();
t.allow_hard_drop = true;
Player p(0, q, t);
for (int y = 2; y < p.board().height(); ++y)
for (int x = 0; x < p.board().width(); ++x)
if (x != 0) p.board().set(x, y, cell::kBlock);
p.step({});
Input drop;
drop.hard_drop = true;
const FrameResult r = p.step(drop); // O rests in hidden rows 0-1 => row 1 occupied, no clear
CHECK(r.locked);
CHECK(r.topped_out);
CHECK(p.dead());
}
void test_rom_cycle_timing() {
// re/notes/timing.md: lock -> next piece falling is 56 frames without a clear (4 lock tail + 52 queue animation).
PieceQueue q(always(PieceType::O), 1);
Timing t;
t.allow_hard_drop = true;
Player p(0, q, t);
int f = 0;
while (!p.active() && f < 1000) p.step({}), ++f;
CHECK(f == t.queue_animation);
Input drop;
drop.hard_drop = true;
CHECK(p.step(drop).locked);
int gap = 0;
while (!p.active() && gap < 1000) p.step({}), ++gap;
CHECK(gap == 56);
}
void test_generators() {
PieceGenerator bag = make_generator(GeneratorKind::Bag7, 3, 0);
for (int run = 0; run < 20; ++run) {
int seen = 0;
for (int k = 0; k < kPieceTypeCount; ++k) seen |= 1 << static_cast<int>(bag().type);
CHECK(seen == (1 << kPieceTypeCount) - 1);
}
std::array<int, kPieceTypeCount> w{};
w[static_cast<std::size_t>(PieceType::I)] = 3;
w[static_cast<std::size_t>(PieceType::T)] = 1;
PieceGenerator weighted = make_generator(GeneratorKind::Weighted, 5, 0, w);
int is = 0;
for (int k = 0; k < 4000; ++k) {
const PieceType t = weighted().type;
CHECK(t == PieceType::I || t == PieceType::T);
is += t == PieceType::I;
}
CHECK(is > 2700 && is < 3300); // ~3/4
}
void test_clear_flash_then_collapse() {
// re/notes/timing.md: lock, 09 0E 0A, 14 flash frames, then the collapse; the next piece falls 21 + n + 52 later.
PieceQueue q(always(PieceType::I), 1);
Timing t;
t.allow_hard_drop = true;
Player p(0, q, t);
const int bottom = p.board().height() - 1;
for (int x = 0; x < p.board().width(); ++x)
if (x < 3 || x > 6) p.board().set(x, bottom, cell::kBlock);
while (!p.active()) p.step({});
Input drop;
drop.hard_drop = true;
CHECK(p.step(drop).cleared.rows == 1);
CHECK(p.clearing() && p.flash_rows().size() == 1);
CHECK(p.board().at(0, bottom) == cell::kBlock); // still visible while flashing
int frames = 0;
while (p.clearing()) p.step({}), ++frames;
CHECK(frames == 3 + 14);
CHECK(p.board().at(0, bottom) == cell::kEmpty);
int gap = frames;
while (!p.active()) p.step({}), ++gap;
CHECK(gap == 21 + 1 + 52);
}
} // namespace
int main() {
test_collision_walls_floor_hidden();
test_line_clear_shifts_rows_and_counts_crystals();
test_hidden_rows_never_clear();
test_original_generator_rules();
test_contested_vs_mirrored_queue();
test_piece_falls_and_locks_at_spawn_column();
test_crystal_cell_locks_as_crystal();
test_line_clear_fills_meter();
test_no_wall_kick();
test_top_out_after_lock_in_row_1();
test_rom_cycle_timing();
test_generators();
test_clear_flash_then_collapse();
run_match_tests();
run_modern_tests();
run_modern_cpu_tests();
if (g_failures == 0) std::printf("all core tests passed\n");
return g_failures;
}