Mercurial > games > semicongine
annotate tests/test_storage.nim @ 1427:676fc13685a9
did: restructure files and imports
| author | sam <sam@basx.dev> |
|---|---|
| date | Sat, 11 Jan 2025 14:04:39 +0700 |
| parents | 3b8a736c45a7 |
| children | 3e3192241ea7 |
| 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 |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
17 proc stressTest(storage: StorageType) = |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
18 for i in 1 .. 10000: |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
19 let key = &"key-{i}" |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
20 store(storage, key, i) |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
21 assert storage.load(key, 0) == i |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
22 |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
23 proc main() = |
|
1423
3b8a736c45a7
did: put almost all global state into a single struct
sam <sam@basx.dev>
parents:
1332
diff
changeset
|
24 initEngine("Test storage") |
|
1286
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
25 SystemStorage.purge() |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
26 echo "SystemStorage: Testing simple store/load" |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
27 SystemStorage.testSimple() |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
28 |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
29 UserStorage.purge() |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
30 echo "UserStorage: Testing simple store/load" |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
31 UserStorage.testSimple() |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
32 |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
33 echo "Stress test with 10'000 saves/loads" |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
34 SystemStorage.stressTest() |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
35 |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
36 SystemStorage.purge() |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
37 UserStorage.purge() |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
38 |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
39 when isMainModule: |
|
ad9091fde244
add: storage tests, fix something not worth mentioning
sam <sam@basx.dev>
parents:
diff
changeset
|
40 main() |
