Mercurial > games > semicongine
annotate tests/test_storage.nim @ 1465:7b2ec9f3d0f6
add: external serialization library
author | sam <sam@basx.dev> |
---|---|
date | Wed, 26 Mar 2025 00:37:50 +0700 |
parents | 3e3192241ea7 |
children | cbca94a95736 |
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 | 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 | 17 proc testWorldAPI() = |
1465 | 18 type Obj1 = object |
19 value: int | |
20 | |
21 type Obj2 = object | |
22 value: string | |
23 | |
1464 | 24 assert listWorlds().len == 0 |
25 | |
1465 | 26 const obj1 = Obj1(value: 42) |
27 "testWorld".storeWorld(obj1) | |
1464 | 28 assert listWorlds() == @["testWorld"] |
1465 | 29 assert loadWorld[Obj1]("testWorld") == obj1 |
1464 | 30 |
1465 | 31 const obj2 = Obj2(value: "Hello world") |
32 "testWorld".storeWorld(obj2) | |
1464 | 33 assert listWorlds() == @["testWorld"] |
1465 | 34 assert loadWorld[Obj2]("testWorld") == obj2 |
1464 | 35 |
1465 | 36 "earth".storeWorld(obj2) |
1464 | 37 assert "earth" in listWorlds() |
38 assert "testWorld" in listWorlds() | |
1465 | 39 assert loadWorld[Obj2]("earth") == obj2 |
1464 | 40 |
41 "earth".purgeWorld() | |
42 assert listWorlds() == @["testWorld"] | |
43 | |
44 "testWorld".purgeWorld() | |
45 assert listWorlds().len == 0 | |
46 | |
1286
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
47 proc stressTest(storage: StorageType) = |
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
48 for i in 1 .. 10000: |
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
49 let key = &"key-{i}" |
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
50 store(storage, key, i) |
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
51 assert storage.load(key, 0) == i |
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
52 |
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
53 proc main() = |
1423
3b8a736c45a7
did: put almost all global state into a single struct
sam <sam@basx.dev>
parents:
1332
diff
changeset
|
54 initEngine("Test storage") |
1286
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
55 SystemStorage.purge() |
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
56 echo "SystemStorage: Testing simple store/load" |
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
57 SystemStorage.testSimple() |
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
58 |
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
59 UserStorage.purge() |
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
60 echo "UserStorage: Testing simple store/load" |
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
61 UserStorage.testSimple() |
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
62 |
1464 | 63 echo "Testing world-storage API" |
64 testWorldAPI() | |
65 | |
1286
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
66 echo "Stress test with 10'000 saves/loads" |
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
67 SystemStorage.stressTest() |
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 SystemStorage.purge() |
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
70 UserStorage.purge() |
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
71 |
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
72 when isMainModule: |
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
73 main() |