File: array_spec.rb

package info (click to toggle)
ruby-concurrent 1.1.6%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 30,284 kB
  • sloc: ruby: 30,875; java: 6,117; ansic: 288; makefile: 9; sh: 6
file content (94 lines) | stat: -rw-r--r-- 2,846 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
module Concurrent
  RSpec.describe Array do
    let!(:ary) { described_class.new }

    describe '.[]' do
      describe 'when initializing with no arguments' do
        it do
          expect(described_class[]).to be_empty
        end
      end

      describe 'when initializing with arguments' do
        it 'creates an array with the given objects' do
          expect(described_class[:hello, :world]).to eq [:hello, :world]
        end
      end
    end

    describe '.new' do
      describe 'when initializing with no arguments' do
        it do
          expect(described_class.new).to be_empty
        end
      end

      describe 'when initializing with a size argument' do
        let(:size) { 3 }

        it 'creates an array with size elements set to nil' do
          expect(described_class.new(size)).to eq [nil, nil, nil]
        end

        describe 'when initializing with a default value argument' do
          let(:default_value) { :ruby }

          it 'creates an array with size elements set to the default value' do
            expect(described_class.new(size, default_value)).to eq [:ruby, :ruby, :ruby]
          end
        end

        describe 'when initializing with a block argument' do
          let(:block_argument) { proc { |index| :"ruby#{index}" } }

          it 'creates an array with size elements set to the default value' do
            expect(described_class.new(size, &block_argument)).to eq [:ruby0, :ruby1, :ruby2]
          end
        end
      end

      describe 'when initializing with another array as an argument' do
        let(:other_array) { [:hello, :world] }
        let(:fake_other_array) { double('Fake array', to_ary: other_array) }

        it 'creates a new array' do
          expect(described_class.new(other_array)).to_not be other_array
        end

        it 'creates an array with the same contents as the other array' do
          expect(described_class.new(other_array)).to eq [:hello, :world]
        end

        it 'creates an array with the results of calling #to_ary on the other array' do
          expect(described_class.new(fake_other_array)).to eq [:hello, :world]
        end
      end
    end

    context 'concurrency' do
      it do
        (1..Concurrent::ThreadSafe::Test::THREADS).map do |i|
          in_thread(ary) do |ary|
            1000.times do
              ary << i
              ary.each { |x| x * 2 }
              ary.shift
              ary.last
            end
          end
        end.map(&:join)
        expect(ary).to be_empty
      end
    end

    describe '#slice' do
      # This is mostly relevant on Rubinius and TruffleRuby
      it 'correctly initializes the monitor' do
        ary.concat([0, 1, 2, 3, 4, 5, 6, 7, 8])

        sliced = ary.slice!(0..2)
        expect { sliced[0] }.not_to raise_error
      end
    end
  end
end