Mercurial > games > semicongine
annotate tests/test_storage.nim @ 1098:45cf94a6535c
did: undo complicated background storage API, sync is good enough for now
author | sam <sam@basx.dev> |
---|---|
date | Sun, 07 Apr 2024 21:56:43 +0700 |
parents | bc3efccc2bf4 |
children | 1705e005cdee |
rev | line source |
---|---|
1095 | 1 import std/os |
1096
9809bd9e2cdb
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
1095
diff
changeset
|
2 import std/strformat |
1095 | 3 |
4 import semicongine | |
5 | |
6 proc testSimple(storage: StorageType) = | |
7 const TEST_VALUE = 42 | |
8 const KEY = "test" | |
9 | |
10 # get default | |
1098
45cf94a6535c
did: undo complicated background storage API, sync is good enough for now
sam <sam@basx.dev>
parents:
1097
diff
changeset
|
11 assert load[int](storage, KEY) == default(type(TEST_VALUE)) |
1095 | 12 |
13 # save and load custom | |
1098
45cf94a6535c
did: undo complicated background storage API, sync is good enough for now
sam <sam@basx.dev>
parents:
1097
diff
changeset
|
14 store(storage, KEY, TEST_VALUE) |
45cf94a6535c
did: undo complicated background storage API, sync is good enough for now
sam <sam@basx.dev>
parents:
1097
diff
changeset
|
15 assert load[int](storage, KEY) == TEST_VALUE |
1096
9809bd9e2cdb
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
1095
diff
changeset
|
16 |
9809bd9e2cdb
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
1095
diff
changeset
|
17 proc stressTest(storage: StorageType) = |
9809bd9e2cdb
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
1095
diff
changeset
|
18 for i in 1 .. 10000: |
9809bd9e2cdb
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
1095
diff
changeset
|
19 let key = &"key-{i}" |
1098
45cf94a6535c
did: undo complicated background storage API, sync is good enough for now
sam <sam@basx.dev>
parents:
1097
diff
changeset
|
20 store(storage, key, i) |
45cf94a6535c
did: undo complicated background storage API, sync is good enough for now
sam <sam@basx.dev>
parents:
1097
diff
changeset
|
21 assert load[int](storage, key) == i |
1096
9809bd9e2cdb
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
1095
diff
changeset
|
22 |
1095 | 23 proc main() = |
1096
9809bd9e2cdb
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
1095
diff
changeset
|
24 SystemStorage.purge() |
1095 | 25 echo "SystemStorage: Testing simple store/load" |
26 SystemStorage.testSimple() | |
27 | |
28 UserStorage.purge() | |
29 echo "UserStorage: Testing simple store/load" | |
30 UserStorage.testSimple() | |
1096
9809bd9e2cdb
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
1095
diff
changeset
|
31 |
9809bd9e2cdb
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
1095
diff
changeset
|
32 echo "Stress test with 10'000 saves/loads" |
9809bd9e2cdb
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
1095
diff
changeset
|
33 SystemStorage.stressTest() |
9809bd9e2cdb
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
1095
diff
changeset
|
34 |
9809bd9e2cdb
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
1095
diff
changeset
|
35 SystemStorage.purge() |
1095 | 36 UserStorage.purge() |
37 | |
1096
9809bd9e2cdb
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
1095
diff
changeset
|
38 |
1095 | 39 when isMainModule: |
1098
45cf94a6535c
did: undo complicated background storage API, sync is good enough for now
sam <sam@basx.dev>
parents:
1097
diff
changeset
|
40 main() |