aboutsummaryrefslogtreecommitdiffhomepage
path: root/fish_tests.cpp
diff options
context:
space:
mode:
authorGravatar ridiculousfish <corydoras@ridiculousfish.com>2013-11-24 23:21:00 -0800
committerGravatar ridiculousfish <corydoras@ridiculousfish.com>2013-11-24 23:21:00 -0800
commit34540babdb6cc527a53003dcb5a13c961bf6ddc7 (patch)
tree2ceea618983a029e1634cd4bd389d626d3d74d3f /fish_tests.cpp
parent5d84e86d89e532d1a6979e2ed8528a05a7b7de9a (diff)
parent9f6223311e7ae6a9d6d21e33bf0fa67822da6fb6 (diff)
Merge branch 'master' into ast
Conflicts: complete.cpp fish_tests.cpp
Diffstat (limited to 'fish_tests.cpp')
-rw-r--r--fish_tests.cpp77
1 files changed, 48 insertions, 29 deletions
diff --git a/fish_tests.cpp b/fish_tests.cpp
index 6b5b2dcc..153ca70e 100644
--- a/fish_tests.cpp
+++ b/fish_tests.cpp
@@ -86,8 +86,7 @@ static bool should_test_function(const char *func_name)
/**
The number of tests to run
*/
-//#define ESCAPE_TEST_COUNT 1000000
-#define ESCAPE_TEST_COUNT 10000
+#define ESCAPE_TEST_COUNT 100000
/**
The average length of strings to unescape
*/
@@ -139,45 +138,65 @@ static void err(const wchar_t *blah, ...)
wprintf(L"\n");
}
+/* Test sane escapes */
+static void test_unescape_sane()
+{
+ const struct test_t {const wchar_t * input; const wchar_t * expected;} tests[] =
+ {
+ {L"abcd", L"abcd"},
+ {L"'abcd'", L"abcd"},
+ {L"'abcd\\n'", L"abcd\\n"},
+ {L"\"abcd\\n\"", L"abcd\\n"},
+ {L"\"abcd\\n\"", L"abcd\\n"},
+ {L"\\143", L"c"},
+ {L"'\\143'", L"\\143"},
+ {L"\\n", L"\n"} // \n normally becomes newline
+ };
+ wcstring output;
+ for (size_t i=0; i < sizeof tests / sizeof *tests; i++)
+ {
+ bool ret = unescape_string(tests[i].input, &output, UNESCAPE_DEFAULT);
+ if (! ret)
+ {
+ err(L"Failed to unescape '%ls'\n", tests[i].input);
+ }
+ else if (output != tests[i].expected)
+ {
+ err(L"In unescaping '%ls', expected '%ls' but got '%ls'\n", tests[i].input, tests[i].expected, output.c_str());
+ }
+ }
+}
+
/**
Test the escaping/unescaping code by escaping/unescaping random
strings and verifying that the original string comes back.
*/
-static void test_escape()
-{
- int i;
- wcstring sb;
+static void test_escape_crazy()
+{
say(L"Testing escaping and unescaping");
-
- for (i=0; i<ESCAPE_TEST_COUNT; i++)
+ wcstring random_string;
+ wcstring escaped_string;
+ wcstring unescaped_string;
+ for (size_t i=0; i<ESCAPE_TEST_COUNT; i++)
{
- const wchar_t *o, *e, *u;
-
- sb.clear();
+ random_string.clear();
while (rand() % ESCAPE_TEST_LENGTH)
{
- sb.push_back((rand() %ESCAPE_TEST_CHAR) +1);
- }
- o = (const wchar_t *)sb.c_str();
- e = escape(o, 1);
- u = unescape(e, 0);
- if (!o || !e || !u)
- {
- err(L"Escaping cycle of string %ls produced null pointer on %ls", o, e?L"unescaping":L"escaping");
-
+ random_string.push_back((rand() % ESCAPE_TEST_CHAR) +1);
}
+ escaped_string = escape_string(random_string, ESCAPE_ALL);
+ bool unescaped_success = unescape_string(escaped_string, &unescaped_string, UNESCAPE_DEFAULT);
- if (wcscmp(o, u))
+ if (! unescaped_success)
{
- err(L"Escaping cycle of string %ls produced different string %ls", o, u);
-
-
+ err(L"Failed to unescape string <%ls>", escaped_string.c_str());
+ }
+ else if (unescaped_string != random_string)
+ {
+ err(L"Escaped and then unescaped string '%ls', but got back a different string '%ls'", random_string.c_str(), unescaped_string.c_str());
}
- free((void *)e);
- free((void *)u);
-
}
}
@@ -2324,9 +2343,9 @@ int main(int argc, char **argv)
//if (should_test_function("new_parser_fuzzing")) test_new_parser_fuzzing(); //fuzzing is expensive
if (should_test_function("new_parser_correctness")) test_new_parser_correctness();
if (should_test_function("new_parser")) test_new_parser();
-
+ if (should_test_function("escape")) test_unescape_sane();
+ if (should_test_function("escape")) test_escape_crazy();
if (should_test_function("format")) test_format();
- if (should_test_function("escape")) test_escape();
if (should_test_function("convert")) test_convert();
if (should_test_function("convert_nulls")) test_convert_nulls();
if (should_test_function("tok")) test_tok();