aboutsummaryrefslogtreecommitdiff
path: root/cache/cache_test.go
diff options
context:
space:
mode:
authorn-peugnet <n.peugnet@free.fr>2021-09-08 18:54:11 +0200
committern-peugnet <n.peugnet@free.fr>2021-09-08 18:54:11 +0200
commit66cb179e0c751c081fbb9ec769a409a7a8115459 (patch)
tree2c3dbdace7f8a36f22b66de5ba929d7f2b5d9da4 /cache/cache_test.go
parent31432f931012a6cb3ee2c153127e6669a4dbb959 (diff)
downloaddna-backup-66cb179e0c751c081fbb9ec769a409a7a8115459.tar.gz
dna-backup-66cb179e0c751c081fbb9ec769a409a7a8115459.zip
add cache and start using it in repo
Diffstat (limited to 'cache/cache_test.go')
-rw-r--r--cache/cache_test.go80
1 files changed, 80 insertions, 0 deletions
diff --git a/cache/cache_test.go b/cache/cache_test.go
new file mode 100644
index 0000000..6b84b79
--- /dev/null
+++ b/cache/cache_test.go
@@ -0,0 +1,80 @@
+package cache
+
+import (
+ "bytes"
+ "testing"
+)
+
+func TestFifoChunkCache(t *testing.T) {
+ var v []byte
+ var e bool
+ var cache Cacher = NewFifoCache(3)
+ k0 := 0
+ k1 := 1
+ k2 := 2
+ k3 := 3
+ v0 := []byte{'0'}
+ v1 := []byte{'1'}
+ v2 := []byte{'2'}
+ v3 := []byte{'3'}
+
+ if cache.Len() != 0 {
+ t.Fatal("Cache should be of size 0")
+ }
+
+ v, e = cache.Get(k0)
+ if e {
+ t.Fatal("There should not be any value")
+ }
+
+ cache.Set(k0, v0)
+ cache.Set(k1, v1)
+ cache.Set(k2, v2)
+
+ if cache.Len() != 3 {
+ t.Fatal("Cache should be of size 3")
+ }
+
+ v, e = cache.Get(k0)
+ if !e {
+ t.Fatal("Value should exist for k0")
+ }
+ if bytes.Compare(v, v0) != 0 {
+ t.Fatal("Value for k0 does not match")
+ }
+
+ cache.Set(k3, v3)
+
+ if cache.Len() != 3 {
+ t.Fatal("Cache should still be of size 3")
+ }
+
+ v, e = cache.Get(k0)
+ if e {
+ t.Fatal("Value should not exist for k0")
+ }
+
+ v, e = cache.Get(k1)
+ if !e {
+ t.Fatal("Value should exist for k1")
+ }
+ if bytes.Compare(v, v1) != 0 {
+ t.Fatal("Value for k1 does not match")
+ }
+
+ v, e = cache.Get(k2)
+ if !e {
+ t.Fatal("Value should exist for k2")
+ }
+ if bytes.Compare(v, v2) != 0 {
+ t.Fatal("Value for k2 does not match")
+ }
+
+ v, e = cache.Get(k3)
+ if !e {
+ t.Fatal("Value should exist for k3")
+ }
+ if bytes.Compare(v, v3) != 0 {
+ t.Fatal("Value for k3 does not match")
+ }
+}