1
0

testargdstr.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*******************************************************************************
  2. * This file is part of the argtable3 library.
  3. *
  4. * Copyright (C) 2013-2019 Tom G. Huang
  5. * <tomghuang@gmail.com>
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * * Neither the name of STEWART HEITMANN nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT,
  23. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. ******************************************************************************/
  30. #include <string.h>
  31. #include <time.h>
  32. #include <stdio.h>
  33. #include "CuTest.h"
  34. #include "argtable3.h"
  35. #if defined(_MSC_VER)
  36. #pragma warning(push)
  37. #pragma warning(disable : 4204)
  38. #endif
  39. void test_argdstr_basic_001(CuTest* tc) {
  40. arg_dstr_t ds = arg_dstr_create();
  41. arg_dstr_set(ds, "hello ", ARG_DSTR_VOLATILE);
  42. CuAssertTrue(tc, strcmp(arg_dstr_cstr(ds), "hello ") == 0);
  43. arg_dstr_cat(ds, "world");
  44. CuAssertTrue(tc, strcmp(arg_dstr_cstr(ds), "hello world") == 0);
  45. arg_dstr_destroy(ds);
  46. }
  47. void test_argdstr_basic_002(CuTest* tc) {
  48. arg_dstr_t ds = arg_dstr_create();
  49. arg_dstr_set(ds, "hello world", ARG_DSTR_VOLATILE);
  50. CuAssertTrue(tc, strcmp(arg_dstr_cstr(ds), "hello world") == 0);
  51. arg_dstr_reset(ds);
  52. CuAssertTrue(tc, strcmp(arg_dstr_cstr(ds), "") == 0);
  53. arg_dstr_set(ds, "good", ARG_DSTR_VOLATILE);
  54. CuAssertTrue(tc, strcmp(arg_dstr_cstr(ds), "good") == 0);
  55. arg_dstr_destroy(ds);
  56. }
  57. void test_argdstr_basic_003(CuTest* tc) {
  58. arg_dstr_t ds = arg_dstr_create();
  59. arg_dstr_cat(ds, "hello world");
  60. CuAssertTrue(tc, strcmp(arg_dstr_cstr(ds), "hello world") == 0);
  61. arg_dstr_destroy(ds);
  62. }
  63. void test_argdstr_basic_004(CuTest* tc) {
  64. arg_dstr_t ds = arg_dstr_create();
  65. arg_dstr_catf(ds, "%s %d", "hello world", 1);
  66. CuAssertTrue(tc, strcmp(arg_dstr_cstr(ds), "hello world 1") == 0);
  67. arg_dstr_destroy(ds);
  68. }
  69. void test_argdstr_basic_005(CuTest* tc) {
  70. arg_dstr_t ds = arg_dstr_create();
  71. arg_dstr_catf(ds, "%d.", 1);
  72. arg_dstr_catf(ds, "%d.", 2);
  73. arg_dstr_catf(ds, "%d.", 3);
  74. arg_dstr_cat(ds, "456");
  75. CuAssertTrue(tc, strcmp(arg_dstr_cstr(ds), "1.2.3.456") == 0);
  76. arg_dstr_destroy(ds);
  77. }
  78. void test_argdstr_basic_006(CuTest* tc) {
  79. int i;
  80. arg_dstr_t ds = arg_dstr_create();
  81. for (i = 0; i < 100000; i++) {
  82. arg_dstr_catf(ds, "%s", "1234567890");
  83. }
  84. CuAssertTrue(tc, strlen(arg_dstr_cstr(ds)) == 1000000);
  85. arg_dstr_destroy(ds);
  86. }
  87. CuSuite* get_argdstr_testsuite() {
  88. CuSuite* suite = CuSuiteNew();
  89. SUITE_ADD_TEST(suite, test_argdstr_basic_001);
  90. SUITE_ADD_TEST(suite, test_argdstr_basic_002);
  91. SUITE_ADD_TEST(suite, test_argdstr_basic_003);
  92. SUITE_ADD_TEST(suite, test_argdstr_basic_004);
  93. SUITE_ADD_TEST(suite, test_argdstr_basic_005);
  94. SUITE_ADD_TEST(suite, test_argdstr_basic_006);
  95. return suite;
  96. }
  97. #if defined(_MSC_VER)
  98. #pragma warning(pop)
  99. #endif