arc_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package arc
  2. import (
  3. "testing"
  4. )
  5. func TestInsertion(t *testing.T) {
  6. cache := New[string, string](WithSize[string, string](3))
  7. if got, want := cache.Len(), 0; got != want {
  8. t.Errorf("empty cache.Len(): got %d want %d", cache.Len(), want)
  9. }
  10. const (
  11. k1 = "Hello"
  12. k2 = "Hallo"
  13. k3 = "Ciao"
  14. k4 = "Salut"
  15. v1 = "World"
  16. v2 = "Worlds"
  17. v3 = "Welt"
  18. )
  19. // Insert the first value
  20. cache.Set(k1, v1)
  21. if got, want := cache.Len(), 1; got != want {
  22. t.Errorf("insertion of key #%d: cache.Len(): got %d want %d", want, cache.Len(), want)
  23. }
  24. if got, ok := cache.Get(k1); !ok || got != v1 {
  25. t.Errorf("cache.Get(%v): got (%v,%t) want (%v,true)", k1, got, ok, v1)
  26. }
  27. // Replace existing value for a given key
  28. cache.Set(k1, v2)
  29. if got, want := cache.Len(), 1; got != want {
  30. t.Errorf("re-insertion: cache.Len(): got %d want %d", cache.Len(), want)
  31. }
  32. if got, ok := cache.Get(k1); !ok || got != v2 {
  33. t.Errorf("re-insertion: cache.Get(%v): got (%v,%t) want (%v,true)", k1, got, ok, v2)
  34. }
  35. // Add a second different key
  36. cache.Set(k2, v3)
  37. if got, want := cache.Len(), 2; got != want {
  38. t.Errorf("insertion of key #%d: cache.Len(): got %d want %d", want, cache.Len(), want)
  39. }
  40. if got, ok := cache.Get(k1); !ok || got != v2 {
  41. t.Errorf("cache.Get(%v): got (%v,%t) want (%v,true)", k1, got, ok, v2)
  42. }
  43. if got, ok := cache.Get(k2); !ok || got != v3 {
  44. t.Errorf("cache.Get(%v): got (%v,%t) want (%v,true)", k2, got, ok, v3)
  45. }
  46. // Fill cache
  47. cache.Set(k3, v1)
  48. if got, want := cache.Len(), 3; got != want {
  49. t.Errorf("insertion of key #%d: cache.Len(): got %d want %d", want, cache.Len(), want)
  50. }
  51. // Exceed size, this should not exceed size:
  52. cache.Set(k4, v1)
  53. if got, want := cache.Len(), 3; got != want {
  54. t.Errorf("insertion of key out of size: cache.Len(): got %d want %d", cache.Len(), want)
  55. }
  56. }
  57. func TestEviction(t *testing.T) {
  58. size := 3
  59. cache := New[string, string](WithSize[string, string](size))
  60. if got, want := cache.Len(), 0; got != want {
  61. t.Errorf("empty cache.Len(): got %d want %d", cache.Len(), want)
  62. }
  63. tests := []struct {
  64. k, v string
  65. }{
  66. {"k1", "v1"},
  67. {"k2", "v2"},
  68. {"k3", "v3"},
  69. {"k4", "v4"},
  70. }
  71. for i, tt := range tests[:size] {
  72. cache.Set(tt.k, tt.v)
  73. if got, want := cache.Len(), i+1; got != want {
  74. t.Errorf("insertion of key #%d: cache.Len(): got %d want %d", want, cache.Len(), want)
  75. }
  76. }
  77. // Exceed size and check we don't outgrow it:
  78. cache.Set(tests[size].k, tests[size].v)
  79. if got := cache.Len(); got != size {
  80. t.Errorf("insertion of overflow key #%d: cache.Len(): got %d want %d", 4, cache.Len(), size)
  81. }
  82. // Check that LRU got evicted:
  83. if got, ok := cache.Get(tests[0].k); ok || got != "" {
  84. t.Errorf("cache.Get(%v): got (%v,%t) want (<nil>,true)", tests[0].k, got, ok)
  85. }
  86. for _, tt := range tests[1:] {
  87. if got, ok := cache.Get(tt.k); !ok || got != tt.v {
  88. t.Errorf("cache.Get(%v): got (%v,%t) want (%v,true)", tt.k, got, ok, tt.v)
  89. }
  90. }
  91. }