hashery 1.4.0 → 1.5.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.
- data/.ruby +57 -92
- data/.yardopts +8 -0
- data/COPYING.rdoc +45 -0
- data/HISTORY.rdoc +18 -0
- data/QED.rdoc +1 -0
- data/README.rdoc +42 -16
- data/lib/hashery.rb +16 -9
- data/lib/hashery.yml +57 -92
- data/lib/hashery/association.rb +3 -1
- data/lib/hashery/basic_object.rb +74 -0
- data/lib/hashery/basic_struct.rb +288 -1
- data/lib/hashery/basicobject.rb +1 -74
- data/lib/hashery/basicstruct.rb +1 -280
- data/lib/hashery/casting_hash.rb +171 -1
- data/lib/hashery/castinghash.rb +1 -171
- data/lib/hashery/core_ext.rb +82 -0
- data/lib/hashery/dictionary.rb +3 -0
- data/lib/hashery/fuzzy_hash.rb +154 -1
- data/lib/hashery/fuzzyhash.rb +1 -154
- data/lib/hashery/ini.rb +3 -2
- data/lib/hashery/key_hash.rb +186 -0
- data/lib/hashery/keyhash.rb +1 -0
- data/lib/hashery/linked_list.rb +195 -1
- data/lib/hashery/linkedlist.rb +1 -195
- data/lib/hashery/lru_hash.rb +273 -1
- data/lib/hashery/lruhash.rb +1 -273
- data/lib/hashery/open_cascade.rb +99 -1
- data/lib/hashery/open_hash.rb +77 -1
- data/lib/hashery/opencascade.rb +1 -99
- data/lib/hashery/openhash.rb +1 -77
- data/lib/hashery/ordered_hash.rb +168 -1
- data/lib/hashery/orderedhash.rb +1 -167
- data/lib/hashery/property_hash.rb +97 -1
- data/lib/hashery/propertyhash.rb +1 -97
- data/lib/hashery/query_hash.rb +35 -1
- data/lib/hashery/queryhash.rb +1 -35
- data/lib/hashery/stash.rb +3 -174
- data/lib/hashery/static_hash.rb +48 -1
- data/lib/hashery/statichash.rb +1 -48
- data/qed/06_opencascade.rdoc +12 -12
- data/test/case_association.rb +29 -15
- data/test/case_basicstruct.rb +192 -0
- data/test/case_dictionary.rb +149 -109
- data/test/case_keyhash.rb +175 -0
- data/test/case_opencascade.rb +89 -43
- data/test/case_openhash.rb +15 -11
- metadata +85 -78
- data/LICENSE +0 -206
- data/NOTICE +0 -11
- data/lib/hashery/sparse_array.rb +0 -1
- data/lib/hashery/sparsearray.rb +0 -577
- data/test/case_openobject.rb +0 -130
- data/test/case_sparsearray.rb +0 -316
- data/test/case_stash.rb +0 -131
data/lib/hashery/static_hash.rb
CHANGED
@@ -1 +1,48 @@
|
|
1
|
-
|
1
|
+
# = StaticHash
|
2
|
+
#
|
3
|
+
# A Hash object which raises an error if any
|
4
|
+
# previously-defined key attempts to be set again.
|
5
|
+
#
|
6
|
+
# == Synopsis
|
7
|
+
#
|
8
|
+
# foo = StaticHash.new
|
9
|
+
# foo['name'] = 'Tom' #=> 'Tom'
|
10
|
+
# foo['age'] = 30 #=> 30
|
11
|
+
# foo['name'] = 'Bob'
|
12
|
+
#
|
13
|
+
# _produces_
|
14
|
+
#
|
15
|
+
# ArgumentError: Duplicate key for StaticHash -- 'name'
|
16
|
+
#
|
17
|
+
# == Credit
|
18
|
+
#
|
19
|
+
# StaticHash has it's orgins in Gavin Kistner's WriteOnceHash
|
20
|
+
# class found in his +basiclibrary.rb+ script.
|
21
|
+
|
22
|
+
class StaticHash < Hash
|
23
|
+
|
24
|
+
# Set a value for a key. Raises an error if that key already
|
25
|
+
# exists with a different value.
|
26
|
+
|
27
|
+
def []=(key, value)
|
28
|
+
if key?(key) && self[key] != value
|
29
|
+
raise ArgumentError, "Duplicate key for StaticHash -- #{key.inspect}"
|
30
|
+
end
|
31
|
+
super(key, value)
|
32
|
+
end
|
33
|
+
|
34
|
+
#
|
35
|
+
def update(hash)
|
36
|
+
dups = (keys | hash.keys)
|
37
|
+
if dups.empty?
|
38
|
+
super(hash)
|
39
|
+
else
|
40
|
+
raise ArgumentError, "Duplicate key for StaticHash -- #{dups.inspect}"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
#
|
45
|
+
alias_method :merge!, :update
|
46
|
+
|
47
|
+
end
|
48
|
+
|
data/lib/hashery/statichash.rb
CHANGED
@@ -1,48 +1 @@
|
|
1
|
-
|
2
|
-
#
|
3
|
-
# A Hash object which raises an error if any
|
4
|
-
# previously-defined key attempts to be set again.
|
5
|
-
#
|
6
|
-
# == Synopsis
|
7
|
-
#
|
8
|
-
# foo = Hash::Static.new
|
9
|
-
# foo['name'] = 'Tom' #=> 'Tom'
|
10
|
-
# foo['age'] = 30 #=> 30
|
11
|
-
# foo['name'] = 'Bob'
|
12
|
-
#
|
13
|
-
# _produces_
|
14
|
-
#
|
15
|
-
# ArgumentError: Duplicate key for StaticHash -- 'name'
|
16
|
-
#
|
17
|
-
# == Credit
|
18
|
-
#
|
19
|
-
# StaticHash has it's orgins in Gavin Kistner's WriteOnceHash
|
20
|
-
# class found in his +basiclibrary.rb+ script.
|
21
|
-
|
22
|
-
class StaticHash < Hash
|
23
|
-
|
24
|
-
# Set a value for a key. Raises an error if that key already
|
25
|
-
# exists with a different value.
|
26
|
-
|
27
|
-
def []=(key, value)
|
28
|
-
if key?(key) && self[key] != value
|
29
|
-
raise ArgumentError, "Duplicate key for StaticHash -- #{key.inspect}"
|
30
|
-
end
|
31
|
-
super(key, value)
|
32
|
-
end
|
33
|
-
|
34
|
-
#
|
35
|
-
def update(hash)
|
36
|
-
dups = (keys | hash.keys)
|
37
|
-
if dups.empty?
|
38
|
-
super(hash)
|
39
|
-
else
|
40
|
-
raise ArgumentError, "Duplicate key for StaticHash -- #{dups.inspect}"
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
#
|
45
|
-
alias_method :merge!, :update
|
46
|
-
|
47
|
-
end
|
48
|
-
|
1
|
+
require 'hashery/static_hash'
|
data/qed/06_opencascade.rdoc
CHANGED
@@ -5,36 +5,36 @@ significant ways.
|
|
5
5
|
|
6
6
|
require 'hashery/opencascade'
|
7
7
|
|
8
|
-
The
|
8
|
+
The reason this class is labeled "cascade", is that every internal
|
9
9
|
Hash is transformed into an OpenCascade dynamically upon access.
|
10
|
-
This makes it easy to create
|
10
|
+
This makes it easy to create _cascading_ references.
|
11
11
|
|
12
12
|
h = { :x => { :y => { :z => 1 } } }
|
13
13
|
c = OpenCascade[h]
|
14
|
-
c.x.y.z
|
14
|
+
assert c.x.y.z == 1
|
15
15
|
|
16
16
|
As soon as you access a node it automatically becomes an OpenCascade.
|
17
17
|
|
18
18
|
c = OpenCascade.new
|
19
|
-
c.r.
|
20
|
-
c.a.b.
|
19
|
+
assert c.r.is_a?(OpenCascade)
|
20
|
+
assert c.a.b.is_a?(OpenCascade)
|
21
21
|
|
22
22
|
But if you set a node, then that will be it's value.
|
23
23
|
|
24
24
|
c.a.b = 4
|
25
|
-
c.a.b
|
25
|
+
assert c.a.b == 4
|
26
26
|
|
27
27
|
To query a node without causing the auto-creation of an OpenCasade
|
28
28
|
object, use the ?-mark.
|
29
29
|
|
30
|
-
c.a.z
|
30
|
+
assert c.a.z? == nil
|
31
31
|
|
32
32
|
OpenCascade also transforms Hashes within Arrays.
|
33
33
|
|
34
34
|
h = { :x=>[ {:a=>1}, {:a=>2} ], :y=>1 }
|
35
35
|
c = OpenCascade[h]
|
36
|
-
c.x.first.a
|
37
|
-
c.x.last.a
|
36
|
+
assert c.x.first.a == 1
|
37
|
+
assert c.x.last.a == 2
|
38
38
|
|
39
39
|
Like OpenObject, OpenCascade allows you to insert entries as array
|
40
40
|
pairs.
|
@@ -43,15 +43,15 @@ pairs.
|
|
43
43
|
c << [:x,8]
|
44
44
|
c << [:y,9]
|
45
45
|
|
46
|
-
c.x
|
47
|
-
c.y
|
46
|
+
assert c.x == 8
|
47
|
+
assert c.y == 9
|
48
48
|
|
49
49
|
Finally, you can call methods ending in a !-mark to access the
|
50
50
|
underlying hash (Note that these differ in behavior from the
|
51
51
|
built-in !-methods).
|
52
52
|
|
53
53
|
bk = c.map!{ |k,v| k.to_s.upcase }
|
54
|
-
bk.sort.assert ==
|
54
|
+
bk.sort.assert == ['X', 'Y']
|
55
55
|
|
56
56
|
So you can see that for the most an OpenCascade is just like
|
57
57
|
OpenObject, but it allows you to conveniently build open sub-layers.
|
data/test/case_association.rb
CHANGED
@@ -1,27 +1,41 @@
|
|
1
|
+
require 'lemon'
|
2
|
+
require 'ae'
|
3
|
+
|
1
4
|
require 'hashery/association'
|
2
5
|
|
3
|
-
|
6
|
+
testcase Association do
|
4
7
|
|
5
|
-
|
6
|
-
|
8
|
+
class_method :new do
|
9
|
+
test do
|
10
|
+
Association.new(:A, :B)
|
11
|
+
end
|
7
12
|
end
|
8
13
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
14
|
+
method :to_ary do
|
15
|
+
test do
|
16
|
+
k,v = [],[]
|
17
|
+
ohash = [ 'A' >> '3', 'B' >> '2', 'C' >> '1' ]
|
18
|
+
ohash.each { |e1,e2| k << e1 ; v << e2 }
|
19
|
+
k.assert == ['A','B','C']
|
20
|
+
v.assert == ['3','2','1']
|
21
|
+
end
|
15
22
|
end
|
16
23
|
|
17
|
-
|
18
|
-
|
19
|
-
|
24
|
+
method :index do
|
25
|
+
test do
|
26
|
+
complex = [ 'Drop Menu' >> [ 'Button 1', 'Button 2', 'Button 3' ], 'Help' ]
|
27
|
+
complex[0].index.assert == 'Drop Menu'
|
28
|
+
end
|
20
29
|
end
|
21
30
|
|
22
|
-
|
23
|
-
|
24
|
-
|
31
|
+
end
|
32
|
+
|
33
|
+
testcase Object do
|
34
|
+
method :associations do
|
35
|
+
test do
|
36
|
+
complex = [ :a >> :b, :a >> :c ]
|
37
|
+
:a.associations.assert == [ :b, :c ]
|
38
|
+
end
|
25
39
|
end
|
26
40
|
|
27
41
|
end
|
@@ -0,0 +1,192 @@
|
|
1
|
+
require 'lemon'
|
2
|
+
require 'ae'
|
3
|
+
require 'ae/legacy'
|
4
|
+
|
5
|
+
require 'hashery/basic_struct'
|
6
|
+
|
7
|
+
testcase BasicStruct do
|
8
|
+
include AE::Legacy::Assertions
|
9
|
+
|
10
|
+
method :respond_to? do
|
11
|
+
test do
|
12
|
+
o = BasicStruct.new
|
13
|
+
t = o.respond_to?(:key?)
|
14
|
+
assert t
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
method :is_a? do
|
19
|
+
test do
|
20
|
+
assert BasicStruct[{}].is_a?(Hash)
|
21
|
+
assert BasicStruct[{}].is_a?(BasicStruct)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
method :[] do
|
26
|
+
test "subhash access" do
|
27
|
+
o = BasicStruct[:a=>1,:b=>{:x=>9}]
|
28
|
+
assert( o[:b][:x] == 9 )
|
29
|
+
assert( o.b[:x] == 9 )
|
30
|
+
end
|
31
|
+
|
32
|
+
test "indifferent key access" do
|
33
|
+
o = BasicStruct["a"=>1,"b"=>{:x=>9}]
|
34
|
+
assert( o["a"] == 1 )
|
35
|
+
assert( o[:a] == 1 )
|
36
|
+
assert( o["b"] == {:x=>9} )
|
37
|
+
assert( o[:b] == {:x=>9} )
|
38
|
+
assert( o["b"][:x] == 9 )
|
39
|
+
assert( o[:b]["x"] == nil )
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
method :[]= do
|
44
|
+
test "setting first entry" do
|
45
|
+
f0 = BasicStruct.new
|
46
|
+
f0[:a] = 1
|
47
|
+
assert( f0.to_h == {:a=>1} )
|
48
|
+
end
|
49
|
+
|
50
|
+
test "setting an additional entry" do
|
51
|
+
f0 = BasicStruct[:a=>1]
|
52
|
+
f0[:b] = 2
|
53
|
+
assert( f0.to_h == {:a=>1,:b=>2} )
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
method :method_missing do
|
58
|
+
test "reading entries" do
|
59
|
+
f0 = BasicStruct[:class=>1]
|
60
|
+
assert( f0.class == 1 )
|
61
|
+
end
|
62
|
+
|
63
|
+
test "setting entries" do
|
64
|
+
fo = BasicStruct.new
|
65
|
+
9.times{ |i| fo.__send__("n#{i}=", 1) }
|
66
|
+
9.times{ |i|
|
67
|
+
assert( fo.__send__("n#{i}") == 1 )
|
68
|
+
}
|
69
|
+
end
|
70
|
+
|
71
|
+
test "using bang" do
|
72
|
+
o = BasicStruct.new
|
73
|
+
o.a = 10
|
74
|
+
o.b = 20
|
75
|
+
h = {}
|
76
|
+
o.each!{ |k,v| h[k] = v + 10 }
|
77
|
+
assert( h == {:a=>20, :b=>30} )
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
#method :as_hash do
|
82
|
+
# test do
|
83
|
+
# f0 = BasicStruct[:f0=>"f0"]
|
84
|
+
# h0 = { :h0=>"h0" }
|
85
|
+
# assert( BasicStruct[:f0=>"f0", :h0=>"h0"] == f0.as_hash.merge(h0) )
|
86
|
+
# assert( {:f0=>"f0", :h0=>"h0"} == h0.merge(f0) )
|
87
|
+
# end
|
88
|
+
#end
|
89
|
+
|
90
|
+
method :as_hash do
|
91
|
+
test do
|
92
|
+
f1 = BasicStruct[:f1=>"f1"]
|
93
|
+
h1 = { :h1=>"h1" }
|
94
|
+
f1.as_hash.update(h1)
|
95
|
+
assert( f1 == BasicStruct[:f1=>"f1", :h1=>"h1"] )
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
#method :as_hash do
|
100
|
+
# test do
|
101
|
+
# f1 = BasicStruct[:f1=>"f1"]
|
102
|
+
# h1 = { :h1=>"h1" }
|
103
|
+
# f1.as_hash.update(h1)
|
104
|
+
# h1.update(f1)
|
105
|
+
# assert( f1 == BasicStruct[:f1=>"f1", :h1=>"h1"] )
|
106
|
+
# assert( h1 == {:f1=>"f1", :h1=>"h1"} )
|
107
|
+
# end
|
108
|
+
#end
|
109
|
+
|
110
|
+
method :<< do
|
111
|
+
test "passing a hash" do
|
112
|
+
fo = BasicStruct.new
|
113
|
+
fo << {:a=>1,:b=>2}
|
114
|
+
assert( fo.to_h == {:a=>1, :b=>2} )
|
115
|
+
end
|
116
|
+
|
117
|
+
test "passing a pair" do
|
118
|
+
fo = BasicStruct.new
|
119
|
+
fo << [:a, 1]
|
120
|
+
fo << [:b, 2]
|
121
|
+
assert( fo.to_h == {:a=>1, :b=>2} )
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
method :to_h do
|
126
|
+
test do
|
127
|
+
ho = {}
|
128
|
+
fo = BasicStruct.new
|
129
|
+
5.times{ |i| ho["n#{i}".to_sym] = 1 }
|
130
|
+
5.times{ |i| fo.__send__("n#{i}=", 1) }
|
131
|
+
assert( fo.to_h == ho )
|
132
|
+
end
|
133
|
+
test "BasicStruct within BasicStruct" do
|
134
|
+
o = BasicStruct.new
|
135
|
+
o.a = 10
|
136
|
+
o.b = 20
|
137
|
+
o.x = BasicStruct.new
|
138
|
+
o.x.a = 100
|
139
|
+
o.x.b = 200
|
140
|
+
o.x.c = 300
|
141
|
+
assert( o.to_h == {:a=>10, :b=>20, :x=>{:a=>100, :b=>200, :c=>300}} )
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
method :to_proc do
|
146
|
+
test do
|
147
|
+
#p = Proc.new{ |x| x.word = "Hello" }
|
148
|
+
o = BasicStruct[:a=>1,:b=>2]
|
149
|
+
assert( Proc === o.to_proc )
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
end
|
154
|
+
|
155
|
+
TestCase Hash do
|
156
|
+
|
157
|
+
method :to_basicstruct do
|
158
|
+
test do
|
159
|
+
h = {'a'=>1, 'b'=>2}
|
160
|
+
o = h.to_basicstruct
|
161
|
+
assert( o.a == 1 )
|
162
|
+
assert( o.b == 2 )
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
method :update do
|
167
|
+
test "by BasicStruct" do
|
168
|
+
raise NotImplementedError, "Ruby 1.8 does not know #to_hash."
|
169
|
+
|
170
|
+
h1 = { :h1=>"h1" }
|
171
|
+
f1 = BasicStruct[:f1=>"f1"]
|
172
|
+
h1.update(f1)
|
173
|
+
assert( h1 == {:f1=>"f1", :h1=>"h1"} )
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
end
|
178
|
+
|
179
|
+
=begin
|
180
|
+
TestCase Proc do
|
181
|
+
|
182
|
+
method :to_basicstruct do
|
183
|
+
test do
|
184
|
+
p = lambda { |x| x.word = "Hello" }
|
185
|
+
o = p.to_basicstruct
|
186
|
+
assert( o.word == "Hello" )
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
end
|
191
|
+
=end
|
192
|
+
|
data/test/case_dictionary.rb
CHANGED
@@ -1,141 +1,181 @@
|
|
1
|
-
require '
|
1
|
+
require 'lemon'
|
2
|
+
require 'ae'
|
2
3
|
require 'ae/legacy'
|
3
4
|
|
4
|
-
|
5
|
+
require 'hashery/dictionary'
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
7
|
+
testcase Dictionary do
|
8
|
+
include AE::Legacy::Assertions
|
9
|
+
|
10
|
+
class_method :[] do
|
11
|
+
test do
|
12
|
+
d = Dictionary['z', 1, 'a', 2, 'c', 3]
|
13
|
+
assert_equal( ['z','a','c'], d.keys )
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class_method :new do
|
18
|
+
test "with default" do
|
19
|
+
d = Dictionary.new{ |hash,key| hash[key] = 0 }
|
20
|
+
d[:a] = 0
|
21
|
+
d[:b] += 1
|
22
|
+
assert_equal [0, 1], d.values
|
23
|
+
assert_equal [:a,:b], d.keys
|
24
|
+
end
|
9
25
|
end
|
10
26
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
assert_equal( ['z','a','c'], d.keys )
|
27
|
+
method :[] do
|
28
|
+
test do
|
29
|
+
d = Dictionary['a', 1]
|
30
|
+
d['a'].assert == 1
|
31
|
+
end
|
17
32
|
end
|
18
33
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
assert_equal( ["begin", "a", "c", "z", "end"], d.keys )
|
28
|
-
assert_equal( ["end", 15], d.pop )
|
29
|
-
assert_equal( ["begin", "a", "c", "z"], d.keys )
|
30
|
-
assert_equal( ["begin", 50], d.shift )
|
34
|
+
method :[]= do
|
35
|
+
test do
|
36
|
+
d = Dictionary.new
|
37
|
+
d['z'] = 1
|
38
|
+
d['a'] = 2
|
39
|
+
d['c'] = 3
|
40
|
+
assert_equal( ['z','a','c'], d.keys )
|
41
|
+
end
|
31
42
|
end
|
32
43
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
assert_equal( r, d )
|
44
|
+
method :[]= do
|
45
|
+
test do
|
46
|
+
d = Dictionary[]
|
47
|
+
d[:a] = 1
|
48
|
+
d[:c] = 3
|
49
|
+
assert_equal( [1,3], d.values )
|
50
|
+
d[:b,1] = 2
|
51
|
+
assert_equal( [1,2,3], d.values )
|
52
|
+
assert_equal( [:a,:b,:c], d.keys )
|
53
|
+
end
|
44
54
|
end
|
45
55
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
56
|
+
method :push do
|
57
|
+
test do
|
58
|
+
d = Dictionary['a', 1, 'c', 2, 'z', 3]
|
59
|
+
assert( d.push('end', 15) )
|
60
|
+
assert_equal( 15, d['end'] )
|
61
|
+
assert( ! d.push('end', 30) )
|
62
|
+
assert( d.unshift('begin', 50) )
|
63
|
+
assert_equal( 50, d['begin'] )
|
64
|
+
assert( ! d.unshift('begin', 60) )
|
65
|
+
assert_equal( ["begin", "a", "c", "z", "end"], d.keys )
|
66
|
+
assert_equal( ["end", 15], d.pop )
|
67
|
+
assert_equal( ["begin", "a", "c", "z"], d.keys )
|
68
|
+
assert_equal( ["begin", 50], d.shift )
|
69
|
+
end
|
59
70
|
end
|
60
71
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
+
method :insert do
|
73
|
+
test "front" do
|
74
|
+
d = Dictionary['a', 1, 'b', 2, 'c', 3]
|
75
|
+
r = Dictionary['d', 4, 'a', 1, 'b', 2, 'c', 3]
|
76
|
+
assert_equal( 4, d.insert(0,'d',4) )
|
77
|
+
assert_equal( r, d )
|
78
|
+
end
|
79
|
+
test "back" do
|
80
|
+
d = Dictionary['a', 1, 'b', 2, 'c', 3]
|
81
|
+
r = Dictionary['a', 1, 'b', 2, 'c', 3, 'd', 4]
|
82
|
+
assert_equal( 4, d.insert(-1,'d',4) )
|
83
|
+
assert_equal( r, d )
|
84
|
+
end
|
72
85
|
end
|
73
86
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
87
|
+
method :update do
|
88
|
+
test "with other orderred hash" do
|
89
|
+
d = Dictionary['a', 1, 'b', 2, 'c', 3]
|
90
|
+
c = Dictionary['d', 4]
|
91
|
+
r = Dictionary['a', 1, 'b', 2, 'c', 3, 'd', 4]
|
92
|
+
assert_equal( r, d.update(c) )
|
93
|
+
assert_equal( r, d )
|
94
|
+
end
|
95
|
+
test "with other hash" do
|
96
|
+
d = Dictionary['a', 1, 'b', 2, 'c', 3]
|
97
|
+
c = { 'd' => 4 }
|
98
|
+
r = Dictionary['a', 1, 'b', 2, 'c', 3, 'd', 4]
|
99
|
+
assert_equal( r, d.update(c) )
|
100
|
+
assert_equal( r, d )
|
101
|
+
end
|
79
102
|
end
|
80
103
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
104
|
+
method :merge do
|
105
|
+
test "with other orderred hash" do
|
106
|
+
d = Dictionary['a', 1, 'b', 2, 'c', 3]
|
107
|
+
c = Dictionary['d', 4]
|
108
|
+
r = Dictionary['a', 1, 'b', 2, 'c', 3, 'd', 4]
|
109
|
+
assert_equal( r, d.merge(c) )
|
110
|
+
end
|
111
|
+
test "with other hash" do
|
112
|
+
d = Dictionary['a', 1, 'b', 2, 'c', 3]
|
113
|
+
c = { 'd' => 4 }
|
114
|
+
r = Dictionary['a', 1, 'b', 2, 'c', 3, 'd', 4]
|
115
|
+
assert_equal( r, d.merge(c) )
|
116
|
+
end
|
89
117
|
end
|
90
118
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
119
|
+
method :order_by do
|
120
|
+
test do
|
121
|
+
d = Dictionary['a', 3, 'b', 2, 'c', 1]
|
122
|
+
d.order_by{ |k,v| v }
|
123
|
+
assert_equal( [1,2,3], d.values )
|
124
|
+
assert_equal( ['c','b','a'], d.keys )
|
125
|
+
end
|
95
126
|
end
|
96
127
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
128
|
+
method :reverse! do
|
129
|
+
test do
|
130
|
+
d = Dictionary['z', 1, 'a', 2, 'c', 3]
|
131
|
+
d.reverse!
|
132
|
+
assert_equal( ['c','a','z'], d.keys )
|
133
|
+
end
|
102
134
|
end
|
103
135
|
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
136
|
+
method :collect do
|
137
|
+
test "enumerable method" do
|
138
|
+
d = Dictionary[]
|
139
|
+
d[:a] = "a"
|
140
|
+
d[:c] = "b"
|
141
|
+
r = d.collect{|k,v| v.capitalize}
|
142
|
+
r.assert == ["A","B"]
|
143
|
+
end
|
110
144
|
end
|
111
145
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
146
|
+
method :dup do
|
147
|
+
test "with array values" do
|
148
|
+
d = Dictionary.new
|
149
|
+
d.dup
|
150
|
+
d[:a]=['t',5]
|
151
|
+
assert_equal(d, d.dup)
|
152
|
+
end
|
117
153
|
end
|
118
154
|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
155
|
+
method :first do
|
156
|
+
test do
|
157
|
+
d = Dictionary[]
|
158
|
+
d[:a] = "a"
|
159
|
+
d[:b] = "b"
|
160
|
+
d[:c] = "c"
|
161
|
+
d.first.assert == "a"
|
162
|
+
d.first(0).assert == []
|
163
|
+
assert_equal ["a"] , d.first(1)
|
164
|
+
assert_equal ["a", "b"] , d.first(2)
|
165
|
+
end
|
128
166
|
end
|
129
167
|
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
168
|
+
method :last do
|
169
|
+
test do
|
170
|
+
d = Dictionary[]
|
171
|
+
d[:a] = "a"
|
172
|
+
d[:b] = "b"
|
173
|
+
d[:c] = "c"
|
174
|
+
d.last.assert == "c"
|
175
|
+
d.last(0).assert == []
|
176
|
+
d.last(1).assert == ["c"]
|
177
|
+
d.last(2).assert == ["b", "c"]
|
178
|
+
end
|
139
179
|
end
|
140
180
|
|
141
181
|
end
|