69 lines
2.4 KiB
Elixir
69 lines
2.4 KiB
Elixir
# original source: https://github.com/nikolaostoji/elixir-lru-cache/blob/47742fe1f49a75a08ef4a4146373d762348b6546/test/lru_cache_test.exs
|
|
defmodule LruCacheTest do
|
|
use ExUnit.Case
|
|
doctest Floof.LruCache
|
|
|
|
setup do
|
|
{:ok,server_pid} = Floof.LruCache.create(3, :LruCache)
|
|
{:ok,server: server_pid}
|
|
end
|
|
|
|
test "tests put() returns true when adding element" do
|
|
assert Floof.LruCache.put(:LruCache, "abc", "123") == true
|
|
end
|
|
|
|
test "tests get() returns ok if key is in cache" do
|
|
Floof.LruCache.put(:LruCache, "abc", "123")
|
|
assert Floof.LruCache.get(:LruCache, "abc") == "123"
|
|
end
|
|
|
|
test "tests get() returns nil if key DNE in cache" do
|
|
assert Floof.LruCache.get(:LruCache, "abc") == nil
|
|
end
|
|
|
|
test "tests delete() removes element" do
|
|
Floof.LruCache.put(:LruCache, "abc", "123")
|
|
assert Floof.LruCache.get(:LruCache, "abc") == "123"
|
|
|
|
Floof.LruCache.delete(:LruCache, "abc")
|
|
assert Floof.LruCache.get(:LruCache, "abc") == nil
|
|
end
|
|
|
|
test "tests adding when at capacity will remove item that was lru" do
|
|
Floof.LruCache.put(:LruCache, "a", "1")
|
|
Floof.LruCache.put(:LruCache, "b", "2")
|
|
Floof.LruCache.put(:LruCache, "c", "3")
|
|
Floof.LruCache.put(:LruCache, "d", "4")
|
|
|
|
assert Floof.LruCache.get(:LruCache, "a") == nil
|
|
assert Floof.LruCache.get(:LruCache, "b") == "2"
|
|
assert Floof.LruCache.get(:LruCache, "c") == "3"
|
|
assert Floof.LruCache.get(:LruCache, "d") == "4"
|
|
end
|
|
|
|
test "tests fetching an item will reset its position" do
|
|
Floof.LruCache.put(:LruCache, "a", "1")
|
|
Floof.LruCache.put(:LruCache, "b", "2")
|
|
Floof.LruCache.put(:LruCache, "c", "3")
|
|
Floof.LruCache.get(:LruCache, "a")
|
|
Floof.LruCache.put(:LruCache, "d", "4")
|
|
|
|
assert Floof.LruCache.get(:LruCache, "a") == "1"
|
|
assert Floof.LruCache.get(:LruCache, "b") == nil
|
|
assert Floof.LruCache.get(:LruCache, "c") == "3"
|
|
assert Floof.LruCache.get(:LruCache, "d") == "4"
|
|
end
|
|
|
|
test "tests deleting an item frees up room" do
|
|
Floof.LruCache.put(:LruCache, "a", "1")
|
|
Floof.LruCache.put(:LruCache, "b", "2")
|
|
Floof.LruCache.put(:LruCache, "c", "3")
|
|
Floof.LruCache.delete(:LruCache, "c")
|
|
Floof.LruCache.put(:LruCache, "d", "4")
|
|
|
|
assert Floof.LruCache.get(:LruCache, "a") == "1"
|
|
assert Floof.LruCache.get(:LruCache, "b") == "2"
|
|
assert Floof.LruCache.get(:LruCache, "c") == nil
|
|
assert Floof.LruCache.get(:LruCache, "d") == "4"
|
|
end
|
|
end
|