testc_str.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "faux/str.h"
  5. int testc_faux_str_nextword(void)
  6. {
  7. const char* line = "asd\"\\\"\"mmm \"``\" `ll\"l\\p\\\\m```j`j`` ```kk``pp``` ll\\ l jj\\\"kk ll\\\\nn \"aaa\"bbb`ccc```ddd``eee ``lk\\\"";
  8. const char* etalon[] = {
  9. "asd\"mmm",
  10. "``",
  11. "ll\"l\\p\\\\mj`j",
  12. "kk``pp",
  13. "ll l",
  14. "jj\"kk",
  15. "ll\\nn",
  16. "aaabbbcccdddeee",
  17. "lk\\\"", // Unclosed quotes
  18. NULL
  19. };
  20. int retval = 0;
  21. int i = 0;
  22. const char *saveptr = line;
  23. bool_t closed_quotes = BOOL_FALSE;
  24. printf("Line : [%s]\n", line);
  25. for (i = 0; etalon[i]; i++) {
  26. int r = -1;
  27. char *res = NULL;
  28. printf("Etalon %d : [%s]\n", i, etalon[i]);
  29. res = faux_str_nextword(saveptr, &saveptr, "`", &closed_quotes);
  30. if (!res) {
  31. printf("The faux_str_nextword() return value is NULL\n");
  32. break;
  33. } else {
  34. printf("Result %d : [%s]\n", i, res);
  35. }
  36. r = strcmp(etalon[i], res);
  37. if (r < 0) {
  38. printf("Not equal %d\n", i);
  39. retval = -1;
  40. }
  41. faux_str_free(res);
  42. }
  43. // Last quote is unclosed
  44. if (closed_quotes) {
  45. printf("Closed quotes flag is wrong\n");
  46. retval = -1;
  47. } else {
  48. printf("Really unclosed quotes\n");
  49. }
  50. return retval;
  51. }
  52. int testc_faux_str_getline(void)
  53. {
  54. const char* line = "arg 0\narg 1\narg 2";
  55. const char* etalon[] = {
  56. "arg 0",
  57. "arg 1",
  58. "arg 2",
  59. NULL
  60. };
  61. size_t num_etalon = 3;
  62. size_t index = 0;
  63. char *str = NULL;
  64. const char *saveptr = NULL;
  65. printf("Line : [%s]\n", line);
  66. saveptr = line;
  67. while ((str = faux_str_getline(saveptr, &saveptr)) && (index < num_etalon)) {
  68. int r = -1;
  69. printf("Etalon %ld : [%s]\n", index, etalon[index]);
  70. r = strcmp(etalon[index], str);
  71. if (r < 0) {
  72. printf("Not equal %ld [%s]\n", index, str);
  73. return -1;
  74. }
  75. faux_str_free(str);
  76. index++;
  77. }
  78. if (index != num_etalon) {
  79. printf("Number of args is not equal real=%ld etalon=%ld\n", index, num_etalon);
  80. return -1;
  81. }
  82. return 0;
  83. }
  84. int testc_faux_str_numcmp(void)
  85. {
  86. if (faux_str_numcmp("abc2", "abc10") >= 0) {
  87. printf("'abc2' >= 'abc10'\n");
  88. return -1;
  89. }
  90. if (faux_str_numcmp("abc2ccc", "abc10ccc") >= 0) {
  91. printf("'abc2ccc' >= 'abc10ccc'\n");
  92. return -1;
  93. }
  94. if (faux_str_numcmp("abc2ccc", "abcaccc") >= 0) {
  95. printf("'abc2ccc' >= 'abcaccc'\n");
  96. return -1;
  97. }
  98. if (faux_str_numcmp("abc222222222222222ccc", "abc222222222222222cdc") >= 0) {
  99. printf("'abc222222222222222ccc' >= 'abc222222222222222cdc'\n");
  100. return -1;
  101. }
  102. // Overflow
  103. if (faux_str_numcmp("abc222222222222222222222222222222ccc", "abc1022222222222222222222222222222ccc") <= 0) {
  104. printf("'abc222222222222222222222222222222ccc' <= 'abc1022222222222222222222222222222ccc'\n");
  105. return -1;
  106. }
  107. return 0;
  108. }
  109. int testc_faux_str_c_esc_quote(void)
  110. {
  111. char *src = NULL;
  112. char *etalon = NULL;
  113. char *esc = NULL;
  114. src = "aaa\\bbb\"";
  115. etalon = "aaa\\\\bbb\\\"";
  116. esc = faux_str_c_esc_quote(src);
  117. if (strcmp(esc, etalon) != 0) {
  118. printf("Problem with string without spaces\n");
  119. printf("src=[%s], etalon=[%s], esc=[%s]\n",
  120. src, etalon, esc);
  121. return -1;
  122. }
  123. faux_str_free(esc);
  124. src = "aaa\\ bbb\"";
  125. etalon = "\"aaa\\\\ bbb\\\"\"";
  126. esc = faux_str_c_esc_quote(src);
  127. if (strcmp(esc, etalon) != 0) {
  128. printf("Problem with string with spaces\n");
  129. printf("src=[%s], etalon=[%s], esc=[%s]\n",
  130. src, etalon, esc);
  131. return -1;
  132. }
  133. faux_str_free(esc);
  134. return 0;
  135. }