File: shuffle_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 (43 lines) | stat: -rw-r--r-- 1,193 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
require 'spec_helper'

describe Immutable::Vector do
  describe '#shuffle' do
    let(:vector) { V[1,2,3,4] }

    it 'returns the same values, in a usually different order' do
      different = false
      10.times do
        shuffled = vector.shuffle
        shuffled.sort.should eql(vector)
        different ||= (shuffled != vector)
      end
      different.should be(true)
    end

    it 'leaves the original unchanged' do
      vector.shuffle
      vector.should eql(V[1,2,3,4])
    end

    context 'from a subclass' do
      it 'returns an instance of the subclass' do
        subclass = Class.new(Immutable::Vector)
        instance = subclass.new([1,2,3])
        instance.shuffle.class.should be(subclass)
      end
    end

    [32, 33, 1023, 1024, 1025].each do |size|
      context "on a #{size}-item vector" do
        it 'works correctly' do
          vector = V.new(1..size)
          shuffled = vector.shuffle
          shuffled = vector.shuffle while shuffled.eql?(vector) # in case we get the same
          vector.should eql(V.new(1..size))
          shuffled.size.should == vector.size
          shuffled.sort.should eql(vector)
        end
      end
    end
  end
end