aunit_24.0.0_67fdc2b6/include/aunit/framework/calendar/aunit-time_measure.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
168
169
170
171
172
173
174
175
176
177
178
179
180
------------------------------------------------------------------------------
--                                                                          --
--                         GNAT COMPILER COMPONENTS                         --
--                                                                          --
--                     A U N I T . T I M E _ M E A S U R E                  --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--                                                                          --
--                    Copyright (C) 2006-2019, AdaCore                      --
--                                                                          --
-- GNAT is free software;  you can  redistribute it  and/or modify it under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
--                                                                          --
-- GNAT is maintained by AdaCore (http://www.adacore.com)                   --
--                                                                          --
------------------------------------------------------------------------------

with Ada.Strings;       use Ada.Strings;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;

package body AUnit.Time_Measure is

   -------------------
   -- Start_Measure --
   -------------------

   procedure Start_Measure (T : in out Time) is
   begin
      T.Start := Ada.Calendar.Clock;
   end Start_Measure;

   ------------------
   -- Stop_Measure --
   ------------------

   procedure Stop_Measure (T : in out Time) is
   begin
      T.Stop := Ada.Calendar.Clock;
   end Stop_Measure;

   -----------------
   -- Get_Measure --
   -----------------

   function Get_Measure (T : Time) return AUnit_Duration is
      use type Ada.Calendar.Time;
   begin
      return AUnit_Duration (T.Stop - T.Start);
   end Get_Measure;

   ---------------------
   -- Gen_Put_Measure --
   ---------------------

   procedure Gen_Put_Measure (File    : AUnit.IO.File_Type;
                              Measure : AUnit_Duration) is
      H, M, S  : Integer := 0;
      T        : Duration := Duration (Measure);
      Force    : Boolean;

      procedure Put (N : Integer; Length : Integer);
      --  Put N using at least Length digits.

      procedure Put (N : Integer; Length : Integer) is
      begin
         for Dig in reverse 1 .. Length - 1 loop
            if N < 10**Dig then
               Put (File, "0");
            else
               exit;
            end if;
         end loop;

         Put (File, Trim (N'Img, Left));
      end Put;

   begin
      --  Calculate the number of hours, minutes and seconds
      while T >= 3600.0 loop
         H := H + 1;
         T := T - 3600.0;
      end loop;

      while T >= 60.0 loop
         M := M + 1;
         T := T - 60.0;
      end loop;

      while T >= 1.0 loop
         S := S + 1;
         T := T - 1.0;
      end loop;

      --  Now display the result
      Force := False;

      if H > 0 then
         Put (File, Trim (H'Img, Left));
         Put (File, "h");
         Force := True;
      end if;

      if M > 0 or else Force then
         if not Force then
            Put (File, Trim (M'Img, Left));
         else
            --  In case some output is already done, then we force a 2 digits
            --  output so that the output is normalized.
            Put (M, 2);
         end if;

         Put (File, "min. ");
         Force := True;
      end if;

      if not Force then
         Put (File, Trim (S'Img, Left));
      else
         Put (S, 2);
      end if;

      Put (File, ".");
      Put (Integer (T * 1_000_000.0), 6);
      Put (File, " sec.");
   end Gen_Put_Measure;

   --------------------------------
   -- Gen_Put_Measure_In_Seconds --
   --------------------------------

   procedure Gen_Put_Measure_In_Seconds (File    : AUnit.IO.File_Type;
                                         Measure : AUnit_Duration) is
      S  : Integer := 0;
      T  : Duration := Duration (Measure);

      procedure Put (N : Integer; Length : Integer);
      --  Put N using at least Length digits.

      procedure Put (N : Integer; Length : Integer) is
      begin
         for Dig in reverse 1 .. Length - 1 loop
            if N < 10**Dig then
               Put (File, "0");
            else
               exit;
            end if;
         end loop;

         Put (File, Trim (N'Img, Left));
      end Put;

   begin

      while T >= 1.0 loop
         S := S + 1;
         T := T - 1.0;
      end loop;

      Put (File, Trim (S'Img, Left));

      Put (File, ".");
      Put (Integer (T * 1_000_000.0), 9);
      Put (File, "s");
   end Gen_Put_Measure_In_Seconds;

end AUnit.Time_Measure;