Files
mercury/tests/hard_coded/equality_pred_which_requires_boxing.m
Julien Fischer 1f6d83692a Update programming style in tests/hard_coded.
tests/hard_coded/*.m:
    Update programming style, unless doing so would change
    the meaning of the test, in particular:

    - use '.' as a module qualifier in place of '__'
    - use {write,print}_line where appropriate
    - use if-then-else in place of C -> T ; E
    - use state variables in place of DCGs

tests/hard_coded/dir_test.m:
    Document what the expected outputs correspond to.

    Use a uniform module qualifier in the output.

tests/hard_coded/dir_test.exp*:
    Conform to the above change.
2021-01-07 13:58:12 +11:00

133 lines
4.0 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ts=4 sw=4 et ft=mercury
%---------------------------------------------------------------------------%
%
% equality_pred_which_requires_boxing
%
% Check that the unification predicate we generate handles the case where
% the arguments are boxed and unboxed correctly.
:- module equality_pred_which_requires_boxing.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- type type_which_needs_boxing.
:- pragma foreign_type(c, type_which_needs_boxing, "double")
where equality is unify_ft.
:- pragma foreign_type("C#", type_which_needs_boxing,
"System.Double") where equality is unify_ft.
:- pragma foreign_type(java, type_which_needs_boxing,
"Double") where equality is unify_ft.
:- type type_which_needs_boxing(T).
:- pragma foreign_type(c, type_which_needs_boxing(T), "double")
where equality is unify_ft_T.
:- pragma foreign_type("C#", type_which_needs_boxing(T),
"System.Double")
where equality is unify_ft_T.
:- pragma foreign_type(java, type_which_needs_boxing(T), "Double")
where equality is unify_ft_T.
main(!IO) :-
% Test a builtin type which requires boxing.
A = float_a,
B = float_b,
unify("float (boxed)", A, B, !IO),
( if A = B then
io.write_string("float (unboxed): true\n", !IO)
else
io.write_string("float (unboxed): false\n", !IO)
),
% Test a simple foreign_type.
X = create(A),
Y = create(B),
unify("foreign_type (boxed)", X, Y, !IO),
( if X = Y then
io.write_string("foreign_type (unboxed): true\n", !IO)
else
io.write_string("foreign_type (unboxed): false\n", !IO)
),
% Test a foreign_type which requires a type_info.
G = create_T(A),
H = create_T(B),
unify("foreign_type(T) (boxed)", G, H, !IO),
( if G = H then
io.write_string("foreign_type(T) (unboxed): true\n", !IO)
else
io.write_string("foreign_type(T) (unboxed): false\n", !IO)
).
% Here we pass in a generic type, so if needed the input
% arguments will have been boxed. We then test if the
% unification pred handles this correctly.
:- pred unify(string::in, T::in, T::in, io::di, io::uo) is det.
unify(S, X, Y, !IO) :-
io.write_string(S, !IO),
( if X = Y then
io.write_string(": true\n", !IO)
else
io.write_string(": false\n", !IO)
).
:- func create(float) = type_which_needs_boxing.
:- pragma foreign_proc("C", create(X::in) = (Y::out), [promise_pure], "
Y = X;
").
:- pragma foreign_proc("C#", create(X::in) = (Y::out), [promise_pure], "
Y = X;
").
:- pragma foreign_proc("Java", create(X::in) = (Y::out), [promise_pure], "
Y = X;
").
:- func create_T(float) = type_which_needs_boxing(int).
:- pragma foreign_proc("C", create_T(X::in) = (Y::out), [promise_pure], "
Y = X;
").
:- pragma foreign_proc("C#", create_T(X::in) = (Y::out), [promise_pure], "
Y = X;
").
:- pragma foreign_proc("Java", create_T(X::in) = (Y::out), [promise_pure], "
Y = X;
").
:- pred unify_ft(type_which_needs_boxing::in, type_which_needs_boxing::in)
is semidet.
:- pragma foreign_proc("C", unify_ft(X::in, Y::in), [promise_pure], "
SUCCESS_INDICATOR = (X == Y);
").
:- pragma foreign_proc("C#", unify_ft(X::in, Y::in), [promise_pure], "
SUCCESS_INDICATOR = (X == Y);
").
:- pragma foreign_proc("Java", unify_ft(X::in, Y::in), [promise_pure], "
SUCCESS_INDICATOR = X.equals(Y);
").
:- pred unify_ft_T(type_which_needs_boxing(T)::in,
type_which_needs_boxing(T)::in) is semidet.
:- pragma foreign_proc("C", unify_ft_T(X::in, Y::in), [promise_pure], "
SUCCESS_INDICATOR = (X == Y);
").
:- pragma foreign_proc("C#", unify_ft_T(X::in, Y::in), [promise_pure], "
SUCCESS_INDICATOR = (X == Y);
").
:- pragma foreign_proc("Java", unify_ft_T(X::in, Y::in), [promise_pure], "
SUCCESS_INDICATOR = X.equals(Y);
").
:- pragma no_inline(float_a/0).
:- func float_a = float.
float_a = 1.0.
:- pragma no_inline(float_b/0).
:- func float_b = float.
float_b = 1.0.