comparison tests/test_storage.nim @ 1464:3e3192241ea7

add: API for world/level storage
author sam <sam@basx.dev>
date Mon, 24 Mar 2025 22:57:47 +0700
parents 676fc13685a9
children 7b2ec9f3d0f6
comparison
equal deleted inserted replaced
1463:f9d86889e018 1464:3e3192241ea7
11 assert storage.load(KEY, 0) == default(type(TEST_VALUE)) 11 assert storage.load(KEY, 0) == default(type(TEST_VALUE))
12 12
13 # save and load custom 13 # save and load custom
14 store(storage, KEY, TEST_VALUE) 14 store(storage, KEY, TEST_VALUE)
15 assert storage.load(KEY, 0) == TEST_VALUE 15 assert storage.load(KEY, 0) == TEST_VALUE
16
17 proc testWorldAPI() =
18 assert listWorlds().len == 0
19
20 "testWorld".storeWorld(42)
21 assert listWorlds() == @["testWorld"]
22 assert loadWorld[int]("testWorld") == 42
23
24 "testWorld".storeWorld("hello")
25 assert listWorlds() == @["testWorld"]
26 assert loadWorld[string]("testWorld") == "hello"
27
28 "earth".storeWorld("hello")
29 assert "earth" in listWorlds()
30 assert "testWorld" in listWorlds()
31 assert loadWorld[string]("earth") == "hello"
32
33 "earth".purgeWorld()
34 assert listWorlds() == @["testWorld"]
35
36 "testWorld".purgeWorld()
37 assert listWorlds().len == 0
16 38
17 proc stressTest(storage: StorageType) = 39 proc stressTest(storage: StorageType) =
18 for i in 1 .. 10000: 40 for i in 1 .. 10000:
19 let key = &"key-{i}" 41 let key = &"key-{i}"
20 store(storage, key, i) 42 store(storage, key, i)
28 50
29 UserStorage.purge() 51 UserStorage.purge()
30 echo "UserStorage: Testing simple store/load" 52 echo "UserStorage: Testing simple store/load"
31 UserStorage.testSimple() 53 UserStorage.testSimple()
32 54
55 echo "Testing world-storage API"
56 testWorldAPI()
57
33 echo "Stress test with 10'000 saves/loads" 58 echo "Stress test with 10'000 saves/loads"
34 SystemStorage.stressTest() 59 SystemStorage.stressTest()
35 60
36 SystemStorage.purge() 61 SystemStorage.purge()
37 UserStorage.purge() 62 UserStorage.purge()