[m-rev.] for review: Test string case folding and case-insensitive comparison.

Peter Wang novalazy at gmail.com
Mon Jun 20 14:13:15 AEST 2016


tests/hard_coded/Mmakefile:
tests/hard_coded/string_case.m:
tests/hard_coded/string_case.exp:
	Add tests string.to_lower, string.to_upper and string.compare_ci_ascii.
---
 tests/hard_coded/Mmakefile       |   1 +
 tests/hard_coded/string_case.exp |   6 +++
 tests/hard_coded/string_case.m   | 111 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 118 insertions(+)
 create mode 100644 tests/hard_coded/string_case.exp
 create mode 100644 tests/hard_coded/string_case.m

diff --git a/tests/hard_coded/Mmakefile b/tests/hard_coded/Mmakefile
index d6cc299..d2dc634 100644
--- a/tests/hard_coded/Mmakefile
+++ b/tests/hard_coded/Mmakefile
@@ -288,6 +288,7 @@ ORDINARY_PROGS =	\
 	string_append_ioi \
 	string_append_ooi \
 	string_builder_test \
+	string_case \
 	string_class \
 	string_code_unit \
 	string_codepoint \
diff --git a/tests/hard_coded/string_case.exp b/tests/hard_coded/string_case.exp
new file mode 100644
index 0000000..2070beb
--- /dev/null
+++ b/tests/hard_coded/string_case.exp
@@ -0,0 +1,6 @@
+testing string.to_lower(in, out)
+testing string.to_upper(in, out)
+testing string.to_lower(in, in)
+testing string.to_upper(in, in)
+testing string.compare_ci_ascii
+done.
diff --git a/tests/hard_coded/string_case.m b/tests/hard_coded/string_case.m
new file mode 100644
index 0000000..b338d30
--- /dev/null
+++ b/tests/hard_coded/string_case.m
@@ -0,0 +1,111 @@
+%---------------------------------------------------------------------------%
+% vim: ts=4 sw=4 et ft=mercury
+%---------------------------------------------------------------------------%
+
+:- module string_case.
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is cc_multi.
+
+%---------------------------------------------------------------------------%
+%---------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module char.
+:- import_module list.
+:- import_module require.
+:- import_module solutions.
+:- import_module string.
+
+%---------------------------------------------------------------------------%
+
+main(!IO) :-
+    write_string("testing string.to_lower(in, out)\n", !IO),
+    foldl(test_to_lower_in_out, strings, !IO),
+
+    write_string("testing string.to_upper(in, out)\n", !IO),
+    foldl(test_to_upper_in_out, strings, !IO),
+
+    write_string("testing string.to_lower(in, in)\n", !IO),
+    unsorted_aggregate(string_pairs, test_to_lower_in_in, !IO),
+
+    write_string("testing string.to_upper(in, in)\n", !IO),
+    unsorted_aggregate(string_pairs, test_to_upper_in_in, !IO),
+
+    write_string("testing string.compare_ci_ascii\n", !IO),
+    aggregate(string_pairs, test_compare_ci_ascii, !IO),
+
+    write_string("done.\n", !IO).
+
+:- func strings = list(string).
+
+strings = [
+    "",
+    "0123456789",
+    "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
+    "abcdefghijklmnopqrstuvwxyz",
+    "Dog",
+    "DOGgie",
+    "็‹—ไป”\U0001F415dog",
+    "็‹—ไป”\U0001F415DOG"
+].
+
+:- pred string_pairs({string, string}::out) is nondet.
+
+string_pairs({X, Y}) :-
+    member(X, strings),
+    member(Y, strings).
+
+:- pred test_to_lower_in_out(string::in, io::di, io::uo) is det.
+
+test_to_lower_in_out(S, !IO) :-
+    string.to_lower(S, LowerS),
+
+    string.to_char_list(S, Chars),
+    list.map(char.to_lower, Chars, LowerChars),
+    string.from_char_list(LowerChars, Expected),
+
+    expect(unify(LowerS, Expected), $module, $pred, "LowerS != Expected").
+
+:- pred test_to_upper_in_out(string::in, io::di, io::uo) is det.
+
+test_to_upper_in_out(S, !IO) :-
+    string.to_upper(S, UpperS),
+
+    string.to_char_list(S, Chars),
+    list.map(char.to_upper, Chars, UpperChars),
+    string.from_char_list(UpperChars, Expected),
+
+    expect(unify(UpperS, Expected), $module, $pred, "UpperS != Expected").
+
+:- pred test_to_lower_in_in({string, string}::in, io::di, io::uo) is det.
+
+test_to_lower_in_in({X, Y}, !IO) :-
+    compare(Expected, to_lower(X), Y),
+    ( if string.to_lower(X, Y) then
+        expect(unify(Expected, (=)), $module, $pred, "to_lower wrong")
+    else
+        expect_not(unify(Expected, (=)), $module, $pred, "to_lower wrong")
+    ).
+
+:- pred test_to_upper_in_in({string, string}::in, io::di, io::uo) is det.
+
+test_to_upper_in_in({X, Y}, !IO) :-
+    compare(Expected, to_upper(X), Y),
+    ( if string.to_upper(X, Y) then
+        expect(unify(Expected, (=)), $module, $pred, "to_upper wrong")
+    else
+        expect_not(unify(Expected, (=)), $module, $pred, "to_upper wrong")
+    ).
+
+:- pred test_compare_ci_ascii({string, string}::in, io::di, io::uo) is det.
+
+test_compare_ci_ascii({X, Y}, !IO) :-
+    compare_ci_ascii(ResCI, X, Y),
+    compare(ResLower, to_lower(X), to_lower(Y) : string),
+    compare(ResUpper, to_upper(X), to_upper(Y) : string),
+    expect(unify(ResCI, ResLower), $module, $pred, "ResCI != ResLower"),
+    expect(unify(ResCI, ResUpper), $module, $pred, "ResCI != ResUpper").
-- 
2.6.4



More information about the reviews mailing list