stephes_ada_library_3.7.3_08b48307/test/test_stats.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
--  Abstract :
--
--  See spec.
--
--  Copyright (C) 2003, 2005, 2009, 2010, 2012, 2014, 2015, 2016 Stephen Leake.  All Rights Reserved.
--
--  SAL is free software; you can redistribute it and/or modify it
--  under terms of the GNU General Public License as published by the
--  Free Software Foundation; either version 2, or (at your option) any
--  later version. SAL is distributed in the hope that it will be
--  useful, but WITHOUT ANY WARRANTY; without even the implied warranty
--  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
--  General Public License for more details. You should have received a
--  copy of the GNU General Public License distributed with SAL; see
--  file COPYING. If not, write to the Free Software Foundation, 59
--  Temple Place - Suite 330, Boston, MA 02111-1307, USA.

pragma License (GPL);

with Ada.Float_Text_IO;
with Ada.Numerics.Elementary_Functions;
with AUnit.Assertions;
with SAL.Gen_Stats.Gen_Image;
package body Test_Stats is

   Default_Mean_Threshold : constant := 10.0e-5;
   Default_SD_Threshold   : constant := 2.0e-6;

   package Stats is new SAL.Gen_Stats (Float, Ada.Numerics.Elementary_Functions);
   package Stats_Image is new Stats.Gen_Image (Ada.Float_Text_IO);

   procedure Check
     (Message   : in String;
      Computed  : in Float;
      Expected  : in Float;
      Threshold : in Float)
   is begin
      AUnit.Assertions.Assert
        (abs (Computed - Expected) < Threshold,
         Message &
           " failed; expected " & Float'Image (Expected) &
           " got " & Float'Image (Computed));
   end Check;

   procedure Check
     (Message        : in String;
      Computed       : in Stats.Stats_Type;
      Mean           : in Float;
      SD             : in Float;
      Min            : in Float;
      Max            : in Float;
      Mean_Threshold : in Float := Default_Mean_Threshold;
      SD_Threshold   : in Float := Default_SD_Threshold)
   is
      Computed_Mean : constant Float := Stats.Mean (Computed);
      Computed_SD   : constant Float := Stats.Standard_Deviation (Computed);
      Computed_Min  : constant Float := Stats.Min (Computed);
      Computed_Max  : constant Float := Stats.Max (Computed);
   begin
      Check (Message & ".Mean", Computed_Mean, Mean, Mean_Threshold);
      Check (Message & ".SD", Computed_SD, SD, SD_Threshold);
      Check (Message & ".Min", Computed_Min, Min, Mean_Threshold);
      Check (Message & ".Max", Computed_Max, Max, Mean_Threshold);
   end Check;

   procedure Check
     (Message : in String;
      Computed : in String;
      Expected : in String)
   is begin
      AUnit.Assertions.Assert
        (Computed = Expected,
         Message &
           " failed; expected '" & Expected &
           "' got '" & Computed);
   end Check;

   procedure Test_Stats (T : in out AUnit.Test_Cases.Test_Case'Class)
   is
      pragma Unreferenced (T);

      use Stats;
      Stats : Stats_Type;
   begin
      for I in 1 .. 10 loop
         if I <= 5 then
            Accumulate (Stats, 1.1);
         else
            Accumulate (Stats, 0.9);
         end if;
      end loop;
      Check ("case_one", Stats, Mean => 1.0, SD => 0.105411, Min => 0.9, Max => 1.1);

      declare
         Image : constant String := Stats_Image.Image
           (Display (Stats),
            Mean_Fore => 1,
            Mean_Aft  => 1,
            Mean_Exp  => 0,
            SD_Fore   => 1,
            SD_Aft    => 4,
            SD_Exp    => 3);
      begin
         Check ("case_one.image", Image, "( 1.0, 1.0541E-01,  0.9,  1.1)");
      end;

      Reset (Stats);

      for I in 1 .. 10 loop
         if I <= 5 then
            Accumulate (Stats, -10.1);
         else
            Accumulate (Stats, -9.9);
         end if;
      end loop;

      declare
         Image : String := Stats_Image.Image
           (Display (Stats),
            Mean_Fore => 1,
            Mean_Aft  => 1,
            Mean_Exp  => 0,
            SD_Fore   => 1,
            SD_Aft    => 4,
            SD_Exp    => 3);
      begin
         --  Last digit changes between 32 and 64 bit compilers; mask it.
         Image (13) := 'M';
         Check ("overflow.image", Image, "(****, 1.053ME-01, ****, -9.9)");
         --  Value correct for GNAT 7.1.2
      end;

      declare
         Image : String := Stats_Image.Image
           (Display (Stats),
            Mean_Fore => 2,
            Mean_Aft  => 2,
            Mean_Exp  => 0,
            SD_Fore   => 1,
            SD_Aft    => 4,
            SD_Exp    => 3);
      begin
         --  Last digit changes between 32 and 64 bit compilers; mask it.
         Image (15) := 'M';
         Check ("case_two.image", Image, "(-10.00, 1.053ME-01, -10.10,  -9.90)");
         --  Value correct for GNAT 7.1.2
      end;
   end Test_Stats;

   ----------
   --  Public routines

   overriding function Name (T : Test_Case) return AUnit.Message_String
   is
      pragma Unreferenced (T);
   begin
      return new String'("Test_Math_Float_Stats");
   end Name;

   overriding procedure Register_Tests (T : in out Test_Case)
   is
      use AUnit.Test_Cases.Registration;
   begin
      Register_Routine (T, Test_Stats'Access, "Test_Stats");
   end Register_Tests;

end Test_Stats;