Mercurial > games > semicongine
annotate tests/test_storage.nim @ 1499:1f58458b7ef7 default tip main
did: fix a few issues with tests
| author | sam <sam@basx.dev> | 
|---|---|
| date | Tue, 14 Oct 2025 00:27:28 +0700 | 
| parents | 63364723d460 | 
| children | 
| rev | line source | 
|---|---|
| 1286 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 1 import std/strformat | 
| 1499 | 2 import std/sequtils | 
| 1286 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 3 | 
| 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 4 import ../semicongine | 
| 1427 | 5 import ../semicongine/storage | 
| 1286 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 6 | 
| 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 7 proc testSimple(storage: StorageType) = | 
| 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 8 const TEST_VALUE = 42 | 
| 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 9 const KEY = "test" | 
| 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 10 | 
| 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 11 # get default | 
| 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 12 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 | 13 | 
| 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 14 # save and load custom | 
| 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 15 store(storage, KEY, TEST_VALUE) | 
| 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 16 assert storage.load(KEY, 0) == TEST_VALUE | 
| 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 17 | 
| 1470 
63364723d460
fix: make de-/serialization work with all kinds of weird object configurtions
 sam <sam@basx.dev> parents: 
1468diff
changeset | 18 type ID = distinct int | 
| 
63364723d460
fix: make de-/serialization work with all kinds of weird object configurtions
 sam <sam@basx.dev> parents: 
1468diff
changeset | 19 proc `==`(a, b: ID): bool = | 
| 
63364723d460
fix: make de-/serialization work with all kinds of weird object configurtions
 sam <sam@basx.dev> parents: 
1468diff
changeset | 20 `==`(int(a), int(b)) | 
| 
63364723d460
fix: make de-/serialization work with all kinds of weird object configurtions
 sam <sam@basx.dev> parents: 
1468diff
changeset | 21 | 
| 1464 | 22 proc testWorldAPI() = | 
| 1465 | 23 type Obj1 = object | 
| 24 value: int | |
| 1470 
63364723d460
fix: make de-/serialization work with all kinds of weird object configurtions
 sam <sam@basx.dev> parents: 
1468diff
changeset | 25 id: ID | 
| 1465 | 26 | 
| 27 type Obj2 = object | |
| 1468 | 28 a: string | 
| 29 b: Obj1 | |
| 30 c: seq[int] | |
| 31 d: array[3, Obj1] | |
| 32 e: bool | |
| 1465 | 33 | 
| 1464 | 34 assert listWorlds().len == 0 | 
| 35 | |
| 1470 
63364723d460
fix: make de-/serialization work with all kinds of weird object configurtions
 sam <sam@basx.dev> parents: 
1468diff
changeset | 36 const obj1 = Obj1(value: 42, id: ID(1)) | 
| 1465 | 37 "testWorld".storeWorld(obj1) | 
| 1499 | 38 assert listWorlds()[0][1] == "testWorld" | 
| 39 var myWorld: Obj1 | |
| 40 loadWorld[Obj1]("testWorld", myWorld) | |
| 41 assert myWorld == obj1 | |
| 1464 | 42 | 
| 1468 | 43 const obj2 = Obj2( | 
| 44 a: "Hello world", | |
| 1470 
63364723d460
fix: make de-/serialization work with all kinds of weird object configurtions
 sam <sam@basx.dev> parents: 
1468diff
changeset | 45 b: Obj1(value: 20, id: ID(20)), | 
| 1468 | 46 c: @[1, 2, 3, 4], | 
| 1470 
63364723d460
fix: make de-/serialization work with all kinds of weird object configurtions
 sam <sam@basx.dev> parents: 
1468diff
changeset | 47 d: [ | 
| 
63364723d460
fix: make de-/serialization work with all kinds of weird object configurtions
 sam <sam@basx.dev> parents: 
1468diff
changeset | 48 Obj1(value: 1, id: ID(11)), Obj1(value: 2, id: ID(22)), Obj1(value: 3, id: ID(33)) | 
| 
63364723d460
fix: make de-/serialization work with all kinds of weird object configurtions
 sam <sam@basx.dev> parents: 
1468diff
changeset | 49 ], | 
| 1468 | 50 e: true, | 
| 51 ) | |
| 1465 | 52 "testWorld".storeWorld(obj2) | 
| 1499 | 53 assert listWorlds()[0][1] == "testWorld" | 
| 54 var otherWorld: Obj2 | |
| 55 loadWorld[Obj2]("testWorld", otherWorld) | |
| 56 assert otherWorld == obj2 | |
| 1464 | 57 | 
| 1465 | 58 "earth".storeWorld(obj2) | 
| 1499 | 59 assert "earth" in listWorlds().mapIt(it[1]) | 
| 60 assert "testWorld" in listWorlds().mapIt(it[1]) | |
| 61 var moreWorld: Obj2 | |
| 62 loadWorld[Obj2]("earth", moreWorld) | |
| 63 assert moreWorld == obj2 | |
| 1464 | 64 | 
| 65 "earth".purgeWorld() | |
| 1499 | 66 assert listWorlds()[0][1] == "testWorld" | 
| 1464 | 67 | 
| 68 "testWorld".purgeWorld() | |
| 69 assert listWorlds().len == 0 | |
| 70 | |
| 1286 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 71 proc stressTest(storage: StorageType) = | 
| 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 72 for i in 1 .. 10000: | 
| 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 73 let key = &"key-{i}" | 
| 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 74 store(storage, key, i) | 
| 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 75 assert storage.load(key, 0) == i | 
| 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 76 | 
| 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 77 proc main() = | 
| 1423 
3b8a736c45a7
did: put almost all global state into a single struct
 sam <sam@basx.dev> parents: 
1332diff
changeset | 78 initEngine("Test storage") | 
| 1286 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 79 SystemStorage.purge() | 
| 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 80 echo "SystemStorage: Testing simple store/load" | 
| 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 81 SystemStorage.testSimple() | 
| 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 82 | 
| 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 83 UserStorage.purge() | 
| 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 84 echo "UserStorage: Testing simple store/load" | 
| 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 85 UserStorage.testSimple() | 
| 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 86 | 
| 1464 | 87 echo "Testing world-storage API" | 
| 88 testWorldAPI() | |
| 89 | |
| 1286 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 90 echo "Stress test with 10'000 saves/loads" | 
| 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 91 SystemStorage.stressTest() | 
| 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 92 | 
| 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 93 SystemStorage.purge() | 
| 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 94 UserStorage.purge() | 
| 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 95 | 
| 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 96 when isMainModule: | 
| 
ad9091fde244
add: storage tests, fix something not worth mentioning
 sam <sam@basx.dev> parents: diff
changeset | 97 main() | 
