diff tests/test_storage.nim @ 1286:ad9091fde244

add: storage tests, fix something not worth mentioning
author sam <sam@basx.dev>
date Tue, 30 Jul 2024 14:48:30 +0700
parents
children df3c075e5dea
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test_storage.nim	Tue Jul 30 14:48:30 2024 +0700
@@ -0,0 +1,39 @@
+import std/strformat
+
+import ../semicongine
+
+proc testSimple(storage: StorageType) =
+  const TEST_VALUE = 42
+  const KEY = "test"
+
+  # get default
+  assert storage.load(KEY, 0) == default(type(TEST_VALUE))
+
+  # save and load custom
+  store(storage, KEY, TEST_VALUE)
+  assert storage.load(KEY, 0) == TEST_VALUE
+
+proc stressTest(storage: StorageType) =
+  for i in 1 .. 10000:
+    let key = &"key-{i}"
+    store(storage, key, i)
+    assert storage.load(key, 0) == i
+
+proc main() =
+  SystemStorage.purge()
+  echo "SystemStorage: Testing simple store/load"
+  SystemStorage.testSimple()
+
+  UserStorage.purge()
+  echo "UserStorage: Testing simple store/load"
+  UserStorage.testSimple()
+
+  echo "Stress test with 10'000 saves/loads"
+  SystemStorage.stressTest()
+
+  SystemStorage.purge()
+  UserStorage.purge()
+
+
+when isMainModule:
+  main()