File: int_allocator_spec.rb

package info (click to toggle)
ruby-amq-protocol 2.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 572 kB
  • sloc: ruby: 5,975; python: 248; makefile: 4
file content (136 lines) | stat: -rw-r--r-- 3,202 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
require "amq/int_allocator"

RSpec.describe AMQ::IntAllocator do

  #
  # Environment
  #

  subject do
    described_class.new(1, 5)
  end


  #
  # Examples
  #

  describe "#initialize" do
    it "raises ArgumentError when hi <= lo" do
      expect { described_class.new(5, 5) }.to raise_error(ArgumentError)
      expect { described_class.new(10, 5) }.to raise_error(ArgumentError)
    end
  end

  describe "#number_of_bits" do
    it "returns number of bits available for allocation" do
      expect(subject.number_of_bits).to eq(4)
    end
  end


  describe "#hi" do
    it "returns upper bound of the allocation range" do
      expect(subject.hi).to eq(5)
    end
  end

  describe "#lo" do
    it "returns lower bound of the allocation range" do
      expect(subject.lo).to eq(1)
    end
  end


  describe "#allocate" do
    context "when integer in the range is available" do
      it "returns allocated integer" do
        expect(subject.allocate).to eq(1)
        expect(subject.allocate).to eq(2)
        expect(subject.allocate).to eq(3)
        expect(subject.allocate).to eq(4)

        expect(subject.allocate).to eq(-1)
      end
    end

    context "when integer in the range IS NOT available" do
      it "returns -1" do
        4.times { subject.allocate }

        expect(subject.allocate).to eq(-1)
        expect(subject.allocate).to eq(-1)
        expect(subject.allocate).to eq(-1)
        expect(subject.allocate).to eq(-1)
      end
    end
  end


  describe "#free" do
    context "when the integer WAS allocated" do
      it "returns frees that integer" do
        4.times { subject.allocate }
        expect(subject.allocate).to eq(-1)

        subject.free(1)
        expect(subject.allocate).to eq(1)
        expect(subject.allocate).to eq(-1)
        subject.free(2)
        expect(subject.allocate).to eq(2)
        expect(subject.allocate).to eq(-1)
        subject.free(3)
        expect(subject.allocate).to eq(3)
        expect(subject.allocate).to eq(-1)
      end
    end

    context "when the integer WAS NOT allocated" do
      it "has no effect" do
        32.times { subject.free(1) }
        expect(subject.allocate).to eq(1)
      end
    end
  end


  describe "#allocated?" do
    context "when given position WAS allocated" do
      it "returns true" do
        3.times { subject.allocate }

        expect(subject.allocated?(1)).to be_truthy
        expect(subject.allocated?(2)).to be_truthy
        expect(subject.allocated?(3)).to be_truthy
      end
    end

    context "when given position WAS NOT allocated" do
      it "returns false" do
        2.times { subject.allocate }

        expect(subject.allocated?(3)).to be_falsey
        expect(subject.allocated?(4)).to be_falsey
      end
    end
  end

  describe "#release" do
    it "is an alias for #free" do
      subject.allocate
      expect(subject.allocated?(1)).to be_truthy
      subject.release(1)
      expect(subject.allocated?(1)).to be_falsey
    end
  end

  describe "#reset" do
    it "releases all allocations" do
      4.times { subject.allocate }
      expect(subject.allocate).to eq(-1)

      subject.reset
      expect(subject.allocate).to eq(1)
    end
  end
end