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.

zz_bounds_test.gno

1.25 Kb · 42 lines
 1package vault
 2
 3import (
 4	"strconv"
 5	"strings"
 6	"testing"
 7
 8	"gno.land/p/nt/avl/v0"
 9	"gno.land/p/nt/testutils/v0"
10	"gno.land/p/nt/uassert/v0"
11)
12
13// A vault holds a bounded number of entries of bounded size; overwriting an
14// existing key is always allowed.
15func TestVaultIsBounded(cur realm, t *testing.T) {
16	testing.SetRealm(testing.NewUserRealm(testutils.TestAddress("bounded")))
17	uassert.AbortsContains(t, cur, "too long", func() { Set(cross(cur), strings.Repeat("k", MaxKeyLen+1), "v") })
18	for i := 0; i < MaxEntries; i++ {
19		Set(cross(cur), "k"+strconv.Itoa(i), "v")
20	}
21	uassert.AbortsContains(t, cur, "at most", func() { Set(cross(cur), "one-more", "v") })
22	Set(cross(cur), "k0", "overwritten")
23}
24
25// The vault count is bounded globally, not just entries per vault.
26func TestVaultCountIsBounded(cur realm, t *testing.T) {
27	var added []string
28	for i := 0; vaults.Size() < MaxVaults; i++ {
29		k := testutils.TestAddress("v" + strconv.Itoa(i)).String()
30		if vaults.Get(k) == nil {
31			vaults.Set(k, &avl.Tree{})
32			added = append(added, k)
33		}
34	}
35	defer func() {
36		for _, k := range added {
37			vaults.Remove(k)
38		}
39	}()
40	testing.SetRealm(testing.NewUserRealm(testutils.TestAddress("latecomer")))
41	uassert.AbortsContains(t, cur, "vault is full", func() { Set(cross(cur), "k", "v") })
42}