lrucache_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. package lrucache
  2. import (
  3. "container/list"
  4. "testing"
  5. )
  6. type Elem struct {
  7. key int
  8. value string
  9. }
  10. func Test_New(t *testing.T) {
  11. lc := New(5)
  12. if lc.Len() != 0 {
  13. t.Error("case 1 failed")
  14. }
  15. }
  16. func Test_Put(t *testing.T) {
  17. lc := New(0)
  18. lc.Put(1, "1")
  19. if lc.Len() != 0 {
  20. t.Error("case 1.1 failed")
  21. }
  22. lc = New(5)
  23. lc.Put(1, "1")
  24. lc.Put(2, "2")
  25. lc.Put(1, "3")
  26. if lc.Len() != 2 {
  27. t.Error("case 2.1 failed")
  28. }
  29. l := list.New()
  30. l.PushBack(&Elem{1, "3"})
  31. l.PushBack(&Elem{2, "2"})
  32. e := l.Front()
  33. for c := lc.Front(); c != nil; c = c.Next() {
  34. v := e.Value.(*Elem)
  35. if c.Key.(int) != v.key {
  36. t.Error("case 2.2 failed: ", c.Key.(int), v.key)
  37. }
  38. if c.Value.(string) != v.value {
  39. t.Error("case 2.3 failed: ", c.Value.(string), v.value)
  40. }
  41. e = e.Next()
  42. }
  43. lc.Put(3, "4")
  44. lc.Put(4, "5")
  45. lc.Put(5, "6")
  46. lc.Put(2, "7")
  47. if lc.Len() != 5 {
  48. t.Error("case 3.1 failed")
  49. }
  50. l = list.New()
  51. l.PushBack(&Elem{2, "7"})
  52. l.PushBack(&Elem{5, "6"})
  53. l.PushBack(&Elem{4, "5"})
  54. l.PushBack(&Elem{3, "4"})
  55. l.PushBack(&Elem{1, "3"})
  56. rl := list.New()
  57. rl.PushBack(&Elem{1, "3"})
  58. rl.PushBack(&Elem{3, "4"})
  59. rl.PushBack(&Elem{4, "5"})
  60. rl.PushBack(&Elem{5, "6"})
  61. rl.PushBack(&Elem{2, "7"})
  62. e = l.Front()
  63. for c := lc.Front(); c != nil; c = c.Next() {
  64. v := e.Value.(*Elem)
  65. if c.Key.(int) != v.key {
  66. t.Error("case 3.2 failed: ", c.Key.(int), v.key)
  67. }
  68. if c.Value.(string) != v.value {
  69. t.Error("case 3.3 failed: ", c.Value.(string), v.value)
  70. }
  71. e = e.Next()
  72. }
  73. e = rl.Front()
  74. for c := lc.Back(); c != nil; c = c.Prev() {
  75. v := e.Value.(*Elem)
  76. if c.Key.(int) != v.key {
  77. t.Error("case 3.4 failed: ", c.Key.(int), v.key)
  78. }
  79. if c.Value.(string) != v.value {
  80. t.Error("case 3.5 failed: ", c.Value.(string), v.value)
  81. }
  82. e = e.Next()
  83. }
  84. lc.Put(6, "8")
  85. if lc.Len() != 5 {
  86. t.Error("case 4.1 failed")
  87. }
  88. l = list.New()
  89. l.PushBack(&Elem{6, "8"})
  90. l.PushBack(&Elem{2, "7"})
  91. l.PushBack(&Elem{5, "6"})
  92. l.PushBack(&Elem{4, "5"})
  93. l.PushBack(&Elem{3, "4"})
  94. e = l.Front()
  95. for c := lc.Front(); c != nil; c = c.Next() {
  96. v := e.Value.(*Elem)
  97. if c.Key.(int) != v.key {
  98. t.Error("case 4.2 failed: ", c.Key.(int), v.key)
  99. }
  100. if c.Value.(string) != v.value {
  101. t.Error("case 4.3 failed: ", c.Value.(string), v.value)
  102. }
  103. e = e.Next()
  104. }
  105. }
  106. func Test_Get(t *testing.T) {
  107. lc := New(2)
  108. lc.Put(1, "1")
  109. lc.Put(2, "2")
  110. if v, _ := lc.Get(1); v != "1" {
  111. t.Error("case 1.1 failed")
  112. }
  113. lc.Put(3, "3")
  114. if lc.Len() != 2 {
  115. t.Error("case 1.2 failed")
  116. }
  117. l := list.New()
  118. l.PushBack(&Elem{3, "3"})
  119. l.PushBack(&Elem{1, "1"})
  120. e := l.Front()
  121. for c := lc.Front(); c != nil; c = c.Next() {
  122. v := e.Value.(*Elem)
  123. if c.Key.(int) != v.key {
  124. t.Error("case 1.3 failed: ", c.Key.(int), v.key)
  125. }
  126. if c.Value.(string) != v.value {
  127. t.Error("case 1.4 failed: ", c.Value.(string), v.value)
  128. }
  129. e = e.Next()
  130. }
  131. }
  132. func Test_Delete(t *testing.T) {
  133. lc := New(5)
  134. lc.Put(3, "4")
  135. lc.Put(4, "5")
  136. lc.Put(5, "6")
  137. lc.Put(2, "7")
  138. lc.Put(6, "8")
  139. lc.Delete(5)
  140. l := list.New()
  141. l.PushBack(&Elem{6, "8"})
  142. l.PushBack(&Elem{2, "7"})
  143. l.PushBack(&Elem{4, "5"})
  144. l.PushBack(&Elem{3, "4"})
  145. if lc.Len() != 4 {
  146. t.Error("case 1.1 failed")
  147. }
  148. e := l.Front()
  149. for c := lc.Front(); c != nil; c = c.Next() {
  150. v := e.Value.(*Elem)
  151. if c.Key.(int) != v.key {
  152. t.Error("case 1.2 failed: ", c.Key.(int), v.key)
  153. }
  154. if c.Value.(string) != v.value {
  155. t.Error("case 1.3 failed: ", c.Value.(string), v.value)
  156. }
  157. e = e.Next()
  158. }
  159. lc.Delete(6)
  160. l = list.New()
  161. l.PushBack(&Elem{2, "7"})
  162. l.PushBack(&Elem{4, "5"})
  163. l.PushBack(&Elem{3, "4"})
  164. if lc.Len() != 3 {
  165. t.Error("case 2.1 failed")
  166. }
  167. e = l.Front()
  168. for c := lc.Front(); c != nil; c = c.Next() {
  169. v := e.Value.(*Elem)
  170. if c.Key.(int) != v.key {
  171. t.Error("case 2.2 failed: ", c.Key.(int), v.key)
  172. }
  173. if c.Value.(string) != v.value {
  174. t.Error("case 2.3 failed: ", c.Value.(string), v.value)
  175. }
  176. e = e.Next()
  177. }
  178. lc.Delete(3)
  179. l = list.New()
  180. l.PushBack(&Elem{2, "7"})
  181. l.PushBack(&Elem{4, "5"})
  182. if lc.Len() != 2 {
  183. t.Error("case 3.1 failed")
  184. }
  185. e = l.Front()
  186. for c := lc.Front(); c != nil; c = c.Next() {
  187. v := e.Value.(*Elem)
  188. if c.Key.(int) != v.key {
  189. t.Error("case 3.2 failed: ", c.Key.(int), v.key)
  190. }
  191. if c.Value.(string) != v.value {
  192. t.Error("case 3.3 failed: ", c.Value.(string), v.value)
  193. }
  194. e = e.Next()
  195. }
  196. }
  197. func Test_Range(t *testing.T) {
  198. lc := New(5)
  199. lc.Put(3, "4")
  200. lc.Put(4, "5")
  201. lc.Put(5, "6")
  202. lc.Put(2, "7")
  203. lc.Put(6, "8")
  204. l := list.New()
  205. l.PushBack(&Elem{6, "8"})
  206. l.PushBack(&Elem{2, "7"})
  207. l.PushBack(&Elem{5, "6"})
  208. l.PushBack(&Elem{4, "5"})
  209. l.PushBack(&Elem{3, "4"})
  210. e := l.Front()
  211. lc.Range(
  212. func(key, value interface{}) bool {
  213. v := e.Value.(*Elem)
  214. if key.(int) != v.key {
  215. t.Error("case 1.1 failed: ", key.(int), v.key)
  216. }
  217. if value.(string) != v.value {
  218. t.Error("case 1.2 failed: ", value.(string), v.value)
  219. }
  220. e = e.Next()
  221. return true
  222. })
  223. if e != nil {
  224. t.Error("case 1.3 failed: ", e.Value)
  225. }
  226. }