Mercurial > games > semicongine
annotate tests/test_storage.nim @ 1139:114f395b9144
did: finish refactoring and updated all tests accordingly
author | sam <sam@basx.dev> |
---|---|
date | Sat, 08 Jun 2024 14:58:25 +0700 |
parents | 71315636ba82 |
children |
rev | line source |
---|---|
987
49d73ed5a1ec
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
986
diff
changeset
|
1 import std/strformat |
986 | 2 |
3 import semicongine | |
4 | |
5 proc testSimple(storage: StorageType) = | |
6 const TEST_VALUE = 42 | |
7 const KEY = "test" | |
8 | |
9 # get default | |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1136
diff
changeset
|
10 assert storage.Load(KEY, 0) == default(type(TEST_VALUE)) |
986 | 11 |
12 # save and load custom | |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1136
diff
changeset
|
13 Store(storage, KEY, TEST_VALUE) |
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1136
diff
changeset
|
14 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
|
15 |
49d73ed5a1ec
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
986
diff
changeset
|
16 proc stressTest(storage: StorageType) = |
49d73ed5a1ec
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
986
diff
changeset
|
17 for i in 1 .. 10000: |
49d73ed5a1ec
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
986
diff
changeset
|
18 let key = &"key-{i}" |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1136
diff
changeset
|
19 Store(storage, key, i) |
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1136
diff
changeset
|
20 assert storage.Load(key, 0) == i |
987
49d73ed5a1ec
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
986
diff
changeset
|
21 |
986 | 22 proc main() = |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1136
diff
changeset
|
23 SystemStorage.Purge() |
986 | 24 echo "SystemStorage: Testing simple store/load" |
25 SystemStorage.testSimple() | |
26 | |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1136
diff
changeset
|
27 UserStorage.Purge() |
986 | 28 echo "UserStorage: Testing simple store/load" |
29 UserStorage.testSimple() | |
987
49d73ed5a1ec
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
986
diff
changeset
|
30 |
49d73ed5a1ec
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
986
diff
changeset
|
31 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
|
32 SystemStorage.stressTest() |
49d73ed5a1ec
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
986
diff
changeset
|
33 |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1136
diff
changeset
|
34 SystemStorage.Purge() |
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1136
diff
changeset
|
35 UserStorage.Purge() |
986 | 36 |
987
49d73ed5a1ec
add: tests and did some simplification of code
sam <sam@basx.dev>
parents:
986
diff
changeset
|
37 |
986 | 38 when isMainModule: |
989
3e0116b5d2ed
did: undo complicated background storage API, sync is good enough for now
sam <sam@basx.dev>
parents:
988
diff
changeset
|
39 main() |