run-tdb_foreach.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "ntdb-source.h"
  2. #include "tap-interface.h"
  3. #include "logging.h"
  4. #include "helprun-external-agent.h"
  5. static int drop_count(struct ntdb_context *ntdb, unsigned int *count)
  6. {
  7. if (--(*count) == 0)
  8. return 1;
  9. return 0;
  10. }
  11. static int set_found(struct ntdb_context *ntdb, bool found[3])
  12. {
  13. unsigned int idx;
  14. if (strcmp(ntdb_name(ntdb), "run-ntdb_foreach0.ntdb") == 0)
  15. idx = 0;
  16. else if (strcmp(ntdb_name(ntdb), "run-ntdb_foreach1.ntdb") == 0)
  17. idx = 1;
  18. else if (strcmp(ntdb_name(ntdb), "run-ntdb_foreach2.ntdb") == 0)
  19. idx = 2;
  20. else
  21. abort();
  22. if (found[idx])
  23. abort();
  24. found[idx] = true;
  25. return 0;
  26. }
  27. int main(int argc, char *argv[])
  28. {
  29. unsigned int i, count;
  30. bool found[3];
  31. struct ntdb_context *ntdb0, *ntdb1, *ntdb;
  32. int flags[] = { NTDB_DEFAULT, NTDB_NOMMAP,
  33. NTDB_CONVERT, NTDB_NOMMAP|NTDB_CONVERT };
  34. plan_tests(sizeof(flags) / sizeof(flags[0]) * 8);
  35. for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
  36. ntdb0 = ntdb_open("run-ntdb_foreach0.ntdb",
  37. flags[i]|MAYBE_NOSYNC,
  38. O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
  39. ntdb1 = ntdb_open("run-ntdb_foreach1.ntdb",
  40. flags[i]|MAYBE_NOSYNC,
  41. O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
  42. ntdb = ntdb_open("run-ntdb_foreach2.ntdb",
  43. flags[i]|MAYBE_NOSYNC,
  44. O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
  45. memset(found, 0, sizeof(found));
  46. ntdb_foreach(set_found, found);
  47. ok1(found[0] && found[1] && found[2]);
  48. /* Test premature iteration termination */
  49. count = 1;
  50. ntdb_foreach(drop_count, &count);
  51. ok1(count == 0);
  52. ntdb_close(ntdb1);
  53. memset(found, 0, sizeof(found));
  54. ntdb_foreach(set_found, found);
  55. ok1(found[0] && !found[1] && found[2]);
  56. ntdb_close(ntdb);
  57. memset(found, 0, sizeof(found));
  58. ntdb_foreach(set_found, found);
  59. ok1(found[0] && !found[1] && !found[2]);
  60. ntdb1 = ntdb_open("run-ntdb_foreach1.ntdb",
  61. flags[i]|MAYBE_NOSYNC,
  62. O_RDWR, 0600, &tap_log_attr);
  63. memset(found, 0, sizeof(found));
  64. ntdb_foreach(set_found, found);
  65. ok1(found[0] && found[1] && !found[2]);
  66. ntdb_close(ntdb0);
  67. memset(found, 0, sizeof(found));
  68. ntdb_foreach(set_found, found);
  69. ok1(!found[0] && found[1] && !found[2]);
  70. ntdb_close(ntdb1);
  71. memset(found, 0, sizeof(found));
  72. ntdb_foreach(set_found, found);
  73. ok1(!found[0] && !found[1] && !found[2]);
  74. ok1(tap_log_messages == 0);
  75. }
  76. return exit_status();
  77. }