multimap_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package treemap
  2. import (
  3. "github.com/stretchr/testify/assert"
  4. "testing"
  5. )
  6. func TestMultiMap(t *testing.T) {
  7. m := NewMultiMap()
  8. assert.Equal(t, 0, m.Size())
  9. assert.False(t, m.Contains(5))
  10. assert.Equal(t, nil, m.Get(3))
  11. for i := 9; i >= 0; i-- {
  12. m.Insert(i, i+1000)
  13. }
  14. assert.Equal(t, 10, m.Size())
  15. assert.True(t, m.Contains(5))
  16. assert.Equal(t, 3+1000, m.Get(3))
  17. m.Erase(3)
  18. assert.Equal(t, nil, m.Get(3))
  19. m.Clear()
  20. assert.False(t, m.Contains(50))
  21. assert.Equal(t, 0, m.Size())
  22. }
  23. func TestMultiMapIterator(t *testing.T) {
  24. m := NewMultiMap(WithGoroutineSafe())
  25. for i := 1; i <= 10; i++ {
  26. m.Insert(i, i)
  27. }
  28. i := 1
  29. for iter := m.First(); iter.IsValid(); iter.Next() {
  30. assert.Equal(t, i, iter.Value().(int))
  31. i++
  32. }
  33. i = 10
  34. for iter := m.Last(); iter.IsValid(); iter.Prev() {
  35. assert.Equal(t, i, iter.Value().(int))
  36. i--
  37. }
  38. assert.True(t, m.Begin().Equal(m.First()))
  39. iter := m.Find(8)
  40. assert.Equal(t, 8, iter.Value().(int))
  41. iter = m.LowerBound(8)
  42. assert.Equal(t, 8, iter.Value().(int))
  43. iter = m.UpperBound(5)
  44. assert.Equal(t, 6, iter.Value().(int))
  45. }
  46. func TestMultiMap_Traversal(t *testing.T) {
  47. m := NewMultiMap()
  48. for i := 1; i <= 5; i++ {
  49. m.Insert(i, i+1000)
  50. }
  51. for i := 1; i <= 5; i++ {
  52. m.Insert(i, i+1000)
  53. }
  54. i := 1
  55. count := 0
  56. m.Traversal(func(key, value interface{}) bool {
  57. assert.Equal(t, i, key)
  58. assert.Equal(t, i+1000, value)
  59. count++
  60. if count%2 == 0 {
  61. i++
  62. }
  63. return true
  64. })
  65. }