diff --git a/spec/spec-database.cr b/spec/spec-database.cr index 5fd9319..82d964e 100644 --- a/spec/spec-database.cr +++ b/spec/spec-database.cr @@ -26,10 +26,10 @@ end class SPECDB::FIFO(V) < DODB::Storage::Stacked(V) property storage_dir : String - def initialize(storage_ext = "", @max_entries = 100_000, remove_previous_data = true) + def initialize(storage_ext = "", @max_entries : UInt32 = 5_000, remove_previous_data = true) @storage_dir = "specdb-storage-fifo-#{@max_entries}#{storage_ext}" ::FileUtils.rm_rf storage_dir if remove_previous_data - super storage_dir + super storage_dir, max_entries end def rm_storage_dir diff --git a/spec/test-fifo.cr b/spec/test-fifo.cr new file mode 100644 index 0000000..8ded150 --- /dev/null +++ b/spec/test-fifo.cr @@ -0,0 +1,41 @@ +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) + + 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 << car3 + db.data.keys.sort.should eq([1, 2, 3] of Int32) + end +end