hamster 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -124,7 +124,7 @@ module Hamster
124
124
  def_delegator :self, :eql?, :==
125
125
 
126
126
  def hash
127
- reduce(0) { |h, key, value| h ^ key.hash ^ value.hash }
127
+ reduce(0) { |hash, key, value| (hash << 32) - hash + key.hash + value.hash }
128
128
  end
129
129
 
130
130
  def_delegator :self, :dup, :uniq
@@ -450,7 +450,7 @@ module Hamster
450
450
  def_delegator :self, :eql?, :==
451
451
 
452
452
  def hash
453
- reduce(0) { |h, item| h ^ item.hash }
453
+ reduce(0) { |hash, item| (hash << 5) - hash + item.hash }
454
454
  end
455
455
 
456
456
  def dup
@@ -249,7 +249,7 @@ module Hamster
249
249
  def_delegator :self, :eql?, :==
250
250
 
251
251
  def hash
252
- reduce(0) { |h, item| h ^ item.hash }
252
+ reduce(0) { |hash, item| (hash << 5) - hash + item.hash }
253
253
  end
254
254
 
255
255
  def_delegator :self, :dup, :uniq
@@ -1,5 +1,5 @@
1
1
  module Hamster
2
2
 
3
- VERSION = "0.3.2".freeze
3
+ VERSION = "0.3.3".freeze
4
4
 
5
5
  end
@@ -6,37 +6,30 @@ describe Hamster::Hash do
6
6
 
7
7
  describe "#hash" do
8
8
 
9
- describe "on an empty hash" do
10
-
11
- before do
12
- @result = Hamster.hash.hash
13
- end
14
-
15
- it "returns 0" do
16
- @result.should == 0
17
- end
18
-
9
+ it "values are sufficiently distributed" do
10
+ (1..4000)
11
+ .each_slice(4)
12
+ .map { |ka, va, kb, vb| Hamster.hash(ka => va, kb => vb).hash }
13
+ .uniq
14
+ .size.should == 1000
19
15
  end
20
16
 
21
- describe "on a non-empty hash" do
22
-
23
- class Item
24
-
25
- attr_reader :hash
17
+ it "differs given the same keys and different values" do
18
+ Hamster.hash("ka" => "va").hash.should_not == Hamster.hash("ka" => "vb").hash
19
+ end
26
20
 
27
- def initialize(h)
28
- @hash = h
29
- end
21
+ it "differs given the same values and different keys" do
22
+ Hamster.hash("ka" => "va").hash.should_not == Hamster.hash("kb" => "va").hash
23
+ end
30
24
 
31
- end
25
+ describe "on an empty hash" do
32
26
 
33
27
  before do
34
- hash = Hamster.hash(Item.new(19) => Item.new(100), Item.new(31) => Item.new(78942), Item.new(107) => Item.new(309))
35
- @result = hash.hash
28
+ @result = Hamster.hash.hash
36
29
  end
37
30
 
38
- it "returns XOR of each key/value pair hash" do
39
- @result.should == 79208
31
+ it "returns 0" do
32
+ @result.should == 0
40
33
  end
41
34
 
42
35
  end
@@ -30,27 +30,12 @@ describe Hamster::List do
30
30
 
31
31
  end
32
32
 
33
- describe "on a non-empty list" do
34
-
35
- class Item
36
-
37
- attr_reader :hash
38
-
39
- def initialize(h)
40
- @hash = h
41
- end
42
-
43
- end
44
-
45
- before do
46
- list = Hamster.list(Item.new(19), Item.new(31), Item.new(107))
47
- @result = list.hash
48
- end
49
-
50
- it "returns XOR of each item's hash" do
51
- @result.should == 103
52
- end
53
-
33
+ it "values are sufficiently distributed" do
34
+ (1..4000)
35
+ .each_slice(4)
36
+ .map { |a, b, c, d| Hamster.list(a, b, c, d).hash }
37
+ .uniq
38
+ .size.should == 1000
54
39
  end
55
40
 
56
41
  end
@@ -18,27 +18,12 @@ describe Hamster::Set do
18
18
 
19
19
  end
20
20
 
21
- describe "on a non-empty set" do
22
-
23
- class Item
24
-
25
- attr_reader :hash
26
-
27
- def initialize(h)
28
- @hash = h
29
- end
30
-
31
- end
32
-
33
- before do
34
- set = Hamster.set(Item.new(19), Item.new(31), Item.new(107))
35
- @result = set.hash
36
- end
37
-
38
- it "returns XOR of each item's hash" do
39
- @result.should == 103
40
- end
41
-
21
+ it "values are sufficiently distributed" do
22
+ (1..4000)
23
+ .each_slice(4)
24
+ .map { |a, b, c, d| Hamster.set(a, b, c, d).hash }
25
+ .uniq
26
+ .size.should == 1000
42
27
  end
43
28
 
44
29
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 2
9
- version: 0.3.2
8
+ - 3
9
+ version: 0.3.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Simon Harris