Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

brawl_test.gno

2.29 Kb · 104 lines
  1package brawl
  2
  3import "testing"
  4
  5// noisy makes a full match of inputs: hold a direction for a few ticks,
  6// press a button now and then.
  7func noisy(seed uint32) string {
  8	b := make([]byte, Ticks)
  9	s := seed
 10	dir, hold := 0, 0
 11	for t := 0; t < Ticks; t++ {
 12		s = s*1103515245 + 12345
 13		r := int(s >> 8)
 14		if hold == 0 {
 15			dir = r % 17
 16			hold = 3 + r%8
 17		}
 18		hold--
 19		act := None
 20		if r%7 == 0 {
 21			act = 1 + (r>>4)%4
 22		}
 23		b[t] = Encode(dir, act)
 24	}
 25	return string(b)
 26}
 27
 28func TestInputsOnly(t *testing.T) {
 29	a, b := noisy(1), noisy(2)
 30	if len(a) != Ticks || len(b) != Ticks {
 31		t.Fatal("length")
 32	}
 33}
 34
 35func TestFullMatch(t *testing.T) {
 36	a, b := noisy(1), noisy(2)
 37	ga, gb := Play(42, Hunter, Brawler, a, b)
 38	t.Logf("gold %d - %d", ga, gb)
 39	if ga+gb == 0 {
 40		t.Fatal("nobody scored")
 41	}
 42}
 43
 44func TestDeterministic(t *testing.T) {
 45	a, b := noisy(5), noisy(6)
 46	x1, y1 := Play(7, Coward, Reader, a, b)
 47	x2, y2 := Play(7, Coward, Reader, a, b)
 48	if x1 != x2 || y1 != y2 {
 49		t.Fatal("replay differs")
 50	}
 51}
 52
 53func TestWallsHold(t *testing.T) {
 54	for seed := uint32(0); seed < 6; seed++ {
 55		g := New(seed, Coward, Brawler)
 56		a, b := noisy(seed+10), noisy(seed+20)
 57		for i := 0; i < Ticks; i++ {
 58			m0, a0 := in(a, i)
 59			m1, a1 := in(b, i)
 60			g.Step(m0, a0, m1, a1)
 61			for s := 0; s < 2; s++ {
 62				f := g.F[s]
 63				cx, cy := f.X/U, f.Y/U
 64				if g.floor(cx, cy) == Wall {
 65					lx, ly := f.X-cx*U, f.Y-cy*U
 66					if lx > 100 && lx < 900 && ly > 100 && ly < 900 {
 67						t.Fatalf("seed %d tick %d: fighter %d inside a wall", seed, i, s)
 68					}
 69				}
 70			}
 71		}
 72	}
 73}
 74
 75func TestLayouts(t *testing.T) {
 76	for arena := uint32(0); arena < 3; arena++ {
 77		g := New(arena, Hunter, Hunter)
 78		n := 0
 79		for c := 0; c < Cells; c++ {
 80			k := g.Floor[c]
 81			if k == Empty {
 82				continue
 83			}
 84			n++
 85			if g.Floor[Cells-1-c] != k {
 86				t.Fatalf("%s: square %d not mirrored", ArenaName(arena), c)
 87			}
 88			x, y := c%Size, c/Size
 89			if (x-1)*(x-1)+(y-1)*(y-1) <= 10 || (x-13)*(x-13)+(y-13)*(y-13) <= 10 {
 90				t.Fatalf("%s: square %d,%d too close to a start", ArenaName(arena), x, y)
 91			}
 92			if abs(x-Mid) <= 1 && abs(y-Mid) <= 1 {
 93				t.Fatalf("%s: square %d,%d in the middle", ArenaName(arena), x, y)
 94			}
 95		}
 96		want := 0
 97		for _, l := range layouts[arena] {
 98			want += len(l)
 99		}
100		if n != want {
101			t.Fatalf("%s: %d squares, want %d (overlap?)", ArenaName(arena), n, want)
102		}
103	}
104}