% ---------------------------------------------------------------------------- %
% lists.m
% Ralph Becket <rbeck@microsoft.com>
% Tue Jan  9 13:50:50 GMT 2001
% vim: ts=4 sw=4 et tw=0 wm=0 ff=unix
%
% NOTE: this is not really a fair test since the Mercury list
% library does not implement doubly-linked lists as the C and
% (presumably) Python versions do.
% ---------------------------------------------------------------------------- %

:- module mytest.
:- interface.

:- import_module io.



:- pred main(io__state, io__state).
:- mode main(di, uo) is cc_multi.



:- implementation.



:- import_module string, list, int, require, benchmarking.



main -->
    io__command_line_arguments(ArgV),
    (   { ArgV = [],        Repeats = 1 }
    ;   { ArgV = [Arg],     Repeats = string__det_to_int(Arg) }
    ;   { ArgV = [_,_|_],   error("usage: nestedloops [Repeats]") }
    ),
    { benchmarking__benchmark_det(test_list_ops, 0, N, Repeats, Time) },
    io__format("%d\n", [i(N)]).



:- func size = int.
size = 10000.



:- pred test_list_ops(int, int).
:- mode test_list_ops(in, out) is det.

test_list_ops(_, N) :-
    L1 = 1 `..` size,                   % Build [1, 2, ..., size].
    copy(L1, L2),                       % Make a copy.
                                        % Do a naive reverse.
    L3 = list__foldl(func(X, L) = L ++ [X], L2, []),
                                        % Now do a weird copy.
    L4 = list__foldr(func(X, L) = L ++ [X], L3, []),
    L5 = list__reverse(L1),             % Standard reverse.
    (
             if list__det_head(L5) \= size  then N = 0
        else if L1 \= L2                    then N = 0
        else                                     N = list__length(L4)
    ).