check_std_c_str.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * libcsync -- a library to sync a directory with another
  3. *
  4. * Copyright (c) 2008-2013 by Andreas Schneider <asn@cryptomilk.org>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <errno.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include "torture.h"
  25. #include "std/c_string.h"
  26. static void check_c_streq_equal(void **state)
  27. {
  28. (void) state; /* unused */
  29. assert_true(c_streq("test", "test"));
  30. }
  31. static void check_c_streq_not_equal(void **state)
  32. {
  33. (void) state; /* unused */
  34. assert_false(c_streq("test", "test2"));
  35. }
  36. static void check_c_streq_null(void **state)
  37. {
  38. (void) state; /* unused */
  39. assert_false(c_streq(NULL, "test"));
  40. assert_false(c_streq("test", NULL));
  41. assert_false(c_streq(NULL, NULL));
  42. }
  43. static void check_c_strlist_new(void **state)
  44. {
  45. c_strlist_t *strlist = NULL;
  46. (void) state; /* unused */
  47. strlist = c_strlist_new(42);
  48. assert_non_null(strlist);
  49. assert_int_equal(strlist->size, 42);
  50. assert_int_equal(strlist->count, 0);
  51. c_strlist_destroy(strlist);
  52. }
  53. static void check_c_strlist_add(void **state)
  54. {
  55. int rc;
  56. size_t i = 0;
  57. c_strlist_t *strlist = NULL;
  58. (void) state; /* unused */
  59. strlist = c_strlist_new(42);
  60. assert_non_null(strlist);
  61. assert_int_equal(strlist->size, 42);
  62. assert_int_equal(strlist->count, 0);
  63. for (i = 0; i < strlist->size; i++) {
  64. rc = c_strlist_add(strlist, (char *) "foobar");
  65. assert_int_equal(rc, 0);
  66. }
  67. assert_int_equal(strlist->count, 42);
  68. assert_string_equal(strlist->vector[0], "foobar");
  69. assert_string_equal(strlist->vector[41], "foobar");
  70. c_strlist_destroy(strlist);
  71. }
  72. static void check_c_strlist_expand(void **state)
  73. {
  74. c_strlist_t *strlist;
  75. size_t i = 0;
  76. int rc;
  77. (void) state; /* unused */
  78. strlist = c_strlist_new(42);
  79. assert_non_null(strlist);
  80. assert_int_equal(strlist->size, 42);
  81. assert_int_equal(strlist->count, 0);
  82. strlist = c_strlist_expand(strlist, 84);
  83. assert_non_null(strlist);
  84. assert_int_equal(strlist->size, 84);
  85. for (i = 0; i < strlist->size; i++) {
  86. rc = c_strlist_add(strlist, (char *) "foobar");
  87. assert_int_equal(rc, 0);
  88. }
  89. c_strlist_destroy(strlist);
  90. }
  91. int torture_run_tests(void)
  92. {
  93. const UnitTest tests[] = {
  94. unit_test(check_c_streq_equal),
  95. unit_test(check_c_streq_not_equal),
  96. unit_test(check_c_streq_null),
  97. unit_test(check_c_strlist_new),
  98. unit_test(check_c_strlist_add),
  99. unit_test(check_c_strlist_expand),
  100. };
  101. return run_tests(tests);
  102. }