github.com/mymmsc/gox@v1.3.33/util/treeset/treeset_test.go (about) 1 // Copyright (c) 2015, Emir Pasic. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package treeset 6 7 import ( 8 "fmt" 9 "testing" 10 ) 11 12 func TestSetNew(t *testing.T) { 13 set := NewWithIntComparator(2, 1) 14 if actualValue := set.Size(); actualValue != 2 { 15 t.Errorf("Got %v expected %v", actualValue, 2) 16 } 17 values := set.Values() 18 if actualValue := values[0]; actualValue != 1 { 19 t.Errorf("Got %v expected %v", actualValue, 1) 20 } 21 if actualValue := values[1]; actualValue != 2 { 22 t.Errorf("Got %v expected %v", actualValue, 2) 23 } 24 } 25 26 func TestSetAdd(t *testing.T) { 27 set := NewWithIntComparator() 28 set.Add() 29 set.Add(1) 30 set.Add(2) 31 set.Add(2, 3) 32 set.Add() 33 if actualValue := set.Empty(); actualValue != false { 34 t.Errorf("Got %v expected %v", actualValue, false) 35 } 36 if actualValue := set.Size(); actualValue != 3 { 37 t.Errorf("Got %v expected %v", actualValue, 3) 38 } 39 if actualValue, expectedValue := fmt.Sprintf("%d%d%d", set.Values()...), "123"; actualValue != expectedValue { 40 t.Errorf("Got %v expected %v", actualValue, expectedValue) 41 } 42 } 43 44 func TestSetContains(t *testing.T) { 45 set := NewWithIntComparator() 46 set.Add(3, 1, 2) 47 if actualValue := set.Contains(); actualValue != true { 48 t.Errorf("Got %v expected %v", actualValue, true) 49 } 50 if actualValue := set.Contains(1); actualValue != true { 51 t.Errorf("Got %v expected %v", actualValue, true) 52 } 53 if actualValue := set.Contains(1, 2, 3); actualValue != true { 54 t.Errorf("Got %v expected %v", actualValue, true) 55 } 56 if actualValue := set.Contains(1, 2, 3, 4); actualValue != false { 57 t.Errorf("Got %v expected %v", actualValue, false) 58 } 59 } 60 61 func TestSetRemove(t *testing.T) { 62 set := NewWithIntComparator() 63 set.Add(3, 1, 2) 64 set.Remove() 65 if actualValue := set.Size(); actualValue != 3 { 66 t.Errorf("Got %v expected %v", actualValue, 3) 67 } 68 set.Remove(1) 69 if actualValue := set.Size(); actualValue != 2 { 70 t.Errorf("Got %v expected %v", actualValue, 2) 71 } 72 set.Remove(3) 73 set.Remove(3) 74 set.Remove() 75 set.Remove(2) 76 if actualValue := set.Size(); actualValue != 0 { 77 t.Errorf("Got %v expected %v", actualValue, 0) 78 } 79 } 80 81 func TestSetEach(t *testing.T) { 82 set := NewWithStringComparator() 83 set.Add("c", "a", "b") 84 set.Each(func(index int, value interface{}) { 85 switch index { 86 case 0: 87 if actualValue, expectedValue := value, "a"; actualValue != expectedValue { 88 t.Errorf("Got %v expected %v", actualValue, expectedValue) 89 } 90 case 1: 91 if actualValue, expectedValue := value, "b"; actualValue != expectedValue { 92 t.Errorf("Got %v expected %v", actualValue, expectedValue) 93 } 94 case 2: 95 if actualValue, expectedValue := value, "c"; actualValue != expectedValue { 96 t.Errorf("Got %v expected %v", actualValue, expectedValue) 97 } 98 default: 99 t.Errorf("Too many") 100 } 101 }) 102 } 103 104 func TestSetMap(t *testing.T) { 105 set := NewWithStringComparator() 106 set.Add("c", "a", "b") 107 mappedSet := set.Map(func(index int, value interface{}) interface{} { 108 return "mapped: " + value.(string) 109 }) 110 if actualValue, expectedValue := mappedSet.Contains("mapped: a", "mapped: b", "mapped: c"), true; actualValue != expectedValue { 111 t.Errorf("Got %v expected %v", actualValue, expectedValue) 112 } 113 if actualValue, expectedValue := mappedSet.Contains("mapped: a", "mapped: b", "mapped: x"), false; actualValue != expectedValue { 114 t.Errorf("Got %v expected %v", actualValue, expectedValue) 115 } 116 if mappedSet.Size() != 3 { 117 t.Errorf("Got %v expected %v", mappedSet.Size(), 3) 118 } 119 } 120 121 func TestSetSelect(t *testing.T) { 122 set := NewWithStringComparator() 123 set.Add("c", "a", "b") 124 selectedSet := set.Select(func(index int, value interface{}) bool { 125 return value.(string) >= "a" && value.(string) <= "b" 126 }) 127 if actualValue, expectedValue := selectedSet.Contains("a", "b"), true; actualValue != expectedValue { 128 fmt.Println("A: ", selectedSet.Contains("b")) 129 t.Errorf("Got %v (%v) expected %v (%v)", actualValue, selectedSet.Values(), expectedValue, "[a b]") 130 } 131 if actualValue, expectedValue := selectedSet.Contains("a", "b", "c"), false; actualValue != expectedValue { 132 t.Errorf("Got %v (%v) expected %v (%v)", actualValue, selectedSet.Values(), expectedValue, "[a b]") 133 } 134 if selectedSet.Size() != 2 { 135 t.Errorf("Got %v expected %v", selectedSet.Size(), 3) 136 } 137 } 138 139 func TestSetAny(t *testing.T) { 140 set := NewWithStringComparator() 141 set.Add("c", "a", "b") 142 any := set.Any(func(index int, value interface{}) bool { 143 return value.(string) == "c" 144 }) 145 if any != true { 146 t.Errorf("Got %v expected %v", any, true) 147 } 148 any = set.Any(func(index int, value interface{}) bool { 149 return value.(string) == "x" 150 }) 151 if any != false { 152 t.Errorf("Got %v expected %v", any, false) 153 } 154 } 155 156 func TestSetAll(t *testing.T) { 157 set := NewWithStringComparator() 158 set.Add("c", "a", "b") 159 all := set.All(func(index int, value interface{}) bool { 160 return value.(string) >= "a" && value.(string) <= "c" 161 }) 162 if all != true { 163 t.Errorf("Got %v expected %v", all, true) 164 } 165 all = set.All(func(index int, value interface{}) bool { 166 return value.(string) >= "a" && value.(string) <= "b" 167 }) 168 if all != false { 169 t.Errorf("Got %v expected %v", all, false) 170 } 171 } 172 173 func TestSetFind(t *testing.T) { 174 set := NewWithStringComparator() 175 set.Add("c", "a", "b") 176 foundIndex, foundValue := set.Find(func(index int, value interface{}) bool { 177 return value.(string) == "c" 178 }) 179 if foundValue != "c" || foundIndex != 2 { 180 t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, "c", 2) 181 } 182 foundIndex, foundValue = set.Find(func(index int, value interface{}) bool { 183 return value.(string) == "x" 184 }) 185 if foundValue != nil || foundIndex != -1 { 186 t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, nil, nil) 187 } 188 } 189 190 func TestSetChaining(t *testing.T) { 191 set := NewWithStringComparator() 192 set.Add("c", "a", "b") 193 } 194 195 func TestSetIteratorNextOnEmpty(t *testing.T) { 196 set := NewWithStringComparator() 197 it := set.Iterator() 198 for it.Next() { 199 t.Errorf("Shouldn't iterate on empty set") 200 } 201 } 202 203 func TestSetIteratorPrevOnEmpty(t *testing.T) { 204 set := NewWithStringComparator() 205 it := set.Iterator() 206 for it.Prev() { 207 t.Errorf("Shouldn't iterate on empty set") 208 } 209 } 210 211 func TestSetIteratorNext(t *testing.T) { 212 set := NewWithStringComparator() 213 set.Add("c", "a", "b") 214 it := set.Iterator() 215 count := 0 216 for it.Next() { 217 count++ 218 index := it.Index() 219 value := it.Value() 220 switch index { 221 case 0: 222 if actualValue, expectedValue := value, "a"; actualValue != expectedValue { 223 t.Errorf("Got %v expected %v", actualValue, expectedValue) 224 } 225 case 1: 226 if actualValue, expectedValue := value, "b"; actualValue != expectedValue { 227 t.Errorf("Got %v expected %v", actualValue, expectedValue) 228 } 229 case 2: 230 if actualValue, expectedValue := value, "c"; actualValue != expectedValue { 231 t.Errorf("Got %v expected %v", actualValue, expectedValue) 232 } 233 default: 234 t.Errorf("Too many") 235 } 236 if actualValue, expectedValue := index, count-1; actualValue != expectedValue { 237 t.Errorf("Got %v expected %v", actualValue, expectedValue) 238 } 239 } 240 if actualValue, expectedValue := count, 3; actualValue != expectedValue { 241 t.Errorf("Got %v expected %v", actualValue, expectedValue) 242 } 243 } 244 245 func TestSetIteratorPrev(t *testing.T) { 246 set := NewWithStringComparator() 247 set.Add("c", "a", "b") 248 it := set.Iterator() 249 for it.Prev() { 250 } 251 count := 0 252 for it.Next() { 253 count++ 254 index := it.Index() 255 value := it.Value() 256 switch index { 257 case 0: 258 if actualValue, expectedValue := value, "a"; actualValue != expectedValue { 259 t.Errorf("Got %v expected %v", actualValue, expectedValue) 260 } 261 case 1: 262 if actualValue, expectedValue := value, "b"; actualValue != expectedValue { 263 t.Errorf("Got %v expected %v", actualValue, expectedValue) 264 } 265 case 2: 266 if actualValue, expectedValue := value, "c"; actualValue != expectedValue { 267 t.Errorf("Got %v expected %v", actualValue, expectedValue) 268 } 269 default: 270 t.Errorf("Too many") 271 } 272 if actualValue, expectedValue := index, count-1; actualValue != expectedValue { 273 t.Errorf("Got %v expected %v", actualValue, expectedValue) 274 } 275 } 276 if actualValue, expectedValue := count, 3; actualValue != expectedValue { 277 t.Errorf("Got %v expected %v", actualValue, expectedValue) 278 } 279 } 280 281 func TestSetIteratorBegin(t *testing.T) { 282 set := NewWithStringComparator() 283 it := set.Iterator() 284 it.Begin() 285 set.Add("a", "b", "c") 286 for it.Next() { 287 } 288 it.Begin() 289 it.Next() 290 if index, value := it.Index(), it.Value(); index != 0 || value != "a" { 291 t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a") 292 } 293 } 294 295 func TestSetIteratorEnd(t *testing.T) { 296 set := NewWithStringComparator() 297 it := set.Iterator() 298 299 if index := it.Index(); index != -1 { 300 t.Errorf("Got %v expected %v", index, -1) 301 } 302 303 it.End() 304 if index := it.Index(); index != 0 { 305 t.Errorf("Got %v expected %v", index, 0) 306 } 307 308 set.Add("a", "b", "c") 309 it.End() 310 if index := it.Index(); index != set.Size() { 311 t.Errorf("Got %v expected %v", index, set.Size()) 312 } 313 314 it.Prev() 315 if index, value := it.Index(), it.Value(); index != set.Size()-1 || value != "c" { 316 t.Errorf("Got %v,%v expected %v,%v", index, value, set.Size()-1, "c") 317 } 318 } 319 320 func TestSetIteratorFirst(t *testing.T) { 321 set := NewWithStringComparator() 322 set.Add("a", "b", "c") 323 it := set.Iterator() 324 if actualValue, expectedValue := it.First(), true; actualValue != expectedValue { 325 t.Errorf("Got %v expected %v", actualValue, expectedValue) 326 } 327 if index, value := it.Index(), it.Value(); index != 0 || value != "a" { 328 t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a") 329 } 330 } 331 332 func TestSetIteratorLast(t *testing.T) { 333 set := NewWithStringComparator() 334 set.Add("a", "b", "c") 335 it := set.Iterator() 336 if actualValue, expectedValue := it.Last(), true; actualValue != expectedValue { 337 t.Errorf("Got %v expected %v", actualValue, expectedValue) 338 } 339 if index, value := it.Index(), it.Value(); index != 2 || value != "c" { 340 t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "c") 341 } 342 } 343 344 func TestSetSerialization(t *testing.T) { 345 set := NewWithStringComparator() 346 set.Add("a", "b", "c") 347 348 var err error 349 assert := func() { 350 if actualValue, expectedValue := set.Size(), 3; actualValue != expectedValue { 351 t.Errorf("Got %v expected %v", actualValue, expectedValue) 352 } 353 if actualValue := set.Contains("a", "b", "c"); actualValue != true { 354 t.Errorf("Got %v expected %v", actualValue, true) 355 } 356 if err != nil { 357 t.Errorf("Got error %v", err) 358 } 359 } 360 361 assert() 362 363 json, err := set.ToJSON() 364 assert() 365 366 err = set.FromJSON(json) 367 assert() 368 } 369 370 func benchmarkContains(b *testing.B, set *Set, size int) { 371 for i := 0; i < b.N; i++ { 372 for n := 0; n < size; n++ { 373 set.Contains(n) 374 } 375 } 376 } 377 378 func benchmarkAdd(b *testing.B, set *Set, size int) { 379 for i := 0; i < b.N; i++ { 380 for n := 0; n < size; n++ { 381 set.Add(n) 382 } 383 } 384 } 385 386 func benchmarkRemove(b *testing.B, set *Set, size int) { 387 for i := 0; i < b.N; i++ { 388 for n := 0; n < size; n++ { 389 set.Remove(n) 390 } 391 } 392 } 393 394 func BenchmarkTreeSetContains100(b *testing.B) { 395 b.StopTimer() 396 size := 100 397 set := NewWithIntComparator() 398 for n := 0; n < size; n++ { 399 set.Add(n) 400 } 401 b.StartTimer() 402 benchmarkContains(b, set, size) 403 } 404 405 func BenchmarkTreeSetContains1000(b *testing.B) { 406 b.StopTimer() 407 size := 1000 408 set := NewWithIntComparator() 409 for n := 0; n < size; n++ { 410 set.Add(n) 411 } 412 b.StartTimer() 413 benchmarkContains(b, set, size) 414 } 415 416 func BenchmarkTreeSetContains10000(b *testing.B) { 417 b.StopTimer() 418 size := 10000 419 set := NewWithIntComparator() 420 for n := 0; n < size; n++ { 421 set.Add(n) 422 } 423 b.StartTimer() 424 benchmarkContains(b, set, size) 425 } 426 427 func BenchmarkTreeSetContains100000(b *testing.B) { 428 b.StopTimer() 429 size := 100000 430 set := NewWithIntComparator() 431 for n := 0; n < size; n++ { 432 set.Add(n) 433 } 434 b.StartTimer() 435 benchmarkContains(b, set, size) 436 } 437 438 func BenchmarkTreeSetAdd100(b *testing.B) { 439 b.StopTimer() 440 size := 100 441 set := NewWithIntComparator() 442 b.StartTimer() 443 benchmarkAdd(b, set, size) 444 } 445 446 func BenchmarkTreeSetAdd1000(b *testing.B) { 447 b.StopTimer() 448 size := 1000 449 set := NewWithIntComparator() 450 for n := 0; n < size; n++ { 451 set.Add(n) 452 } 453 b.StartTimer() 454 benchmarkAdd(b, set, size) 455 } 456 457 func BenchmarkTreeSetAdd10000(b *testing.B) { 458 b.StopTimer() 459 size := 10000 460 set := NewWithIntComparator() 461 for n := 0; n < size; n++ { 462 set.Add(n) 463 } 464 b.StartTimer() 465 benchmarkAdd(b, set, size) 466 } 467 468 func BenchmarkTreeSetAdd100000(b *testing.B) { 469 b.StopTimer() 470 size := 100000 471 set := NewWithIntComparator() 472 for n := 0; n < size; n++ { 473 set.Add(n) 474 } 475 b.StartTimer() 476 benchmarkAdd(b, set, size) 477 } 478 479 func BenchmarkTreeSetRemove100(b *testing.B) { 480 b.StopTimer() 481 size := 100 482 set := NewWithIntComparator() 483 for n := 0; n < size; n++ { 484 set.Add(n) 485 } 486 b.StartTimer() 487 benchmarkRemove(b, set, size) 488 } 489 490 func BenchmarkTreeSetRemove1000(b *testing.B) { 491 b.StopTimer() 492 size := 1000 493 set := NewWithIntComparator() 494 for n := 0; n < size; n++ { 495 set.Add(n) 496 } 497 b.StartTimer() 498 benchmarkRemove(b, set, size) 499 } 500 501 func BenchmarkTreeSetRemove10000(b *testing.B) { 502 b.StopTimer() 503 size := 10000 504 set := NewWithIntComparator() 505 for n := 0; n < size; n++ { 506 set.Add(n) 507 } 508 b.StartTimer() 509 benchmarkRemove(b, set, size) 510 } 511 512 func BenchmarkTreeSetRemove100000(b *testing.B) { 513 b.StopTimer() 514 size := 100000 515 set := NewWithIntComparator() 516 for n := 0; n < size; n++ { 517 set.Add(n) 518 } 519 b.StartTimer() 520 benchmarkRemove(b, set, size) 521 }