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.

calc_test.gno

1.82 Kb · 86 lines
 1package calc
 2
 3import (
 4	"testing"
 5
 6	"gno.land/p/nt/testutils/v0"
 7	"gno.land/p/nt/uassert/v0"
 8)
 9
10func TestPushAndAdd(cur realm, t *testing.T) {
11	testing.SetRealm(testing.NewUserRealm(testutils.TestAddress("alice")))
12
13	Clear(cross(cur))
14	Push(cross(cur), "5")
15	Push(cross(cur), "3")
16	Push(cross(cur), "+")
17
18	uassert.Equal(t, 1, Depth())
19	top, ok := Top()
20	uassert.True(t, ok)
21	uassert.Equal(t, 8, top)
22}
23
24func TestChainedOps(cur realm, t *testing.T) {
25	testing.SetRealm(testing.NewUserRealm(testutils.TestAddress("bob")))
26
27	Clear(cross(cur))
28	// (10 - 4) * 2 = 12
29	Push(cross(cur), "10")
30	Push(cross(cur), "4")
31	Push(cross(cur), "-")
32	Push(cross(cur), "2")
33	Push(cross(cur), "*")
34
35	top, ok := Top()
36	uassert.True(t, ok)
37	uassert.Equal(t, 12, top)
38	uassert.Equal(t, 1, Depth())
39}
40
41func TestDivision(cur realm, t *testing.T) {
42	testing.SetRealm(testing.NewUserRealm(testutils.TestAddress("carol")))
43
44	Clear(cross(cur))
45	Push(cross(cur), "20")
46	Push(cross(cur), "6")
47	Push(cross(cur), "/")
48
49	top, _ := Top()
50	uassert.Equal(t, 3, top) // integer division
51}
52
53func TestDivideByZeroAborts(cur realm, t *testing.T) {
54	testing.SetRealm(testing.NewUserRealm(testutils.TestAddress("dave")))
55
56	Clear(cross(cur))
57	Push(cross(cur), "1")
58	Push(cross(cur), "0")
59
60	uassert.AbortsWithMessage(t, cur, ErrDivByZero.Error(), func() {
61		Push(cross(cur), "/")
62	})
63}
64
65func TestUnderflowAborts(cur realm, t *testing.T) {
66	testing.SetRealm(testing.NewUserRealm(testutils.TestAddress("erin")))
67
68	Clear(cross(cur))
69	Push(cross(cur), "42")
70
71	uassert.AbortsWithMessage(t, cur, ErrUnderflow.Error(), func() {
72		Push(cross(cur), "+")
73	})
74}
75
76func TestClearEmpties(cur realm, t *testing.T) {
77	testing.SetRealm(testing.NewUserRealm(testutils.TestAddress("frank")))
78
79	Push(cross(cur), "7")
80	Push(cross(cur), "9")
81	Clear(cross(cur))
82
83	uassert.Equal(t, 0, Depth())
84	_, ok := Top()
85	uassert.False(t, ok)
86}