testc_str.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. ssize_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. const char *res = NULL;
  70. printf("Etalon %ld : [%s]\n", index, etalon[index]);
  71. r = strcmp(etalon[index], str);
  72. if (r < 0) {
  73. printf("Not equal %ld [%s]\n", index, str);
  74. return -1;
  75. }
  76. faux_str_free(str);
  77. index++;
  78. }
  79. if (index != num_etalon) {
  80. printf("Number of args is not equal real=%ld etalon=%ld\n", index, num_etalon);
  81. return -1;
  82. }
  83. return 0;
  84. }
  85. int testc_faux_str_numcmp(void)
  86. {
  87. if (faux_str_numcmp("abc2", "abc10") >= 0) {
  88. printf("'abc2' >= 'abc10'\n");
  89. return -1;
  90. }
  91. if (faux_str_numcmp("abc2ccc", "abc10ccc") >= 0) {
  92. printf("'abc2ccc' >= 'abc10ccc'\n");
  93. return -1;
  94. }
  95. if (faux_str_numcmp("abc2ccc", "abcaccc") >= 0) {
  96. printf("'abc2ccc' >= 'abcaccc'\n");
  97. return -1;
  98. }
  99. if (faux_str_numcmp("abc222222222222222ccc", "abc222222222222222cdc") >= 0) {
  100. printf("'abc222222222222222ccc' >= 'abc222222222222222cdc'\n");
  101. return -1;
  102. }
  103. // Overflow
  104. if (faux_str_numcmp("abc222222222222222222222222222222ccc", "abc1022222222222222222222222222222ccc") <= 0) {
  105. printf("'abc222222222222222222222222222222ccc' <= 'abc1022222222222222222222222222222ccc'\n");
  106. return -1;
  107. }
  108. return 0;
  109. }