Skip to content Search Apps Documentation Source Content 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.

eightball_test.gno

0.96 Kb · 54 lines
 1package eightball
 2
 3import (
 4	"testing"
 5
 6	"gno.land/p/nt/uassert/v0"
 7)
 8
 9func TestAnswerIndexInRange(t *testing.T) {
10	cases := []struct {
11		height int64
12		n      int
13	}{
14		{0, 0}, {1, 0}, {19, 0}, {20, 0}, {21, 5},
15		{999999, 3}, {-1, 0}, {-1000, 7},
16	}
17	for _, c := range cases {
18		idx := answerIndex(c.height, c.n)
19		uassert.True(t, idx >= 0 && idx < len(answers),
20			"index out of range for height="+itoa(c.height))
21	}
22}
23
24func TestAnswerIndexDeterministic(t *testing.T) {
25	uassert.Equal(t, answerIndex(42, 3), answerIndex(42, 3), "same inputs, same slot")
26}
27
28func TestRenderEmpty(t *testing.T) {
29	out := Render("")
30	uassert.NotEmpty(t, out, "render must not be empty")
31	uassert.True(t, len(answers) == 20, "classic 8-ball has 20 answers")
32}
33
34func itoa(n int64) string {
35	neg := n < 0
36	if neg {
37		n = -n
38	}
39	if n == 0 {
40		return "0"
41	}
42	var b [24]byte
43	i := len(b)
44	for n > 0 {
45		i--
46		b[i] = byte('0' + n%10)
47		n /= 10
48	}
49	if neg {
50		i--
51		b[i] = '-'
52	}
53	return string(b[i:])
54}