[go: nahoru, domu]

Skip to content

Commit

Permalink
7bag: Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
ejona86 committed Nov 22, 2023
1 parent e40dad4 commit 85fd8d2
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
59 changes: 59 additions & 0 deletions 7bag-test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require("ypcall") -- must be first, as it changes globals
require("asm")
require("testing")

local labels = asm.loadlabels("build/7bag.lbl")

function test_fullbag ()
asm.waitbefore()
asm.waitbefore()
local spawnTable = {0x02,0x07,0x08,0x0A,0x0B,0x0E,0x12}
math.randomseed(1)
memory.writebyte(labels.spawnID, 0)
local results = {}
for i=1,14 do
asm.waitbefore()
memory.writebyte(labels.rng_seed, math.random(256))
asm.jsr(labels.pickRandomTetrimino_7bag)
local a = memory.getregister("a")
if results[a] == nil then
results[a] = 0
end
results[a] = results[a] + 1
end
for _, orientID in ipairs(spawnTable) do
local cnt = results[orientID]
assert(cnt == 2, "orientation: " .. orientID .. " count: " .. cnt)
end
end

function test_distribution ()
local spawnTable = {0x02,0x07,0x08,0x0A,0x0B,0x0E,0x12}
math.randomseed(1)
local results = {}
for i=1,100*7 do
asm.waitbefore()
asm.waitbefore()
memory.writebyte(labels.spawnID, 0)
memory.writebyte(labels.rng_seed, math.random(256))
asm.jsr(labels.pickRandomTetrimino_7bag)
local a = memory.getregister("a")
if results[a] == nil then
results[a] = 0
end
results[a] = results[a] + 1
asm.waitbefore()
emu.poweron()
end
local imbalanced = false
for _, orientID in ipairs(spawnTable) do
local cnt = results[orientID]
print("orientation: " .. orientID .. " count: " .. cnt)
if cnt < 80 or cnt > 120 then
imbalanced = true
end
end
assert(not imbalanced, "Imbalanced")
end

testing.run()
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,13 @@ build/hard-drop-taus-no-ghost.nes: build/tetris.nes

.PHONY: 7bag
all: 7bag
test: build/7bag-test.test
7bag: build/7bag.nes
build/7bag.o: build/tetris.inc
build/7bag.ips.cfg: build/7bag.o
build/7bag.ips: build/7bag.o build/ips.o
build/7bag.nes: build/tetris.nes
build/7bag-test.test: 7bag-test.lua build/7bag.nes

.PHONY: taus
all: taus
Expand Down

0 comments on commit 85fd8d2

Please sign in to comment.