test_str.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* _
  2. * ___ __ _ __ _ _ _(_)
  3. * / __|/ _` |/ _` | | | | |
  4. * \__ \ (_| | (_| | |_| | |
  5. * |___/\__,_|\__, |\__,_|_|
  6. * |___/
  7. *
  8. * Cross-platform library which helps to develop web servers or frameworks.
  9. *
  10. * Copyright (C) 2016-2024 Silvio Clecio <[email protected]>
  11. *
  12. * Sagui library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * Sagui library is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with Sagui library; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. */
  26. #include "sg_assert.h"
  27. #include <errno.h>
  28. #include <string.h>
  29. #include <sagui.h>
  30. static void test_str_write(struct sg_str *str, const char *val, size_t len) {
  31. ASSERT(sg_str_write(NULL, val, len) == EINVAL);
  32. ASSERT(sg_str_write(str, NULL, len) == EINVAL);
  33. ASSERT(sg_str_write(str, val, 0) == EINVAL);
  34. sg_str_clear(str);
  35. ASSERT(sg_str_write(str, val, len) == 0);
  36. ASSERT(sg_str_length(str) == len);
  37. }
  38. static void test_str_printf_va(struct sg_str *str, const char *fmt,
  39. va_list ap) {
  40. ASSERT(sg_str_printf_va(NULL, fmt, ap) == EINVAL);
  41. ASSERT(sg_str_printf_va(str, NULL, ap) == EINVAL);
  42. #if !(defined(__arm__) || defined(__arm64__))
  43. ASSERT(sg_str_printf_va(str, fmt, NULL) == EINVAL);
  44. #endif /* !(__arm__ || __arm64__) */
  45. sg_str_clear(str);
  46. sg_str_printf_va(str, fmt, ap);
  47. ASSERT(strcmp(sg_str_content(str), "abc123def456") == 0);
  48. }
  49. static void test_str_printf(struct sg_str *str, const char *fmt, ...) {
  50. va_list ap;
  51. ASSERT(sg_str_printf(NULL, "%s", "") == EINVAL);
  52. ASSERT(sg_str_printf(str, NULL) == EINVAL); //-V::575,618
  53. sg_str_clear(str);
  54. ASSERT(sg_str_printf(str, "%s", "") == 0);
  55. ASSERT(strlen(sg_str_content(str)) == 0);
  56. ASSERT(sg_str_printf(str, "%s%d", "abc", 123) == 0);
  57. ASSERT(strcmp(sg_str_content(str), "abc123") == 0);
  58. va_start(ap, fmt);
  59. test_str_printf_va(str, fmt, ap);
  60. va_end(ap);
  61. }
  62. static void test_str_content(struct sg_str *str, const char *val, size_t len) {
  63. errno = 0;
  64. ASSERT(!sg_str_content(NULL));
  65. ASSERT(errno == EINVAL);
  66. sg_str_clear(str);
  67. ASSERT(strlen(sg_str_content(str)) == 0);
  68. sg_str_write(str, val, len);
  69. ASSERT(strcmp(sg_str_content(str), val) == 0);
  70. }
  71. static void test_str_length(struct sg_str *str, const char *val, size_t len) {
  72. errno = 0;
  73. ASSERT(sg_str_length(NULL) == 0);
  74. ASSERT(errno == EINVAL);
  75. sg_str_clear(str);
  76. ASSERT(sg_str_length(str) == 0);
  77. sg_str_write(str, val, len);
  78. ASSERT(sg_str_length(str) == len);
  79. }
  80. static void test_str_clear(struct sg_str *str, const char *val, size_t len) {
  81. ASSERT(sg_str_clear(NULL) == EINVAL);
  82. sg_str_clear(str);
  83. sg_str_write(str, val, len);
  84. ASSERT(sg_str_length(str) > 0);
  85. sg_str_clear(str);
  86. ASSERT(sg_str_length(str) == 0);
  87. }
  88. int main(void) {
  89. struct sg_str *str;
  90. const char *val = "abc123def456";
  91. const size_t len = strlen(val);
  92. str = sg_str_new();
  93. ASSERT(str);
  94. test_str_write(str, val, len);
  95. test_str_printf(str, "%s%d%s%d", "abc", 123, "def", 456);
  96. /* the function `sg_str_printf_va()` is already tested by `test_str_printf()`. */
  97. test_str_content(str, val, len);
  98. test_str_length(str, val, len);
  99. test_str_clear(str, val, len);
  100. sg_str_free(str);
  101. return EXIT_SUCCESS;
  102. }