File: size_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 (51 lines) | stat: -rw-r--r-- 1,382 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
require 'spec_helper'

describe Immutable::Hash do
  [:size, :length].each do |method|
    describe "##{method}" do
      [
        [[], 0],
        [['A' => 'aye'], 1],
        [['A' => 'bee', 'B' => 'bee', 'C' => 'see'], 3],
      ].each do |values, result|

        it "returns #{result} for #{values.inspect}" do
          H[*values].send(method).should == result
        end
      end

      lots = (1..10_842).to_a
      srand 89_533_474
      random_things = (lots + lots).sort_by { |x|rand }

      it 'has the correct size after adding lots of things with colliding keys and such' do
        h = H.empty
        random_things.each do |thing|
          h = h.put(thing, thing * 2)
        end
        h.size.should == 10_842
      end

      random_actions = (lots.map { |x|[:add, x] } + lots.map { |x|[:add, x] } + lots.map { |x|[:remove, x] }).sort_by { |x|rand }
      ending_size = random_actions.reduce({}) do |h, (act, ob)|
        if act == :add
          h[ob] = 1
        else
          h.delete(ob)
        end
        h
      end.size
      it 'has the correct size after lots of addings and removings' do
        h = H.empty
        random_actions.each do |(act, ob)|
          if act == :add
            h = h.put(ob, ob * 3)
          else
            h = h.delete(ob)
          end
        end
        h.size.should == ending_size
      end
    end
  end
end