Mercurial > games > semicongine
annotate tests/test_storage.nim @ 1010:34be258400a6
fix: dirty namespace on windows
author | sam <sam@basx.dev> |
---|---|
date | Tue, 16 Apr 2024 02:42:39 +0700 |
parents | 1705e005cdee |
children | 71315636ba82 |
rev | line source |
---|---|
986 | 1 import std/os |
987
49d73ed5a1ec
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
986
diff
changeset
|
2 import std/strformat |
986 | 3 |
4 import semicongine | |
5 | |
6 proc testSimple(storage: StorageType) = | |
7 const TEST_VALUE = 42 | |
8 const KEY = "test" | |
9 | |
10 # get default | |
990
1705e005cdee
add: enforce adding default value for storage loads
sam <sam@basx.dev>
parents:
989
diff
changeset
|
11 assert storage.load(KEY, 0) == default(type(TEST_VALUE)) |
986 | 12 |
13 # save and load custom | |
989
3e0116b5d2ed
did: undo complicated background storage API, sync is good enough for now
sam <sam@basx.dev>
parents:
988
diff
changeset
|
14 store(storage, KEY, TEST_VALUE) |
990
1705e005cdee
add: enforce adding default value for storage loads
sam <sam@basx.dev>
parents:
989
diff
changeset
|
15 assert storage.load(KEY, 0) == TEST_VALUE |
987
49d73ed5a1ec
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
986
diff
changeset
|
16 |
49d73ed5a1ec
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
986
diff
changeset
|
17 proc stressTest(storage: StorageType) = |
49d73ed5a1ec
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
986
diff
changeset
|
18 for i in 1 .. 10000: |
49d73ed5a1ec
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
986
diff
changeset
|
19 let key = &"key-{i}" |
989
3e0116b5d2ed
did: undo complicated background storage API, sync is good enough for now
sam <sam@basx.dev>
parents:
988
diff
changeset
|
20 store(storage, key, i) |
990
1705e005cdee
add: enforce adding default value for storage loads
sam <sam@basx.dev>
parents:
989
diff
changeset
|
21 assert storage.load(key, 0) == i |
987
49d73ed5a1ec
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
986
diff
changeset
|
22 |
986 | 23 proc main() = |
987
49d73ed5a1ec
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
986
diff
changeset
|
24 SystemStorage.purge() |
986 | 25 echo "SystemStorage: Testing simple store/load" |
26 SystemStorage.testSimple() | |
27 | |
28 UserStorage.purge() | |
29 echo "UserStorage: Testing simple store/load" | |
30 UserStorage.testSimple() | |
987
49d73ed5a1ec
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
986
diff
changeset
|
31 |
49d73ed5a1ec
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
986
diff
changeset
|
32 echo "Stress test with 10'000 saves/loads" |
49d73ed5a1ec
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
986
diff
changeset
|
33 SystemStorage.stressTest() |
49d73ed5a1ec
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
986
diff
changeset
|
34 |
49d73ed5a1ec
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
986
diff
changeset
|
35 SystemStorage.purge() |
986 | 36 UserStorage.purge() |
37 | |
987
49d73ed5a1ec
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
986
diff
changeset
|
38 |
986 | 39 when isMainModule: |
989
3e0116b5d2ed
did: undo complicated background storage API, sync is good enough for now
sam <sam@basx.dev>
parents:
988
diff
changeset
|
40 main() |