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.

codec_test.gno

8.17 Kb · 277 lines
  1package arena
  2
  3import (
  4	"strconv"
  5	"strings"
  6	"testing"
  7
  8	"gno.land/p/nym-gfanton001/gnomepit/engine/v0"
  9	"gno.land/p/nt/uassert/v0"
 10	"gno.land/p/nt/urequire/v0"
 11)
 12
 13// panicsWith fails unless f panics with exactly want.
 14func panicsWith(t *testing.T, want string, f func()) {
 15	t.Helper()
 16	got := "no panic"
 17	func() {
 18		defer func() {
 19			if r := recover(); r != nil {
 20				got, _ = r.(string)
 21			}
 22		}()
 23		f()
 24	}()
 25	uassert.Equal(t, want, got)
 26}
 27
 28func TestBits(cur realm, t *testing.T) {
 29	uassert.Equal(t, uint64(0xB), bitsAt(0xAB, 0, 4))
 30	uassert.Equal(t, uint64(0xA), bitsAt(0xAB, 4, 4))
 31	uassert.Equal(t, uint64(0xA5), withBits(0xA0, 0, 4, 5))
 32	uassert.Equal(t, uint64(1)<<63, withBits(0, 63, 1, 1))
 33	uassert.Equal(t, uint64(0), withBits(^uint64(0), 0, 64, 0))
 34	panicsWith(t, "value does not fit", func() {
 35		withBits(0, 0, 4, 16)
 36	})
 37}
 38
 39func TestWorkedExamplePlans(cur realm, t *testing.T) {
 40	uassert.Equal(t, uint64(0x71A), withPlan(0, 1, engine.Plan{6, 7}))
 41	uassert.Equal(t, uint64(0x4107)<<20, withPlan(0, 2, engine.Plan{1, 1, 1}))
 42	uassert.Equal(t, uint64(0x103A), withPlan(0, 1, engine.Plan{14, 16}))
 43}
 44
 45func TestPlansRoundTrip(cur realm, t *testing.T) {
 46	rules := engine.ProofOfConcept()
 47	for _, clan := range rules.Clans {
 48		for _, p := range rules.LegalPlans(rules.Start(), engine.SideA, clan.Pool) {
 49			for round := 1; round <= 3; round++ {
 50				got := planAt(withPlan(0, round, p), round)
 51				urequire.True(t, samePlan(p, got), "plan lost in round "+string(rune('0'+round)))
 52			}
 53		}
 54	}
 55}
 56
 57func TestPlanSlotsDoNotOverlap(cur realm, t *testing.T) {
 58	var plans uint64
 59	for round := 1; round <= 3; round++ {
 60		plans = withPlan(plans, round, engine.Plan{63, 63, 63})
 61	}
 62	uassert.Equal(t, 3, plansMade(plans))
 63	plans = withPlan(plans, 2, engine.Plan{1})
 64	uassert.True(t, samePlan(engine.Plan{1}, planAt(plans, 2)))
 65	uassert.True(t, samePlan(engine.Plan{63, 63, 63}, planAt(plans, 1)))
 66	uassert.True(t, samePlan(engine.Plan{63, 63, 63}, planAt(plans, 3)))
 67
 68	plans = withPlan(0, 1, engine.Plan{63, 63, 63})
 69	uassert.Equal(t, 1, plansMade(plans))
 70	uassert.Equal(t, 0, plansMade(0))
 71	uassert.True(t, planAt(plans, 2) == nil)
 72}
 73
 74func TestPlanLimits(cur realm, t *testing.T) {
 75	for _, c := range []struct {
 76		round int
 77		plan  engine.Plan
 78	}{
 79		{0, engine.Plan{1}},
 80		{4, engine.Plan{1}},
 81		{1, nil},
 82		{1, engine.Plan{1, 1, 1, 1}},
 83		{1, engine.Plan{64}},
 84	} {
 85		panicsWith(t, "invalid plan", func() {
 86			withPlan(0, c.round, c.plan)
 87		})
 88	}
 89}
 90
 91func TestDeckOnPool(cur realm, t *testing.T) {
 92	rules := engine.ProofOfConcept()
 93	for _, clan := range rules.Clans {
 94		n := 0
 95		for i := 0; i < 64; i++ {
 96			if clan.Pool&(1<<uint(i)) != 0 {
 97				n++
 98			}
 99		}
100		uassert.True(t, n <= 16, clan.Name+" pool holds more than 16 cards")
101	}
102
103	for i, deck := range []string{deckA, deckB} {
104		pool := rules.Clans[i].Pool
105		bits := deckBits(rules, rules.Clans[i], deck)
106		uassert.Equal(t, bits, deckFromPool(pool, deckOnPool(pool, bits)))
107	}
108
109	panicsWith(t, "deck outside the pool", func() {
110		deckOnPool(rules.Clans[1].Pool, deckBits(rules, rules.Clans[0], deckA))
111	})
112	panicsWith(t, "pool holds more than 16 cards", func() {
113		deckOnPool(^uint64(0), 1)
114	})
115}
116
117func TestMatchRecordRoundTrip(cur realm, t *testing.T) {
118	rules := engine.ProofOfConcept()
119	deckBitsA := deckBits(rules, rules.Clans[0], deckA)
120	deckBitsB := deckBits(rules, rules.Clans[1], deckB)
121
122	box := make([]byte, 104)
123	for i := range box {
124		box[i] = byte(i * 3)
125	}
126
127	open := record{
128		phase:      phaseOpen,
129		clans:      [2]int{1, 0},
130		deadline:   1_790_000_000,
131		challenger: alice,
132		opponent:   bob,
133		deckSeal:   sealOf(saltA, deckA),
134		seal:       sealOf(salt1, "Chalk Dust,Double Tap"),
135		box:        "box1",
136	}
137	running := record{
138		phase:      phasePlay,
139		clans:      [2]int{1, 2},
140		deadline:   1_790_086_400,
141		decks:      [2]uint64{deckBitsA, deckBitsB},
142		plans:      [2]uint64{withPlan(0, 1, engine.Plan{6, 7}), withPlan(0, 1, engine.Plan{14, 16})},
143		hp:         [3][2]int{{10, 7}},
144		carried:    [2]engine.Vitals{{Vulnerable: 1}, {Strength: 2}},
145		challenger: alice,
146		acceptor:   bob,
147		seal:       sealOf(salt2, picks),
148		box:        string(box),
149	}
150	finished := record{
151		phase:      phaseEnded,
152		result:     "b",
153		reason:     "knockout",
154		clans:      [2]int{2, 1},
155		deadline:   1_790_172_800,
156		decks:      [2]uint64{deckBitsB, deckBitsA},
157		plans:      [2]uint64{withPlan(withPlan(0, 1, engine.Plan{1}), 2, engine.Plan{0, 0, 0}), withPlan(0, 3, engine.Plan{63, 63, 63})},
158		hp:         [3][2]int{{10, 7}, {4, 0}, {-3, 0}},
159		carried:    [2]engine.Vitals{{Vulnerable: 3, Strength: 8}, {Vulnerable: 3, Strength: 8}},
160		challenger: alice,
161		acceptor:   bob,
162	}
163
164	for name, want := range map[string]record{"open": open, "running": running, "finished": finished} {
165		b := encodeMatch(rules, want)
166		got := decodeMatch(rules, b)
167		uassert.True(t, want == got, name+" record changed in a round trip")
168	}
169	uassert.Equal(t, 115, len(encodeMatch(rules, finished)))
170}
171
172func TestMatchRecordLimits(cur realm, t *testing.T) {
173	rules := engine.ProofOfConcept()
174	base := record{phase: phaseEnded, result: "a", reason: "hp", clans: [2]int{1, 2}, challenger: alice, acceptor: bob}
175
176	hp := base
177	hp.hp[0][0] = 128
178	panicsWith(t, "hp does not fit a byte", func() { encodeMatch(rules, hp) })
179
180	vuln := base
181	vuln.carried[0].Vulnerable = 16
182	panicsWith(t, "carried state does not fit", func() { encodeMatch(rules, vuln) })
183
184	phase := base
185	phase.phase = "lost"
186	panicsWith(t, "unknown phase", func() { encodeMatch(rules, phase) })
187}
188
189func TestMatchRecordBox(cur realm, t *testing.T) {
190	rules := engine.ProofOfConcept()
191	base := record{
192		phase: phasePlay, clans: [2]int{1, 2}, challenger: alice, acceptor: bob,
193		seal: sealOf(salt2, picks),
194	}
195
196	for _, n := range []int{1, 64, maxBox} {
197		m := base
198		m.box = strings.Repeat("\x00\xff\x07", n)[:n]
199		b := encodeMatch(rules, m)
200		uassert.Equal(t, 115+32+2+n, len(b))
201		uassert.True(t, decodeMatch(rules, b) == m, "box of "+strconv.Itoa(n)+" bytes changed")
202	}
203
204	over := base
205	over.box = strings.Repeat("x", maxBox+1)
206	panicsWith(t, "box too long", func() { encodeMatch(rules, over) })
207}
208
209func TestChunkSlots(cur realm, t *testing.T) {
210	var chunk string
211	chunk = withChunkRecord(chunk, 0, "zero")
212	chunk = withChunkRecord(chunk, 5, "five")
213	chunk = withChunkRecord(chunk, 63, "sixty-three")
214	uassert.Equal(t, "zero", chunkRecord(chunk, 0))
215	uassert.Equal(t, "five", chunkRecord(chunk, 5))
216	uassert.Equal(t, "sixty-three", chunkRecord(chunk, 63))
217	uassert.Equal(t, "", chunkRecord(chunk, 3))
218	uassert.Equal(t, "", chunkRecord(chunk, 64))
219
220	chunk = withChunkRecord(chunk, 5, "")
221	uassert.Equal(t, "", chunkRecord(chunk, 5))
222	uassert.Equal(t, "sixty-three", chunkRecord(chunk, 63))
223
224	chunk = withChunkRecord(chunk, 0, "longer than before")
225	uassert.Equal(t, "longer than before", chunkRecord(chunk, 0))
226	uassert.Equal(t, "sixty-three", chunkRecord(chunk, 63))
227
228	rec := "abc"
229	uassert.Equal(t, 2+2+2+len(rec), len(withChunkRecord("", 2, rec)))
230	uassert.Equal(t, "", chunkRecord("", 0))
231}
232
233func TestPlayerRecordRoundTrip(cur realm, t *testing.T) {
234	locks := []sessionLock{
235		{session: bob, id: 7, expires: 1_790_000_000},
236		{session: carol, id: 9, expires: 1_790_086_400},
237	}
238	for _, p := range []playerRecord{
239		{},
240		{ids: []int{1, 70000, 3}},
241		{locks: locks},
242		{ids: []int{4, 5, 6}, locks: locks},
243	} {
244		s := encodePlayer(p)
245		got := decodePlayer(s)
246		urequire.Equal(t, len(p.ids), len(got.ids))
247		urequire.Equal(t, len(p.locks), len(got.locks))
248		for i := range p.ids {
249			uassert.Equal(t, p.ids[i], got.ids[i])
250		}
251		for i := range p.locks {
252			uassert.True(t, p.locks[i] == got.locks[i])
253		}
254		uassert.Equal(t, 2+4*len(p.ids)+48*len(p.locks), len(s))
255	}
256	got := decodePlayer("")
257	uassert.Equal(t, 0, len(got.ids)+len(got.locks))
258}
259
260func TestOpenListRoundTrip(cur realm, t *testing.T) {
261	entries := []openEntry{{1, 1_790_000_000}, {2, 1_790_000_100}, {300000, 1_790_000_200}}
262	s := encodeOpen(entries)
263	uassert.Equal(t, 8*len(entries), len(s))
264	got := openEntries(s)
265	urequire.Equal(t, len(entries), len(got))
266	for i := range entries {
267		uassert.True(t, entries[i] == got[i])
268	}
269	uassert.Equal(t, 0, len(openEntries("")))
270	panicsWith(t, "malformed open list", func() {
271		openEntries("short")
272	})
273}
274
275func TestMalformedPlayer(cur realm, t *testing.T) {
276	panicsWith(t, "malformed player", func() { decodePlayer("\x00\x00" + strings.Repeat("\x00", 10)) })
277}