test_str.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <str.h>
  6. void test_str_split() {
  7. char *s = "abc 123 233 xyz";
  8. char **list = str_split(s, ' ');
  9. assert(list[4] == NULL);
  10. assert(list[3] != NULL);
  11. assert(strcmp(list[0], "abc") == 0);
  12. assert(strcmp(list[3], "xyz") == 0);
  13. destroy_str_list(list);
  14. s = "abc 123 233 xyz";
  15. list = str_split(s, ' ');
  16. assert(list[4] == NULL);
  17. assert(list[3] != NULL);
  18. assert(strcmp(list[0], "abc") == 0);
  19. assert(strcmp(list[3], "xyz") == 0);
  20. destroy_str_list(list);
  21. s = " abc 123 233 xyz";
  22. list = str_split(s, ' ');
  23. assert(list[4] == NULL);
  24. assert(list[3] != NULL);
  25. assert(strcmp(list[0], "abc") == 0);
  26. assert(strcmp(list[3], "xyz") == 0);
  27. destroy_str_list(list);
  28. s = " abc \t 123\n 233\nxyz";
  29. list = str_split(s, '\0');
  30. assert(list[4] == NULL);
  31. assert(list[3] != NULL);
  32. assert(strcmp(list[0], "abc") == 0);
  33. assert(strcmp(list[3], "xyz") == 0);
  34. destroy_str_list(list);
  35. s = "a";
  36. list = str_split(s, ' ');
  37. assert(list[1] == NULL);
  38. assert(list[0] != NULL);
  39. assert(strcmp(list[0], "a") == 0);
  40. destroy_str_list(list);
  41. s = "";
  42. list = str_split(s, ' ');
  43. assert(list[0] == NULL);
  44. destroy_str_list(list);
  45. s = " ";
  46. list = str_split(s, ' ');
  47. assert(list[0] == NULL);
  48. destroy_str_list(list);
  49. }
  50. void test_str_strip() {
  51. char *s;
  52. s = str_strip("hello ");
  53. assert(strcmp(s, "hello") == 0);
  54. s = str_strip("hello");
  55. assert(strcmp(s, "hello") == 0);
  56. s = str_strip("\nhello ");
  57. assert(strcmp(s, "hello") == 0);
  58. s = str_strip("\nhello");
  59. assert(strcmp(s, "hello") == 0);
  60. s = str_strip("");
  61. assert(strcmp(s, "") == 0);
  62. s = str_strip("\n\t ");
  63. assert(strcmp(s, "") == 0);
  64. s = str_strip(" ");
  65. assert(strcmp(s, "") == 0);
  66. }
  67. void test_str_bulider() {
  68. str_builder_t sb;
  69. init_str_builder(&sb);
  70. str_builder_append(&sb, "%s", "hello");
  71. assert(sb.size == 5);
  72. assert(strcmp(sb.buf, "hello") == 0);
  73. assert(strlen(sb.buf) == 5);
  74. str_builder_append(&sb, "hello");
  75. assert(sb.size == 10);
  76. assert(strcmp(sb.buf, "hellohello") == 0);
  77. str_builder_append(&sb, "%c1", 'c');
  78. assert(sb.size == 12);
  79. assert(strcmp(sb.buf, "hellohelloc1") == 0);
  80. str_builder_append_char(&sb, 'x');
  81. assert(sb.size == 13);
  82. assert(strcmp(sb.buf, "hellohelloc1x") == 0);
  83. }
  84. int main() {
  85. printf("[TEST] str\n");
  86. test_str_split();
  87. test_str_strip();
  88. printf("[PASS] str\n");
  89. return 0;
  90. }