(* Collection of code snippets by Arne Vajhøj *)
(* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) *)
[inherit('sys$library:pascal$lib_routines')]
program native_test(input, output);

type
    string = varying[32767] of char;

procedure printres(t1, t2 : Cardinal; n1, n2 : integer; ops : string);

var
    xperf : double;

begin
    xperf := n1 * 1.0 * n2 / ((t2 - t1) / 1000.0);
    writeln((xperf / 1000000):1:4, ' million ', ops, ' per second');
end;

const
    NINT = 10000;
    NFP = 1000;
    NSTR = 100;
    N = 1000000;

procedure testint(scale : integer);

var
    i, j, nintscale, sum : integer;
    t1, t2 : Cardinal;

begin
    nintscale := NINT div scale;
    t1 := Clock;
    for i := 1 to nintscale do begin
        sum := i - 1;
        for j := 1 to N do begin
            sum := ((sum + 1) * 2 + 1) div 2;
        end;
        if sum <> (i - 1 + N) then begin
            writeln('Integer test error');
            halt;
        end;
    end;
    t2 := Clock;
    printres(t1, t2, nintscale, N, 'integer operations');
end;

procedure testfp(scale : integer);

var
    i, j, nfpscale : integer;
    sum : double;
    t1, t2 : Cardinal;

begin
    nfpscale := NFP div scale;
    t1 := Clock;
    for i := 1 to nfpscale do begin
        sum := i - 1;
        for j := 1 to N do begin
            sum := ((sum + 1) * 2 + 1) / 2;
        end;
        if abs(sum - (i - 1 + 1.5 * N)) > 1 then begin
            writeln('Floating point test error');
            halt;
        end;
    end;
    t2 := Clock;
    printres(t1, t2, nfpscale, N, 'floating point operations');
end;

const
    ALFA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    VMS_FACTOR = 100;

procedure teststr(scale : integer);

var
    i, j, nstrscale, ix, ix1, ix2 : integer;
    s, buf : string;
    t1, t2 : Cardinal;

begin
    nstrscale := NSTR div scale;
    t1 := Clock;
    for i := 1 to nstrscale * VMS_FACTOR do begin
        buf := '';
        for j := 1 to ((N div VMS_FACTOR) div 10) do begin
            s := ALFA + ALFA;
            ix := (i - 1 + 10 * (j - 1)) mod length(ALFA);
            buf := buf + substr(s, ix + 1, 1) + substr(s, ix + 2, 2) + substr(s, ix + 4, 3) + substr(s, ix + 7, 4);
        end;
        ix1 := (N div VMS_FACTOR) div 3;
        ix2 := 2 * (N div VMS_FACTOR) div 3;
        if (length(buf) <> (N div VMS_FACTOR)) or
           (buf[ix1] <> ALFA[((i - 1 + ix1 - 1) mod length(ALFA)) + 1]) or
           (buf[ix2] <> ALFA[((i - 1 + ix2 - 1) mod length(ALFA)) + 1]) then begin
            writeln('String test error');
            halt;
        end;
    end;
    t2 := Clock;
    printres(t1, t2, nstrscale, N div 10, 'string operations');
end;

const
    REP = 10;

var
    i, scale : integer;
    cmdlin : string;

begin
    writeln('Pascal:');
    lib$get_foreign(resultant_string:=cmdlin.body,resultant_length:=cmdlin.length);
    if cmdlin.length > 0 then begin
        readv(cmdlin, scale);
    end else begin
        scale := 1;
    end;
    for i := 1 to REP do begin
        testint(scale);
    end;
    for i := 1 to REP do begin
        testfp(scale);
    end;
    for i := 1 to REP do begin
        teststr(scale);
    end;
end.
