-- Collection of code snippets by Arne Vajhøj 
-- (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
with Ada.Command_line,
     Ada.Task_Identification,
     Ada.Strings.Unbounded,
     Ada.Text_IO,
     Ada.Integer_Text_IO,
     Ada.Float_Text_IO,
     Ada.Real_Time;

use Ada.Command_line,
    Ada.Task_Identification,
    Ada.Strings.Unbounded,
    Ada.Text_IO,
    Ada.Integer_Text_IO,
    Ada.Float_Text_IO,
    Ada.Real_Time;

procedure Native_Test is

procedure Print_Res(t1, t2 : Time; n1, n2 : Integer; ops: String) is

xperf : Float;

begin
    xperf := Float(n1) * Float(n2) / Float(To_Duration(t2 - t1));
    Put(xperf / 1000000.0, Aft => 4, Exp => 0);
    Put(" million ");
    Put(ops);
    Put(" per second");
    New_Line;
end Print_Res;

NINT : constant Integer := 10000;
NFP : constant Integer := 1000;
NSTR : constant Integer := 100;
N : constant Integer := 1000000;

procedure Test_Int(scale : Integer) is

nintscale, sum : Integer;
t1, t2 : Time;
tsk : Task_Id;

begin
    nintscale := NINT / scale;
    t1 := Clock;
    for i in 1..nintscale loop
        sum := i - 1;
        for j in 1..N loop
            sum := ((sum + 1) * 2 + 1) / 2;
        end loop;
        if sum /= (i - 1 + N) then
            Put_Line("Integer test error");
            tsk := Current_Task;
            Abort_Task(tsk);
        end if;
    end loop;
    t2 := Clock;
    Print_Res(t1, t2, nintscale, N, "integer operations");
end Test_Int;

procedure Test_FP(scale : Integer) is

nfpscale : Integer;
sum : Long_Float;
t1, t2 : Time;
tsk : Task_Id;

begin
    nfpscale := NFP / scale;
    t1 := Clock;
    for i in 1..nfpscale loop
        sum := Long_Float(i - 1);
        for j in 1..N loop
            sum := ((sum + 1.0) * 2.0 + 1.0) / 2.0;
        end loop;
        if abs (sum - (Long_Float(i - 1) + 1.5 * Long_Float(N))) > 1.0 then
            Put_Line("Floating point test error");
            tsk := Current_Task;
            Abort_Task(tsk);
        end if;
    end loop;
    t2 := Clock;
    Print_Res(t1, t2, nfpscale, N, "floating point operations");
end Test_Fp;

procedure Test_Str(scale : Integer) is

ALFA : constant String := "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
VMS_FACTOR : constant Integer := 100;
s : constant String := ALFA & ALFA;
nstrscale, ix, ix1, ix2, ix1alfa, ix2alfa : Integer;
buf : Unbounded_String;
t1, t2 : Time;
tsk : Task_Id;

begin
    nstrscale := NSTR / scale;
    t1 := Clock;
    for i in 1..(nstrscale * VMS_FACTOR) loop
        buf := To_Unbounded_String("");
        for j in 1..(N / VMS_FACTOR) /10 loop
            ix := (i - 1 + 10 * (j - 1)) mod ALFA'Length;
            Append(buf, s(ix+1..ix+1));
            Append(buf, s(ix+2..ix+3));
            Append(buf, s(ix+4..ix+6));
            Append(buf, s(ix+7..ix+10));
        end loop;
        ix1 := (N / VMS_FACTOR) / 3;
        ix2 := 2 * (N / VMS_FACTOR) / 3;
        ix1alfa := (i - 1 + ix1 - 1) mod ALFA'Length + 1;
        ix2alfa := (i - 1 + ix2 - 1) mod ALFA'Length + 1;
        if To_String(buf)'Length /= (N / VMS_FACTOR) or To_String(buf)(ix1..ix1) /= ALFA(ix1alfa..ix1alfa) or To_String(buf)(ix2..ix2) /= ALFA(ix2alfa..ix2alfa) then
            Put_Line("String test error");
            tsk := Current_Task;
            Abort_Task (tsk);
        end if;
    end loop;
    t2 := Clock;
    Print_Res(t1, t2, nstrscale, N/10, "string operations");
end Test_Str;

REP : constant Integer := 10;
scale : Integer;

begin
    Put_Line("Ada:");
    if Argument_Count > 0 then
        scale := Integer'Value(Argument(1));
    else
        scale := 1;
    end if;
    for i in 1..REP loop
        Test_Int(scale);
    end loop;
    for i in 1..REP loop
        Test_FP(scale);
    end loop;
    for i in 1..REP loop
        Test_Str(scale);
    end loop;
end Native_Test;
