File: hash_spec.rb

package info (click to toggle)
ruby-immutable-ruby 0.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,852 kB
  • sloc: ruby: 16,556; makefile: 4
file content (29 lines) | stat: -rw-r--r-- 923 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
require 'spec_helper'

describe Immutable::Hash do
  describe '#hash' do
    it 'values are sufficiently distributed' do
      (1..4000).each_slice(4).map { |ka, va, kb, vb| H[ka => va, kb => vb].hash }.uniq.size.should == 1000
    end

    it 'differs given the same keys and different values' do
      H['ka' => 'va'].hash.should_not == H['ka' => 'vb'].hash
    end

    it 'differs given the same values and different keys' do
      H['ka' => 'va'].hash.should_not == H['kb' => 'va'].hash
    end

    it 'generates the same hash value for a hash regardless of the order things were added to it' do
      key1 = DeterministicHash.new('abc', 1)
      key2 = DeterministicHash.new('xyz', 1)
      H.empty.put(key1, nil).put(key2, nil).hash.should == H.empty.put(key2, nil).put(key1, nil).hash
    end

    describe 'on an empty hash' do
      it 'returns 0' do
        H.empty.hash.should == 0
      end
    end
  end
end