-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtests.php
More file actions
1264 lines (1056 loc) · 43.1 KB
/
tests.php
File metadata and controls
1264 lines (1056 loc) · 43.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
StorX Complete Test Suite
v5.3, 2026-02-13
AGPLv3, @aaviator42
Complete test suite for StorX library covering:
- File operations (create, check, delete, open, close, copy)
- Key operations (write, read, modify, delete, check)
- Configuration (exceptions, timeout)
- Data integrity (types, large data, edge cases)
- Concurrency (file locking)
- Performance and stress testing
- Exception code verification
Contains 58 comprehensive tests:
- Tests 1-34: Return-value behavior
- Tests 35-58: Exception code verification
*/
header('Content-Type: text/plain');
require 'StorX.php';
use StorX\Sx;
// Test configuration
$testDbFile = 'testdb.db';
$performanceIterations = 1000;
$stressTestKeys = 100;
$largeDataSize = 1024 * 1024; // 1MB
// Initialize test counters
$totalTests = 0;
$passedTests = 0;
$testsPassed = true;
$startTime = microtime(true);
// Helper function for standard test output
function runTest($testNumber, $description, $function, $expecting, $callback) {
global $totalTests, $passedTests, $testsPassed;
$totalTests++;
echo "Test $testNumber: $description" . PHP_EOL;
echo "Function: $function" . PHP_EOL;
echo "Expecting: $expecting" . PHP_EOL;
echo "Result: ";
$result = $callback();
if ($result['success']) {
echo $result['message'] . " (OK)";
$passedTests++;
} else {
echo $result['message'] . " (ERROR)";
$testsPassed = false;
}
if (isset($result['details'])) {
echo PHP_EOL . $result['details'];
}
echo PHP_EOL . PHP_EOL;
}
// Helper function for exception tests
function runExceptionTest($testNumber, $description, $expectedCode, $callback) {
global $totalTests, $passedTests, $testsPassed;
$totalTests++;
echo "Test $testNumber: $description" . PHP_EOL;
echo "Expected: Exception code $expectedCode" . PHP_EOL;
echo "Result: ";
$result = $callback();
if ($result['success']) {
echo "Code {$result['code']} thrown (OK)";
$passedTests++;
} else {
// Handle case where wrong exception code was thrown
if (isset($result['code'])) {
echo "Wrong code {$result['code']} thrown, expected $expectedCode (ERROR)";
} else {
echo $result['message'] . " (ERROR)";
}
$testsPassed = false;
}
echo PHP_EOL . PHP_EOL;
}
// Pre-test cleanup
if (file_exists($testDbFile)) {
unlink($testDbFile);
}
// Clean up any leftover test files
foreach (glob('test_*.db') as $file) {
unlink($file);
}
foreach (glob('stress_*.db') as $file) {
unlink($file);
}
foreach (glob('test_exc_*.db') as $file) {
unlink($file);
}
echo "Beginning StorX Tests..." . PHP_EOL;
echo "Test file v5.3, 2026-02-24" . PHP_EOL;
echo "Total planned tests: 58" . PHP_EOL;
echo PHP_EOL;
// Initialize Sx objects for different configurations
$sx = new Sx();
$sx->throwExceptions(false);
$sxThrow = new Sx();
$sxThrow->throwExceptions(true);
echo "Configuration:" . PHP_EOL;
echo " THROW_EXCEPTIONS: " . ($sx->throwExceptions() ? "true" : "false") . PHP_EOL;
echo " BUSY_TIMEOUT: " . $sx->setTimeout() . " ms" . PHP_EOL;
echo PHP_EOL;
// ============================================================================
echo "=== SECTION 1: FILE OPERATIONS ===" . PHP_EOL;
echo PHP_EOL;
// ============================================================================
// Test 1: createFile() success
runTest(1, "Creating DB file", "createFile('$testDbFile')", "1", function() use ($sx, $testDbFile) {
$result = $sx->createFile($testDbFile);
$success = ($result === 1 && file_exists($testDbFile));
return ['success' => $success, 'message' => $result];
});
// Test 2: createFile() duplicate
runTest(2, "Creating DB file that already exists", "createFile('$testDbFile')", "0", function() use ($sx, $testDbFile) {
$result = $sx->createFile($testDbFile);
return ['success' => $result === 0, 'message' => $result];
});
// Test 3: checkFile() valid
runTest(3, "Check valid DB file", "checkFile('$testDbFile')", "1", function() use ($sx, $testDbFile) {
$result = $sx->checkFile($testDbFile);
return ['success' => $result === 1, 'message' => $result];
});
// Test 4: checkFile() non-existent
runTest(4, "Check non-existent DB file", "checkFile('testdb2.db')", "0", function() use ($sx) {
$result = $sx->checkFile('testdb2.db');
return ['success' => $result === 0, 'message' => $result];
});
// Test 5: checkFile() invalid format
runTest(5, "Check invalid format file", "checkFile('testdb2.db')", "5", function() use ($sx) {
file_put_contents('testdb2.db', "0000000000");
$result = $sx->checkFile('testdb2.db');
unlink('testdb2.db');
return ['success' => $result === 5, 'message' => $result];
});
// Test 6: deleteFile() success
runTest(6, "Delete DB file", "deleteFile('$testDbFile')", "1", function() use ($sx, $testDbFile) {
$result = $sx->deleteFile($testDbFile);
$success = ($result === 1 && !file_exists($testDbFile));
return ['success' => $success, 'message' => $result];
});
// Test 7: deleteFile() non-existent
runTest(7, "Delete non-existent DB file", "deleteFile('$testDbFile')", "1", function() use ($sx, $testDbFile) {
$result = $sx->deleteFile($testDbFile);
return ['success' => $result === 1, 'message' => $result];
});
// Test 8: openFile() non-existent
runTest(8, "Open non-existent DB file", "openFile('$testDbFile')", "0", function() use ($sx, $testDbFile) {
$result = $sx->openFile($testDbFile);
return ['success' => $result === 0, 'message' => $result];
});
// Test 9: copyFile() success with data integrity
runTest(9, "Copy DB file with data", "copyFile()", "successful copy with data integrity", function() use ($sx) {
$sourceFile = 'test_copy_source.db';
$destFile = 'test_copy_dest.db';
// Create source file with test data
$sx->createFile($sourceFile);
$sx->openFile($sourceFile, 1);
$sx->modifyKey('testKey1', 'testValue1');
$sx->modifyKey('testKey2', ['array', 'data', 123]);
$sx->modifyKey('testKey3', null);
$sx->closeFile();
// Copy the file
$result = $sx->copyFile($sourceFile, $destFile);
if ($result !== 1) {
$sx->deleteFile($sourceFile);
return ['success' => false, 'message' => "copyFile returned $result"];
}
// Verify destination file exists and has correct data
$sx->openFile($destFile, 0);
$val1 = $sx->returnKey('testKey1');
$val2 = $sx->returnKey('testKey2');
$val3 = $sx->returnKey('testKey3');
$sx->closeFile();
// Clean up
$sx->deleteFile($sourceFile);
$sx->deleteFile($destFile);
$success = ($val1 === 'testValue1' &&
$val2 === ['array', 'data', 123] &&
$val3 === null);
return [
'success' => $success,
'message' => $success ? "File copied with data integrity preserved" : "Data mismatch after copy"
];
});
// Test 10: copyFile() source doesn't exist
runTest(10, "Copy non-existent source file", "copyFile()", "0", function() use ($sx) {
$result = $sx->copyFile('nonexistent_source.db', 'test_dest.db');
return ['success' => $result === 0, 'message' => $result];
});
// Test 11: copyFile() destination already exists
runTest(11, "Copy to existing destination", "copyFile()", "0", function() use ($sx) {
$sourceFile = 'test_copy_src.db';
$destFile = 'test_copy_dst.db';
// Create both files
$sx->createFile($sourceFile);
$sx->createFile($destFile);
// Try to copy - should fail
$result = $sx->copyFile($sourceFile, $destFile);
// Clean up
$sx->deleteFile($sourceFile);
$sx->deleteFile($destFile);
return ['success' => $result === 0, 'message' => $result];
});
// Test 12: copyFile() invalid source file
runTest(12, "Copy invalid source file", "copyFile()", "0", function() use ($sx) {
$sourceFile = 'test_invalid_source.db';
$destFile = 'test_copy_dest.db';
// Create an invalid file (not a StorX DB)
file_put_contents($sourceFile, 'not a valid database');
$result = $sx->copyFile($sourceFile, $destFile);
// Clean up
unlink($sourceFile);
if (file_exists($destFile)) unlink($destFile);
return ['success' => $result === 0, 'message' => $result];
});
// Test 13: openFile() when file already open
runTest(13, "openFile() when file already open", "openFile()", "0 (reject second open)", function() use ($sx) {
$filename = 'test_double_open.db';
$sx->createFile($filename);
$result1 = $sx->openFile($filename, 1); // First open - should succeed
// Try to open another file while first is still open
$result2 = $sx->openFile($filename, 0); // Second open - should fail
$sx->closeFile();
$sx->deleteFile($filename);
$success = ($result1 === 1 && $result2 === 0);
return [
'success' => $success,
'message' => $success ? "Second openFile correctly rejected" : "First: $result1, Second: $result2",
'details' => "First open returned: $result1, Second open returned: $result2"
];
});
// Test 14: closeFile() when no file open
runTest(14, "closeFile() when no file open", "closeFile()", "1 (idempotent)", function() {
$sx2 = new Sx();
$sx2->throwExceptions(false);
// Call closeFile on fresh object with no file open
$result = $sx2->closeFile();
return [
'success' => $result === 1,
'message' => $result === 1 ? "closeFile correctly returns 1 when no file open" : "Unexpected return: $result"
];
});
// Test 15: commitFile() persists data while keeping file open
runTest(15, "commitFile() persists data", "commitFile()", "data visible to other readers", function() use ($sx) {
$filename = 'test_commit.db';
$sx2 = new Sx();
$sx2->throwExceptions(false);
$sx->createFile($filename);
$sx->openFile($filename, 1);
$sx->writeKey('commit_test', 'value_before_commit');
$commitResult = $sx->commitFile();
// Without closing, open with another object and verify data is persisted
$sx2->openFile($filename, 0); // readonly
$readValue = $sx2->returnKey('commit_test');
$sx2->closeFile();
// Now close the original
$sx->closeFile();
$sx->deleteFile($filename);
$success = ($commitResult === 1 && $readValue === 'value_before_commit');
return [
'success' => $success,
'message' => $success ? "commitFile persists data while keeping file open" : "commitResult: $commitResult, readValue: " . var_export($readValue, true),
'details' => "Wrote and committed with \$sx, read with \$sx2 before \$sx closed"
];
});
// ============================================================================
echo "=== SECTION 2: KEY OPERATIONS ===" . PHP_EOL;
echo PHP_EOL;
// ============================================================================
// Recreate test file for key operation tests
$sx->createFile($testDbFile);
// Test 16: write and read key
runTest(16, "Write and read key", "writeKey(), returnKey()", "successful read/write", function() use ($sx, $testDbFile) {
$testValue = "test_000";
$sx->openFile($testDbFile, 1);
$sx->writeKey('test_key', $testValue);
$sx->closeFile();
$sx->openFile($testDbFile);
$readValue = $sx->returnKey('test_key');
$sx->closeFile();
$success = ($testValue === $readValue);
$details = "Data written: " . var_export($testValue, true) . PHP_EOL . "Data read: " . var_export($readValue, true);
return [
'success' => $success,
'message' => $success ? "Test data successfully written to and read from file" : "Data mismatch",
'details' => $details
];
});
// Test 17: deleteKey()
runTest(17, "Delete key from DB file", "deleteKey(), returnKey()", "key deletion", function() use ($sx, $testDbFile) {
$sx->openFile($testDbFile, 1);
$result = $sx->deleteKey('test_key');
$sx->closeFile();
$sx->openFile($testDbFile);
$readValue = $sx->returnKey('test_key');
$sx->closeFile();
$success = ($result == 1 && $readValue === "STORX_ERROR");
return [
'success' => $success,
'message' => $success ? "Key successfully deleted from file" : "Unable to delete key from file"
];
});
// Test 18: deleteKey() non-existent
runTest(18, "Delete non-existent key", "deleteKey()", "1", function() use ($sx, $testDbFile) {
$sx->openFile($testDbFile, 1);
$result = $sx->deleteKey('test_key');
$sx->closeFile();
return ['success' => $result == 1, 'message' => $result];
});
// Test 19: deleteKey() without write access
runTest(19, "Delete key without write access", "deleteKey()", "0", function() use ($sx, $testDbFile) {
$sx->openFile($testDbFile, 1);
$sx->writeKey('test_key', 'test_value_0000');
$sx->closeFile();
$sx->openFile($testDbFile, 0); // readonly
$result = $sx->deleteKey('test_key');
$sx->closeFile();
return ['success' => $result == 0, 'message' => $result];
});
// Test 20: checkKey() exists
runTest(20, "Check existing key", "checkKey()", "1", function() use ($sx, $testDbFile) {
$sx->openFile($testDbFile, 1);
$sx->modifyKey("test_key", "test_value_0000");
$result = $sx->checkKey("test_key");
$sx->closeFile();
return ['success' => $result == 1, 'message' => $result];
});
// Test 21: checkKey() non-existent
runTest(21, "Check non-existent key", "checkKey()", "0", function() use ($sx, $testDbFile) {
$sx->openFile($testDbFile, 1);
$result = $sx->checkKey("test_key_2");
$sx->closeFile();
return ['success' => $result == 0, 'message' => $result];
});
// Test 22: modifyKey()
runTest(22, "Modify key", "modifyKey()", "1", function() use ($sx, $testDbFile) {
$testValue = 'test_value_0001';
$sx->openFile($testDbFile, 1);
$result = $sx->modifyKey("test_key", $testValue);
$sx->readKey("test_key", $readValue);
$sx->closeFile();
$success = ($result === 1 && $readValue === $testValue);
return ['success' => $success, 'message' => $result];
});
// Test 23: writeKey() invalid name
runTest(23, "Write key with invalid name", "writeKey()", "0", function() use ($sx, $testDbFile) {
$keyName = 'INV@LID';
$sx->openFile($testDbFile, 1);
$sx->deleteKey('test_key');
$result = $sx->writeKey($keyName, 'test_value_0001');
$sx->closeFile();
return ['success' => $result === 0, 'message' => $result];
});
// Test 24: modifyMultipleKeys() and readAllKeys()
runTest(24, "Modify/read multiple keys", "modifyMultipleKeys(), readAllKeys()", "data integrity", function() use ($sx, $testDbFile) {
$keysWrite = [
'key_1' => [1, 2, 3],
'key_2' => ["abc", "def"],
'key_3' => 500,
'key_4' => NULL,
'key_5' => [123, 50.5, "abc", NULL, true, false],
'key_6' => [-101.1, -60, 0, 60, 101.1],
'key_7' => ["ping" => "pong", "foo" => "bar"],
'key_8' => [[0, 1, 2, 3], ['a', 'b', 'c', 'd'], [true, false, null]]
];
$sx->openFile($testDbFile, 1);
$sx->modifyMultipleKeys($keysWrite);
$sx->readAllKeys($keysRead);
$sx->closeFile();
$result = ($keysWrite === $keysRead);
return [
'success' => $result,
'message' => $result ? "written_data === read_data" : "data mismatch"
];
});
// ============================================================================
echo "=== SECTION 3: CONFIGURATION ===" . PHP_EOL;
echo PHP_EOL;
// ============================================================================
// Test 25: Exception handling mode
runTest(25, "Exception handling mode", "throwExceptions(true)", "exceptions thrown", function() use ($sxThrow) {
$exceptionCaught = false;
try {
$sxThrow->openFile('nonexistent.db');
} catch (Exception $e) {
$exceptionCaught = true;
}
return [
'success' => $exceptionCaught,
'message' => $exceptionCaught ? "Exception properly thrown" : "No exception thrown"
];
});
// Test 26: Timeout configuration
runTest(26, "Timeout configuration", "setTimeout()", "configurable timeout", function() use ($sx) {
$originalTimeout = $sx->setTimeout();
$newTimeout = 3000;
$sx->setTimeout($newTimeout);
$currentTimeout = $sx->setTimeout();
$sx->setTimeout($originalTimeout); // restore
$success = ($currentTimeout === $newTimeout);
return [
'success' => $success,
'message' => "Timeout set to $currentTimeout ms",
'details' => "Original: $originalTimeout ms, Set: $newTimeout ms, Current: $currentTimeout ms"
];
});
// ============================================================================
echo "=== SECTION 4: DATA INTEGRITY ===" . PHP_EOL;
echo PHP_EOL;
// ============================================================================
// Test 27: Large data storage
runTest(27, "Large data storage (1MB)", "writeKey(), returnKey()", "large data handling", function() use ($sx) {
global $largeDataSize;
$largeData = str_repeat('A', $largeDataSize);
$filename = 'test_large.db';
$sx->createFile($filename);
$sx->openFile($filename, 1);
$writeResult = $sx->writeKey('large_key', $largeData);
$sx->closeFile();
if ($writeResult !== 1) {
unlink($filename);
return ['success' => false, 'message' => "Failed to write large data"];
}
$sx->openFile($filename);
$readData = $sx->returnKey('large_key');
$sx->closeFile();
$success = ($readData === $largeData);
$details = "Data size: " . strlen($largeData) . " bytes";
unlink($filename);
return [
'success' => $success,
'message' => $success ? "Large data stored and retrieved correctly" : "Large data mismatch",
'details' => $details
];
});
// Test 28: Data type preservation
runTest(28, "Data type preservation", "Various data types", "type integrity", function() use ($sx) {
$filename = 'test_types.db';
$testData = [
'string' => 'hello world',
'integer' => 42,
'float' => 3.14159,
'boolean_true' => true,
'boolean_false' => false,
'null' => null,
'array' => ['a', 'b', 'c'],
'assoc_array' => ['key1' => 'value1', 'key2' => 'value2'],
'nested_array' => [['nested' => 'data'], [1, 2, 3]],
'object' => (object)['prop' => 'value']
];
$sx->createFile($filename);
$sx->openFile($filename, 1);
$allGood = true;
foreach ($testData as $key => $value) {
$sx->modifyKey($key, $value);
$retrieved = $sx->returnKey($key);
// For objects, use == instead of === since serialize/unserialize creates new instances
if (is_object($value)) {
if ($retrieved != $value || !is_object($retrieved)) {
$allGood = false;
break;
}
} else {
if ($retrieved !== $value) {
$allGood = false;
break;
}
}
}
$sx->closeFile();
unlink($filename);
return [
'success' => $allGood,
'message' => $allGood ? "All data types preserved correctly" : "Data type preservation failed",
'details' => "Tested: " . implode(', ', array_keys($testData)) . " (objects compared by value, not reference)"
];
});
// Test 29: Edge case - empty values
runTest(29, "Edge case - empty values", "Various empty values", "empty value handling", function() use ($sx) {
$filename = 'test_empty.db';
$emptyValues = [
'empty_string' => '',
'zero' => 0,
'empty_array' => [],
'false_value' => false
];
$sx->createFile($filename);
$sx->openFile($filename, 1);
$allGood = true;
foreach ($emptyValues as $key => $value) {
$sx->modifyKey($key, $value);
$retrieved = $sx->returnKey($key);
if ($retrieved !== $value) {
$allGood = false;
break;
}
}
$sx->closeFile();
unlink($filename);
return [
'success' => $allGood,
'message' => $allGood ? "Empty values handled correctly" : "Empty value handling failed"
];
});
// ============================================================================
echo "=== SECTION 5: CONCURRENCY ===" . PHP_EOL;
echo PHP_EOL;
// ============================================================================
// Test 30: File locking behavior
runTest(30, "File locking behavior", "Multiple file handles", "proper locking", function() use ($sx) {
$filename = 'test_lock.db';
$sx2 = new Sx();
$sx2->throwExceptions(false);
$sx->createFile($filename);
$sx->openFile($filename, 1); // Lock for writing
// Try to open same file with another instance
$result = $sx2->openFile($filename, 1);
$sx->closeFile();
unlink($filename);
// Should fail due to lock
return [
'success' => $result === 0,
'message' => $result === 0 ? "File locking works correctly" : "File locking failed"
];
});
// ============================================================================
echo "=== SECTION 6: PERFORMANCE ===" . PHP_EOL;
echo PHP_EOL;
// ============================================================================
// Test 31: Performance test - rapid operations
runTest(31, "Rapid operations", "$performanceIterations write/read cycles", "good performance", function() use ($sx) {
global $performanceIterations;
$filename = 'test_perf.db';
$sx->createFile($filename);
$startTime = microtime(true);
$sx->openFile($filename, 1);
for ($i = 0; $i < $performanceIterations; $i++) {
$key = "perf_key_$i";
$value = "performance_test_value_$i";
$sx->modifyKey($key, $value);
}
$sx->closeFile();
// Now read them back
$sx->openFile($filename);
$readErrors = 0;
for ($i = 0; $i < $performanceIterations; $i++) {
$key = "perf_key_$i";
$expected = "performance_test_value_$i";
$actual = $sx->returnKey($key);
if ($actual !== $expected) {
$readErrors++;
}
}
$sx->closeFile();
$endTime = microtime(true);
$duration = round(($endTime - $startTime) * 1000, 2);
unlink($filename);
$success = ($readErrors === 0);
return [
'success' => $success,
'message' => $success ? "Performance test completed successfully" : "$readErrors read errors occurred",
'details' => "Duration: {$duration}ms for $performanceIterations operations (" . round($performanceIterations / ($duration / 1000), 2) . " ops/sec)"
];
});
// Test 32: Stress test - many keys
runTest(32, "Bulk key operations", "$stressTestKeys keys via modifyMultipleKeys()", "bulk operations", function() use ($sx) {
global $stressTestKeys;
$filename = 'test_stress.db';
$stressData = [];
// Generate test data
for ($i = 0; $i < $stressTestKeys; $i++) {
$stressData["stress_key_$i"] = [
'id' => $i,
'data' => str_repeat("data_$i", 10),
'metadata' => ['created' => time(), 'index' => $i]
];
}
$sx->createFile($filename);
$sx->openFile($filename, 1);
$startTime = microtime(true);
$writeResult = $sx->modifyMultipleKeys($stressData);
$sx->readAllKeys($readData);
$endTime = microtime(true);
$sx->closeFile();
unlink($filename);
$duration = round(($endTime - $startTime) * 1000, 2);
$dataMatch = (count($readData) === $stressTestKeys);
$success = ($writeResult === 1 && $dataMatch);
return [
'success' => $success,
'message' => $success ? "Stress test completed successfully" : "Stress test failed",
'details' => "Duration: {$duration}ms for $stressTestKeys keys (" . round($stressTestKeys / ($duration / 1000), 2) . " keys/sec)"
];
});
// Test 33: File state detection
runTest(33, "File state detection", "checkFile() various states", "corruption detection", function() use ($sx) {
$results = [];
// Test 1: Non-existent file
$result1 = $sx->checkFile('nonexistent.db');
$results[] = ($result1 === 0) ? "Non-existent: $result1 (OK)" : "Non-existent: $result1 (FAIL)";
// Test 2: Invalid format file
file_put_contents('invalid.db', 'not a database');
$result2 = $sx->checkFile('invalid.db');
unlink('invalid.db');
$results[] = ($result2 === 5) ? "Invalid format: $result2 (OK)" : "Invalid format: $result2 (FAIL)";
// Test 3: Valid StorX file
$sx->createFile('valid.db');
$result3 = $sx->checkFile('valid.db');
unlink('valid.db');
$results[] = ($result3 === 1) ? "Valid StorX: $result3 (OK)" : "Valid StorX: $result3 (FAIL)";
$allGood = ($result1 === 0 && $result2 === 5 && $result3 === 1);
return [
'success' => $allGood,
'message' => $allGood ? "File state detection works correctly" : "File state detection failed",
'details' => implode(PHP_EOL, $results)
];
});
// Test 34: Memory usage monitoring
runTest(34, "Memory usage", "Large dataset operations", "memory efficiency", function() use ($sx) {
$filename = 'test_memory.db';
$memoryBefore = memory_get_usage();
// Create a dataset that would use significant memory if not handled efficiently
$largeDataset = [];
for ($i = 0; $i < 500; $i++) {
$largeDataset["mem_key_$i"] = str_repeat("data_chunk_$i", 100);
}
$sx->createFile($filename);
$sx->openFile($filename, 1);
$sx->modifyMultipleKeys($largeDataset);
$sx->closeFile();
// Clear the dataset from PHP memory
unset($largeDataset);
// Read back the data
$sx->openFile($filename);
$sx->readAllKeys($readData);
$sx->closeFile();
$memoryAfter = memory_get_usage();
$memoryIncrease = $memoryAfter - $memoryBefore;
unlink($filename);
// Memory increase should be reasonable (less than 10MB for this test)
$success = ($memoryIncrease < (10 * 1024 * 1024)) && (count($readData) === 500);
return [
'success' => $success,
'message' => $success ? "Memory usage is reasonable" : "Memory usage too high",
'details' => "Memory increase: " . round($memoryIncrease / 1024 / 1024, 2) . " MB, Keys processed: " . count($readData)
];
});
// ============================================================================
// CLEANUP BEFORE EXCEPTION TESTS
// ============================================================================
$sx->deleteFile($testDbFile);
foreach (glob('test_*.db') as $file) {
if (file_exists($file)) unlink($file);
}
echo "Configuration:" . PHP_EOL;
echo " THROW_EXCEPTIONS: " . ($sxThrow->throwExceptions() ? "true" : "false") . PHP_EOL;
echo " BUSY_TIMEOUT: " . $sxThrow->setTimeout() . " ms" . PHP_EOL;
echo PHP_EOL;
// ============================================================================
echo "=== SECTION 7: EXCEPTION CODES - FILE OPERATIONS ===" . PHP_EOL;
echo PHP_EOL;
// ============================================================================
$testDbFile = 'testdb_exc.db';
// Test 35: Exception 101 - File does not exist (openFile)
runExceptionTest(35, "openFile() on non-existent file", 101, function() use ($sxThrow) {
try {
$sxThrow->openFile('nonexistent.db');
return ['success' => false, 'message' => 'No exception thrown'];
} catch (Exception $e) {
return ['success' => $e->getCode() === 101, 'code' => $e->getCode()];
}
});
// Test 36: Exception 101 - File does not exist (copyFile source)
runExceptionTest(36, "copyFile() with non-existent source", 101, function() use ($sxThrow) {
try {
$sxThrow->copyFile('nonexistent.db', 'dest.db');
if (file_exists('dest.db')) unlink('dest.db');
return ['success' => false, 'message' => 'No exception thrown'];
} catch (Exception $e) {
if (file_exists('dest.db')) unlink('dest.db');
return ['success' => $e->getCode() === 101, 'code' => $e->getCode()];
}
});
// Test 37: Exception 104 - File is locked (second writer)
runExceptionTest(37, "openFile() for writing on already-locked file", 104, function() use ($sxThrow) {
$testFile = 'test_exc_locked.db';
$sxThrow->createFile($testFile);
$sxThrow->openFile($testFile, 1); // First writer locks the file
$sx2 = new Sx();
$sx2->throwExceptions(true);
try {
$sx2->openFile($testFile, 1); // Second writer - should throw 104
$sxThrow->closeFile();
$sxThrow->deleteFile($testFile);
return ['success' => false, 'message' => 'No exception thrown'];
} catch (Exception $e) {
$sxThrow->closeFile();
$sxThrow->deleteFile($testFile);
return ['success' => $e->getCode() === 104, 'code' => $e->getCode()];
}
});
// Test 38: Exception 105 - File not of matching StorX version
runExceptionTest(38, "openFile() on non-StorX SQLite file", 105, function() use ($sxThrow) {
$testFile = 'test_exc_invalid.db';
// Create a valid SQLite file but not a StorX DB
$db = new \SQLite3($testFile);
$db->exec("CREATE TABLE other (id INTEGER)");
$db->close();
try {
$sxThrow->openFile($testFile);
unlink($testFile);
return ['success' => false, 'message' => 'No exception thrown'];
} catch (Exception $e) {
unlink($testFile);
return ['success' => $e->getCode() === 105, 'code' => $e->getCode()];
}
});
// Test 39: Exception 106 - Unable to create file (already exists)
runExceptionTest(39, "createFile() when file already exists", 106, function() use ($sxThrow, $testDbFile) {
$sxThrow->createFile($testDbFile);
try {
$sxThrow->createFile($testDbFile);
$sxThrow->deleteFile($testDbFile);
return ['success' => false, 'message' => 'No exception thrown'];
} catch (Exception $e) {
$sxThrow->deleteFile($testDbFile);
return ['success' => $e->getCode() === 106, 'code' => $e->getCode()];
}
});
// Test 40: Exception 106 - copyFile() destination already exists
runExceptionTest(40, "copyFile() when destination exists", 106, function() use ($sxThrow) {
$sourceFile = 'test_exc_src.db';
$destFile = 'test_exc_dst.db';
$sxThrow->createFile($sourceFile);
$sxThrow->createFile($destFile);
try {
$sxThrow->copyFile($sourceFile, $destFile);
$sxThrow->deleteFile($sourceFile);
$sxThrow->deleteFile($destFile);
return ['success' => false, 'message' => 'No exception thrown'];
} catch (Exception $e) {
$sxThrow->deleteFile($sourceFile);
$sxThrow->deleteFile($destFile);
return ['success' => $e->getCode() === 106, 'code' => $e->getCode()];
}
});
// Test 41: Exception 108 - openFile() when file already open
runExceptionTest(41, "openFile() when file already open", 108, function() use ($sxThrow) {
$testFile = 'test_exc_double_open.db';
$sxThrow->createFile($testFile);
$sxThrow->openFile($testFile, 1); // First open
try {
$sxThrow->openFile($testFile, 0); // Second open - should throw
$sxThrow->closeFile();
$sxThrow->deleteFile($testFile);
return ['success' => false, 'message' => 'No exception thrown'];
} catch (Exception $e) {
$sxThrow->closeFile();
$sxThrow->deleteFile($testFile);
return ['success' => $e->getCode() === 108, 'code' => $e->getCode()];
}
});
// ============================================================================
echo "=== SECTION 8: EXCEPTION CODES - NO FILE OPEN ===" . PHP_EOL;
echo PHP_EOL;
// ============================================================================
// Test 42: Exception 102 - No file open (readKey)
runExceptionTest(42, "readKey() with no file open", 102, function() use ($sxThrow) {
try {
$sxThrow->readKey('test', $val);
return ['success' => false, 'message' => 'No exception thrown'];
} catch (Exception $e) {
return ['success' => $e->getCode() === 102, 'code' => $e->getCode()];
}
});
// Test 43: Exception 102 - No file open (writeKey)
runExceptionTest(43, "writeKey() with no file open", 102, function() use ($sxThrow) {
try {
$sxThrow->writeKey('test', 'value');
return ['success' => false, 'message' => 'No exception thrown'];
} catch (Exception $e) {
return ['success' => $e->getCode() === 102, 'code' => $e->getCode()];
}
});
// Test 44: Exception 102 - No file open (modifyKey)
runExceptionTest(44, "modifyKey() with no file open", 102, function() use ($sxThrow) {
try {
$sxThrow->modifyKey('test', 'value');
return ['success' => false, 'message' => 'No exception thrown'];
} catch (Exception $e) {
return ['success' => $e->getCode() === 102, 'code' => $e->getCode()];
}
});
// Test 45: Exception 102 - No file open (deleteKey)
runExceptionTest(45, "deleteKey() with no file open", 102, function() use ($sxThrow) {
try {
$sxThrow->deleteKey('test');
return ['success' => false, 'message' => 'No exception thrown'];
} catch (Exception $e) {
return ['success' => $e->getCode() === 102, 'code' => $e->getCode()];
}
});
// Test 46: Exception 102 - No file open (checkKey)
runExceptionTest(46, "checkKey() with no file open", 102, function() use ($sxThrow) {
try {
$sxThrow->checkKey('test');
return ['success' => false, 'message' => 'No exception thrown'];
} catch (Exception $e) {
return ['success' => $e->getCode() === 102, 'code' => $e->getCode()];
}
});
// Test 47: Exception 102 - No file open (commitFile)
runExceptionTest(47, "commitFile() with no file open", 102, function() use ($sxThrow) {
try {
$sxThrow->commitFile();
return ['success' => false, 'message' => 'No exception thrown'];
} catch (Exception $e) {
return ['success' => $e->getCode() === 102, 'code' => $e->getCode()];
}
});
// ============================================================================
echo "=== SECTION 9: EXCEPTION CODES - FILE NOT LOCKED ===" . PHP_EOL;
echo PHP_EOL;
// ============================================================================
// Test 48: Exception 103 - File not locked for writing (writeKey)
runExceptionTest(48, "writeKey() on readonly file", 103, function() use ($sxThrow, $testDbFile) {