Mercurial > games > semicongine
annotate 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 |
| 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() = |
| 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 | |
| 38 | |
|
1286
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
39 proc stressTest(storage: StorageType) = |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
40 for i in 1 .. 10000: |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
41 let key = &"key-{i}" |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
42 store(storage, key, i) |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
43 assert storage.load(key, 0) == i |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
44 |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
45 proc main() = |
|
1423
3b8a736c45a7
did: put almost all global state into a single struct
sam <sam@basx.dev>
parents:
1332
diff
changeset
|
46 initEngine("Test storage") |
|
1286
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
47 SystemStorage.purge() |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
48 echo "SystemStorage: Testing simple store/load" |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
49 SystemStorage.testSimple() |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
50 |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
51 UserStorage.purge() |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
52 echo "UserStorage: Testing simple store/load" |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
53 UserStorage.testSimple() |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
54 |
| 1464 | 55 echo "Testing world-storage API" |
| 56 testWorldAPI() | |
| 57 | |
|
1286
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
58 echo "Stress test with 10'000 saves/loads" |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
59 SystemStorage.stressTest() |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
60 |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
61 SystemStorage.purge() |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
62 UserStorage.purge() |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
63 |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
64 when isMainModule: |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
65 main() |
