lru_test.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package cache_test
  2. import (
  3. "testing"
  4. . "v2ray.com/core/common/cache"
  5. )
  6. func TestLruReplaceValue(t *testing.T) {
  7. lru := NewLru(2)
  8. lru.Put(2, 6)
  9. lru.Put(1, 5)
  10. lru.Put(1, 2)
  11. v, _ := lru.Get(1)
  12. if v != 2 {
  13. t.Error("should get 2", v)
  14. }
  15. v, _ = lru.Get(2)
  16. if v != 6 {
  17. t.Error("should get 6", v)
  18. }
  19. }
  20. func TestLruRemoveOld(t *testing.T) {
  21. lru := NewLru(2)
  22. v, ok := lru.Get(2)
  23. if ok {
  24. t.Error("should get nil", v)
  25. }
  26. lru.Put(1, 1)
  27. lru.Put(2, 2)
  28. v, _ = lru.Get(1)
  29. if v != 1 {
  30. t.Error("should get 1", v)
  31. }
  32. lru.Put(3, 3)
  33. v, ok = lru.Get(2)
  34. if ok {
  35. t.Error("should get nil", v)
  36. }
  37. lru.Put(4, 4)
  38. v, ok = lru.Get(1)
  39. if ok {
  40. t.Error("should get nil", v)
  41. }
  42. v, _ = lru.Get(3)
  43. if v != 3 {
  44. t.Error("should get 3", v)
  45. }
  46. v, _ = lru.Get(4)
  47. if v != 4 {
  48. t.Error("should get 4", v)
  49. }
  50. }
  51. func TestGetKeyFromValue(t *testing.T) {
  52. lru := NewLru(2)
  53. lru.Put(3, 3)
  54. lru.Put(2, 2)
  55. lru.Put(1, 1)
  56. v, ok := lru.GetKeyFromValue(3)
  57. if ok {
  58. t.Error("should get nil", v)
  59. }
  60. v, _ = lru.GetKeyFromValue(2)
  61. if v != 2 {
  62. t.Error("should get 2", v)
  63. }
  64. }