lru_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. Copyright 2015 To gocql authors
  3. Copyright 2013 Google Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package lru
  15. import (
  16. "testing"
  17. )
  18. type simpleStruct struct {
  19. int
  20. string
  21. }
  22. type complexStruct struct {
  23. int
  24. simpleStruct
  25. }
  26. var getTests = []struct {
  27. name string
  28. keyToAdd string
  29. keyToGet string
  30. expectedOk bool
  31. }{
  32. {"string_hit", "mystring", "mystring", true},
  33. {"string_miss", "mystring", "nonsense", false},
  34. {"simple_struct_hit", "two", "two", true},
  35. {"simeple_struct_miss", "two", "noway", false},
  36. }
  37. func TestGet(t *testing.T) {
  38. for _, tt := range getTests {
  39. lru := New(0)
  40. lru.Add(tt.keyToAdd, 1234)
  41. val, ok := lru.Get(tt.keyToGet)
  42. if ok != tt.expectedOk {
  43. t.Fatalf("%s: cache hit = %v; want %v", tt.name, ok, !ok)
  44. } else if ok && val != 1234 {
  45. t.Fatalf("%s expected get to return 1234 but got %v", tt.name, val)
  46. }
  47. }
  48. }
  49. func TestRemove(t *testing.T) {
  50. lru := New(0)
  51. lru.Add("mystring", 1234)
  52. if val, ok := lru.Get("mystring"); !ok {
  53. t.Fatal("TestRemove returned no match")
  54. } else if val != 1234 {
  55. t.Fatalf("TestRemove failed. Expected %d, got %v", 1234, val)
  56. }
  57. lru.Remove("mystring")
  58. if _, ok := lru.Get("mystring"); ok {
  59. t.Fatal("TestRemove returned a removed entry")
  60. }
  61. }