cache_spec.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. var Cache = require('../../../src/cache')
  2. /**
  3. * Debug function to assert cache state
  4. *
  5. * @param {Cache} cache
  6. */
  7. function toString (cache) {
  8. var s = ''
  9. var entry = cache.head
  10. while (entry) {
  11. s += String(entry.key) + ':' + entry.value
  12. entry = entry.newer
  13. if (entry) {
  14. s += ' < '
  15. }
  16. }
  17. return s
  18. }
  19. describe('Cache', function () {
  20. var c = new Cache(4)
  21. it('put', function () {
  22. c.put('adam', 29)
  23. c.put('john', 26)
  24. c.put('angela', 24)
  25. c.put('bob', 48)
  26. expect(c.size).toBe(4)
  27. expect(toString(c)).toBe('adam:29 < john:26 < angela:24 < bob:48')
  28. })
  29. it('get', function () {
  30. expect(c.get('adam')).toBe(29)
  31. expect(c.get('john')).toBe(26)
  32. expect(c.get('angela')).toBe(24)
  33. expect(c.get('bob')).toBe(48)
  34. expect(toString(c)).toBe('adam:29 < john:26 < angela:24 < bob:48')
  35. expect(c.get('angela')).toBe(24)
  36. // angela should now be the tail
  37. expect(toString(c)).toBe('adam:29 < john:26 < bob:48 < angela:24')
  38. })
  39. it('expire', function () {
  40. c.put('ygwie', 81)
  41. expect(c.size).toBe(4)
  42. expect(toString(c)).toBe('john:26 < bob:48 < angela:24 < ygwie:81')
  43. expect(c.get('adam')).toBeUndefined()
  44. })
  45. })