Mercurial > games > semicongine
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 |
| 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 | |
| 1468 | 22 a: string |
| 23 b: Obj1 | |
| 24 c: seq[int] | |
| 25 d: array[3, Obj1] | |
| 26 e: bool | |
| 1465 | 27 |
| 1464 | 28 assert listWorlds().len == 0 |
| 29 | |
| 1465 | 30 const obj1 = Obj1(value: 42) |
| 31 "testWorld".storeWorld(obj1) | |
| 1464 | 32 assert listWorlds() == @["testWorld"] |
| 1465 | 33 assert loadWorld[Obj1]("testWorld") == obj1 |
| 1464 | 34 |
| 1468 | 35 const obj2 = Obj2( |
| 36 a: "Hello world", | |
| 37 b: Obj1(value: 20), | |
| 38 c: @[1, 2, 3, 4], | |
| 39 d: [Obj1(value: 1), Obj1(value: 2), Obj1(value: 3)], | |
| 40 e: true, | |
| 41 ) | |
| 1465 | 42 "testWorld".storeWorld(obj2) |
| 1464 | 43 assert listWorlds() == @["testWorld"] |
| 1465 | 44 assert loadWorld[Obj2]("testWorld") == obj2 |
| 1464 | 45 |
| 1465 | 46 "earth".storeWorld(obj2) |
| 1464 | 47 assert "earth" in listWorlds() |
| 48 assert "testWorld" in listWorlds() | |
| 1465 | 49 assert loadWorld[Obj2]("earth") == obj2 |
| 1464 | 50 |
| 51 "earth".purgeWorld() | |
| 52 assert listWorlds() == @["testWorld"] | |
| 53 | |
| 54 "testWorld".purgeWorld() | |
| 55 assert listWorlds().len == 0 | |
| 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 | 73 echo "Testing world-storage API" |
| 74 testWorldAPI() | |
| 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() |
