annotate tests/test_storage.nim @ 1468:cbca94a95736

add: custom serialization
author sam <sam@basx.dev>
date Tue, 01 Apr 2025 00:26:14 +0700
parents 7b2ec9f3d0f6
children 63364723d460
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1286
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
1 import std/strformat
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
2
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
3 import ../semicongine
1427
676fc13685a9 did: restructure files and imports
sam <sam@basx.dev>
parents: 1423
diff changeset
4 import ../semicongine/storage
1286
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
5
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
6 proc testSimple(storage: StorageType) =
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
7 const TEST_VALUE = 42
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
8 const KEY = "test"
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
9
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
10 # get default
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
11 assert storage.load(KEY, 0) == default(type(TEST_VALUE))
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
12
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
13 # save and load custom
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
14 store(storage, KEY, TEST_VALUE)
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
15 assert storage.load(KEY, 0) == TEST_VALUE
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
16
1464
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
17 proc testWorldAPI() =
1465
7b2ec9f3d0f6 add: external serialization library
sam <sam@basx.dev>
parents: 1464
diff changeset
18 type Obj1 = object
7b2ec9f3d0f6 add: external serialization library
sam <sam@basx.dev>
parents: 1464
diff changeset
19 value: int
7b2ec9f3d0f6 add: external serialization library
sam <sam@basx.dev>
parents: 1464
diff changeset
20
7b2ec9f3d0f6 add: external serialization library
sam <sam@basx.dev>
parents: 1464
diff changeset
21 type Obj2 = object
1468
cbca94a95736 add: custom serialization
sam <sam@basx.dev>
parents: 1465
diff changeset
22 a: string
cbca94a95736 add: custom serialization
sam <sam@basx.dev>
parents: 1465
diff changeset
23 b: Obj1
cbca94a95736 add: custom serialization
sam <sam@basx.dev>
parents: 1465
diff changeset
24 c: seq[int]
cbca94a95736 add: custom serialization
sam <sam@basx.dev>
parents: 1465
diff changeset
25 d: array[3, Obj1]
cbca94a95736 add: custom serialization
sam <sam@basx.dev>
parents: 1465
diff changeset
26 e: bool
1465
7b2ec9f3d0f6 add: external serialization library
sam <sam@basx.dev>
parents: 1464
diff changeset
27
1464
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
28 assert listWorlds().len == 0
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
29
1465
7b2ec9f3d0f6 add: external serialization library
sam <sam@basx.dev>
parents: 1464
diff changeset
30 const obj1 = Obj1(value: 42)
7b2ec9f3d0f6 add: external serialization library
sam <sam@basx.dev>
parents: 1464
diff changeset
31 "testWorld".storeWorld(obj1)
1464
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
32 assert listWorlds() == @["testWorld"]
1465
7b2ec9f3d0f6 add: external serialization library
sam <sam@basx.dev>
parents: 1464
diff changeset
33 assert loadWorld[Obj1]("testWorld") == obj1
1464
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
34
1468
cbca94a95736 add: custom serialization
sam <sam@basx.dev>
parents: 1465
diff changeset
35 const obj2 = Obj2(
cbca94a95736 add: custom serialization
sam <sam@basx.dev>
parents: 1465
diff changeset
36 a: "Hello world",
cbca94a95736 add: custom serialization
sam <sam@basx.dev>
parents: 1465
diff changeset
37 b: Obj1(value: 20),
cbca94a95736 add: custom serialization
sam <sam@basx.dev>
parents: 1465
diff changeset
38 c: @[1, 2, 3, 4],
cbca94a95736 add: custom serialization
sam <sam@basx.dev>
parents: 1465
diff changeset
39 d: [Obj1(value: 1), Obj1(value: 2), Obj1(value: 3)],
cbca94a95736 add: custom serialization
sam <sam@basx.dev>
parents: 1465
diff changeset
40 e: true,
cbca94a95736 add: custom serialization
sam <sam@basx.dev>
parents: 1465
diff changeset
41 )
1465
7b2ec9f3d0f6 add: external serialization library
sam <sam@basx.dev>
parents: 1464
diff changeset
42 "testWorld".storeWorld(obj2)
1464
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
43 assert listWorlds() == @["testWorld"]
1465
7b2ec9f3d0f6 add: external serialization library
sam <sam@basx.dev>
parents: 1464
diff changeset
44 assert loadWorld[Obj2]("testWorld") == obj2
1464
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
45
1465
7b2ec9f3d0f6 add: external serialization library
sam <sam@basx.dev>
parents: 1464
diff changeset
46 "earth".storeWorld(obj2)
1464
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
47 assert "earth" in listWorlds()
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
48 assert "testWorld" in listWorlds()
1465
7b2ec9f3d0f6 add: external serialization library
sam <sam@basx.dev>
parents: 1464
diff changeset
49 assert loadWorld[Obj2]("earth") == obj2
1464
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
50
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
51 "earth".purgeWorld()
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
52 assert listWorlds() == @["testWorld"]
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
53
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
54 "testWorld".purgeWorld()
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
55 assert listWorlds().len == 0
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
56
1286
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
57 proc stressTest(storage: StorageType) =
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
58 for i in 1 .. 10000:
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
59 let key = &"key-{i}"
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
60 store(storage, key, i)
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
61 assert storage.load(key, 0) == i
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
62
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
63 proc main() =
1423
3b8a736c45a7 did: put almost all global state into a single struct
sam <sam@basx.dev>
parents: 1332
diff changeset
64 initEngine("Test storage")
1286
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
65 SystemStorage.purge()
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
66 echo "SystemStorage: Testing simple store/load"
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
67 SystemStorage.testSimple()
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
68
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
69 UserStorage.purge()
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
70 echo "UserStorage: Testing simple store/load"
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
71 UserStorage.testSimple()
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
72
1464
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
73 echo "Testing world-storage API"
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
74 testWorldAPI()
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
75
1286
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
76 echo "Stress test with 10'000 saves/loads"
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
77 SystemStorage.stressTest()
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
78
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
79 SystemStorage.purge()
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
80 UserStorage.purge()
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
81
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
82 when isMainModule:
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
83 main()