Files
mercury/tests/hard_coded/transform_value.m
Zoltan Somogyi 2bd7c5ee3e Rename X's aux modules as X_helper_N in hard_coded.
tests/hard_coded/*.m:
    Rename modules as mentioned above.

    In a few cases, where the main module's name itself had a suffix,
    such as "_mod_a" or "_main", remove that suffix. This entails
    renaming the .exp file as well. (In some cases, this meant that
    the name of a helper module was "taken over" by the main module
    of the test case.)

    Update all references to the moved modules.

    General updates to programming style, such as

    - replacing DCG notation with state var notation
    - replacing (C->T;E) with (if C then T else E)
    - moving pred/func declarations to just before their code
    - replacing io.write/io.nl sequences with io.write_line
    - replacing io.print/io.nl sequences with io.print_line
    - fixing too-long lines
    - fixing grammar errors in comments

tests/hard_coded/Mmakefile:
tests/hard_coded/Mercury.options:
    Update all references to the moved modules.

    Enable the constant_prop_int test case. The fact that it wasn't enabled
    before is probably an accident. (When constant_prop_int.m was created,
    the test case was added to a list in the Mmakefile, but that list
    was later removed due to never being referenced.)

tests/hard_coded/constant_prop_int.{m,exp}:
    Delete the calls to shift operations with negative shift amounts,
    since we have added a compile-time error for these since the test
    was originally created.
2023-06-16 08:33:22 +02:00

82 lines
2.1 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ts=4 sw=4 et ft=mercury
%---------------------------------------------------------------------------%
%
% Test the map.transform_value predicate.
%
:- module transform_value.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module assoc_list.
:- import_module int.
:- import_module list.
:- import_module map.
:- import_module rbtree.
main(!IO) :-
some [!M] (
!:M = map.init,
map.set(1, 1, !M),
map.set(2, 1, !M),
map.set(3, 1, !M),
map.set(4, 1, !M),
map.set(5, 1, !M),
map.set(6, 1, !M),
map.set(7, 1, !M),
map.set(8, 1, !M),
M0 = !.M,
( if map.transform_value(add1, 2, !.M, M1) then
io.write_int(M1 ^ det_elem(2), !IO)
else
io.write_string("key not found", !IO)
),
io.nl(!IO),
( if map.transform_value(add1, 9, !.M, M2) then
io.write_int(M2 ^ det_elem(9), !IO)
else
io.write_string("key not found", !IO)
),
io.nl(!IO),
map.det_transform_value(add1, 3, !M),
io.write_int(!.M ^ det_elem(3), !IO),
io.nl(!IO),
M3 = map.det_transform_value(f, 7, !.M),
io.write_int(M3 ^ det_elem(7), !IO),
io.nl(!IO),
list.foldl(map.det_transform_value(add1),
[1, 2, 3, 4, 5, 6, 7, 8], M0, M4),
A`with_type`assoc_list(int, int) = map.to_assoc_list(M4),
io.write(A, !IO),
io.nl(!IO),
RB0 = rbtree.init,
rbtree.set(1, 1, RB0, RB1),
rbtree.set(2, 1, RB1, RB2),
( if
rbtree.transform_value(add1, 1, RB2, RB3),
rbtree.transform_value(add1, 2, RB3, RB4)
then
A2`with_type`assoc_list(int, int) =
rbtree.rbtree_to_assoc_list(RB4),
io.write(A2, !IO)
else
io.write_string("key not found", !IO)
),
io.nl(!IO)
).
:- pred add1(int::in, int::out) is det.
add1(X, X+1).
:- func f(int) = int.
f(X) = X+1.