Skip to content

Commit f02c394

Browse files
Add IO::Event::PriorityHeap#empty?.
1 parent e444027 commit f02c394

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

lib/io/event/priority_heap.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ def peek
2727
def size
2828
@contents.size
2929
end
30+
31+
# @returns [Boolean] true if the heap is empty, false otherwise.
32+
def empty?
33+
@contents.empty?
34+
end
3035

3136
# Removes and returns the smallest element in the heap, or nil if the heap is empty.
3237
#

test/io/event/priority_heap.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
it "should report its size as zero" do
2121
expect(priority_heap.size).to be(:zero?)
2222
end
23+
24+
it "should report as empty" do
25+
expect(priority_heap).to be(:empty?)
26+
end
2327
end
2428

2529
it "returns the same element after inserting a single element" do
@@ -29,6 +33,25 @@
2933
expect(priority_heap.size).to be(:zero?)
3034
end
3135

36+
with "#empty?" do
37+
it "should return false when heap contains elements" do
38+
priority_heap.push(1)
39+
expect(priority_heap).not.to be(:empty?)
40+
end
41+
42+
it "should return true after popping all elements" do
43+
priority_heap.push(1)
44+
priority_heap.push(2)
45+
expect(priority_heap).not.to be(:empty?)
46+
47+
priority_heap.pop
48+
expect(priority_heap).not.to be(:empty?)
49+
50+
priority_heap.pop
51+
expect(priority_heap).to be(:empty?)
52+
end
53+
end
54+
3255
it "should return inserted elements in ascending order no matter the insertion order" do
3356
(1..10).to_a.shuffle.each do |e|
3457
priority_heap.push(e)

0 commit comments

Comments
 (0)