File: product_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 (70 lines) | stat: -rw-r--r-- 2,367 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
require 'spec_helper'

describe Immutable::Vector do
  describe '#product' do
    context 'when passed no arguments' do
      it 'multiplies all items in vector' do
        [
          [[], 1],
          [[2], 2],
          [[1, 3, 5, 7, 11], 1155],
        ].each do |values, expected|
          V[*values].product.should == expected
        end
      end
    end

    context 'when passed one or more vectors' do
      let(:vector) { V[1,2,3] }

      context 'when passed a block' do
        it 'yields an array for each combination of items from the vectors' do
          yielded = []
          vector.product(vector) { |obj| yielded << obj }
          yielded.should eql([[1,1], [1,2], [1,3], [2,1], [2,2], [2,3], [3,1], [3,2], [3,3]])

          yielded = []
          vector.product(V[3,4,5], V[6,8]) { |obj| yielded << obj }
          yielded.should eql(
            [[1, 3, 6], [1, 3, 8], [1, 4, 6], [1, 4, 8], [1, 5, 6], [1, 5, 8],
             [2, 3, 6], [2, 3, 8], [2, 4, 6], [2, 4, 8], [2, 5, 6], [2, 5, 8],
             [3, 3, 6], [3, 3, 8], [3, 4, 6], [3, 4, 8], [3, 5, 6], [3, 5, 8]])
        end

        it 'returns self' do
          vector.product(V.empty) {}.should be(vector)
          vector.product(V[1,2], V[3]) {}.should be(vector)
          V.empty.product(vector) {}.should be(V.empty)
        end
      end

      context 'when not passed a block' do
        it 'returns the cartesian product in an array' do
          V[1,2].product(V[3,4,5], V[6,8]).should eql(
            [[1, 3, 6], [1, 3, 8], [1, 4, 6], [1, 4, 8], [1, 5, 6], [1, 5, 8],
            [2, 3, 6], [2, 3, 8], [2, 4, 6], [2, 4, 8], [2, 5, 6], [2, 5, 8]])
        end
      end

      context 'when one of the arguments is empty' do
        it 'returns an empty array' do
          vector.product(V.empty, V[4,5,6]).should eql([])
        end
      end

      context 'when the receiver is empty' do
        it 'returns an empty array' do
          V.empty.product(vector, V[4,5,6]).should eql([])
        end
      end
    end

    context 'when passed one or more Arrays' do
      it 'also calculates the cartesian product correctly' do
        V[1,2].product([3,4,5], [6,8]).should eql(
          [[1, 3, 6], [1, 3, 8], [1, 4, 6], [1, 4, 8], [1, 5, 6], [1, 5, 8],
          [2, 3, 6], [2, 3, 8], [2, 4, 6], [2, 4, 8], [2, 5, 6], [2, 5, 8]])
      end
    end
  end
end