testc_argv.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "faux/argv.h"
  5. int testc_faux_argv_parse(void)
  6. {
  7. faux_argv_t *fargv = NULL;
  8. 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\\\"";
  9. const char* etalon[] = {
  10. "asd\"mmm",
  11. "``",
  12. "ll\"l\\p\\\\mj`j",
  13. "kk``pp",
  14. "ll l",
  15. "jj\"kk",
  16. "ll\\nn",
  17. "aaabbbcccdddeee",
  18. "lk\\\"", // Unclosed quotes
  19. NULL
  20. };
  21. int retval = 0;
  22. int i = 0;
  23. ssize_t num = 0;
  24. ssize_t num_etalon = 9;
  25. faux_argv_node_t *iter = NULL;
  26. printf("Line : [%s]\n", line);
  27. fargv = faux_argv_new();
  28. faux_argv_quotes(fargv, "`");
  29. num = faux_argv_parse(fargv, line);
  30. if (num < 0) {
  31. printf("Error: Can't parse line\n");
  32. faux_argv_free(fargv);
  33. return -1;
  34. }
  35. if (num != num_etalon) {
  36. printf("Error: Wrong argument number\n");
  37. printf("Real number : %ld\n", num);
  38. printf("Etalon number : %ld\n", num_etalon);
  39. faux_argv_free(fargv);
  40. retval = -1;
  41. }
  42. iter = faux_argv_iter(fargv);
  43. for (i = 0; etalon[i]; i++) {
  44. int r = -1;
  45. const char *res = NULL;
  46. printf("Etalon %d : [%s]\n", i, etalon[i]);
  47. res = faux_argv_each(&iter);
  48. if (!res) {
  49. printf("The faux_argv_each() return value is NULL\n");
  50. break;
  51. } else {
  52. printf("Result %d : [%s]\n", i, res);
  53. }
  54. r = strcmp(etalon[i], res);
  55. if (r < 0) {
  56. printf("Not equal %d\n", i);
  57. retval = -1;
  58. }
  59. }
  60. // Last quote is unclosed
  61. if (!faux_argv_is_continuable(fargv)) {
  62. printf("Continuable flag is wrong\n");
  63. retval = -1;
  64. } else {
  65. printf("Continuable flag is on\n");
  66. }
  67. return retval;
  68. }
  69. int testc_faux_argv_is_continuable(void)
  70. {
  71. faux_argv_t *fargv = NULL;
  72. 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\\\" ";
  73. int retval = 0;
  74. ssize_t num = 0;
  75. printf("Line : [%s]\n", line);
  76. fargv = faux_argv_new();
  77. faux_argv_quotes(fargv, "`");
  78. num = faux_argv_parse(fargv, line);
  79. if (num < 0) {
  80. printf("Error: Can't parse line\n");
  81. faux_argv_free(fargv);
  82. return -1;
  83. }
  84. // Not continuable
  85. if (faux_argv_is_continuable(fargv)) {
  86. printf("Continuable flag is wrong\n");
  87. retval = -1;
  88. } else {
  89. printf("Line is not continuable\n");
  90. }
  91. return retval;
  92. }