FilesystemDbmTest.vala 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /**
  2. * FilesystemDbmTest - Unit tests for FilesystemDbm storage
  3. */
  4. using Implexus.Storage;
  5. public static int main(string[] args) {
  6. int passed = 0;
  7. int failed = 0;
  8. // Test 1: Basic set and get
  9. if (test_basic_set_get()) {
  10. passed++;
  11. stdout.puts("PASS: test_basic_set_get\n");
  12. } else {
  13. failed++;
  14. stdout.puts("FAIL: test_basic_set_get\n");
  15. }
  16. // Test 2: Get non-existent key
  17. if (test_get_nonexistent()) {
  18. passed++;
  19. stdout.puts("PASS: test_get_nonexistent\n");
  20. } else {
  21. failed++;
  22. stdout.puts("FAIL: test_get_nonexistent\n");
  23. }
  24. // Test 3: Delete key
  25. if (test_delete_key()) {
  26. passed++;
  27. stdout.puts("PASS: test_delete_key\n");
  28. } else {
  29. failed++;
  30. stdout.puts("FAIL: test_delete_key\n");
  31. }
  32. // Test 4: Has key
  33. if (test_has_key()) {
  34. passed++;
  35. stdout.puts("PASS: test_has_key\n");
  36. } else {
  37. failed++;
  38. stdout.puts("FAIL: test_has_key\n");
  39. }
  40. // Test 5: Keys iteration
  41. if (test_keys_iteration()) {
  42. passed++;
  43. stdout.puts("PASS: test_keys_iteration\n");
  44. } else {
  45. failed++;
  46. stdout.puts("FAIL: test_keys_iteration\n");
  47. }
  48. // Test 6: Transaction commit
  49. if (test_transaction_commit()) {
  50. passed++;
  51. stdout.puts("PASS: test_transaction_commit\n");
  52. } else {
  53. failed++;
  54. stdout.puts("FAIL: test_transaction_commit\n");
  55. }
  56. // Test 7: Transaction rollback
  57. if (test_transaction_rollback()) {
  58. passed++;
  59. stdout.puts("PASS: test_transaction_rollback\n");
  60. } else {
  61. failed++;
  62. stdout.puts("FAIL: test_transaction_rollback\n");
  63. }
  64. // Test 8: Transaction with delete
  65. if (test_transaction_delete()) {
  66. passed++;
  67. stdout.puts("PASS: test_transaction_delete\n");
  68. } else {
  69. failed++;
  70. stdout.puts("FAIL: test_transaction_delete\n");
  71. }
  72. // Test 9: Overwrite value
  73. if (test_overwrite_value()) {
  74. passed++;
  75. stdout.puts("PASS: test_overwrite_value\n");
  76. } else {
  77. failed++;
  78. stdout.puts("FAIL: test_overwrite_value\n");
  79. }
  80. // Test 10: Special key names
  81. if (test_special_key_names()) {
  82. passed++;
  83. stdout.puts("PASS: test_special_key_names\n");
  84. } else {
  85. failed++;
  86. stdout.puts("FAIL: test_special_key_names\n");
  87. }
  88. // Test 11: Binary data
  89. if (test_binary_data()) {
  90. passed++;
  91. stdout.puts("PASS: test_binary_data\n");
  92. } else {
  93. failed++;
  94. stdout.puts("FAIL: test_binary_data\n");
  95. }
  96. // Test 12: Empty key
  97. if (test_empty_key()) {
  98. passed++;
  99. stdout.puts("PASS: test_empty_key\n");
  100. } else {
  101. failed++;
  102. stdout.puts("FAIL: test_empty_key\n");
  103. }
  104. // Test 13: Large value
  105. if (test_large_value()) {
  106. passed++;
  107. stdout.puts("PASS: test_large_value\n");
  108. } else {
  109. failed++;
  110. stdout.puts("FAIL: test_large_value\n");
  111. }
  112. // Test 14: Multiple operations
  113. if (test_multiple_operations()) {
  114. passed++;
  115. stdout.puts("PASS: test_multiple_operations\n");
  116. } else {
  117. failed++;
  118. stdout.puts("FAIL: test_multiple_operations\n");
  119. }
  120. // Test 15: Persistence
  121. if (test_persistence()) {
  122. passed++;
  123. stdout.puts("PASS: test_persistence\n");
  124. } else {
  125. failed++;
  126. stdout.puts("FAIL: test_persistence\n");
  127. }
  128. stdout.printf("\nResults: %d passed, %d failed\n", passed, failed);
  129. return failed > 0 ? 1 : 0;
  130. }
  131. // Helper to create temporary directory
  132. string create_temp_dir() {
  133. string temp_dir = DirUtils.mkdtemp("implexus_test_XXXXXX");
  134. return temp_dir;
  135. }
  136. // Helper to create BinaryData from string (includes null terminator)
  137. Invercargill.BinaryData string_to_binary(string str) {
  138. // Include null terminator for proper string reconstruction when reading back
  139. uint8[] data = new uint8[str.length + 1];
  140. Memory.copy(data, str, str.length);
  141. data[str.length] = 0; // null terminator
  142. return new Invercargill.DataStructures.ByteBuffer.from_byte_array(data);
  143. }
  144. // Helper to convert BinaryData to string
  145. string binary_to_string(Invercargill.BinaryData data) {
  146. var bytes = data.to_bytes();
  147. return (string) bytes.get_data();
  148. }
  149. // Test 1: Basic set and get
  150. bool test_basic_set_get() {
  151. string temp_dir = create_temp_dir();
  152. try {
  153. var dbm = new FilesystemDbm(temp_dir);
  154. dbm.set("test_key", string_to_binary("test_value"));
  155. var value = dbm.get("test_key");
  156. if (value == null) return false;
  157. if (binary_to_string((!) value) != "test_value") return false;
  158. // Cleanup
  159. cleanup_dir(temp_dir);
  160. return true;
  161. } catch (Error e) {
  162. cleanup_dir(temp_dir);
  163. return false;
  164. }
  165. }
  166. // Test 2: Get non-existent key
  167. bool test_get_nonexistent() {
  168. string temp_dir = create_temp_dir();
  169. var dbm = new FilesystemDbm(temp_dir);
  170. var value = dbm.get("nonexistent");
  171. cleanup_dir(temp_dir);
  172. return value == null;
  173. }
  174. // Test 3: Delete key
  175. bool test_delete_key() {
  176. string temp_dir = create_temp_dir();
  177. try {
  178. var dbm = new FilesystemDbm(temp_dir);
  179. dbm.set("key1", string_to_binary("value1"));
  180. if (!dbm.has_key("key1")) return false;
  181. dbm.delete("key1");
  182. if (dbm.has_key("key1")) return false;
  183. cleanup_dir(temp_dir);
  184. return true;
  185. } catch (Error e) {
  186. cleanup_dir(temp_dir);
  187. return false;
  188. }
  189. }
  190. // Test 4: Has key
  191. bool test_has_key() {
  192. string temp_dir = create_temp_dir();
  193. try {
  194. var dbm = new FilesystemDbm(temp_dir);
  195. if (dbm.has_key("key1")) return false; // Should not exist
  196. dbm.set("key1", string_to_binary("value1"));
  197. if (!dbm.has_key("key1")) return false; // Should exist
  198. if (dbm.has_key("key2")) return false; // Should not exist
  199. cleanup_dir(temp_dir);
  200. return true;
  201. } catch (Error e) {
  202. cleanup_dir(temp_dir);
  203. return false;
  204. }
  205. }
  206. // Test 5: Keys iteration
  207. bool test_keys_iteration() {
  208. string temp_dir = create_temp_dir();
  209. try {
  210. var dbm = new FilesystemDbm(temp_dir);
  211. dbm.set("key1", string_to_binary("value1"));
  212. dbm.set("key2", string_to_binary("value2"));
  213. dbm.set("key3", string_to_binary("value3"));
  214. var keys = dbm.keys;
  215. if (keys.count() != 3) return false;
  216. // Check all keys are present
  217. var key_set = new Invercargill.DataStructures.HashSet<string>();
  218. foreach (var key in keys) {
  219. key_set.add(key);
  220. }
  221. if (!key_set.has("key1")) return false;
  222. if (!key_set.has("key2")) return false;
  223. if (!key_set.has("key3")) return false;
  224. cleanup_dir(temp_dir);
  225. return true;
  226. } catch (Error e) {
  227. cleanup_dir(temp_dir);
  228. return false;
  229. }
  230. }
  231. // Test 6: Transaction commit
  232. bool test_transaction_commit() {
  233. string temp_dir = create_temp_dir();
  234. try {
  235. var dbm = new FilesystemDbm(temp_dir);
  236. dbm.set("key1", string_to_binary("value1"));
  237. dbm.begin_transaction();
  238. dbm.set("key2", string_to_binary("value2"));
  239. dbm.set("key3", string_to_binary("value3"));
  240. // Before commit, values should be in transaction buffer
  241. var value2 = dbm.get("key2");
  242. if (value2 == null) return false;
  243. dbm.commit_transaction();
  244. // After commit, values should be persisted
  245. if (!dbm.has_key("key1")) return false;
  246. if (!dbm.has_key("key2")) return false;
  247. if (!dbm.has_key("key3")) return false;
  248. cleanup_dir(temp_dir);
  249. return true;
  250. } catch (Error e) {
  251. cleanup_dir(temp_dir);
  252. return false;
  253. }
  254. }
  255. // Test 7: Transaction rollback
  256. bool test_transaction_rollback() {
  257. string temp_dir = create_temp_dir();
  258. try {
  259. var dbm = new FilesystemDbm(temp_dir);
  260. dbm.set("key1", string_to_binary("value1"));
  261. dbm.begin_transaction();
  262. dbm.set("key2", string_to_binary("value2"));
  263. // Value should be visible in transaction
  264. if (!dbm.has_key("key2")) return false;
  265. dbm.rollback_transaction();
  266. // After rollback, key2 should not exist
  267. if (dbm.has_key("key2")) return false;
  268. // key1 should still exist
  269. if (!dbm.has_key("key1")) return false;
  270. cleanup_dir(temp_dir);
  271. return true;
  272. } catch (Error e) {
  273. cleanup_dir(temp_dir);
  274. return false;
  275. }
  276. }
  277. // Test 8: Transaction with delete
  278. bool test_transaction_delete() {
  279. string temp_dir = create_temp_dir();
  280. try {
  281. var dbm = new FilesystemDbm(temp_dir);
  282. dbm.set("key1", string_to_binary("value1"));
  283. dbm.set("key2", string_to_binary("value2"));
  284. dbm.begin_transaction();
  285. dbm.delete("key1");
  286. // In transaction, key1 should not be visible
  287. if (dbm.has_key("key1")) return false;
  288. dbm.commit_transaction();
  289. // After commit, key1 should be deleted
  290. if (dbm.has_key("key1")) return false;
  291. // key2 should still exist
  292. if (!dbm.has_key("key2")) return false;
  293. cleanup_dir(temp_dir);
  294. return true;
  295. } catch (Error e) {
  296. cleanup_dir(temp_dir);
  297. return false;
  298. }
  299. }
  300. // Test 9: Overwrite value
  301. bool test_overwrite_value() {
  302. string temp_dir = create_temp_dir();
  303. try {
  304. var dbm = new FilesystemDbm(temp_dir);
  305. dbm.set("key1", string_to_binary("value1"));
  306. var value = dbm.get("key1");
  307. if (value == null || binary_to_string((!) value) != "value1") return false;
  308. dbm.set("key1", string_to_binary("value2"));
  309. value = dbm.get("key1");
  310. if (value == null || binary_to_string((!) value) != "value2") return false;
  311. cleanup_dir(temp_dir);
  312. return true;
  313. } catch (Error e) {
  314. cleanup_dir(temp_dir);
  315. return false;
  316. }
  317. }
  318. // Test 10: Special key names
  319. bool test_special_key_names() {
  320. string temp_dir = create_temp_dir();
  321. try {
  322. var dbm = new FilesystemDbm(temp_dir);
  323. string[] special_keys = {
  324. "key/with/slashes",
  325. "key:with:colons",
  326. "key.with.dots",
  327. "key with spaces",
  328. "key\nwith\nnewlines",
  329. "日本語キー",
  330. "key\0with\0nulls"
  331. };
  332. foreach (var key in special_keys) {
  333. dbm.set(key, string_to_binary(@"value for $key"));
  334. var value = dbm.get(key);
  335. if (value == null) return false;
  336. if (binary_to_string((!) value) != @"value for $key") return false;
  337. }
  338. cleanup_dir(temp_dir);
  339. return true;
  340. } catch (Error e) {
  341. cleanup_dir(temp_dir);
  342. return false;
  343. }
  344. }
  345. // Test 11: Binary data
  346. bool test_binary_data() {
  347. string temp_dir = create_temp_dir();
  348. try {
  349. var dbm = new FilesystemDbm(temp_dir);
  350. // Create binary data with all byte values
  351. var data = new uint8[256];
  352. for (int i = 0; i < 256; i++) {
  353. data[i] = (uint8) i;
  354. }
  355. var binary = new Invercargill.DataStructures.ByteBuffer.from_byte_array(data);
  356. dbm.set("binary_key", binary);
  357. var value = dbm.get("binary_key");
  358. if (value == null) return false;
  359. var read_data = ((!) value).to_bytes();
  360. if (read_data.length != 256) return false;
  361. for (int i = 0; i < 256; i++) {
  362. if (read_data.get_data()[i] != (uint8) i) return false;
  363. }
  364. cleanup_dir(temp_dir);
  365. return true;
  366. } catch (Error e) {
  367. cleanup_dir(temp_dir);
  368. return false;
  369. }
  370. }
  371. // Test 12: Empty key
  372. bool test_empty_key() {
  373. string temp_dir = create_temp_dir();
  374. try {
  375. var dbm = new FilesystemDbm(temp_dir);
  376. dbm.set("", string_to_binary("empty_key_value"));
  377. var value = dbm.get("");
  378. if (value == null) return false;
  379. if (binary_to_string((!) value) != "empty_key_value") return false;
  380. cleanup_dir(temp_dir);
  381. return true;
  382. } catch (Error e) {
  383. cleanup_dir(temp_dir);
  384. return false;
  385. }
  386. }
  387. // Test 13: Large value
  388. bool test_large_value() {
  389. string temp_dir = create_temp_dir();
  390. try {
  391. var dbm = new FilesystemDbm(temp_dir);
  392. // Create a 1MB value
  393. var data = new uint8[1024 * 1024];
  394. for (int i = 0; i < data.length; i++) {
  395. data[i] = (uint8) (i % 256);
  396. }
  397. var binary = new Invercargill.DataStructures.ByteBuffer.from_byte_array(data);
  398. dbm.set("large_key", binary);
  399. var value = dbm.get("large_key");
  400. if (value == null) return false;
  401. var read_data = ((!) value).to_bytes();
  402. if (read_data.length != data.length) return false;
  403. for (int i = 0; i < data.length; i++) {
  404. if (read_data.get_data()[i] != data[i]) return false;
  405. }
  406. cleanup_dir(temp_dir);
  407. return true;
  408. } catch (Error e) {
  409. cleanup_dir(temp_dir);
  410. return false;
  411. }
  412. }
  413. // Test 14: Multiple operations
  414. bool test_multiple_operations() {
  415. string temp_dir = create_temp_dir();
  416. try {
  417. var dbm = new FilesystemDbm(temp_dir);
  418. // Set multiple keys
  419. for (int i = 0; i < 100; i++) {
  420. dbm.set(@"key$i", string_to_binary(@"value$i"));
  421. }
  422. // Verify all keys
  423. for (int i = 0; i < 100; i++) {
  424. var value = dbm.get(@"key$i");
  425. if (value == null) return false;
  426. if (binary_to_string((!) value) != @"value$i") return false;
  427. }
  428. // Delete half the keys
  429. for (int i = 0; i < 50; i++) {
  430. dbm.delete(@"key$i");
  431. }
  432. // Verify deleted keys are gone
  433. for (int i = 0; i < 50; i++) {
  434. if (dbm.has_key(@"key$i")) return false;
  435. }
  436. // Verify remaining keys still exist
  437. for (int i = 50; i < 100; i++) {
  438. if (!dbm.has_key(@"key$i")) return false;
  439. }
  440. cleanup_dir(temp_dir);
  441. return true;
  442. } catch (Error e) {
  443. cleanup_dir(temp_dir);
  444. return false;
  445. }
  446. }
  447. // Test 15: Persistence
  448. bool test_persistence() {
  449. string temp_dir = create_temp_dir();
  450. try {
  451. // Create and write
  452. var dbm1 = new FilesystemDbm(temp_dir);
  453. dbm1.set("persistent_key", string_to_binary("persistent_value"));
  454. // Create new instance with same directory
  455. var dbm2 = new FilesystemDbm(temp_dir);
  456. // Data should persist
  457. var value = dbm2.get("persistent_key");
  458. if (value == null) return false;
  459. if (binary_to_string((!) value) != "persistent_value") return false;
  460. cleanup_dir(temp_dir);
  461. return true;
  462. } catch (Error e) {
  463. cleanup_dir(temp_dir);
  464. return false;
  465. }
  466. }
  467. // Cleanup helper
  468. void cleanup_dir(string path) {
  469. // Remove directory and contents
  470. try {
  471. Dir dir = Dir.open(path, 0);
  472. string? name;
  473. while ((name = dir.read_name()) != null) {
  474. FileUtils.unlink(Path.build_filename(path, name));
  475. }
  476. } catch (FileError e) {
  477. // Ignore errors
  478. }
  479. DirUtils.remove(path);
  480. }