37 lines
1.3 KiB
Crystal
37 lines
1.3 KiB
Crystal
require "spec"
|
|
require "../src/fifo.cr"
|
|
|
|
describe "FIFO" do
|
|
it "add and remove values" do
|
|
fifo = FIFO(Int32).new 3 # Only 3 allowed entries.
|
|
(fifo << 1).should be_nil # there is still room in the fifo
|
|
(fifo << 2).should be_nil # there is still room in the fifo
|
|
(fifo << 3).should be_nil # last entry without exceeding the allowed size
|
|
(fifo << 4).should eq 1 # -> 1 (least recently used data)
|
|
(fifo << 4).should be_nil # -> nil (already in the fifo)
|
|
(fifo << 2).should be_nil # -> nil (already in the fifo)
|
|
(fifo << 5).should eq 3 # -> 3 (least recently used data)
|
|
fifo.data.should eq([5, 2, 4] of Int32)
|
|
|
|
fifo.delete 2
|
|
fifo.data.should eq([5, 4] of Int32)
|
|
end
|
|
end
|
|
|
|
describe "EfficientFIFO" do
|
|
it "add and remove values" do
|
|
fifo = EfficientFIFO(Int32).new 3 # Only 3 allowed entries.
|
|
(fifo << 1).should be_nil # there is still room in the fifo
|
|
(fifo << 2).should be_nil # there is still room in the fifo
|
|
(fifo << 3).should be_nil # last entry without exceeding the allowed size
|
|
(fifo << 4).should eq 1 # -> 1 (least recently used data)
|
|
(fifo << 4).should be_nil # -> nil (already in the fifo)
|
|
(fifo << 2).should be_nil # -> nil (already in the fifo)
|
|
(fifo << 5).should eq 3 # -> 3 (least recently used data)
|
|
fifo.list.to_s.should eq "[ 5, 2, 4 ]"
|
|
|
|
fifo.delete 2
|
|
fifo.list.to_s.should eq "[ 5, 4 ]"
|
|
end
|
|
end
|