File: hash_examples.rb

package info (click to toggle)
ruby-multimap 1.1.2%2Bgh-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 228 kB
  • ctags: 96
  • sloc: ruby: 1,464; ansic: 18; makefile: 2
file content (264 lines) | stat: -rw-r--r-- 7,956 bytes parent folder | download | duplicates (2)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
shared_examples "Hash Multimap with inital values {'a' => [100], 'b' => [200, 300]}" do
  before do
    @container ||= Array
  end

  it "should be equal to another Multimap if they contain the same keys and values" do
    map2 = Multimap.new(@container.new)
    map2["a"] = 100
    map2["b"] = 200
    map2["b"] = 300
    @map.should eql(map2)
  end

  it "should not be equal to another Multimap if they contain different values" do
    @map.should_not == Multimap["a" => [100], "b" => [200]]
  end

  it "should retrieve container of values for key" do
    @map["a"].should eql(@container.new([100]))
    @map["b"].should eql(@container.new([200, 300]))
    @map["z"].should eql(@container.new)
  end

  it "should append values to container at key" do
    @map["a"] = 400
    @map.store("b", 500)
    @map["a"].should eql(@container.new([100, 400]))
    @map["b"].should eql(@container.new([200, 300, 500]))
  end

  it "should clear all key/values" do
    @map.clear
    @map.should be_empty
  end

  it "should be the class of the container" do
    @map.default.class.should eql(@container)
  end

  it "should delete all values at key" do
    @map.delete("a")
    @map["a"].should eql(@container.new)
  end

  it "should delete single value at key" do
    @map.delete("b", 200)
    @map["b"].should eql(@container.new([300]))
  end

  it "should delete if key condition is matched" do
    @map.delete_if { |key, value| key >= "b" }.should eql(@map)
    @map["a"].should eql(@container.new([100]))
    @map["b"].should eql(@container.new)

    @map.delete_if { |key, value| key > "z" }.should eql(@map)
  end

  it "should delete if value condition is matched" do
    @map.delete_if { |key, value| value >= 300 }.should eql(@map)
    @map["a"].should eql(@container.new([100]))
    @map["b"].should eql(@container.new([200]))
  end

  it "should duplicate the containers" do
    map2 = @map.dup
    map2.should_not equal(@map)
    map2.should eql(@map)
    map2["a"].should_not equal(@map["a"])
    map2["b"].should_not equal(@map["b"])
    map2.default.should_not equal(@map.default)
    map2.default.should eql(@map.default)
  end

  it "should freeze containers" do
    @map.freeze
    @map.should be_frozen
    @map["a"].should be_frozen
    @map["b"].should be_frozen
  end

  it "should iterate over each key/value pair and yield an array" do
    a = []
    @map.each { |pair| a << pair }
    a.should sorted_eql([["a", 100], ["b", 200], ["b", 300]])
  end

  it "should iterate over each container" do
    a = []
    @map.each_container { |container| a << container }
    a.should sorted_eql([@container.new([100]), @container.new([200, 300])])
  end

  it "should iterate over each key/container" do
    a = []
    @map.each_association { |key, container| a << [key, container] }
    a.should sorted_eql([["a", @container.new([100])], ["b", @container.new([200, 300])]])
  end

  it "should iterate over each key" do
    a = []
    @map.each_key { |key| a << key }
    a.should sorted_eql(["a", "b", "b"])
  end

  it "should iterate over each key/value pair and yield the pair" do
    h = {}
    @map.each_pair { |key, value| (h[key] ||= []) << value }
    h.should eql({ "a" => [100], "b" => [200, 300] })
  end

  it "should iterate over each value" do
    a = []
    @map.each_value { |value| a << value }
    a.should sorted_eql([100, 200, 300])
  end

  it "should be empty if there are no key/value pairs" do
    @map.clear
    @map.should be_empty
  end

  it "should not be empty if there are any key/value pairs" do
    @map.should_not be_empty
  end

  it "should fetch container of values for key" do
    @map.fetch("a").should eql(@container.new([100]))
    @map.fetch("b").should eql(@container.new([200, 300]))
    lambda { @map.fetch("z") }.should raise_error(IndexError)
  end

  it "should check if key is present" do
    @map.has_key?("a").should be_true
    @map.key?("a").should be_true
    @map.has_key?("z").should be_false
    @map.key?("z").should be_false
  end

  it "should check containers when looking up by value" do
    @map.has_value?(100).should be_true
    @map.value?(100).should be_true
    @map.has_value?(999).should be_false
    @map.value?(999).should be_false
  end

  it "it should return the index for value" do
    if @map.respond_to?(:index)
      @map.index(200).should eql(@container.new(["b"]))
      @map.index(999).should eql(@container.new)
    end
  end

  it "should replace the contents of hash" do
    @map.replace({ "c" => @container.new([300]), "d" => @container.new([400]) })
    @map["a"].should eql(@container.new)
    @map["c"].should eql(@container.new([300]))
  end

  it "should return an inverted Multimap" do
    if @map.respond_to?(:invert)
      map2 = Multimap.new(@container.new)
      map2[100] = "a"
      map2[200] = "b"
      map2[300] = "b"
      @map.invert.should eql(map2)
    end
  end

  it "should return array of keys" do
    @map.keys.should eql(["a", "b", "b"])
  end

  it "should return the number of key/value pairs" do
    @map.length.should eql(3)
    @map.size.should eql(3)
  end

  it "should duplicate map and with merged values" do
    map = @map.merge("b" => 254, "c" => @container.new([300]))
    map["a"].should eql(@container.new([100]))
    map["b"].should eql(@container.new([200, 300, 254]))
    map["c"].should eql(@container.new([300]))

    @map["a"].should eql(@container.new([100]))
    @map["b"].should eql(@container.new([200, 300]))
    @map["c"].should eql(@container.new)
  end

  it "should update map" do
    @map.update("b" => 254, "c" => @container.new([300]))
    @map["a"].should eql(@container.new([100]))
    @map["b"].should eql(@container.new([200, 300, 254]))
    @map["c"].should eql(@container.new([300]))

    klass = @map.class
    @map.update(klass[@container.new, {"a" => @container.new([400, 500]), "c" => 600}])
    @map["a"].should eql(@container.new([100, 400, 500]))
    @map["b"].should eql(@container.new([200, 300, 254]))
    @map["c"].should eql(@container.new([300, 600]))
  end

  it "should reject key pairs on copy of the map" do
    map = @map.reject { |key, value| key >= "b" }
    map["b"].should eql(@container.new)
    @map["b"].should eql(@container.new([200, 300]))
  end

  it "should reject value pairs on copy of the map" do
    map = @map.reject { |key, value| value >= 300 }
    map["b"].should eql(@container.new([200]))
    @map["b"].should eql(@container.new([200, 300]))
  end

  it "should reject key pairs" do
    @map.reject! { |key, value| key >= "b" }.should eql(@map)
    @map["a"].should eql(@container.new([100]))
    @map["b"].should eql(@container.new)

    @map.reject! { |key, value| key >= "z" }.should eql(nil)
  end

  it "should reject value pairs" do
    @map.reject! { |key, value| value >= 300 }.should eql(@map)
    @map["a"].should eql(@container.new([100]))
    @map["b"].should eql(@container.new([200]))

    @map.reject! { |key, value| key >= "z" }.should eql(nil)
  end

  it "should select key/value pairs" do
    @map.select { |k, v| k > "a" }.should eql(Multimap["b", [200, 300]])
    @map.select { |k, v| v < 200 }.should eql(Multimap["a", 100])
  end

  it "should convert to hash" do
    @map.to_hash["a"].should eql(@container.new([100]))
    @map.to_hash["b"].should eql(@container.new([200, 300]))
    @map.to_hash.should_not equal(@map)
  end

  it "should return all containers" do
    @map.containers.should sorted_eql([@container.new([100]), @container.new([200, 300])])
  end

  it "should return all values" do
    @map.values.should sorted_eql([100, 200, 300])
  end

  it "should return return values at keys" do
    @map.values_at("a", "b").should eql([@container.new([100]), @container.new([200, 300])])
  end

  it "should marshal hash" do
    data = Marshal.dump(@map)
    Marshal.load(data).should eql(@map)
  end

  it "should dump yaml" do
    require 'yaml'

    data = YAML.dump(@map)
    YAML.load(data).should eql(@map)
  end
end