dodb.cr/spec/test-fifo.cr

55 lines
1.7 KiB
Crystal
Raw Normal View History

2024-05-25 02:22:52 +02:00
require "spec"
2024-05-25 03:41:00 +02:00
require "../src/fifo.cr"
2024-05-25 02:22:52 +02:00
describe "FIFO" do
it "add and remove values" do
fifo = FIFO(Int32).new 3 # Only 3 allowed entries.
2024-05-25 03:41:00 +02:00
(fifo << 1).should be_nil # there is still room in the fifo
(fifo << 2).should be_nil # there is still room in the fifo
2024-05-25 02:22:52 +02:00
(fifo << 3).should be_nil # last entry without exceeding the allowed size
(fifo << 4).should eq 1 # -> 1 (least recently used data)
2024-05-25 03:41:00 +02:00
(fifo << 4).should be_nil # -> nil (already in the fifo)
(fifo << 2).should be_nil # -> nil (already in the fifo)
2024-05-25 02:22:52 +02:00
(fifo << 5).should eq 3 # -> 3 (least recently used data)
fifo.data.should eq([5, 2, 4] of Int32)
2024-05-25 02:36:43 +02:00
fifo.delete 2
fifo.data.should eq([5, 4] of Int32)
2024-05-25 02:22:52 +02:00
end
end
2024-05-26 04:02:11 +02:00
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 ]"
2024-05-27 18:13:14 +02:00
(fifo << 4).should be_nil # -> nil (just a re-order)
fifo.list.to_s.should eq "[ 4, 5 ]"
fifo.delete 5
(fifo << 0).should be_nil
fifo.list.to_s.should eq "[ 0, 4 ]"
(fifo << 1).should be_nil
fifo.list.to_s.should eq "[ 1, 0, 4 ]"
fifo.delete 4
fifo.list.to_s.should eq "[ 1, 0 ]"
fifo.delete 4
fifo.list.to_s.should eq "[ 1, 0 ]"
fifo.list.size.should eq 2
fifo.hash.size.should eq 2
2024-05-26 04:02:11 +02:00
end
end