Files
mercury/tests/hard_coded/string_all_match.m
Peter Wang 2ff6b119ef Fix some handling of ill-formed sequences in string module.
library/string.m:
    Fix string.all_match to fail if the string being tested contains
    any ill-formed code unit sequences.

    Fix the Mercury implementation of string.contains_char to continue
    searching for the character past any ill-formed code unit sequences.
    The foreign code implementations already did so.

    Fix unsafe_index_next_repl and unsafe_prev_index_repl in C grades.
    We indexed the C string with `ReplacedCodeUnit = Str[Index]' but
    since Mercury strings have the C type `char *', ReplacedCodeUnit
    could take on a negative value. When ReplacedCodeUnit == -1,
    the caller would assume there is a valid encoding of a code point
    beginning at Index, and therefore return `not_replaced' instead of
    `replaced_code_unit(255)'.

    Add casts to `unsigned char' in other places where we index C
    strings.

    Clarify documentation.

    Document that string.duplicate_char may throw an exception.

tests/hard_coded/string_all_match.m:
tests/hard_coded/string_all_match.exp:
tests/hard_coded/string_all_match.exp2:
    Add test for string.all_match.

tests/hard_coded/contains_char_2.m:
tests/hard_coded/contains_char_2.exp:
    Delete older test case for string.contains_char.

tests/hard_coded/string_contains_char.m:
tests/hard_coded/string_contains_char.exp:
tests/hard_coded/string_contains_char.exp2:
    Add better test for string.contains_char.

tests/hard_coded/Mmakefile:
    Enable the tests.

NEWS:
    Announce the changes.
2022-07-27 14:56:49 +10:00

62 lines
2.0 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ts=4 sw=4 et ft=mercury
%---------------------------------------------------------------------------%
% A test of string.all_match/2.
%
% The .exp file is for C and C# grades.
% The .exp2 file is for Java grades.
%
%---------------------------------------------------------------------------%
:- module string_all_match.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- implementation.
:- import_module char.
:- import_module int.
:- import_module list.
:- import_module string.
%---------------------------------------------------------------------------%
main(!IO) :-
test_all_match(is_alpha, "is_alpha", "", !IO),
test_all_match(is_alpha, "is_alpha", "abc", !IO),
test_all_match(is_alpha, "is_alpha", "abc123", !IO),
test_all_match(is_chess_piece, "is_chess_piece", "♜♞♝♛♚♝♞♜", !IO),
test_all_match(is_chess_piece, "is_chess_piece", "♜♞♝♛K♝♞♜", !IO),
Ilseq = string.between("😀", 0, 1),
S = "abc" ++ Ilseq ++ "def",
test_all_match(is_alpha, "is_alpha", S, !IO).
:- pred test_all_match(pred(char)::in(pred(in) is semidet),
string::in, string::in, io::di, io::uo) is det.
test_all_match(Pred, PredName, String, !IO) :-
( if string.all_match(Pred, String) then
Result = "true"
else
Result = "false"
),
io.format("all_match(%s, %s) ==> %s.\n",
[s(PredName), s(string(String)), s(Result)], !IO).
:- pred is_chess_piece(char::in) is semidet.
is_chess_piece(Char) :-
char.to_int(Char, CodePoint),
CodePoint >= 0x2654, CodePoint =< 0x265F.
%---------------------------------------------------------------------------%
:- end_module string_all_match.
%---------------------------------------------------------------------------%