Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

move test suite to tap

  • Loading branch information...
commit 01a58a681bbffe168eb100a8ef5fa8559b0df26e 1 parent 83b610b
Trent Mick authored
1  .gitignore
... ...
@@ -0,0 +1 @@
  1
+/node_modules
73  lib/lru-cache.js
@@ -76,76 +76,3 @@ function LRUCache (maxLength) {
76 76
     lruWalk()
77 77
   }
78 78
 }
79  
-
80  
-if (!process || !module || module !== process.mainModule) return undefined
81  
-
82  
-var l = LRUCache(3)
83  
-  , assert = require("assert")
84  
-
85  
-l.set(1, 1)
86  
-l.set(2, 1)
87  
-l.set(3, 1)
88  
-l.set(4, 1)
89  
-l.set(5, 1)
90  
-l.set(6, 1)
91  
-
92  
-assert.equal(l.get(1), undefined)
93  
-assert.equal(l.get(2), undefined)
94  
-assert.equal(l.get(3), undefined)
95  
-assert.equal(l.get(4), 1)
96  
-assert.equal(l.get(5), 1)
97  
-assert.equal(l.get(6), 1)
98  
-
99  
-// now keep re-getting the 6 so it remains the most recently used.
100  
-// in this case, we'll have 6, 7, 8, 9, 10, 11, so the ending length = 5
101  
-l.set(7, 1)
102  
-l.get(6)
103  
-l.set(8, 1)
104  
-l.get(6)
105  
-l.set(9, 1)
106  
-l.get(6)
107  
-l.set(10, 1)
108  
-l.get(6)
109  
-l.set(11, 1)
110  
-assert.equal(l.length, 3)
111  
-assert.equal(l.get(4), undefined)
112  
-assert.equal(l.get(5), undefined)
113  
-assert.equal(l.get(6), 1)
114  
-assert.equal(l.get(7), undefined)
115  
-assert.equal(l.get(8), undefined)
116  
-assert.equal(l.get(9), undefined)
117  
-assert.equal(l.get(10), 1)
118  
-assert.equal(l.get(11), 1)
119  
-
120  
-// test changing the maxLength, verify that the LRU items get dropped.
121  
-l.maxLength = 100
122  
-for (var i = 0; i < 100; i ++) l.set(i, i)
123  
-assert.equal(l.length, 100)
124  
-for (var i = 0; i < 100; i ++) {
125  
-  assert.equal(l.get(i), i)
126  
-}
127  
-l.maxLength = 3
128  
-assert.equal(l.length, 3)
129  
-for (var i = 0; i < 97; i ++) {
130  
-  assert.equal(l.get(i), undefined)
131  
-}
132  
-for (var i = 98; i < 100; i ++) {
133  
-  assert.equal(l.get(i), i)
134  
-}
135  
-
136  
-// now remove the maxLength restriction, and try again.
137  
-l.maxLength = "hello"
138  
-for (var i = 0; i < 100; i ++) l.set(i, i)
139  
-assert.equal(l.length, 100)
140  
-for (var i = 0; i < 100; i ++) {
141  
-  assert.equal(l.get(i), i)
142  
-}
143  
-// should trigger an immediate resize
144  
-l.maxLength = 3
145  
-assert.equal(l.length, 3)
146  
-for (var i = 0; i < 97; i ++) {
147  
-  assert.equal(l.get(i), undefined)
148  
-}
149  
-for (var i = 98; i < 100; i ++) {
150  
-  assert.equal(l.get(i), i)
151  
-}
4  package.json
@@ -2,9 +2,11 @@
2 2
 , "description" : "A cache object that deletes the least-recently-used items."
3 3
 , "version" : "1.0.4"
4 4
 , "author" : "Isaac Z. Schlueter <i@izs.me>"
5  
-, "scripts" : { "test" : "node lib/lru-cache.js" }
  5
+, "scripts" : { "test" : "./node_modules/.bin/tap test" }
6 6
 , "main" : "lib/lru-cache"
7 7
 , "repository" : "git://github.com/isaacs/node-lru-cache.git"
  8
+, "devDependencies" :
  9
+  { "tap" : "0.1" }
8 10
 , "licenses" :
9 11
   [ { "type" : "MIT"
10 12
     , "url" : "http://github.com/isaacs/node-lru-cache/raw/master/LICENSE"
81  test/basic.js
... ...
@@ -0,0 +1,81 @@
  1
+var test = require('tap').test
  2
+  , LRU = require('../')
  3
+
  4
+test('basic', function (t) {
  5
+  var cache = new LRU(10)
  6
+  cache.set("key", "value")
  7
+  t.equal(cache.get("key"), "value")
  8
+  t.equal(cache.get("nada"), undefined)
  9
+  t.equal(cache.length, 1)
  10
+  t.equal(cache.maxLength, 10)
  11
+  t.end()
  12
+})
  13
+
  14
+test('least recently set', function (t) {
  15
+  var cache = new LRU(2)
  16
+  cache.set("a", "A")
  17
+  cache.set("b", "B")
  18
+  cache.set("c", "C")
  19
+  t.equal(cache.get("c"), "C")
  20
+  t.equal(cache.get("b"), "B")
  21
+  t.equal(cache.get("a"), undefined)
  22
+  t.end()
  23
+})
  24
+
  25
+test('lru recently gotten', function (t) {
  26
+  var cache = new LRU(2)
  27
+  cache.set("a", "A")
  28
+  cache.set("b", "B")
  29
+  cache.get("a")
  30
+  cache.set("c", "C")
  31
+  t.equal(cache.get("c"), "C")
  32
+  t.equal(cache.get("b"), undefined)
  33
+  t.equal(cache.get("a"), "A")
  34
+  t.end()
  35
+})
  36
+
  37
+test('del', function (t) {
  38
+  var cache = new LRU(2)
  39
+  cache.set("a", "A")
  40
+  cache.del("a")
  41
+  t.equal(cache.get("a"), undefined)
  42
+  t.end()
  43
+})
  44
+
  45
+test('maxLength', function (t) {
  46
+  var cache = new LRU(3)
  47
+
  48
+  // test changing the maxLength, verify that the LRU items get dropped.
  49
+  cache.maxLength = 100
  50
+  for (var i = 0; i < 100; i ++) cache.set(i, i)
  51
+  t.equal(cache.length, 100)
  52
+  for (var i = 0; i < 100; i ++) {
  53
+    t.equal(cache.get(i), i)
  54
+  }
  55
+  cache.maxLength = 3
  56
+  t.equal(cache.length, 3)
  57
+  for (var i = 0; i < 97; i ++) {
  58
+    t.equal(cache.get(i), undefined)
  59
+  }
  60
+  for (var i = 98; i < 100; i ++) {
  61
+    t.equal(cache.get(i), i)
  62
+  }
  63
+  
  64
+  // now remove the maxLength restriction, and try again.
  65
+  cache.maxLength = "hello"
  66
+  for (var i = 0; i < 100; i ++) cache.set(i, i)
  67
+  t.equal(cache.length, 100)
  68
+  for (var i = 0; i < 100; i ++) {
  69
+    t.equal(cache.get(i), i)
  70
+  }
  71
+  // should trigger an immediate resize
  72
+  cache.maxLength = 3
  73
+  t.equal(cache.length, 3)
  74
+  for (var i = 0; i < 97; i ++) {
  75
+    t.equal(cache.get(i), undefined)
  76
+  }
  77
+  for (var i = 98; i < 100; i ++) {
  78
+    t.equal(cache.get(i), i)
  79
+  }
  80
+  t.end()
  81
+})

0 notes on commit 01a58a6

Please sign in to comment.
Something went wrong with that request. Please try again.