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.

stack_test.gno

2.17 Kb · 78 lines
 1package stack
 2
 3import (
 4	"strings"
 5	"testing"
 6
 7	"gno.land/p/nt/testutils/v0"
 8	"gno.land/p/nt/uassert/v0"
 9)
10
11func resetState() {
12	stackData = nil
13	queueData = nil
14}
15
16// TestStackLIFO checks that the stack pops in reverse order (last in, first out).
17func TestStackLIFO(cur realm, t *testing.T) {
18	resetState()
19	addr := testutils.TestAddress("alice")
20	testing.SetRealm(testing.NewUserRealm(addr))
21
22	Push(cross(cur), 1)
23	Push(cross(cur), 2)
24	Push(cross(cur), 3)
25	uassert.Equal(t, 3, StackSize())
26
27	uassert.Equal(t, 3, Pop(cross(cur)))
28	uassert.Equal(t, 2, Pop(cross(cur)))
29	uassert.Equal(t, 1, Pop(cross(cur)))
30	uassert.Equal(t, 0, StackSize())
31}
32
33// TestQueueFIFO checks that the queue dequeues in insertion order (first in, first out).
34func TestQueueFIFO(cur realm, t *testing.T) {
35	resetState()
36	addr := testutils.TestAddress("bob")
37	testing.SetRealm(testing.NewUserRealm(addr))
38
39	Enqueue(cross(cur), 1)
40	Enqueue(cross(cur), 2)
41	Enqueue(cross(cur), 3)
42	uassert.Equal(t, 3, QueueSize())
43
44	uassert.Equal(t, 1, Dequeue(cross(cur)))
45	uassert.Equal(t, 2, Dequeue(cross(cur)))
46	uassert.Equal(t, 3, Dequeue(cross(cur)))
47	uassert.Equal(t, 0, QueueSize())
48}
49
50// TestEmptyPanics checks that popping/dequeuing empty structures aborts.
51func TestEmptyPanics(cur realm, t *testing.T) {
52	resetState()
53	addr := testutils.TestAddress("carol")
54	testing.SetRealm(testing.NewUserRealm(addr))
55
56	uassert.AbortsWithMessage(t, cur, "stack is empty: nothing to pop", func() {
57		Pop(cross(cur))
58	})
59	uassert.AbortsWithMessage(t, cur, "queue is empty: nothing to dequeue", func() {
60		Dequeue(cross(cur))
61	})
62}
63
64// TestRender checks that Render reports sizes, labels, and the LIFO/FIFO explanation.
65func TestRender(cur realm, t *testing.T) {
66	resetState()
67	addr := testutils.TestAddress("dave")
68	testing.SetRealm(testing.NewUserRealm(addr))
69
70	Push(cross(cur), 7)
71	Enqueue(cross(cur), 9)
72
73	out := Render("")
74	uassert.True(t, strings.Contains(out, "Stack (LIFO)"), "should label the stack")
75	uassert.True(t, strings.Contains(out, "Queue (FIFO)"), "should label the queue")
76	uassert.True(t, strings.Contains(out, "Top: `7`"), "should mark the top of the stack")
77	uassert.True(t, strings.Contains(out, "Front: `9`"), "should mark the front of the queue")
78}