Browse Source

faux.str: Fix faux_str_nextword(). Update correspondent test

Serj Kalichev 4 years ago
parent
commit
15f59ed7e0
2 changed files with 33 additions and 12 deletions
  1. 2 0
      faux/str/str.c
  2. 31 12
      faux/str/testc_str.c

+ 2 - 0
faux/str/str.c

@@ -728,6 +728,8 @@ char *faux_str_nextword(const char *str, const char **saveptr,
 					faux_str_cat(&result, s);
 					faux_str_free(s);
 				}
+				word = string;
+				len = 0;
 				break;
 			// Escaping
 			} else if (*string == '\\') {

+ 31 - 12
faux/str/testc_str.c

@@ -7,21 +7,40 @@
 
 int testc_faux_str_nextword(void)
 {
-	const char* line = "asd\"\\\"\"mmm   lll";
-	const char* etalon = "asd\"mmm";
-	char *res = NULL;
-	int retval = -1;
+	const char* line = "asd\"\\\"\"mmm  `ll\"l\\p\\\\m```j`j`` ```kk``pp``` ll\\ l  \"aaa\"bbb`ccc```ddd``eee ``lk\\\"";
+	const char* etalon[] = {
+		"asd\"mmm",
+		"ll\"l\\p\\\\mj`j",
+		"kk``pp",
+		"ll l",
+		"aaabbbcccdddeee",
+		"lk\\\"", // Unclosed quotes
+		NULL
+		 };
+	int retval = 0;
+	int i = 0;
+	const char *saveptr = line;
 
 	printf("Line   : [%s]\n", line);
-	printf("Etalon : [%s]\n", etalon);
 
-	res = faux_str_nextword(line, NULL, NULL);
-	if (!res)
-		printf("The faux_str_nextword() return value is NULL\n");
-	else
-		printf("Result : [%s]\n", res);
-	retval = strcmp(etalon, res);
-	faux_str_free(res);
+	for (i = 0; etalon[i]; i++) {
+		int r = -1;
+		char *res = NULL;
+		printf("Etalon %d : [%s]\n", i, etalon[i]);
+		res = faux_str_nextword(saveptr, &saveptr, "`");
+		if (!res) {
+			printf("The faux_str_nextword() return value is NULL\n");
+			break;
+		} else {
+			printf("Result %d : [%s]\n", i, res);
+		}
+		r = strcmp(etalon[i], res);
+		if (r < 0) {
+			printf("Not equal %d\n", i);
+			retval = -1;
+		}
+		faux_str_free(res);
+	}
 
 	return retval;
 }