1+ package skiplist
2+
3+ import (
4+ "testing"
5+
6+ "github.com/igevin/algokit/comparator"
7+ "github.com/stretchr/testify/assert"
8+ "github.com/stretchr/testify/require"
9+ )
10+
11+ func TestSkipMapList_Get (t * testing.T ) {
12+ testCases := []struct {
13+ name string
14+ sml * SkipMapList [string , int ]
15+ key string
16+ wantVal int
17+ wantOk bool
18+ }{
19+ {
20+ name : "empty list" ,
21+ sml : NewSkipMapList [string , int ](comparator .PrimeComparator [string ]),
22+ key : "a" ,
23+ wantOk : false ,
24+ },
25+ {
26+ name : "get existing key" ,
27+ sml : func () * SkipMapList [string , int ] {
28+ sml := NewSkipMapList [string , int ](comparator .PrimeComparator [string ])
29+ sml .Put ("apple" , 10 )
30+ sml .Put ("banana" , 20 )
31+ return sml
32+ }(),
33+ key : "apple" ,
34+ wantVal : 10 ,
35+ wantOk : true ,
36+ },
37+ {
38+ name : "get non-existing key" ,
39+ sml : func () * SkipMapList [string , int ] {
40+ sml := NewSkipMapList [string , int ](comparator .PrimeComparator [string ])
41+ sml .Put ("apple" , 10 )
42+ sml .Put ("banana" , 20 )
43+ return sml
44+ }(),
45+ key : "cherry" ,
46+ wantOk : false ,
47+ },
48+ }
49+
50+ for _ , tc := range testCases {
51+ t .Run (tc .name , func (t * testing.T ) {
52+ val , ok := tc .sml .Get (tc .key )
53+ assert .Equal (t , tc .wantOk , ok )
54+ if ok {
55+ assert .Equal (t , tc .wantVal , val )
56+ }
57+ })
58+ }
59+ }
60+
61+ func TestSkipMapList_Put (t * testing.T ) {
62+ testCases := []struct {
63+ name string
64+ sml * SkipMapList [string , int ]
65+ key string
66+ val int
67+ wantLen int
68+ }{
69+ {
70+ name : "put to empty" ,
71+ sml : NewSkipMapList [string , int ](comparator .PrimeComparator [string ]),
72+ key : "apple" ,
73+ val : 10 ,
74+ wantLen : 1 ,
75+ },
76+ {
77+ name : "put new key" ,
78+ sml : func () * SkipMapList [string , int ] {
79+ sml := NewSkipMapList [string , int ](comparator .PrimeComparator [string ])
80+ sml .Put ("apple" , 10 )
81+ return sml
82+ }(),
83+ key : "banana" ,
84+ val : 20 ,
85+ wantLen : 2 ,
86+ },
87+ {
88+ name : "update existing key" ,
89+ sml : func () * SkipMapList [string , int ] {
90+ sml := NewSkipMapList [string , int ](comparator .PrimeComparator [string ])
91+ sml .Put ("apple" , 10 )
92+ return sml
93+ }(),
94+ key : "apple" ,
95+ val : 15 ,
96+ wantLen : 1 ,
97+ },
98+ }
99+
100+ for _ , tc := range testCases {
101+ t .Run (tc .name , func (t * testing.T ) {
102+ tc .sml .Put (tc .key , tc .val )
103+ assert .Equal (t , tc .wantLen , tc .sml .Len ())
104+ val , ok := tc .sml .Get (tc .key )
105+ require .True (t , ok )
106+ assert .Equal (t , tc .val , val )
107+ })
108+ }
109+ }
110+
111+ func TestSkipMapList_Delete (t * testing.T ) {
112+ testCases := []struct {
113+ name string
114+ sml * SkipMapList [int , string ]
115+ key int
116+ wantLen int
117+ }{
118+ {
119+ name : "delete from empty" ,
120+ sml : NewSkipMapList [int , string ](comparator .PrimeComparator [int ]),
121+ key : 1 ,
122+ wantLen : 0 ,
123+ },
124+ {
125+ name : "delete existing" ,
126+ sml : func () * SkipMapList [int , string ] {
127+ sml := NewSkipMapList [int , string ](comparator .PrimeComparator [int ])
128+ sml .Put (1 , "one" )
129+ sml .Put (2 , "two" )
130+ return sml
131+ }(),
132+ key : 1 ,
133+ wantLen : 1 ,
134+ },
135+ {
136+ name : "delete non-existing" ,
137+ sml : func () * SkipMapList [int , string ] {
138+ sml := NewSkipMapList [int , string ](comparator .PrimeComparator [int ])
139+ sml .Put (1 , "one" )
140+ return sml
141+ }(),
142+ key : 2 ,
143+ wantLen : 1 ,
144+ },
145+ }
146+
147+ for _ , tc := range testCases {
148+ t .Run (tc .name , func (t * testing.T ) {
149+ tc .sml .Delete (tc .key )
150+ assert .Equal (t , tc .wantLen , tc .sml .Len ())
151+ _ , ok := tc .sml .Get (tc .key )
152+ assert .False (t , ok )
153+ })
154+ }
155+ }
156+
157+ func TestSkipMapList_Len (t * testing.T ) {
158+ sml := NewSkipMapList [string , bool ](comparator .PrimeComparator [string ])
159+ assert .Equal (t , 0 , sml .Len ())
160+
161+ sml .Put ("a" , true )
162+ assert .Equal (t , 1 , sml .Len ())
163+
164+ sml .Put ("b" , true )
165+ assert .Equal (t , 2 , sml .Len ())
166+
167+ sml .Delete ("a" )
168+ assert .Equal (t , 1 , sml .Len ())
169+
170+ sml .Delete ("b" )
171+ assert .Equal (t , 0 , sml .Len ())
172+ }
0 commit comments