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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
RSpec.shared_examples :priority_queue do
subject{ described_class.new }
context '#initialize' do
it 'sorts from high to low when :order is :max' do
subject = described_class.from_list([2, 1, 4, 5, 3, 0], order: :max)
expect(subject.pop).to eq 5
expect(subject.pop).to eq 4
expect(subject.pop).to eq 3
end
it 'sorts from high to low when :order is :high' do
subject = described_class.new(order: :high)
[2, 1, 4, 5, 3, 0].each{|item| subject << item }
expect(subject.pop).to eq 5
expect(subject.pop).to eq 4
expect(subject.pop).to eq 3
end
it 'sorts from low to high when :order is :min' do
subject = described_class.from_list([2, 1, 4, 5, 3, 0], order: :min)
expect(subject.pop).to eq 0
expect(subject.pop).to eq 1
expect(subject.pop).to eq 2
end
it 'sorts from low to high when :order is :low' do
subject = described_class.new(order: :low)
[2, 1, 4, 5, 3, 0].each{|item| subject << item }
expect(subject.pop).to eq 0
expect(subject.pop).to eq 1
expect(subject.pop).to eq 2
end
it 'sorts from high to low by default' do
subject = described_class.new
subject = described_class.from_list([2, 1, 4, 5, 3, 0])
expect(subject.pop).to eq 5
expect(subject.pop).to eq 4
expect(subject.pop).to eq 3
end
end
context '#clear' do
it 'removes all items from a populated queue' do
10.times{|i| subject << i}
subject.clear
expect(subject).to be_empty
end
it 'has no effect on an empty queue' do
subject.clear
expect(subject).to be_empty
end
specify { expect(subject.clear).to be_truthy }
end
context '#delete' do
it 'deletes the requested item when found' do
10.times{|item| subject << item }
subject.delete(5)
expect(subject.pop).to eq 9
expect(subject.pop).to eq 8
expect(subject.pop).to eq 7
expect(subject.pop).to eq 6
expect(subject.pop).to eq 4
expect(subject.pop).to eq 3
expect(subject.pop).to eq 2
expect(subject.pop).to eq 1
expect(subject.pop).to eq 0
end
it 'deletes the requested item when it is the first element' do
10.times{|item| subject << item }
subject.delete(9)
expect(subject.length).to eq 9
expect(subject.pop).to eq 8
expect(subject.pop).to eq 7
expect(subject.pop).to eq 6
expect(subject.pop).to eq 5
expect(subject.pop).to eq 4
expect(subject.pop).to eq 3
expect(subject.pop).to eq 2
expect(subject.pop).to eq 1
expect(subject.pop).to eq 0
end
it 'deletes the requested item when it is the last element' do
10.times{|item| subject << item }
subject.delete(2)
expect(subject.length).to eq 9
expect(subject.pop).to eq 9
expect(subject.pop).to eq 8
expect(subject.pop).to eq 7
expect(subject.pop).to eq 6
expect(subject.pop).to eq 5
expect(subject.pop).to eq 4
expect(subject.pop).to eq 3
expect(subject.pop).to eq 1
expect(subject.pop).to eq 0
end
it 'deletes multiple matching items when present' do
[2, 1, 2, 2, 2, 3, 2].each{|item| subject << item }
subject.delete(2)
expect(subject.pop).to eq 3
expect(subject.pop).to eq 1
end
it 'returns true when found' do
10.times{|i| subject << i}
expect(subject.delete(2)).to be_truthy
end
it 'returns false when not found' do
10.times{|i| subject << i}
expect(subject.delete(100)).to be_falsey
end
it 'returns false when called on an empty queue' do
expect(subject.delete(:foo)).to be_falsey
end
end
context '#empty?' do
it 'returns true for an empty queue' do
expect(subject).to be_empty
end
it 'returns false for a populated queue' do
10.times{|i| subject << i}
expect(subject).not_to be_empty
end
end
context '#include?' do
it 'returns true if the item is found' do
10.times{|i| subject << i}
expect(subject).to include(5)
end
it 'returns false if the item is not found' do
10.times{|i| subject << i}
expect(subject).not_to include(50)
end
it 'returns false when the queue is empty' do
expect(subject).not_to include(1)
end
it 'is aliased as #has_priority?' do
10.times{|i| subject << i}
expect(subject).to have_priority(5)
end
end
context '#length' do
it 'returns the length of a populated queue' do
10.times{|i| subject << i}
expect(subject.length).to eq 10
end
it 'returns zero when the queue is empty' do
expect(subject.length).to eq 0
end
it 'is aliased as #size' do
10.times{|i| subject << i}
expect(subject.size).to eq 10
end
end
context '#peek' do
it 'returns the item at the head of the queue' do
10.times{|i| subject << i}
expect(subject.peek).to eq 9
end
it 'does not remove the item from the queue' do
10.times{|i| subject << i}
subject.peek
expect(subject.length).to eq 10
expect(subject).to include(9)
end
it 'returns nil when the queue is empty' do
expect(subject.peek).to be_nil
end
end
context '#pop' do
it 'returns the item at the head of the queue' do
10.times{|i| subject << i}
expect(subject.pop).to eq 9
end
it 'removes the item from the queue' do
10.times{|i| subject << i}
subject.pop
expect(subject.length).to eq 9
expect(subject).not_to include(9)
end
it 'returns nil when the queue is empty' do
expect(subject.pop).to be_nil
end
it 'returns nil when called multiple times while empty' do
10.times do
expect(subject.pop).to be nil
end
end
it 'is aliased as #deq' do
10.times{|i| subject << i}
expect(subject.deq).to eq 9
end
it 'is aliased as #shift' do
10.times{|i| subject << i}
expect(subject.shift).to eq 9
end
end
context '#push' do
it 'raises an exception when attempting to enqueue nil' do
expect {
subject.push(nil)
}.to raise_error(ArgumentError)
end
it 'adds the item to the queue' do
subject.push(1)
expect(subject).to include(1)
end
it 'sorts the new item in priority order' do
3.times{|i| subject << i}
expect(subject.pop).to eq 2
expect(subject.pop).to eq 1
expect(subject.pop).to eq 0
end
it 'arbitrarily orders equal items with respect to each other' do
3.times{|i| subject << i}
subject.push(1)
expect(subject.pop).to eq 2
expect(subject.pop).to eq 1
expect(subject.pop).to eq 1
expect(subject.pop).to eq 0
end
specify { expect(subject.push(10)).to be_truthy }
it 'is aliased as <<' do
subject << 1
expect(subject).to include(1)
end
it 'is aliased as enq' do
subject.enq(1)
expect(subject).to include(1)
end
end
context '.from_list' do
it 'creates an empty queue from an empty list' do
subject = described_class.from_list([])
expect(subject).to be_empty
end
it 'creates a sorted, populated queue from an Array' do
subject = described_class.from_list([2, 1, 4, 5, 3, 0])
expect(subject.pop).to eq 5
expect(subject.pop).to eq 4
expect(subject.pop).to eq 3
expect(subject.pop).to eq 2
expect(subject.pop).to eq 1
expect(subject.pop).to eq 0
end
it 'creates a sorted, populated queue from a Hash' do
subject = described_class.from_list(two: 2, one: 1, three: 3, zero: 0)
expect(subject.length).to eq 4
end
end
end
module Concurrent
module Collection
RSpec.describe RubyNonConcurrentPriorityQueue do
it_should_behave_like :priority_queue
end
if Concurrent.on_jruby?
RSpec.describe JavaNonConcurrentPriorityQueue do
it_should_behave_like :priority_queue
end
end
RSpec.describe NonConcurrentPriorityQueue do
if Concurrent.on_jruby?
it 'inherits from JavaNonConcurrentPriorityQueue' do
expect(NonConcurrentPriorityQueue.ancestors).to include(JavaNonConcurrentPriorityQueue)
end
else
it 'inherits from RubyNonConcurrentPriorityQueue' do
expect(NonConcurrentPriorityQueue.ancestors).to include(RubyNonConcurrentPriorityQueue)
end
end
end
end
end
|