annotate tests/test_storage.nim @ 1470:63364723d460

fix: make de-/serialization work with all kinds of weird object configurtions
author sam <sam@basx.dev>
date Wed, 02 Apr 2025 00:49:02 +0700
parents cbca94a95736
children
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
1470
63364723d460 fix: make de-/serialization work with all kinds of weird object configurtions
sam <sam@basx.dev>
parents: 1468
diff changeset
17 type ID = distinct int
63364723d460 fix: make de-/serialization work with all kinds of weird object configurtions
sam <sam@basx.dev>
parents: 1468
diff changeset
18 proc `==`(a, b: ID): bool =
63364723d460 fix: make de-/serialization work with all kinds of weird object configurtions
sam <sam@basx.dev>
parents: 1468
diff changeset
19 `==`(int(a), int(b))
63364723d460 fix: make de-/serialization work with all kinds of weird object configurtions
sam <sam@basx.dev>
parents: 1468
diff changeset
20
1464
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
21 proc testWorldAPI() =
1465
7b2ec9f3d0f6 add: external serialization library
sam <sam@basx.dev>
parents: 1464
diff changeset
22 type Obj1 = object
7b2ec9f3d0f6 add: external serialization library
sam <sam@basx.dev>
parents: 1464
diff changeset
23 value: int
1470
63364723d460 fix: make de-/serialization work with all kinds of weird object configurtions
sam <sam@basx.dev>
parents: 1468
diff changeset
24 id: ID
1465
7b2ec9f3d0f6 add: external serialization library
sam <sam@basx.dev>
parents: 1464
diff changeset
25
7b2ec9f3d0f6 add: external serialization library
sam <sam@basx.dev>
parents: 1464
diff changeset
26 type Obj2 = object
1468
cbca94a95736 add: custom serialization
sam <sam@basx.dev>
parents: 1465
diff changeset
27 a: string
cbca94a95736 add: custom serialization
sam <sam@basx.dev>
parents: 1465
diff changeset
28 b: Obj1
cbca94a95736 add: custom serialization
sam <sam@basx.dev>
parents: 1465
diff changeset
29 c: seq[int]
cbca94a95736 add: custom serialization
sam <sam@basx.dev>
parents: 1465
diff changeset
30 d: array[3, Obj1]
cbca94a95736 add: custom serialization
sam <sam@basx.dev>
parents: 1465
diff changeset
31 e: bool
1465
7b2ec9f3d0f6 add: external serialization library
sam <sam@basx.dev>
parents: 1464
diff changeset
32
1464
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
33 assert listWorlds().len == 0
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
34
1470
63364723d460 fix: make de-/serialization work with all kinds of weird object configurtions
sam <sam@basx.dev>
parents: 1468
diff changeset
35 const obj1 = Obj1(value: 42, id: ID(1))
1465
7b2ec9f3d0f6 add: external serialization library
sam <sam@basx.dev>
parents: 1464
diff changeset
36 "testWorld".storeWorld(obj1)
1464
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
37 assert listWorlds() == @["testWorld"]
1465
7b2ec9f3d0f6 add: external serialization library
sam <sam@basx.dev>
parents: 1464
diff changeset
38 assert loadWorld[Obj1]("testWorld") == obj1
1464
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
39
1468
cbca94a95736 add: custom serialization
sam <sam@basx.dev>
parents: 1465
diff changeset
40 const obj2 = Obj2(
cbca94a95736 add: custom serialization
sam <sam@basx.dev>
parents: 1465
diff changeset
41 a: "Hello world",
1470
63364723d460 fix: make de-/serialization work with all kinds of weird object configurtions
sam <sam@basx.dev>
parents: 1468
diff changeset
42 b: Obj1(value: 20, id: ID(20)),
1468
cbca94a95736 add: custom serialization
sam <sam@basx.dev>
parents: 1465
diff changeset
43 c: @[1, 2, 3, 4],
1470
63364723d460 fix: make de-/serialization work with all kinds of weird object configurtions
sam <sam@basx.dev>
parents: 1468
diff changeset
44 d: [
63364723d460 fix: make de-/serialization work with all kinds of weird object configurtions
sam <sam@basx.dev>
parents: 1468
diff changeset
45 Obj1(value: 1, id: ID(11)), Obj1(value: 2, id: ID(22)), Obj1(value: 3, id: ID(33))
63364723d460 fix: make de-/serialization work with all kinds of weird object configurtions
sam <sam@basx.dev>
parents: 1468
diff changeset
46 ],
1468
cbca94a95736 add: custom serialization
sam <sam@basx.dev>
parents: 1465
diff changeset
47 e: true,
cbca94a95736 add: custom serialization
sam <sam@basx.dev>
parents: 1465
diff changeset
48 )
1465
7b2ec9f3d0f6 add: external serialization library
sam <sam@basx.dev>
parents: 1464
diff changeset
49 "testWorld".storeWorld(obj2)
1464
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
50 assert listWorlds() == @["testWorld"]
1465
7b2ec9f3d0f6 add: external serialization library
sam <sam@basx.dev>
parents: 1464
diff changeset
51 assert loadWorld[Obj2]("testWorld") == obj2
1464
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
52
1465
7b2ec9f3d0f6 add: external serialization library
sam <sam@basx.dev>
parents: 1464
diff changeset
53 "earth".storeWorld(obj2)
1464
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
54 assert "earth" in listWorlds()
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
55 assert "testWorld" in listWorlds()
1465
7b2ec9f3d0f6 add: external serialization library
sam <sam@basx.dev>
parents: 1464
diff changeset
56 assert loadWorld[Obj2]("earth") == obj2
1464
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
57
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
58 "earth".purgeWorld()
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
59 assert listWorlds() == @["testWorld"]
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
60
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
61 "testWorld".purgeWorld()
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
62 assert listWorlds().len == 0
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
63
1286
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
64 proc stressTest(storage: StorageType) =
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
65 for i in 1 .. 10000:
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
66 let key = &"key-{i}"
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
67 store(storage, key, i)
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
68 assert storage.load(key, 0) == i
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
69
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
70 proc main() =
1423
3b8a736c45a7 did: put almost all global state into a single struct
sam <sam@basx.dev>
parents: 1332
diff changeset
71 initEngine("Test storage")
1286
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
72 SystemStorage.purge()
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
73 echo "SystemStorage: Testing simple store/load"
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
74 SystemStorage.testSimple()
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
75
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
76 UserStorage.purge()
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
77 echo "UserStorage: Testing simple store/load"
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
78 UserStorage.testSimple()
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
79
1464
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
80 echo "Testing world-storage API"
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
81 testWorldAPI()
3e3192241ea7 add: API for world/level storage
sam <sam@basx.dev>
parents: 1427
diff changeset
82
1286
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
83 echo "Stress test with 10'000 saves/loads"
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
84 SystemStorage.stressTest()
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
85
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
86 SystemStorage.purge()
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
87 UserStorage.purge()
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
88
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
89 when isMainModule:
ad9091fde244 add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff changeset
90 main()