testc_str.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. }