@@ -82,6 +82,72 @@ func testAdd(t *Pot, pof Pof, j int, values ...string) (_ *Pot, n int, f bool) {
8282 return t , n , f
8383}
8484
85+ // removing non-existing element from pot
86+ func TestPotRemoveNonExisting (t * testing.T ) {
87+ pof := DefaultPof (8 )
88+ n := NewPot (newTestAddr ("00111100" , 0 ), 0 )
89+ n , _ , _ = Remove (n , newTestAddr ("00000101" , 0 ), pof )
90+ exp := "00111100"
91+ got := Label (n .Pin ())
92+ if got [:8 ] != exp {
93+ t .Fatalf ("incorrect pinned value. Expected %v, got %v" , exp , got [:8 ])
94+ }
95+ }
96+
97+ // this test creates hierarchical pot tree, and therefore any child node will have
98+ // child_po = parent_po + 1.
99+ // then removes a node from the middle of the tree.
100+ func TestPotRemoveSameBin (t * testing.T ) {
101+ pof := DefaultPof (8 )
102+ n := NewPot (newTestAddr ("11111111" , 0 ), 0 )
103+ n , _ , _ = testAdd (n , pof , 1 , "00000000" , "01000000" , "01100000" , "01110000" , "01111000" )
104+ n , _ , _ = Remove (n , newTestAddr ("01110000" , 0 ), pof )
105+ inds , po := indexes (n )
106+ goti := n .Size ()
107+ expi := 5
108+ if goti != expi {
109+ t .Fatalf ("incorrect number of elements in Pot. Expected %v, got %v" , expi , goti )
110+ }
111+ inds , po = indexes (n )
112+ got := fmt .Sprintf ("%v" , inds )
113+ exp := "[5 3 2 1 0]"
114+ if got != exp {
115+ t .Fatalf ("incorrect indexes in iteration over Pot. Expected %v, got %v" , exp , got )
116+ }
117+ got = fmt .Sprintf ("%v" , po )
118+ exp = "[3 2 1 0 0]"
119+ if got != exp {
120+ t .Fatalf ("incorrect po-s in iteration over Pot. Expected %v, got %v" , exp , got )
121+ }
122+ }
123+
124+ // this test creates a flat pot tree (all the elements are leafs of one root),
125+ // and therefore they all have the same po.
126+ // then removes an arbitrary element from the pot.
127+ func TestPotRemoveDifferentBins (t * testing.T ) {
128+ pof := DefaultPof (8 )
129+ n := NewPot (newTestAddr ("11111111" , 0 ), 0 )
130+ n , _ , _ = testAdd (n , pof , 1 , "00000000" , "10000000" , "11000000" , "11100000" , "11110000" )
131+ n , _ , _ = Remove (n , newTestAddr ("11100000" , 0 ), pof )
132+ inds , po := indexes (n )
133+ goti := n .Size ()
134+ expi := 5
135+ if goti != expi {
136+ t .Fatalf ("incorrect number of elements in Pot. Expected %v, got %v" , expi , goti )
137+ }
138+ inds , po = indexes (n )
139+ got := fmt .Sprintf ("%v" , inds )
140+ exp := "[1 2 3 5 0]"
141+ if got != exp {
142+ t .Fatalf ("incorrect indexes in iteration over Pot. Expected %v, got %v" , exp , got )
143+ }
144+ got = fmt .Sprintf ("%v" , po )
145+ exp = "[0 1 2 4 0]"
146+ if got != exp {
147+ t .Fatalf ("incorrect po-s in iteration over Pot. Expected %v, got %v" , exp , got )
148+ }
149+ }
150+
85151func TestPotAdd (t * testing.T ) {
86152 pof := DefaultPof (8 )
87153 n := NewPot (newTestAddr ("00111100" , 0 ), 0 )
@@ -135,25 +201,29 @@ func TestPotRemove(t *testing.T) {
135201 t .Fatalf ("incorrect number of elements in Pot. Expected %v, got %v" , expi , goti )
136202 }
137203 inds , po := indexes (n )
204+ got = fmt .Sprintf ("%v" , po )
205+ exp = "[1 3 0]"
206+ if got != exp {
207+ t .Fatalf ("incorrect po-s in iteration over Pot. Expected %v, got %v" , exp , got )
208+ }
138209 got = fmt .Sprintf ("%v" , inds )
139- exp = "[2 4 0 ]"
210+ exp = "[2 4 1 ]"
140211 if got != exp {
141212 t .Fatalf ("incorrect indexes in iteration over Pot. Expected %v, got %v" , exp , got )
142213 }
143- got = fmt .Sprintf ("%v" , po )
144- exp = "[1 3 0]"
214+ n , _ , _ = Remove (n , newTestAddr ("00111100" , 0 ), pof ) // remove again same element
215+ inds , _ = indexes (n )
216+ got = fmt .Sprintf ("%v" , inds )
145217 if got != exp {
146- t .Fatalf ("incorrect po-s in iteration over Pot. Expected %v, got %v" , exp , got )
218+ t .Fatalf ("incorrect indexes in iteration over Pot. Expected %v, got %v" , exp , got )
147219 }
148- // remove again
149- n , _ , _ = Remove (n , newTestAddr ("00111100" , 0 ), pof )
220+ n , _ , _ = Remove (n , newTestAddr ("00000000" , 0 ), pof ) // remove the first element
150221 inds , _ = indexes (n )
151222 got = fmt .Sprintf ("%v" , inds )
152223 exp = "[2 4]"
153224 if got != exp {
154225 t .Fatalf ("incorrect indexes in iteration over Pot. Expected %v, got %v" , exp , got )
155226 }
156-
157227}
158228
159229func TestPotSwap (t * testing.T ) {
0 commit comments