run.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include <ccan/strmap/strmap.h>
  2. #include <ccan/strmap/strmap.c>
  3. #include <ccan/tap/tap.h>
  4. int main(void)
  5. {
  6. STRMAP(char *) map;
  7. const char str[] = "hello";
  8. const char val[] = "there";
  9. const char none[] = "";
  10. char *dup = strdup(str);
  11. char *v;
  12. /* This is how many tests you plan to run */
  13. plan_tests(42);
  14. strmap_init(&map);
  15. ok1(!strmap_get(&map, str));
  16. ok1(errno == ENOENT);
  17. ok1(!strmap_get(&map, none));
  18. ok1(errno == ENOENT);
  19. ok1(!strmap_del(&map, str, NULL));
  20. ok1(errno == ENOENT);
  21. ok1(!strmap_del(&map, none, NULL));
  22. ok1(errno == ENOENT);
  23. ok1(strmap_add(&map, str, val));
  24. ok1(strmap_get(&map, str) == val);
  25. /* We compare the string, not the pointer. */
  26. ok1(strmap_get(&map, dup) == val);
  27. ok1(!strmap_get(&map, none));
  28. ok1(errno == ENOENT);
  29. /* Add a duplicate should fail. */
  30. ok1(!strmap_add(&map, dup, val));
  31. ok1(errno == EEXIST);
  32. ok1(strmap_get(&map, dup) == val);
  33. /* Delete should return original string. */
  34. ok1(strmap_del(&map, dup, &v) == str);
  35. ok1(v == val);
  36. ok1(!strmap_get(&map, str));
  37. ok1(errno == ENOENT);
  38. ok1(!strmap_get(&map, none));
  39. ok1(errno == ENOENT);
  40. /* Try insert and delete of empty string. */
  41. ok1(strmap_add(&map, none, none));
  42. ok1(strmap_get(&map, none) == none);
  43. ok1(!strmap_get(&map, str));
  44. ok1(errno == ENOENT);
  45. /* Delete should return original string. */
  46. ok1(strmap_del(&map, "", &v) == none);
  47. ok1(v == none);
  48. ok1(!strmap_get(&map, str));
  49. ok1(errno == ENOENT);
  50. ok1(!strmap_get(&map, none));
  51. ok1(errno == ENOENT);
  52. /* Both at once... */
  53. ok1(strmap_add(&map, none, none));
  54. ok1(strmap_add(&map, str, val));
  55. ok1(strmap_get(&map, str) == val);
  56. ok1(strmap_get(&map, none) == none);
  57. ok1(strmap_del(&map, "does not exist", NULL) == NULL);
  58. ok1(strmap_del(&map, "", NULL) == none);
  59. ok1(strmap_get(&map, str) == val);
  60. ok1(strmap_del(&map, dup, &v) == str);
  61. ok1(v == val);
  62. ok1(strmap_empty(&map));
  63. free(dup);
  64. /* This exits depending on whether all tests passed */
  65. return exit_status();
  66. }