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

seqid_test.gno

1.06 Kb · 68 lines
 1package seqid
 2
 3import (
 4	"fmt"
 5	"strings"
 6	"testing"
 7)
 8
 9func TestID(t *testing.T) {
10	var i ID
11
12	for j := 0; j < 100; j++ {
13		i.Next()
14	}
15	if i != 100 {
16		t.Fatalf("invalid: wanted %d got %d", 100, i)
17	}
18}
19
20func TestID_Overflow(t *testing.T) {
21	i := ID(maxID)
22
23	defer func() {
24		err := recover()
25		if !strings.Contains(fmt.Sprint(err), "next ID overflows") {
26			t.Errorf("did not overflow")
27		}
28	}()
29
30	i.Next()
31}
32
33func TestID_Binary(cur realm, t *testing.T) {
34	var i ID
35	prev := i.Binary()
36
37	for j := 0; j < 1000; j++ {
38		cur := i.Next().Binary()
39		if cur <= prev {
40			t.Fatalf("cur %x > prev %x", cur, prev)
41		}
42		prev = cur
43	}
44}
45
46func TestID_String(cur realm, t *testing.T) {
47	var i ID
48	prev := i.String()
49
50	for j := 0; j < 1000; j++ {
51		cur := i.Next().String()
52		if cur <= prev {
53			t.Fatalf("cur %s > prev %s", cur, prev)
54		}
55		prev = cur
56	}
57
58	// Test for when cford32 switches over to the long encoding.
59	i = 1<<34 - 512
60	for j := 0; j < 1024; j++ {
61		cur := i.Next().String()
62		// println(cur)
63		if cur <= prev {
64			t.Fatalf("cur %s > prev %s", cur, prev)
65		}
66		prev = cur
67	}
68}