test.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include <stdarg.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <robdb.h>
  7. typedef struct {
  8. void *data;
  9. unsigned int len;
  10. } DATA;
  11. void print_record(const DATA *key, const DATA *data)
  12. {
  13. unsigned int id = 0;
  14. if (4 == key->len) {
  15. unsigned char *pt = key->data;
  16. /* In test.db the key is a big endian 4 bytes integer */
  17. id = pt[3] + (pt[2] << 8) + (pt[1] << 16) + (pt[0] << 24);
  18. printf("Key: %3d Data:\n%s\n", id, (char*)data->data);
  19. } else {
  20. printf("ERROR: Unexpected record key size\n");
  21. }
  22. }
  23. void mylog(const char *msg, ...)
  24. {
  25. va_list ap;
  26. va_start(ap, msg);
  27. printf("bdbreader:" );
  28. vprintf(msg, ap);
  29. va_end(ap);
  30. }
  31. void myfree(void **data)
  32. {
  33. if (*data) {
  34. free(*data);
  35. *data = NULL;
  36. }
  37. }
  38. int main()
  39. {
  40. struct bdb_db *db = NULL;
  41. struct bdb_cur *cur = NULL;
  42. DATA key = {0};
  43. DATA data = {0};
  44. int rc = 0;
  45. char keybuff[6];
  46. int fail = 0;
  47. int count = 0;
  48. int expected_count = 14;
  49. printf("HERE[%d]\n",__LINE__);
  50. /* Initialize all callbacks */
  51. bdbreader_set_calloc_cb(calloc);
  52. bdbreader_set_malloc_cb(malloc);
  53. bdbreader_set_realloc_cb(realloc);
  54. bdbreader_set_free_cb(myfree);
  55. bdbreader_set_log_cb(mylog);
  56. db = bdbreader_bdb_open("test.db");
  57. if (db == NULL) {
  58. perror("Failed to open test.db");
  59. exit(1);
  60. }
  61. cur = bdbreader_cur_open(db);
  62. if (cur == NULL) {
  63. perror("Failed to open cursor");
  64. exit(1);
  65. }
  66. printf(" ********* Dump the dtabase content: *******\n");
  67. do {
  68. rc = bdbreader_cur_next(cur);
  69. if (rc !=0) {
  70. break;
  71. }
  72. rc = bdbreader_cur_getcurval(cur, &key.data, &key.len, &data.data, &data.len);
  73. if (rc == 0) {
  74. print_record(&key, &data);
  75. count++;
  76. }
  77. } while (rc == 0);
  78. printf("Found %d/%d records\n\n", count, expected_count);
  79. if (count != expected_count) {
  80. fail = 1;
  81. }
  82. printf(" ********* Test lookup: *******\n");
  83. keybuff[0] = 0;
  84. keybuff[1] = 0;
  85. keybuff[2] = 0;
  86. keybuff[3] = 7;
  87. keybuff[4] = '@';
  88. key.data = keybuff;
  89. key.len = 5;
  90. printf(" Looking for key == 7@ ... Should not find it.\n");
  91. rc = bdbreader_cur_lookup(cur, key.data, key.len);
  92. if (rc == 0) {
  93. printf("ERROR: key == 7@ unexpectedly found.\n");
  94. fail = 1;
  95. } else {
  96. printf("OK: key == 7@ is not found (as expected).\n");
  97. }
  98. printf(" Looking for key >= 7@ ... Should not find record with key == 8.\n");
  99. rc = bdbreader_cur_lookup_ge(cur, key.data, key.len);
  100. if (rc == 0) {
  101. rc = bdbreader_cur_getcurval(cur, &key.data, &key.len, &data.data, &data.len);
  102. if (rc != 0) {
  103. printf("ERROR: key >= 7@ : unable to get the record.\n");
  104. fail = 1;
  105. } else {
  106. char *ptid = key.data;
  107. if (ptid[3] != 8) {
  108. printf("ERROR: key >= 7@ : found record %d instead of 8.\n", ptid[3]);
  109. fail = 1;
  110. } else {
  111. printf("OK: key >= 7@ : found record 8 (as expected).\n");
  112. }
  113. }
  114. } else {
  115. printf("ERROR: key >= 7@ is not found.\n");
  116. fail = 1;
  117. }
  118. key.data = keybuff;
  119. key.len = 4;
  120. printf(" Looking for key == 7 ... Should find it.\n");
  121. rc = bdbreader_cur_lookup(cur, key.data, key.len);
  122. if (rc == 0) {
  123. rc = bdbreader_cur_getcurval(cur, &key.data, &key.len, &data.data, &data.len);
  124. if (rc != 0) {
  125. printf("ERROR: key == 7 : unable to get the record.\n");
  126. fail = 1;
  127. } else {
  128. char *ptid = key.data;
  129. if (ptid[3] != 7) {
  130. printf("ERROR: key == 7 : found record %d instead of 7.\n", ptid[3]);
  131. fail = 1;
  132. } else {
  133. printf("OK: key == 7 : found record 7 (as expected).\n");
  134. }
  135. }
  136. } else {
  137. printf("ERROR: key == 7 is not found.\n");
  138. fail = 1;
  139. }
  140. bdbreader_cur_close(&cur);
  141. bdbreader_bdb_close(&db);
  142. printf("TEST: %s\n", fail ? "FAIL":"PASS");
  143. return fail;
  144. }
  145. #if 0
  146. /* Get cursor current status. returns -1 if no more records */
  147. int bdbreader_cur_getval(struct bdb_cur *cur);
  148. /* Position the cursor on the key. return -1 if not found */
  149. int bdbreader_cur_lookup(struct bdb_cur *cur, const unsigned char *key, unsigned int keyl);
  150. /* Position the cursor on smallest key >= key. return -1 if not found */
  151. int bdbreader_cur_lookup_ge(struct bdb_cur *cur, const unsigned char *key, unsigned int keyl);
  152. #endif