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.

addrset_test.gno

3.29 Kb · 173 lines
  1package addrset
  2
  3import (
  4	"testing"
  5
  6	"gno.land/p/nt/uassert/v0"
  7)
  8
  9func TestSet(t *testing.T) {
 10	addr1 := address("addr1")
 11	addr2 := address("addr2")
 12	addr3 := address("addr3")
 13
 14	tests := []struct {
 15		name    string
 16		actions func(s *Set)
 17		size    int
 18		has     map[address]bool
 19		addrs   []address // for iteration checks
 20	}{
 21		{
 22			name:    "empty set",
 23			actions: func(s *Set) {},
 24			size:    0,
 25			has:     map[address]bool{addr1: false},
 26		},
 27		{
 28			name: "single address",
 29			actions: func(s *Set) {
 30				s.Add(addr1)
 31			},
 32			size: 1,
 33			has: map[address]bool{
 34				addr1: true,
 35				addr2: false,
 36			},
 37			addrs: []address{addr1},
 38		},
 39		{
 40			name: "multiple addresses",
 41			actions: func(s *Set) {
 42				s.Add(addr1)
 43				s.Add(addr2)
 44				s.Add(addr3)
 45			},
 46			size: 3,
 47			has: map[address]bool{
 48				addr1: true,
 49				addr2: true,
 50				addr3: true,
 51			},
 52			addrs: []address{addr1, addr2, addr3},
 53		},
 54		{
 55			name: "remove address",
 56			actions: func(s *Set) {
 57				s.Add(addr1)
 58				s.Add(addr2)
 59				s.Remove(addr1)
 60			},
 61			size: 1,
 62			has: map[address]bool{
 63				addr1: false,
 64				addr2: true,
 65			},
 66			addrs: []address{addr2},
 67		},
 68		{
 69			name: "duplicate adds",
 70			actions: func(s *Set) {
 71				uassert.True(t, s.Add(addr1))     // first add returns true
 72				uassert.False(t, s.Add(addr1))    // second add returns false
 73				uassert.True(t, s.Remove(addr1))  // remove existing returns true
 74				uassert.False(t, s.Remove(addr1)) // remove non-existing returns false
 75			},
 76			size: 0,
 77			has: map[address]bool{
 78				addr1: false,
 79			},
 80		},
 81	}
 82
 83	for _, tt := range tests {
 84		t.Run(tt.name, func(t *testing.T) {
 85			var set Set
 86
 87			// Execute test actions
 88			tt.actions(&set)
 89
 90			// Check size
 91			uassert.Equal(t, tt.size, set.Size())
 92
 93			// Check existence
 94			for addr, expected := range tt.has {
 95				uassert.Equal(t, expected, set.Has(addr))
 96			}
 97
 98			// Check iteration if addresses are specified
 99			if tt.addrs != nil {
100				collected := []address{}
101				set.IterateByOffset(0, 10, func(addr address) bool {
102					collected = append(collected, addr)
103					return false
104				})
105
106				// Check length
107				uassert.Equal(t, len(tt.addrs), len(collected))
108
109				// Check each address
110				for i, addr := range tt.addrs {
111					uassert.Equal(t, addr, collected[i])
112				}
113			}
114		})
115	}
116}
117
118func TestSetIterationLimits(t *testing.T) {
119	tests := []struct {
120		name     string
121		addrs    []address
122		offset   int
123		limit    int
124		expected int
125	}{
126		{
127			name:     "zero offset full list",
128			addrs:    []address{"a1", "a2", "a3"},
129			offset:   0,
130			limit:    10,
131			expected: 3,
132		},
133		{
134			name:     "offset with limit",
135			addrs:    []address{"a1", "a2", "a3", "a4"},
136			offset:   1,
137			limit:    2,
138			expected: 2,
139		},
140		{
141			name:     "offset beyond size",
142			addrs:    []address{"a1", "a2"},
143			offset:   3,
144			limit:    1,
145			expected: 0,
146		},
147	}
148
149	for _, tt := range tests {
150		t.Run(tt.name, func(t *testing.T) {
151			var set Set
152			for _, addr := range tt.addrs {
153				set.Add(addr)
154			}
155
156			// Test forward iteration
157			count := 0
158			set.IterateByOffset(tt.offset, tt.limit, func(addr address) bool {
159				count++
160				return false
161			})
162			uassert.Equal(t, tt.expected, count)
163
164			// Test reverse iteration
165			count = 0
166			set.ReverseIterateByOffset(tt.offset, tt.limit, func(addr address) bool {
167				count++
168				return false
169			})
170			uassert.Equal(t, tt.expected, count)
171		})
172	}
173}