File: intersection_spec.rb

package info (click to toggle)
ruby-hamster 3.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,932 kB
  • sloc: ruby: 16,915; makefile: 4
file content (53 lines) | stat: -rw-r--r-- 1,633 bytes parent folder | download | duplicates (2)
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
require "spec_helper"
require "hamster/set"

describe Hamster::Set do
  [:intersection, :&].each do |method|
    describe "##{method}" do
      [
        [[], [], []],
        [["A"], [], []],
        [["A"], ["A"], ["A"]],
        [%w[A B C], ["B"], ["B"]],
        [%w[A B C], %w[A C], %w[A C]],
      ].each do |a, b, expected|
        context "for #{a.inspect} and #{b.inspect}"  do
          let(:set_a) { S[*a] }
          let(:set_b) { S[*b] }

          it "returns #{expected.inspect}, without changing the original Sets" do
            set_a.send(method, set_b).should eql(S[*expected])
            set_a.should eql(S.new(a))
            set_b.should eql(S.new(b))
          end
        end

        context "for #{b.inspect} and #{a.inspect}"  do
          let(:set_a) { S[*a] }
          let(:set_b) { S[*b] }

          it "returns #{expected.inspect}, without changing the original Sets" do
            set_b.send(method, set_a).should eql(S[*expected])
            set_a.should eql(S.new(a))
            set_b.should eql(S.new(b))
          end
        end

        context "when passed a Ruby Array" do
          it "returns the expected Set" do
            S[*a].send(method, b.freeze).should eql(S[*expected])
          end
        end
      end

      it "returns results consistent with Array#&" do
        50.times do
          array1 = rand(100).times.map { rand(1000000).to_s(16) }
          array2 = rand(100).times.map { rand(1000000).to_s(16) }
          result = S.new(array1).send(method, S.new(array2))
          result.to_a.sort.should eql((array1 & array2).sort)
        end
      end
    end
  end
end