File: cache_test.rb

package info (click to toggle)
ruby-lru-redux 1.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 184 kB
  • sloc: ruby: 615; makefile: 4
file content (131 lines) | stat: -rw-r--r-- 2,166 bytes parent folder | download
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
require 'lru_redux'
require 'minitest/autorun'
require 'minitest/pride'

class CacheTest < Minitest::Test
  def setup
    @c = LruRedux::Cache.new(3)
  end

  def teardown
    assert_equal true, @c.send(:valid?)
  end

  def test_drops_old
    @c[:a] = 1
    @c[:b] = 2
    @c[:c] = 3
    @c[:d] = 4

    assert_equal [[:d,4],[:c,3],[:b,2]], @c.to_a
    assert_nil @c[:a]
  end

  def test_fetch
    @c[:a] = nil
    @c[:b] = 2
    assert_equal @c.fetch(:a){1}, nil
    assert_equal @c.fetch(:c){3}, 3

    assert_equal [[:a,nil],[:b,2]], @c.to_a
  end

  def test_getset
   assert_equal @c.getset(:a){1}, 1
   @c.getset(:b){2}
   assert_equal @c.getset(:a){11}, 1
   @c.getset(:c){3}
   assert_equal @c.getset(:d){4}, 4

    assert_equal [[:d,4],[:c,3],[:a,1]], @c.to_a
  end

  def test_pushes_lru_to_back
    @c[:a] = 1
    @c[:b] = 2
    @c[:c] = 3

    @c[:a]
    @c[:d] = 4

    assert_equal [[:d,4],[:a,1],[:c,3]], @c.to_a
    assert_nil @c[:b]
  end


  def test_delete
    @c[:a] = 1
    @c[:b] = 2
    @c[:c] = 3
    @c.delete(:a)

    assert_equal [[:c,3],[:b,2]], @c.to_a
    assert_nil @c[:a]

    # Regression test for a bug in the legacy delete method
    @c.delete(:b)
    @c[:d] = 4
    @c[:e] = 5
    @c[:f] = 6

    assert_equal [[:f,6],[:e,5],[:d,4]], @c.to_a
    assert_nil @c[:b]
  end

  def test_key?
    @c[:a] = 1
    @c[:b] = 2

    assert_equal true, @c.key?(:a)
    assert_equal false, @c.key?(:c)
  end

  def test_update
    @c[:a] = 1
    @c[:b] = 2
    @c[:c] = 3
    @c[:a] = 99
    assert_equal [[:a,99],[:c,3],[:b,2]], @c.to_a
  end

  def test_clear
    @c[:a] = 1
    @c[:b] = 2
    @c[:c] = 3

    @c.clear
    assert_equal [], @c.to_a
  end

  def test_grow
    @c[:a] = 1
    @c[:b] = 2
    @c[:c] = 3
    @c.max_size = 4
    @c[:d] = 4
    assert_equal [[:d,4],[:c,3],[:b,2],[:a,1]], @c.to_a
  end

  def test_shrink
    @c[:a] = 1
    @c[:b] = 2
    @c[:c] = 3
    @c.max_size = 1
    assert_equal [[:c,3]], @c.to_a
  end

  def test_each
    @c.max_size = 2
    @c[:a] = 1
    @c[:b] = 2
    @c[:c] = 3

    pairs = []
    @c.each do |pair|
      pairs << pair
    end

    assert_equal [[:c,3],[:b, 2]], pairs

  end
end