dodb.cr/spec/test-fifo.cr

43 lines
1.3 KiB
Crystal
Raw Normal View History

2024-05-25 02:22:52 +02:00
require "spec"
require "./db-cars.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 stack
(fifo << 2).should be_nil # there is still room in the stack
(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 stack)
(fifo << 2).should be_nil # -> nil (already in the stack)
(fifo << 5).should eq 3 # -> 3 (least recently used data)
fifo.data.should eq([5, 2, 4] of Int32)
end
end
describe "SPECDB::FIFO" do
it "basics, 3 values" do
car0 = Car.new "Corvet-0", "red", [] of String
car1 = Car.new "Corvet-1", "red", [] of String
car2 = Car.new "Corvet-2", "red", [] of String
car3 = Car.new "Corvet-3", "red", [] of String
db = SPECDB::FIFO(Car).new "", 3
2024-05-25 02:22:52 +02:00
db.data.keys.sort.should eq([] of Int32)
db << car0
db.data.keys.sort.should eq([0] of Int32)
db << car1
db.data.keys.sort.should eq([0, 1] of Int32)
db << car2
db.data.keys.sort.should eq([0, 1, 2] of Int32)
db[0] # Let's use the first value, it shouldn't be the one to be dropped.
2024-05-25 02:22:52 +02:00
db << car3
db.data.keys.sort.should eq([0, 2, 3] of Int32)
2024-05-25 02:22:52 +02:00
end
end