run-prefix.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include <ccan/strmap/strmap.h>
  2. #include <ccan/strmap/strmap.c>
  3. #include <ccan/tap/tap.h>
  4. #include <stdio.h>
  5. /* Must be > 100, see below. */
  6. #define NUM 200
  7. static bool in_order(const char *index, char *value, unsigned int *count)
  8. {
  9. int i = atoi(index);
  10. ok1(i == atoi(value));
  11. ok1(*count == i);
  12. (*count)++;
  13. return true;
  14. }
  15. static bool find_empty(const char *index, char *value, char *empty)
  16. {
  17. if (index == empty)
  18. pass("Found empty entry!");
  19. return true;
  20. }
  21. int main(void)
  22. {
  23. struct map {
  24. STRMAP_MEMBERS(char *);
  25. };
  26. struct map map;
  27. const struct map *sub;
  28. unsigned int i;
  29. char *str[NUM], *empty;
  30. plan_tests(8 + 2 * (1 + 10 + 100) + 1);
  31. strmap_init(&map);
  32. for (i = 0; i < NUM; i++) {
  33. char template[10];
  34. sprintf(template, "%08u", i);
  35. str[i] = strdup(template);
  36. }
  37. /* All prefixes of an empty map are empty. */
  38. sub = strmap_prefix(&map, "a");
  39. ok1(strmap_empty(sub));
  40. sub = strmap_prefix(&map, "");
  41. ok1(strmap_empty(sub));
  42. for (i = 0; i < NUM; i++)
  43. strmap_add(&map, str[i], str[i]+1);
  44. /* Nothing */
  45. sub = strmap_prefix(&map, "a");
  46. ok1(strmap_empty(sub));
  47. /* Everything */
  48. sub = strmap_prefix(&map, "0");
  49. ok1(sub->raw.u.n == map.raw.u.n);
  50. sub = strmap_prefix(&map, "");
  51. ok1(sub->raw.u.n == map.raw.u.n);
  52. /* Single. */
  53. sub = strmap_prefix(&map, "00000000");
  54. i = 0;
  55. strmap_iterate(sub, in_order, &i);
  56. ok1(i == 1);
  57. /* First 10. */
  58. sub = strmap_prefix(&map, "0000000");
  59. i = 0;
  60. strmap_iterate(sub, in_order, &i);
  61. ok1(i == 10);
  62. /* First 100. */
  63. sub = strmap_prefix(&map, "000000");
  64. i = 0;
  65. strmap_iterate(sub, in_order, &i);
  66. ok1(i == 100);
  67. /* Everything, *plus* empty string. */
  68. empty = strdup("");
  69. strmap_add(&map, empty, empty);
  70. sub = strmap_prefix(&map, "");
  71. /* Check we get *our* empty string back! */
  72. strmap_iterate(sub, find_empty, empty);
  73. strmap_clear(&map);
  74. for (i = 0; i < NUM; i++)
  75. free(str[i]);
  76. free(empty);
  77. /* This exits depending on whether all tests passed */
  78. return exit_status();
  79. }