misc_trim_test.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include <CUnit/CUnit.h>
  2. #include <CUnit/Basic.h>
  3. #include <misc_mosq.h>
  4. static void rtrim_helper(const char *expected, char *buf)
  5. {
  6. char *res;
  7. res = misc__trimblanks(buf);
  8. CU_ASSERT_PTR_NOT_NULL(res);
  9. if(res){
  10. CU_ASSERT_EQUAL(strlen(buf), strlen(res));
  11. CU_ASSERT_STRING_EQUAL(res, expected);
  12. CU_ASSERT_PTR_EQUAL(res, buf);
  13. }
  14. }
  15. static void ltrim_helper(const char *expected, char *buf)
  16. {
  17. char *res;
  18. res = misc__trimblanks(buf);
  19. CU_ASSERT_PTR_NOT_NULL(res);
  20. if(res){
  21. CU_ASSERT_EQUAL(strlen(expected), strlen(res));
  22. CU_ASSERT_STRING_EQUAL(res, expected);
  23. }
  24. }
  25. static void TEST_null_input(void)
  26. {
  27. char *res;
  28. res = misc__trimblanks(NULL);
  29. CU_ASSERT_PTR_NULL(res);
  30. }
  31. static void TEST_empty_input(void)
  32. {
  33. char buf[10];
  34. char *res;
  35. memset(buf, 0, sizeof(buf));
  36. res = misc__trimblanks(buf);
  37. CU_ASSERT_PTR_NOT_NULL(res);
  38. if(res){
  39. CU_ASSERT_STRING_EQUAL(res, "");
  40. }
  41. }
  42. static void TEST_no_blanks(void)
  43. {
  44. char buf[10] = "noblanks";
  45. rtrim_helper("noblanks", buf);
  46. }
  47. static void TEST_rtrim(void)
  48. {
  49. char buf1[20] = "spaces ";
  50. char buf2[20] = "spaces ";
  51. char buf3[20] = "spaces ";
  52. char buf4[20] = "spaces ";
  53. char buf5[20] = "tabs\t";
  54. char buf6[20] = "tabs\t\t";
  55. char buf7[20] = "tabs\t\t\t";
  56. char buf8[20] = "tabs\t\t\t\t";
  57. char buf9[20] = "mixed \t";
  58. char buf10[20] = "mixed\t ";
  59. char buf11[20] = "mixed\t\t ";
  60. char buf12[20] = "mixed \t \t ";
  61. rtrim_helper("spaces", buf1);
  62. rtrim_helper("spaces", buf2);
  63. rtrim_helper("spaces", buf3);
  64. rtrim_helper("spaces", buf4);
  65. rtrim_helper("tabs", buf5);
  66. rtrim_helper("tabs", buf6);
  67. rtrim_helper("tabs", buf7);
  68. rtrim_helper("tabs", buf8);
  69. rtrim_helper("mixed", buf9);
  70. rtrim_helper("mixed", buf10);
  71. rtrim_helper("mixed", buf11);
  72. rtrim_helper("mixed", buf12);
  73. }
  74. static void TEST_ltrim(void)
  75. {
  76. char buf1[20] = " spaces";
  77. char buf2[20] = " spaces";
  78. char buf3[20] = " spaces";
  79. char buf4[20] = " spaces";
  80. char buf5[20] = "\ttabs";
  81. char buf6[20] = "\t\ttabs";
  82. char buf7[20] = "\t\t\ttabs";
  83. char buf8[20] = "\t\t\t\ttabs";
  84. char buf9[20] = "\t mixed";
  85. char buf10[20] = " \tmixed";
  86. char buf11[20] = " \t\tmixed";
  87. char buf12[20] = "\t \t mixed";
  88. ltrim_helper("spaces", buf1);
  89. ltrim_helper("spaces", buf2);
  90. ltrim_helper("spaces", buf3);
  91. ltrim_helper("spaces", buf4);
  92. ltrim_helper("tabs", buf5);
  93. ltrim_helper("tabs", buf6);
  94. ltrim_helper("tabs", buf7);
  95. ltrim_helper("tabs", buf8);
  96. ltrim_helper("mixed", buf9);
  97. ltrim_helper("mixed", buf10);
  98. ltrim_helper("mixed", buf11);
  99. ltrim_helper("mixed", buf12);
  100. }
  101. static void TEST_btrim(void)
  102. {
  103. char buf1[20] = " spaces ";
  104. char buf2[20] = " spaces ";
  105. char buf3[20] = " spaces ";
  106. char buf4[20] = " spaces ";
  107. char buf5[20] = "\ttabs\t";
  108. char buf6[20] = "\t\ttabs\t\t";
  109. char buf7[20] = "\t\t\ttabs\t\t\t";
  110. char buf8[20] = "\t\t\t\ttabs\t\t\t\t";
  111. char buf9[20] = "\t mixed \t";
  112. char buf10[20] = " \tmixed\t ";
  113. char buf11[20] = " \t\tmixed\t\t ";
  114. char buf12[20] = "\t \t mixed \t \t ";
  115. ltrim_helper("spaces", buf1);
  116. ltrim_helper("spaces", buf2);
  117. ltrim_helper("spaces", buf3);
  118. ltrim_helper("spaces", buf4);
  119. ltrim_helper("tabs", buf5);
  120. ltrim_helper("tabs", buf6);
  121. ltrim_helper("tabs", buf7);
  122. ltrim_helper("tabs", buf8);
  123. ltrim_helper("mixed", buf9);
  124. ltrim_helper("mixed", buf10);
  125. ltrim_helper("mixed", buf11);
  126. ltrim_helper("mixed", buf12);
  127. }
  128. /* ========================================================================
  129. * TEST SUITE SETUP
  130. * ======================================================================== */
  131. int init_misc_trim_tests(void)
  132. {
  133. CU_pSuite test_suite = NULL;
  134. test_suite = CU_add_suite("Misc string trim", NULL, NULL);
  135. if(!test_suite){
  136. printf("Error adding CUnit Misc string trim test suite.\n");
  137. return 1;
  138. }
  139. if(0
  140. || !CU_add_test(test_suite, "Null input", TEST_null_input)
  141. || !CU_add_test(test_suite, "Empty input", TEST_empty_input)
  142. || !CU_add_test(test_suite, "No blanks", TEST_no_blanks)
  143. || !CU_add_test(test_suite, "Right trim", TEST_rtrim)
  144. || !CU_add_test(test_suite, "Left trim", TEST_ltrim)
  145. || !CU_add_test(test_suite, "Both trim", TEST_btrim)
  146. ){
  147. printf("Error adding Misc topic CUnit tests.\n");
  148. return 1;
  149. }
  150. return 0;
  151. }