hashery 2.1.1 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,15 +2,51 @@ require 'helper'
2
2
 
3
3
  test_case LRUHash do
4
4
 
5
+ # Build a full hash of :a => 1, :b => 2, :c => 3, used least recently
6
+ # in that order, i.e. :a is next to be evicted.
7
+ def abc(max_size=3)
8
+ h = LRUHash.new(max_size)
9
+ h[:a] = 1
10
+ h[:b] = 2
11
+ h[:c] = 3
12
+ h
13
+ end
14
+
5
15
  class_method :new do
6
- h = LRUHash.new(10)
7
- LRUHash.assert === h
16
+ test do
17
+ h = LRUHash.new(10)
18
+ LRUHash.assert === h
19
+ end
20
+
21
+ test "max_size is converted with #to_i" do
22
+ LRUHash.new("5").max_size.assert == 5
23
+ LRUHash.new(2.9).max_size.assert == 2
24
+ end
25
+
26
+ test "max_size must be positive" do
27
+ ArgumentError.assert.raised? { LRUHash.new(0) }
28
+ ArgumentError.assert.raised? { LRUHash.new(-1) }
29
+ ArgumentError.assert.raised? { LRUHash.new(nil) }
30
+ end
31
+
32
+ test "default value" do
33
+ h = LRUHash.new(10, :none)
34
+ h[:x].assert == :none
35
+ end
8
36
  end
9
37
 
10
38
  method :max_size= do
11
39
  test do
12
40
  h = LRUHash.new(10)
13
41
  h.max_size = 100
42
+ h.max_size.assert == 100
43
+ end
44
+
45
+ test "shrinking evicts least recently used entries" do
46
+ h = abc
47
+ h[:a]
48
+ h.max_size = 1
49
+ h.keys.assert == [:a]
14
50
  end
15
51
  end
16
52
 
@@ -18,7 +54,35 @@ test_case LRUHash do
18
54
  test do
19
55
  h = LRUHash.new(10)
20
56
  h.store(:a, 1)
21
- h[:a].assert == 1
57
+ h[:a].assert == 1
58
+ end
59
+
60
+ test "returns the value" do
61
+ h = LRUHash.new(10)
62
+ h.store(:a, 1).assert == 1
63
+ end
64
+
65
+ test "evicts the least recently used entry when full" do
66
+ h = abc
67
+ h.store(:d, 4)
68
+ h.keys.assert == [:d, :c, :b]
69
+ end
70
+
71
+ test "overwriting a key does not evict" do
72
+ h = abc
73
+ h.store(:a, 10)
74
+ h.size.assert == 3
75
+ h.keys.assert == [:a, :c, :b]
76
+ h[:a].assert == 10
77
+ end
78
+
79
+ test "unfrozen String keys are copied and frozen" do
80
+ h = LRUHash.new(10)
81
+ k = String.new("k")
82
+ h.store(k, 1)
83
+ k << "x"
84
+ h.keys.assert == ["k"]
85
+ h.keys.first.assert.frozen?
22
86
  end
23
87
  end
24
88
 
@@ -33,6 +97,64 @@ test_case LRUHash do
33
97
  h = LRUHash.new(10){ |h,k| h[k] = 0 }
34
98
  h[:a].assert == 0
35
99
  end
100
+
101
+ test "marks the entry most recently used" do
102
+ h = abc
103
+ h[:a]
104
+ h[:d] = 4
105
+ h.keys.assert == [:d, :a, :c]
106
+ end
107
+
108
+ test "nil values are found" do
109
+ h = LRUHash.new(10, :default)
110
+ h[:a] = nil
111
+ h[:a].assert == nil
112
+ end
113
+ end
114
+
115
+ method :fetch do
116
+ test do
117
+ h = LRUHash.new(10)
118
+ h[:a] = 1
119
+ h.fetch(:a).assert == 1
120
+ end
121
+
122
+ test "missing key raises KeyError" do
123
+ h = LRUHash.new(10)
124
+ KeyError.assert.raised? { h.fetch(:x) }
125
+ end
126
+
127
+ test "KeyError names the key and receiver" do
128
+ h = LRUHash.new(10)
129
+ e = (h.fetch(:x) rescue $!)
130
+ KeyError.assert === e
131
+ e.message.assert == "key not found: :x"
132
+ e.key.assert == :x
133
+ e.receiver.assert.equal?(h)
134
+ end
135
+
136
+ test "missing key with block" do
137
+ h = LRUHash.new(10)
138
+ h.fetch(:x){ |k| "no #{k}" }.assert == "no x"
139
+ end
140
+
141
+ test "missing key with default argument" do
142
+ h = LRUHash.new(10)
143
+ h.fetch(:x, 0).assert == 0
144
+ h.fetch(:x, nil).assert == nil
145
+ end
146
+
147
+ test "ignores the hash default" do
148
+ h = LRUHash.new(10, :default)
149
+ KeyError.assert.raised? { h.fetch(:x) }
150
+ end
151
+
152
+ test "marks the entry most recently used" do
153
+ h = abc
154
+ h.fetch(:a)
155
+ h[:d] = 4
156
+ h.keys.assert == [:d, :a, :c]
157
+ end
36
158
  end
37
159
 
38
160
  method :empty? do
@@ -42,6 +164,13 @@ test_case LRUHash do
42
164
  end
43
165
  end
44
166
 
167
+ method :size do
168
+ test do
169
+ abc.size.assert == 3
170
+ abc.length.assert == 3
171
+ end
172
+ end
173
+
45
174
  method :key do
46
175
  test do
47
176
  h = LRUHash.new(10)
@@ -56,6 +185,12 @@ test_case LRUHash do
56
185
  h[:a] = 1
57
186
  h.keys.assert == [:a]
58
187
  end
188
+
189
+ test "most recently used first" do
190
+ h = abc
191
+ h[:a]
192
+ h.keys.assert == [:a, :c, :b]
193
+ end
59
194
  end
60
195
 
61
196
  method :values do
@@ -64,6 +199,12 @@ test_case LRUHash do
64
199
  h[:a] = 1
65
200
  h.values.assert == [1]
66
201
  end
202
+
203
+ test "most recently used first" do
204
+ h = abc
205
+ h[:a]
206
+ h.values.assert == [1, 3, 2]
207
+ end
67
208
  end
68
209
 
69
210
  method :values_at do
@@ -73,6 +214,12 @@ test_case LRUHash do
73
214
  h[:b] = 2
74
215
  h.values_at(:a).assert == [1]
75
216
  end
217
+
218
+ test "missing keys give the default" do
219
+ h = LRUHash.new(10, :none)
220
+ h[:a] = 1
221
+ h.values_at(:a, :x).assert == [1, :none]
222
+ end
76
223
  end
77
224
 
78
225
  method :has_key? do
@@ -80,6 +227,17 @@ test_case LRUHash do
80
227
  h = LRUHash.new(10)
81
228
  h[:a] = 1
82
229
  h.assert.has_key?(:a)
230
+ h.assert.key?(:a)
231
+ h.assert.member?(:a)
232
+ h.assert.include?(:a)
233
+ h.refute.has_key?(:b)
234
+ end
235
+
236
+ test "does not affect LRU order" do
237
+ h = abc
238
+ h.has_key?(:a)
239
+ h[:d] = 4
240
+ h.refute.has_key?(:a)
83
241
  end
84
242
  end
85
243
 
@@ -88,6 +246,7 @@ test_case LRUHash do
88
246
  h = LRUHash.new(10)
89
247
  h[:a] = 1
90
248
  h.assert.has_value?(1)
249
+ h.refute.has_value?(2)
91
250
  end
92
251
  end
93
252
 
@@ -97,6 +256,7 @@ test_case LRUHash do
97
256
  h[:a] = 1
98
257
  h[:b] = 2
99
258
  h.assoc(:a).assert == [:a,1]
259
+ h.assoc(:x).assert == nil
100
260
  end
101
261
  end
102
262
 
@@ -106,6 +266,26 @@ test_case LRUHash do
106
266
  h[:a] = 1
107
267
  h[:b] = 2
108
268
  h.rassoc(1).assert == [:a,1]
269
+ h.rassoc(9).assert == nil
270
+ end
271
+ end
272
+
273
+ method :each do
274
+ test "most recently used first" do
275
+ h = abc
276
+ h[:a]
277
+ h.to_a.assert == [[:a, 1], [:c, 3], [:b, 2]]
278
+ end
279
+
280
+ test "returns an enumerator without a block" do
281
+ Enumerator.assert === abc.each
282
+ abc.each.size.assert == 3
283
+ end
284
+
285
+ test "allows modification while iterating" do
286
+ h = abc
287
+ h.each { |k, v| h[k] = v * 10 }
288
+ h.values.sort.assert == [10, 20, 30]
109
289
  end
110
290
  end
111
291
 
@@ -145,8 +325,18 @@ test_case LRUHash do
145
325
  test do
146
326
  h = LRUHash.new(10)
147
327
  h[:a] = 1
148
- h.delete(:a)
149
- h.assert.empty?
328
+ h.delete(:a).assert == 1
329
+ h.assert.empty?
330
+ end
331
+
332
+ test "missing key returns nil" do
333
+ h = LRUHash.new(10)
334
+ h.delete(:a).assert == nil
335
+ end
336
+
337
+ test "missing key with block" do
338
+ h = LRUHash.new(10)
339
+ h.delete(:a){ |k| "no #{k}" }.assert == "no a"
150
340
  end
151
341
  end
152
342
 
@@ -155,7 +345,87 @@ test_case LRUHash do
155
345
  h = LRUHash.new(10)
156
346
  h[:a] = 1
157
347
  h.delete_if{ |k,v| k == :a }
158
- h.assert.empty?
348
+ h.assert.empty?
349
+ end
350
+
351
+ test "returns self" do
352
+ h = abc
353
+ h.delete_if{ |k,v| v.odd? }.assert.equal?(h)
354
+ h.keys.assert == [:b]
355
+ end
356
+
357
+ test "returns an enumerator without a block" do
358
+ Enumerator.assert === abc.delete_if
359
+ end
360
+ end
361
+
362
+ method :release_proc= do
363
+ test "called for evicted, deleted and cleared entries" do
364
+ log = []
365
+ h = LRUHash.new(2)
366
+ h.release_proc = lambda { |k, v| log << [k, v] }
367
+ h[:a] = 1
368
+ h[:b] = 2
369
+ h[:c] = 3 # evicts :a
370
+ h.delete(:b)
371
+ h[:d] = 4
372
+ h.clear # least recently used first
373
+ log.assert == [[:a, 1], [:b, 2], [:c, 3], [:d, 4]]
374
+ end
375
+
376
+ test "called when shrinking and in delete_if" do
377
+ log = []
378
+ h = abc
379
+ h.release_proc = lambda { |k, v| log << k }
380
+ h.max_size = 2
381
+ h.delete_if { |k, v| k == :c }
382
+ log.assert == [:a, :c]
383
+ end
384
+
385
+ test "not called when a value is overwritten" do
386
+ log = []
387
+ h = LRUHash.new(2)
388
+ h.release_proc = lambda { |k, v| log << k }
389
+ h[:a] = 1
390
+ h[:a] = 2
391
+ log.assert == []
392
+ end
393
+ end
394
+
395
+ method :dup do
396
+ test "copy is independent of the original" do
397
+ a = abc
398
+ b = a.dup
399
+ b[:d] = 4
400
+ b.delete(:b)
401
+ a.to_a.assert == [[:c, 3], [:b, 2], [:a, 1]]
402
+ b.to_a.assert == [[:d, 4], [:c, 3]]
403
+ end
404
+
405
+ test "copy keeps LRU order" do
406
+ a = abc
407
+ a[:a]
408
+ a.dup.keys.assert == [:a, :c, :b]
409
+ end
410
+ end
411
+
412
+ method :inspect do
413
+ test "looks like a Hash, most recently used first" do
414
+ h = LRUHash.new(10)
415
+ h[:a] = "x"
416
+ h[nil] = 1
417
+ h.inspect.assert == {nil => 1, :a => "x"}.inspect
418
+ h.to_s.assert == h.inspect
419
+ end
420
+
421
+ test "empty" do
422
+ LRUHash.new(10).inspect.assert == "{}"
423
+ end
424
+ end
425
+
426
+ method :to_h do
427
+ test do
428
+ abc.to_h.assert == {:a => 1, :b => 2, :c => 3}
159
429
  end
160
430
  end
161
431
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashery
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Trans
@@ -11,39 +11,10 @@ authors:
11
11
  - George Moschovitis
12
12
  - Jeena Paradies
13
13
  - Erik Veenstra
14
- autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
- date: 2013-08-21 00:00:00.000000000 Z
18
- dependencies:
19
- - !ruby/object:Gem::Dependency
20
- name: qed
21
- requirement: !ruby/object:Gem::Requirement
22
- requirements:
23
- - - '>='
24
- - !ruby/object:Gem::Version
25
- version: '0'
26
- type: :development
27
- prerelease: false
28
- version_requirements: !ruby/object:Gem::Requirement
29
- requirements:
30
- - - '>='
31
- - !ruby/object:Gem::Version
32
- version: '0'
33
- - !ruby/object:Gem::Dependency
34
- name: lemon
35
- requirement: !ruby/object:Gem::Requirement
36
- requirements:
37
- - - '>='
38
- - !ruby/object:Gem::Version
39
- version: '0'
40
- type: :development
41
- prerelease: false
42
- version_requirements: !ruby/object:Gem::Requirement
43
- requirements:
44
- - - '>='
45
- - !ruby/object:Gem::Version
46
- version: '0'
16
+ date: 1980-01-02 00:00:00.000000000 Z
17
+ dependencies: []
47
18
  description: The Hashery is a tight collection of Hash-like classes. Included among
48
19
  its many offerings are the auto-sorting Dictionary class, the efficient LRUHash,
49
20
  the flexible OpenHash and the convenient KeyHash. Nearly every class is a subclass
@@ -54,13 +25,27 @@ email:
54
25
  executables: []
55
26
  extensions: []
56
27
  extra_rdoc_files:
57
- - NOTICE.txt
28
+ - HISTORY.md
58
29
  - LICENSE.txt
30
+ - NOTICE.txt
59
31
  - README.md
60
- - HISTORY.md
61
32
  files:
62
- - .index
63
- - .yardopts
33
+ - ".yardopts"
34
+ - HISTORY.md
35
+ - LICENSE.txt
36
+ - NOTICE.txt
37
+ - README.md
38
+ - alt/hashery/castinghash.rb
39
+ - alt/hashery/fuzzyhash.rb
40
+ - alt/hashery/keyhash.rb
41
+ - alt/hashery/linkedlist.rb
42
+ - alt/hashery/lruhash.rb
43
+ - alt/hashery/opencascade.rb
44
+ - alt/hashery/openhash.rb
45
+ - alt/hashery/orderedhash.rb
46
+ - alt/hashery/propertyhash.rb
47
+ - alt/hashery/queryhash.rb
48
+ - alt/hashery/statichash.rb
64
49
  - demo/00_introduction.rdoc
65
50
  - demo/01_open_hash.rdoc
66
51
  - demo/02_query_hash.rdoc
@@ -73,6 +58,7 @@ files:
73
58
  - demo/10_association.rdoc
74
59
  - demo/applique/ae.rb
75
60
  - demo/applique/hashery.rb
61
+ - lib/hashery.rb
76
62
  - lib/hashery/association.rb
77
63
  - lib/hashery/casting_hash.rb
78
64
  - lib/hashery/core_ext.rb
@@ -91,8 +77,7 @@ files:
91
77
  - lib/hashery/query_hash.rb
92
78
  - lib/hashery/stash.rb
93
79
  - lib/hashery/static_hash.rb
94
- - lib/hashery.rb
95
- - lib/hashery.yml
80
+ - lib/hashery/version.rb
96
81
  - test/case_association.rb
97
82
  - test/case_casting_hash.rb
98
83
  - test/case_core_ext.rb
@@ -108,46 +93,30 @@ files:
108
93
  - test/case_query_hash.rb
109
94
  - test/fixture/example.ini
110
95
  - test/helper.rb
111
- - README.md
112
- - HISTORY.md
113
- - NOTICE.txt
114
- - LICENSE.txt
115
- homepage: http://rubyworks.github.com/hashery
96
+ homepage: https://rubyworks.github.io/hashery
116
97
  licenses:
117
98
  - BSD-2-Clause
118
- metadata: {}
119
- post_install_message:
99
+ metadata:
100
+ homepage_uri: https://rubyworks.github.io/hashery
101
+ source_code_uri: https://github.com/rubyworks/hashery
102
+ bug_tracker_uri: https://github.com/rubyworks/hashery/issues
103
+ changelog_uri: https://github.com/rubyworks/hashery/blob/master/HISTORY.md
104
+ documentation_uri: https://rubydoc.info/gems/hashery
120
105
  rdoc_options: []
121
106
  require_paths:
122
107
  - lib
123
108
  required_ruby_version: !ruby/object:Gem::Requirement
124
109
  requirements:
125
- - - '>='
110
+ - - ">="
126
111
  - !ruby/object:Gem::Version
127
- version: '0'
112
+ version: '2.7'
128
113
  required_rubygems_version: !ruby/object:Gem::Requirement
129
114
  requirements:
130
- - - '>='
115
+ - - ">="
131
116
  - !ruby/object:Gem::Version
132
117
  version: '0'
133
118
  requirements: []
134
- rubyforge_project:
135
- rubygems_version: 2.0.3
136
- signing_key:
119
+ rubygems_version: 3.6.9
137
120
  specification_version: 4
138
121
  summary: Facets-bread collection of Hash-like classes.
139
- test_files:
140
- - test/case_ini_hash.rb
141
- - test/case_casting_hash.rb
142
- - test/case_open_cascade.rb
143
- - test/case_crud_hash.rb
144
- - test/case_core_ext.rb
145
- - test/case_lru_hash.rb
146
- - test/case_open_hash.rb
147
- - test/case_dictionary.rb
148
- - test/case_linked_list.rb
149
- - test/case_association.rb
150
- - test/case_property_hash.rb
151
- - test/case_key_hash.rb
152
- - test/helper.rb
153
- - test/case_query_hash.rb
122
+ test_files: []
data/.index DELETED
@@ -1,73 +0,0 @@
1
- ---
2
- revision: 2013
3
- type: ruby
4
- sources:
5
- - Index.yml
6
- - Gemfile
7
- authors:
8
- - name: Trans
9
- email: transfire@gmail.com
10
- - name: Kirk Haines
11
- - name: Robert Klemme
12
- - name: Jan Molic
13
- - name: George Moschovitis
14
- - name: Jeena Paradies
15
- - name: Erik Veenstra
16
- organizations:
17
- - name: RubyWorks (http://rubyworks.github.com/)
18
- requirements:
19
- - groups:
20
- - development
21
- - test
22
- version: '>= 0'
23
- name: qed
24
- - groups:
25
- - development
26
- - test
27
- version: '>= 0'
28
- name: lemon
29
- conflicts: []
30
- alternatives: []
31
- resources:
32
- - type: home
33
- uri: http://rubyworks.github.com/hashery
34
- label: Homepage
35
- - type: code
36
- uri: http://github.com/rubyworks/hashery
37
- label: Source Code
38
- - type: mail
39
- uri: http://groups.google.com/group/rubyworks-mailinglist
40
- label: Mailing List
41
- - type: docs
42
- uri: http://rubydoc.info/github/rubyworks/hashery/master/frames
43
- label: Documentation
44
- - type: wiki
45
- uri: http://wiki.github.com/rubyworks/hashery
46
- label: User Guide
47
- - type: gems
48
- uri: http://rubygems.org/gems/hashery
49
- repositories:
50
- - name: upstream
51
- scm: git
52
- uri: git://github.com/rubyworks/hashery.git
53
- categories: []
54
- copyrights:
55
- - holder: Rubyworks
56
- year: '2010'
57
- license: BSD-2-Clause
58
- customs: []
59
- paths:
60
- lib:
61
- - lib
62
- - alt
63
- name: hashery
64
- title: Hashery
65
- version: 2.1.1
66
- summary: Facets-bread collection of Hash-like classes.
67
- description: The Hashery is a tight collection of Hash-like classes. Included among
68
- its many offerings are the auto-sorting Dictionary class, the efficient LRUHash,
69
- the flexible OpenHash and the convenient KeyHash. Nearly every class is a subclass
70
- of the CRUDHash which defines a CRUD model on top of Ruby's standard Hash making
71
- it a snap to subclass and augment to fit any specific use case.
72
- created: '2010-04-21'
73
- date: '2013-08-21'
data/lib/hashery.yml DELETED
@@ -1,73 +0,0 @@
1
- ---
2
- revision: 2013
3
- type: ruby
4
- sources:
5
- - Index.yml
6
- - Gemfile
7
- authors:
8
- - name: Trans
9
- email: transfire@gmail.com
10
- - name: Kirk Haines
11
- - name: Robert Klemme
12
- - name: Jan Molic
13
- - name: George Moschovitis
14
- - name: Jeena Paradies
15
- - name: Erik Veenstra
16
- organizations:
17
- - name: RubyWorks (http://rubyworks.github.com/)
18
- requirements:
19
- - groups:
20
- - development
21
- - test
22
- version: '>= 0'
23
- name: qed
24
- - groups:
25
- - development
26
- - test
27
- version: '>= 0'
28
- name: lemon
29
- conflicts: []
30
- alternatives: []
31
- resources:
32
- - type: home
33
- uri: http://rubyworks.github.com/hashery
34
- label: Homepage
35
- - type: code
36
- uri: http://github.com/rubyworks/hashery
37
- label: Source Code
38
- - type: mail
39
- uri: http://groups.google.com/group/rubyworks-mailinglist
40
- label: Mailing List
41
- - type: docs
42
- uri: http://rubydoc.info/github/rubyworks/hashery/master/frames
43
- label: Documentation
44
- - type: wiki
45
- uri: http://wiki.github.com/rubyworks/hashery
46
- label: User Guide
47
- - type: gems
48
- uri: http://rubygems.org/gems/hashery
49
- repositories:
50
- - name: upstream
51
- scm: git
52
- uri: git://github.com/rubyworks/hashery.git
53
- categories: []
54
- copyrights:
55
- - holder: Rubyworks
56
- year: '2010'
57
- license: BSD-2-Clause
58
- customs: []
59
- paths:
60
- lib:
61
- - lib
62
- - alt
63
- name: hashery
64
- title: Hashery
65
- version: 2.1.1
66
- summary: Facets-bread collection of Hash-like classes.
67
- description: The Hashery is a tight collection of Hash-like classes. Included among
68
- its many offerings are the auto-sorting Dictionary class, the efficient LRUHash,
69
- the flexible OpenHash and the convenient KeyHash. Nearly every class is a subclass
70
- of the CRUDHash which defines a CRUD model on top of Ruby's standard Hash making
71
- it a snap to subclass and augment to fit any specific use case.
72
- created: '2010-04-21'
73
- date: '2013-08-21'