sdb_record.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License
  4. * as published by the Free Software Foundation; either version 2
  5. * of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. * Author: hakanai
  12. */
  13. #include <stdio.h>
  14. #include <assert.h>
  15. #include <string.h>
  16. #include <libsdb.h>
  17. #include <unistd.h>
  18. #define TFILE "/tmp/oosaAest39ax.sdb"
  19. struct s_record1_t {
  20. int num;
  21. char c;
  22. };
  23. static int simple_append_record() {
  24. sdb_t db;
  25. struct s_record1_t t = { 0 };
  26. t.num = 42;
  27. t.c = 'a';
  28. assert(sdb_open(TFILE, &db) == SDB_OK);
  29. assert(sdb_append(&db, &t, sizeof(t)) == SDB_OK);
  30. assert(sdb_size(&db) == 1);
  31. assert(sdb_append(&db, &t, sizeof(t)) == SDB_OK);
  32. assert(sdb_size(&db) == 2);
  33. for (int i = 3; i < 20; i++) {
  34. assert(sdb_append(&db, &t, sizeof(t)) == SDB_OK);
  35. assert(sdb_size(&db) == i);
  36. }
  37. sdb_close(&db);
  38. unlink(TFILE);
  39. return 0;
  40. }
  41. static int simple_read() {
  42. sdb_t db;
  43. struct s_record1_t t = { 0 }, b = { 0 };
  44. t.num = 29;
  45. t.c = 'F';
  46. unlink(TFILE);
  47. assert(sdb_open(TFILE, &db) == SDB_OK);
  48. assert(sdb_append(&db, &t, sizeof(t)) == SDB_OK);
  49. assert(sdb_size(&db) == 1);
  50. assert(sdb_append(&db, &t, sizeof(t)) == SDB_OK);
  51. assert(sdb_size(&db) == 2);
  52. assert(sdb_read(&db, 0, &b) == SDB_OK);
  53. assert(b.num == 29);
  54. assert(b.c == 'F');
  55. assert(sdb_read(&db, 1, &b) == SDB_OK);
  56. assert(b.num == 29);
  57. assert(b.c == 'F');
  58. sdb_close(&db);
  59. unlink(TFILE);
  60. return 0;
  61. }
  62. struct s_record2_t {
  63. uint32_t a;
  64. uint16_t b;
  65. };
  66. static int read_multiple() {
  67. sdb_t db;
  68. struct s_record2_t t = { 0 }, b = { 0 };
  69. unlink(TFILE);
  70. assert(sdb_open(TFILE, &db) == SDB_OK);
  71. for (int i = 0; i < 200; i++) {
  72. t.a = i;
  73. t.b = i + 2;
  74. assert(sdb_append(&db, &t, sizeof(struct s_record2_t)) == SDB_OK);
  75. }
  76. for (int i = 0; i < 200; i++) {
  77. assert(sdb_read(&db, i, &b) == SDB_OK);
  78. assert(b.a == i);
  79. assert(b.b == (i + 2));
  80. }
  81. sdb_close(&db);
  82. unlink(TFILE);
  83. return 0;
  84. }
  85. int main() {
  86. unlink(TFILE);
  87. assert(!simple_append_record());
  88. assert(!simple_read());
  89. assert(!read_multiple());
  90. return 0;
  91. }