| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package arraylist |
| 6 |
|
| 7 |
import ( |
| 8 |
"cmp" |
| 9 |
"encoding/json" |
| 10 |
"slices" |
| 11 |
"strings" |
| 12 |
"testing" |
| 13 |
) |
| 14 |
|
| 15 |
func TestListNew(t *testing.T) { |
| 16 |
list1 := New[int]() |
| 17 |
|
| 18 |
if actualValue := list1.Empty(); actualValue != true { |
| 19 |
t.Errorf("Got %v expected %v", actualValue, true) |
| 20 |
} |
| 21 |
|
| 22 |
list2 := New[int](1, 2) |
| 23 |
|
| 24 |
if actualValue := list2.Size(); actualValue != 2 { |
| 25 |
t.Errorf("Got %v expected %v", actualValue, 2) |
| 26 |
} |
| 27 |
|
| 28 |
if actualValue, ok := list2.Get(0); actualValue != 1 || !ok { |
| 29 |
t.Errorf("Got %v expected %v", actualValue, 1) |
| 30 |
} |
| 31 |
|
| 32 |
if actualValue, ok := list2.Get(1); actualValue != 2 || !ok { |
| 33 |
t.Errorf("Got %v expected %v", actualValue, 2) |
| 34 |
} |
| 35 |
|
| 36 |
if actualValue, ok := list2.Get(2); actualValue != 0 || ok { |
| 37 |
t.Errorf("Got %v expected %v", actualValue, 0) |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
func TestListAdd(t *testing.T) { |
| 42 |
list := New[string]() |
| 43 |
list.Add("a") |
| 44 |
list.Add("b", "c") |
| 45 |
if actualValue := list.Empty(); actualValue != false { |
| 46 |
t.Errorf("Got %v expected %v", actualValue, false) |
| 47 |
} |
| 48 |
if actualValue := list.Size(); actualValue != 3 { |
| 49 |
t.Errorf("Got %v expected %v", actualValue, 3) |
| 50 |
} |
| 51 |
if actualValue, ok := list.Get(2); actualValue != "c" || !ok { |
| 52 |
t.Errorf("Got %v expected %v", actualValue, "c") |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
func TestListIndexOf(t *testing.T) { |
| 57 |
list := New[string]() |
| 58 |
|
| 59 |
expectedIndex := -1 |
| 60 |
if index := list.IndexOf("a"); index != expectedIndex { |
| 61 |
t.Errorf("Got %v expected %v", index, expectedIndex) |
| 62 |
} |
| 63 |
|
| 64 |
list.Add("a") |
| 65 |
list.Add("b", "c") |
| 66 |
|
| 67 |
expectedIndex = 0 |
| 68 |
if index := list.IndexOf("a"); index != expectedIndex { |
| 69 |
t.Errorf("Got %v expected %v", index, expectedIndex) |
| 70 |
} |
| 71 |
|
| 72 |
expectedIndex = 1 |
| 73 |
if index := list.IndexOf("b"); index != expectedIndex { |
| 74 |
t.Errorf("Got %v expected %v", index, expectedIndex) |
| 75 |
} |
| 76 |
|
| 77 |
expectedIndex = 2 |
| 78 |
if index := list.IndexOf("c"); index != expectedIndex { |
| 79 |
t.Errorf("Got %v expected %v", index, expectedIndex) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
func TestListRemove(t *testing.T) { |
| 84 |
list := New[string]() |
| 85 |
list.Add("a") |
| 86 |
list.Add("b", "c") |
| 87 |
list.Remove(2) |
| 88 |
if actualValue, ok := list.Get(2); actualValue != "" || ok { |
| 89 |
t.Errorf("Got %v expected %v", actualValue, "") |
| 90 |
} |
| 91 |
list.Remove(1) |
| 92 |
list.Remove(0) |
| 93 |
list.Remove(0) |
| 94 |
if actualValue := list.Empty(); actualValue != true { |
| 95 |
t.Errorf("Got %v expected %v", actualValue, true) |
| 96 |
} |
| 97 |
if actualValue := list.Size(); actualValue != 0 { |
| 98 |
t.Errorf("Got %v expected %v", actualValue, 0) |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
func TestListGet(t *testing.T) { |
| 103 |
list := New[string]() |
| 104 |
list.Add("a") |
| 105 |
list.Add("b", "c") |
| 106 |
if actualValue, ok := list.Get(0); actualValue != "a" || !ok { |
| 107 |
t.Errorf("Got %v expected %v", actualValue, "a") |
| 108 |
} |
| 109 |
if actualValue, ok := list.Get(1); actualValue != "b" || !ok { |
| 110 |
t.Errorf("Got %v expected %v", actualValue, "b") |
| 111 |
} |
| 112 |
if actualValue, ok := list.Get(2); actualValue != "c" || !ok { |
| 113 |
t.Errorf("Got %v expected %v", actualValue, "c") |
| 114 |
} |
| 115 |
if actualValue, ok := list.Get(3); actualValue != "" || ok { |
| 116 |
t.Errorf("Got %v expected %v", actualValue, "") |
| 117 |
} |
| 118 |
list.Remove(0) |
| 119 |
if actualValue, ok := list.Get(0); actualValue != "b" || !ok { |
| 120 |
t.Errorf("Got %v expected %v", actualValue, "b") |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
func TestListSwap(t *testing.T) { |
| 125 |
list := New[string]() |
| 126 |
list.Add("a") |
| 127 |
list.Add("b", "c") |
| 128 |
list.Swap(0, 1) |
| 129 |
if actualValue, ok := list.Get(0); actualValue != "b" || !ok { |
| 130 |
t.Errorf("Got %v expected %v", actualValue, "b") |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
func TestListSort(t *testing.T) { |
| 135 |
list := New[string]() |
| 136 |
list.Sort(cmp.Compare[string]) |
| 137 |
list.Add("e", "f", "g", "a", "b", "c", "d") |
| 138 |
list.Sort(cmp.Compare[string]) |
| 139 |
for i := 1; i < list.Size(); i++ { |
| 140 |
a, _ := list.Get(i - 1) |
| 141 |
b, _ := list.Get(i) |
| 142 |
if a > b { |
| 143 |
t.Errorf("Not sorted! %s > %s", a, b) |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
func TestListClear(t *testing.T) { |
| 149 |
list := New[string]() |
| 150 |
list.Add("e", "f", "g", "a", "b", "c", "d") |
| 151 |
list.Clear() |
| 152 |
if actualValue := list.Empty(); actualValue != true { |
| 153 |
t.Errorf("Got %v expected %v", actualValue, true) |
| 154 |
} |
| 155 |
if actualValue := list.Size(); actualValue != 0 { |
| 156 |
t.Errorf("Got %v expected %v", actualValue, 0) |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
func TestListContains(t *testing.T) { |
| 161 |
list := New[string]() |
| 162 |
list.Add("a") |
| 163 |
list.Add("b", "c") |
| 164 |
if actualValue := list.Contains("a"); actualValue != true { |
| 165 |
t.Errorf("Got %v expected %v", actualValue, true) |
| 166 |
} |
| 167 |
if actualValue := list.Contains(""); actualValue != false { |
| 168 |
t.Errorf("Got %v expected %v", actualValue, false) |
| 169 |
} |
| 170 |
if actualValue := list.Contains("a", "b", "c"); actualValue != true { |
| 171 |
t.Errorf("Got %v expected %v", actualValue, true) |
| 172 |
} |
| 173 |
if actualValue := list.Contains("a", "b", "c", "d"); actualValue != false { |
| 174 |
t.Errorf("Got %v expected %v", actualValue, false) |
| 175 |
} |
| 176 |
list.Clear() |
| 177 |
if actualValue := list.Contains("a"); actualValue != false { |
| 178 |
t.Errorf("Got %v expected %v", actualValue, false) |
| 179 |
} |
| 180 |
if actualValue := list.Contains("a", "b", "c"); actualValue != false { |
| 181 |
t.Errorf("Got %v expected %v", actualValue, false) |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
func TestListValues(t *testing.T) { |
| 186 |
list := New[string]() |
| 187 |
list.Add("a") |
| 188 |
list.Add("b", "c") |
| 189 |
if actualValue, expectedValue := list.Values(), []string{"a", "b", "c"}; !slices.Equal(actualValue, expectedValue) { |
| 190 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
func TestListInsert(t *testing.T) { |
| 195 |
list := New[string]() |
| 196 |
list.Insert(0, "b", "c") |
| 197 |
list.Insert(0, "a") |
| 198 |
list.Insert(10, "x") |
| 199 |
if actualValue := list.Size(); actualValue != 3 { |
| 200 |
t.Errorf("Got %v expected %v", actualValue, 3) |
| 201 |
} |
| 202 |
list.Insert(3, "d") |
| 203 |
if actualValue := list.Size(); actualValue != 4 { |
| 204 |
t.Errorf("Got %v expected %v", actualValue, 4) |
| 205 |
} |
| 206 |
if actualValue, expectedValue := strings.Join(list.Values(), ""), "abcd"; actualValue != expectedValue { |
| 207 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
func TestListSet(t *testing.T) { |
| 212 |
list := New[string]() |
| 213 |
list.Set(0, "a") |
| 214 |
list.Set(1, "b") |
| 215 |
if actualValue := list.Size(); actualValue != 2 { |
| 216 |
t.Errorf("Got %v expected %v", actualValue, 2) |
| 217 |
} |
| 218 |
list.Set(2, "c") |
| 219 |
if actualValue := list.Size(); actualValue != 3 { |
| 220 |
t.Errorf("Got %v expected %v", actualValue, 3) |
| 221 |
} |
| 222 |
list.Set(4, "d") |
| 223 |
list.Set(1, "bb") |
| 224 |
if actualValue := list.Size(); actualValue != 3 { |
| 225 |
t.Errorf("Got %v expected %v", actualValue, 3) |
| 226 |
} |
| 227 |
if actualValue, expectedValue := list.Values(), []string{"a", "bb", "c"}; !slices.Equal(actualValue, expectedValue) { |
| 228 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
func TestListEach(t *testing.T) { |
| 233 |
list := New[string]() |
| 234 |
list.Add("a", "b", "c") |
| 235 |
list.Each(func(index int, value string) { |
| 236 |
switch index { |
| 237 |
case 0: |
| 238 |
if actualValue, expectedValue := value, "a"; actualValue != expectedValue { |
| 239 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 240 |
} |
| 241 |
case 1: |
| 242 |
if actualValue, expectedValue := value, "b"; actualValue != expectedValue { |
| 243 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 244 |
} |
| 245 |
case 2: |
| 246 |
if actualValue, expectedValue := value, "c"; actualValue != expectedValue { |
| 247 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 248 |
} |
| 249 |
default: |
| 250 |
t.Errorf("Too many") |
| 251 |
} |
| 252 |
}) |
| 253 |
} |
| 254 |
|
| 255 |
func TestListMap(t *testing.T) { |
| 256 |
list := New[string]() |
| 257 |
list.Add("a", "b", "c") |
| 258 |
mappedList := list.Map(func(index int, value string) string { |
| 259 |
return "mapped: " + value |
| 260 |
}) |
| 261 |
if actualValue, _ := mappedList.Get(0); actualValue != "mapped: a" { |
| 262 |
t.Errorf("Got %v expected %v", actualValue, "mapped: a") |
| 263 |
} |
| 264 |
if actualValue, _ := mappedList.Get(1); actualValue != "mapped: b" { |
| 265 |
t.Errorf("Got %v expected %v", actualValue, "mapped: b") |
| 266 |
} |
| 267 |
if actualValue, _ := mappedList.Get(2); actualValue != "mapped: c" { |
| 268 |
t.Errorf("Got %v expected %v", actualValue, "mapped: c") |
| 269 |
} |
| 270 |
if mappedList.Size() != 3 { |
| 271 |
t.Errorf("Got %v expected %v", mappedList.Size(), 3) |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
func TestListSelect(t *testing.T) { |
| 276 |
list := New[string]() |
| 277 |
list.Add("a", "b", "c") |
| 278 |
selectedList := list.Select(func(index int, value string) bool { |
| 279 |
return value >= "a" && value <= "b" |
| 280 |
}) |
| 281 |
if actualValue, _ := selectedList.Get(0); actualValue != "a" { |
| 282 |
t.Errorf("Got %v expected %v", actualValue, "value: a") |
| 283 |
} |
| 284 |
if actualValue, _ := selectedList.Get(1); actualValue != "b" { |
| 285 |
t.Errorf("Got %v expected %v", actualValue, "value: b") |
| 286 |
} |
| 287 |
if selectedList.Size() != 2 { |
| 288 |
t.Errorf("Got %v expected %v", selectedList.Size(), 3) |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
func TestListAny(t *testing.T) { |
| 293 |
list := New[string]() |
| 294 |
list.Add("a", "b", "c") |
| 295 |
any := list.Any(func(index int, value string) bool { |
| 296 |
return value == "c" |
| 297 |
}) |
| 298 |
if any != true { |
| 299 |
t.Errorf("Got %v expected %v", any, true) |
| 300 |
} |
| 301 |
any = list.Any(func(index int, value string) bool { |
| 302 |
return value == "x" |
| 303 |
}) |
| 304 |
if any != false { |
| 305 |
t.Errorf("Got %v expected %v", any, false) |
| 306 |
} |
| 307 |
} |
| 308 |
func TestListAll(t *testing.T) { |
| 309 |
list := New[string]() |
| 310 |
list.Add("a", "b", "c") |
| 311 |
all := list.All(func(index int, value string) bool { |
| 312 |
return value >= "a" && value <= "c" |
| 313 |
}) |
| 314 |
if all != true { |
| 315 |
t.Errorf("Got %v expected %v", all, true) |
| 316 |
} |
| 317 |
all = list.All(func(index int, value string) bool { |
| 318 |
return value >= "a" && value <= "b" |
| 319 |
}) |
| 320 |
if all != false { |
| 321 |
t.Errorf("Got %v expected %v", all, false) |
| 322 |
} |
| 323 |
} |
| 324 |
func TestListFind(t *testing.T) { |
| 325 |
list := New[string]() |
| 326 |
list.Add("a", "b", "c") |
| 327 |
foundIndex, foundValue := list.Find(func(index int, value string) bool { |
| 328 |
return value == "c" |
| 329 |
}) |
| 330 |
if foundValue != "c" || foundIndex != 2 { |
| 331 |
t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, "c", 2) |
| 332 |
} |
| 333 |
foundIndex, foundValue = list.Find(func(index int, value string) bool { |
| 334 |
return value == "x" |
| 335 |
}) |
| 336 |
if foundValue != "" || foundIndex != -1 { |
| 337 |
t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, nil, nil) |
| 338 |
} |
| 339 |
} |
| 340 |
func TestListChaining(t *testing.T) { |
| 341 |
list := New[string]() |
| 342 |
list.Add("a", "b", "c") |
| 343 |
chainedList := list.Select(func(index int, value string) bool { |
| 344 |
return value > "a" |
| 345 |
}).Map(func(index int, value string) string { |
| 346 |
return value + value |
| 347 |
}) |
| 348 |
if chainedList.Size() != 2 { |
| 349 |
t.Errorf("Got %v expected %v", chainedList.Size(), 2) |
| 350 |
} |
| 351 |
if actualValue, ok := chainedList.Get(0); actualValue != "bb" || !ok { |
| 352 |
t.Errorf("Got %v expected %v", actualValue, "b") |
| 353 |
} |
| 354 |
if actualValue, ok := chainedList.Get(1); actualValue != "cc" || !ok { |
| 355 |
t.Errorf("Got %v expected %v", actualValue, "c") |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
func TestListIteratorNextOnEmpty(t *testing.T) { |
| 360 |
list := New[string]() |
| 361 |
it := list.Iterator() |
| 362 |
for it.Next() { |
| 363 |
t.Errorf("Shouldn't iterate on empty list") |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
func TestListIteratorNext(t *testing.T) { |
| 368 |
list := New[string]() |
| 369 |
list.Add("a", "b", "c") |
| 370 |
it := list.Iterator() |
| 371 |
count := 0 |
| 372 |
for it.Next() { |
| 373 |
count++ |
| 374 |
index := it.Index() |
| 375 |
value := it.Value() |
| 376 |
switch index { |
| 377 |
case 0: |
| 378 |
if actualValue, expectedValue := value, "a"; actualValue != expectedValue { |
| 379 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 380 |
} |
| 381 |
case 1: |
| 382 |
if actualValue, expectedValue := value, "b"; actualValue != expectedValue { |
| 383 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 384 |
} |
| 385 |
case 2: |
| 386 |
if actualValue, expectedValue := value, "c"; actualValue != expectedValue { |
| 387 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 388 |
} |
| 389 |
default: |
| 390 |
t.Errorf("Too many") |
| 391 |
} |
| 392 |
} |
| 393 |
if actualValue, expectedValue := count, 3; actualValue != expectedValue { |
| 394 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
func TestListIteratorPrevOnEmpty(t *testing.T) { |
| 399 |
list := New[string]() |
| 400 |
it := list.Iterator() |
| 401 |
for it.Prev() { |
| 402 |
t.Errorf("Shouldn't iterate on empty list") |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
func TestListIteratorPrev(t *testing.T) { |
| 407 |
list := New[string]() |
| 408 |
list.Add("a", "b", "c") |
| 409 |
it := list.Iterator() |
| 410 |
for it.Next() { |
| 411 |
} |
| 412 |
count := 0 |
| 413 |
for it.Prev() { |
| 414 |
count++ |
| 415 |
index := it.Index() |
| 416 |
value := it.Value() |
| 417 |
switch index { |
| 418 |
case 0: |
| 419 |
if actualValue, expectedValue := value, "a"; actualValue != expectedValue { |
| 420 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 421 |
} |
| 422 |
case 1: |
| 423 |
if actualValue, expectedValue := value, "b"; actualValue != expectedValue { |
| 424 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 425 |
} |
| 426 |
case 2: |
| 427 |
if actualValue, expectedValue := value, "c"; actualValue != expectedValue { |
| 428 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 429 |
} |
| 430 |
default: |
| 431 |
t.Errorf("Too many") |
| 432 |
} |
| 433 |
} |
| 434 |
if actualValue, expectedValue := count, 3; actualValue != expectedValue { |
| 435 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
func TestListIteratorBegin(t *testing.T) { |
| 440 |
list := New[string]() |
| 441 |
it := list.Iterator() |
| 442 |
it.Begin() |
| 443 |
list.Add("a", "b", "c") |
| 444 |
for it.Next() { |
| 445 |
} |
| 446 |
it.Begin() |
| 447 |
it.Next() |
| 448 |
if index, value := it.Index(), it.Value(); index != 0 || value != "a" { |
| 449 |
t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a") |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
func TestListIteratorEnd(t *testing.T) { |
| 454 |
list := New[string]() |
| 455 |
it := list.Iterator() |
| 456 |
|
| 457 |
if index := it.Index(); index != -1 { |
| 458 |
t.Errorf("Got %v expected %v", index, -1) |
| 459 |
} |
| 460 |
|
| 461 |
it.End() |
| 462 |
if index := it.Index(); index != 0 { |
| 463 |
t.Errorf("Got %v expected %v", index, 0) |
| 464 |
} |
| 465 |
|
| 466 |
list.Add("a", "b", "c") |
| 467 |
it.End() |
| 468 |
if index := it.Index(); index != list.Size() { |
| 469 |
t.Errorf("Got %v expected %v", index, list.Size()) |
| 470 |
} |
| 471 |
|
| 472 |
it.Prev() |
| 473 |
if index, value := it.Index(), it.Value(); index != list.Size()-1 || value != "c" { |
| 474 |
t.Errorf("Got %v,%v expected %v,%v", index, value, list.Size()-1, "c") |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
func TestListIteratorFirst(t *testing.T) { |
| 479 |
list := New[string]() |
| 480 |
it := list.Iterator() |
| 481 |
if actualValue, expectedValue := it.First(), false; actualValue != expectedValue { |
| 482 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 483 |
} |
| 484 |
list.Add("a", "b", "c") |
| 485 |
if actualValue, expectedValue := it.First(), true; actualValue != expectedValue { |
| 486 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 487 |
} |
| 488 |
if index, value := it.Index(), it.Value(); index != 0 || value != "a" { |
| 489 |
t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a") |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
func TestListIteratorLast(t *testing.T) { |
| 494 |
list := New[string]() |
| 495 |
it := list.Iterator() |
| 496 |
if actualValue, expectedValue := it.Last(), false; actualValue != expectedValue { |
| 497 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 498 |
} |
| 499 |
list.Add("a", "b", "c") |
| 500 |
if actualValue, expectedValue := it.Last(), true; actualValue != expectedValue { |
| 501 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 502 |
} |
| 503 |
if index, value := it.Index(), it.Value(); index != 2 || value != "c" { |
| 504 |
t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "c") |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
func TestListIteratorNextTo(t *testing.T) { |
| 509 |
|
| 510 |
seek := func(index int, value string) bool { |
| 511 |
return strings.HasSuffix(value, "b") |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
{ |
| 516 |
list := New[string]() |
| 517 |
it := list.Iterator() |
| 518 |
for it.NextTo(seek) { |
| 519 |
t.Errorf("Shouldn't iterate on empty list") |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
{ |
| 525 |
list := New[string]() |
| 526 |
list.Add("xx", "yy") |
| 527 |
it := list.Iterator() |
| 528 |
for it.NextTo(seek) { |
| 529 |
t.Errorf("Shouldn't iterate on empty list") |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
{ |
| 535 |
list := New[string]() |
| 536 |
list.Add("aa", "bb", "cc") |
| 537 |
it := list.Iterator() |
| 538 |
it.Begin() |
| 539 |
if !it.NextTo(seek) { |
| 540 |
t.Errorf("Shouldn't iterate on empty list") |
| 541 |
} |
| 542 |
if index, value := it.Index(), it.Value(); index != 1 || value != "bb" { |
| 543 |
t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb") |
| 544 |
} |
| 545 |
if !it.Next() { |
| 546 |
t.Errorf("Should go to first element") |
| 547 |
} |
| 548 |
if index, value := it.Index(), it.Value(); index != 2 || value != "cc" { |
| 549 |
t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "cc") |
| 550 |
} |
| 551 |
if it.Next() { |
| 552 |
t.Errorf("Should not go past last element") |
| 553 |
} |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
func TestListIteratorPrevTo(t *testing.T) { |
| 558 |
|
| 559 |
seek := func(index int, value string) bool { |
| 560 |
return strings.HasSuffix(value, "b") |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
{ |
| 565 |
list := New[string]() |
| 566 |
it := list.Iterator() |
| 567 |
it.End() |
| 568 |
for it.PrevTo(seek) { |
| 569 |
t.Errorf("Shouldn't iterate on empty list") |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
|
| 574 |
{ |
| 575 |
list := New[string]() |
| 576 |
list.Add("xx", "yy") |
| 577 |
it := list.Iterator() |
| 578 |
it.End() |
| 579 |
for it.PrevTo(seek) { |
| 580 |
t.Errorf("Shouldn't iterate on empty list") |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
{ |
| 586 |
list := New[string]() |
| 587 |
list.Add("aa", "bb", "cc") |
| 588 |
it := list.Iterator() |
| 589 |
it.End() |
| 590 |
if !it.PrevTo(seek) { |
| 591 |
t.Errorf("Shouldn't iterate on empty list") |
| 592 |
} |
| 593 |
if index, value := it.Index(), it.Value(); index != 1 || value != "bb" { |
| 594 |
t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb") |
| 595 |
} |
| 596 |
if !it.Prev() { |
| 597 |
t.Errorf("Should go to first element") |
| 598 |
} |
| 599 |
if index, value := it.Index(), it.Value(); index != 0 || value != "aa" { |
| 600 |
t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "aa") |
| 601 |
} |
| 602 |
if it.Prev() { |
| 603 |
t.Errorf("Should not go before first element") |
| 604 |
} |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
func TestListSerialization(t *testing.T) { |
| 609 |
list := New[string]() |
| 610 |
list.Add("a", "b", "c") |
| 611 |
|
| 612 |
var err error |
| 613 |
assert := func() { |
| 614 |
if actualValue, expectedValue := list.Values(), []string{"a", "b", "c"}; !slices.Equal(actualValue, expectedValue) { |
| 615 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 616 |
} |
| 617 |
if actualValue, expectedValue := list.Size(), 3; actualValue != expectedValue { |
| 618 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 619 |
} |
| 620 |
if err != nil { |
| 621 |
t.Errorf("Got error %v", err) |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
assert() |
| 626 |
|
| 627 |
bytes, err := list.ToJSON() |
| 628 |
assert() |
| 629 |
|
| 630 |
err = list.FromJSON(bytes) |
| 631 |
assert() |
| 632 |
|
| 633 |
bytes, err = json.Marshal([]any{"a", "b", "c", list}) |
| 634 |
if err != nil { |
| 635 |
t.Errorf("Got error %v", err) |
| 636 |
} |
| 637 |
|
| 638 |
err = json.Unmarshal([]byte(`["a","b","c"]`), &list) |
| 639 |
if err != nil { |
| 640 |
t.Errorf("Got error %v", err) |
| 641 |
} |
| 642 |
assert() |
| 643 |
} |
| 644 |
|
| 645 |
func TestListString(t *testing.T) { |
| 646 |
c := New[int]() |
| 647 |
c.Add(1) |
| 648 |
if !strings.HasPrefix(c.String(), "ArrayList") { |
| 649 |
t.Errorf("String should start with container name") |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
func benchmarkGet(b *testing.B, list *List[int], size int) { |
| 654 |
for i := 0; i < b.N; i++ { |
| 655 |
for n := 0; n < size; n++ { |
| 656 |
list.Get(n) |
| 657 |
} |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
func benchmarkAdd(b *testing.B, list *List[int], size int) { |
| 662 |
for i := 0; i < b.N; i++ { |
| 663 |
for n := 0; n < size; n++ { |
| 664 |
list.Add(n) |
| 665 |
} |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
func benchmarkRemove(b *testing.B, list *List[int], size int) { |
| 670 |
for i := 0; i < b.N; i++ { |
| 671 |
for n := 0; n < size; n++ { |
| 672 |
list.Remove(n) |
| 673 |
} |
| 674 |
} |
| 675 |
} |
| 676 |
|
| 677 |
func BenchmarkArrayListGet100(b *testing.B) { |
| 678 |
b.StopTimer() |
| 679 |
size := 100 |
| 680 |
list := New[int]() |
| 681 |
for n := 0; n < size; n++ { |
| 682 |
list.Add(n) |
| 683 |
} |
| 684 |
b.StartTimer() |
| 685 |
benchmarkGet(b, list, size) |
| 686 |
} |
| 687 |
|
| 688 |
func BenchmarkArrayListGet1000(b *testing.B) { |
| 689 |
b.StopTimer() |
| 690 |
size := 1000 |
| 691 |
list := New[int]() |
| 692 |
for n := 0; n < size; n++ { |
| 693 |
list.Add(n) |
| 694 |
} |
| 695 |
b.StartTimer() |
| 696 |
benchmarkGet(b, list, size) |
| 697 |
} |
| 698 |
|
| 699 |
func BenchmarkArrayListGet10000(b *testing.B) { |
| 700 |
b.StopTimer() |
| 701 |
size := 10000 |
| 702 |
list := New[int]() |
| 703 |
for n := 0; n < size; n++ { |
| 704 |
list.Add(n) |
| 705 |
} |
| 706 |
b.StartTimer() |
| 707 |
benchmarkGet(b, list, size) |
| 708 |
} |
| 709 |
|
| 710 |
func BenchmarkArrayListGet100000(b *testing.B) { |
| 711 |
b.StopTimer() |
| 712 |
size := 100000 |
| 713 |
list := New[int]() |
| 714 |
for n := 0; n < size; n++ { |
| 715 |
list.Add(n) |
| 716 |
} |
| 717 |
b.StartTimer() |
| 718 |
benchmarkGet(b, list, size) |
| 719 |
} |
| 720 |
|
| 721 |
func BenchmarkArrayListAdd100(b *testing.B) { |
| 722 |
b.StopTimer() |
| 723 |
size := 100 |
| 724 |
list := New[int]() |
| 725 |
b.StartTimer() |
| 726 |
benchmarkAdd(b, list, size) |
| 727 |
} |
| 728 |
|
| 729 |
func BenchmarkArrayListAdd1000(b *testing.B) { |
| 730 |
b.StopTimer() |
| 731 |
size := 1000 |
| 732 |
list := New[int]() |
| 733 |
for n := 0; n < size; n++ { |
| 734 |
list.Add(n) |
| 735 |
} |
| 736 |
b.StartTimer() |
| 737 |
benchmarkAdd(b, list, size) |
| 738 |
} |
| 739 |
|
| 740 |
func BenchmarkArrayListAdd10000(b *testing.B) { |
| 741 |
b.StopTimer() |
| 742 |
size := 10000 |
| 743 |
list := New[int]() |
| 744 |
for n := 0; n < size; n++ { |
| 745 |
list.Add(n) |
| 746 |
} |
| 747 |
b.StartTimer() |
| 748 |
benchmarkAdd(b, list, size) |
| 749 |
} |
| 750 |
|
| 751 |
func BenchmarkArrayListAdd100000(b *testing.B) { |
| 752 |
b.StopTimer() |
| 753 |
size := 100000 |
| 754 |
list := New[int]() |
| 755 |
for n := 0; n < size; n++ { |
| 756 |
list.Add(n) |
| 757 |
} |
| 758 |
b.StartTimer() |
| 759 |
benchmarkAdd(b, list, size) |
| 760 |
} |
| 761 |
|
| 762 |
func BenchmarkArrayListRemove100(b *testing.B) { |
| 763 |
b.StopTimer() |
| 764 |
size := 100 |
| 765 |
list := New[int]() |
| 766 |
for n := 0; n < size; n++ { |
| 767 |
list.Add(n) |
| 768 |
} |
| 769 |
b.StartTimer() |
| 770 |
benchmarkRemove(b, list, size) |
| 771 |
} |
| 772 |
|
| 773 |
func BenchmarkArrayListRemove1000(b *testing.B) { |
| 774 |
b.StopTimer() |
| 775 |
size := 1000 |
| 776 |
list := New[int]() |
| 777 |
for n := 0; n < size; n++ { |
| 778 |
list.Add(n) |
| 779 |
} |
| 780 |
b.StartTimer() |
| 781 |
benchmarkRemove(b, list, size) |
| 782 |
} |
| 783 |
|
| 784 |
func BenchmarkArrayListRemove10000(b *testing.B) { |
| 785 |
b.StopTimer() |
| 786 |
size := 10000 |
| 787 |
list := New[int]() |
| 788 |
for n := 0; n < size; n++ { |
| 789 |
list.Add(n) |
| 790 |
} |
| 791 |
b.StartTimer() |
| 792 |
benchmarkRemove(b, list, size) |
| 793 |
} |
| 794 |
|
| 795 |
func BenchmarkArrayListRemove100000(b *testing.B) { |
| 796 |
b.StopTimer() |
| 797 |
size := 100000 |
| 798 |
list := New[int]() |
| 799 |
for n := 0; n < size; n++ { |
| 800 |
list.Add(n) |
| 801 |
} |
| 802 |
b.StartTimer() |
| 803 |
benchmarkRemove(b, list, size) |
| 804 |
} |