-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtiny_cache_spec.rb
More file actions
50 lines (35 loc) · 1.28 KB
/
tiny_cache_spec.rb
File metadata and controls
50 lines (35 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
require_relative 'tiny_cache'
RSpec.describe TinyCache do
let(:cache) { TinyCache.new(2) }
it "returns the value of an existing element" do
cache.add_element('tag', 'value')
expect(cache.element('tag')).to eq('value')
end
it "returns nil when the element doesn't exist" do
expect(cache.element('tag')).to eq(nil)
end
it "override the least hit element when it's the last element added" do
cache.add_element('tag1', 'value')
cache.add_element('tag2', 'value')
1.times { cache.element('tag1') }
0.times { cache.element('tag2') }
cache.add_element('tag3', 'value')
expect(cache.element('tag2')).to eq(nil)
end
it "override the least hit element when it's the first element added" do
cache.add_element('tag2', 'value')
cache.add_element('tag1', 'value')
0.times { cache.element('tag1') }
1.times { cache.element('tag2') }
cache.add_element('tag3', 'value')
expect(cache.element('tag1')).to eq(nil)
end
it "add an existing element do not override the least hit element" do
cache.add_element('tag1', 'value')
cache.add_element('tag2', 'value')
1.times { cache.element('tag1') }
0.times { cache.element('tag2') }
cache.add_element('tag1', 'value')
expect(cache.element('tag2')).to eq('value')
end
end