| 1 |
-- CXA5013.A |
| 2 |
-- |
| 3 |
-- Grant of Unlimited Rights |
| 4 |
-- |
| 5 |
-- Under contracts F33600-87-D-0337, F33600-84-D-0280, MDA903-79-C-0687, |
| 6 |
-- F08630-91-C-0015, and DCA100-97-D-0025, the U.S. Government obtained |
| 7 |
-- unlimited rights in the software and documentation contained herein. |
| 8 |
-- Unlimited rights are defined in DFAR 252.227-7013(a)(19). By making |
| 9 |
-- this public release, the Government intends to confer upon all |
| 10 |
-- recipients unlimited rights equal to those held by the Government. |
| 11 |
-- These rights include rights to use, duplicate, release or disclose the |
| 12 |
-- released technical data and computer software in whole or in part, in |
| 13 |
-- any manner and for any purpose whatsoever, and to have or permit others |
| 14 |
-- to do so. |
| 15 |
-- |
| 16 |
-- DISCLAIMER |
| 17 |
-- |
| 18 |
-- ALL MATERIALS OR INFORMATION HEREIN RELEASED, MADE AVAILABLE OR |
| 19 |
-- DISCLOSED ARE AS IS. THE GOVERNMENT MAKES NO EXPRESS OR IMPLIED |
| 20 |
-- WARRANTY AS TO ANY MATTER WHATSOEVER, INCLUDING THE CONDITIONS OF THE |
| 21 |
-- SOFTWARE, DOCUMENTATION OR OTHER INFORMATION RELEASED, MADE AVAILABLE |
| 22 |
-- OR DISCLOSED, OR THE OWNERSHIP, MERCHANTABILITY, OR FITNESS FOR A |
| 23 |
-- PARTICULAR PURPOSE OF SAID MATERIAL. |
| 24 |
--* |
| 25 |
-- |
| 26 |
-- OBJECTIVE: |
| 27 |
-- Check that a discrete random number generator will yield each value |
| 28 |
-- in its result subtype in a finite number of calls, provided that |
| 29 |
-- the number of such values does not exceed 2**15. |
| 30 |
-- |
| 31 |
-- TEST DESCRIPTION: |
| 32 |
-- This test demonstrates certain capabilities of the random number |
| 33 |
-- generator packages in Ada.Numerics. A generic subprogram is |
| 34 |
-- defined that will be instantiated to produce a total of two test |
| 35 |
-- subprograms. |
| 36 |
-- The area examined by this test is the production of random values |
| 37 |
-- over a discrete range. A generic procedure is instantiated with |
| 38 |
-- an instance of the Discrete_Random package, once for an integer type, |
| 39 |
-- and once for an enumeration type. The test procedure performs a |
| 40 |
-- test run, generating a specific number of random numbers over the |
| 41 |
-- range of the type. If this run did not generate each of the values |
| 42 |
-- in the type range, an asynchronous select statement is invoked. This |
| 43 |
-- select statement has a trigger statement delay for a specific |
| 44 |
-- (implementation defined) amount of time during which additional test |
| 45 |
-- runs will be performed. |
| 46 |
-- At the end of each run in this test, an evaluation is made to |
| 47 |
-- determine if each value in the range of possible values have been |
| 48 |
-- generated. At the conclusion of the runs, or if the specified test |
| 49 |
-- delay time expires, the test is concluded with a status value |
| 50 |
-- returned from the test procedure. An implementation is given three |
| 51 |
-- completely separate opportunities to run the test successfully, and |
| 52 |
-- if at the conclusion of all of these tests no successful result has |
| 53 |
-- been returned, the test is considered failed. |
| 54 |
-- |
| 55 |
-- |
| 56 |
-- CHANGE HISTORY: |
| 57 |
-- 27 Apr 95 SAIC Initial prerelease version. |
| 58 |
-- |
| 59 |
--! |
| 60 |
|
| 61 |
with Ada.Numerics.Discrete_Random; |
| 62 |
with ImpDef; |
| 63 |
with Report; |
| 64 |
|
| 65 |
procedure CXA5013 is |
| 66 |
|
| 67 |
begin |
| 68 |
|
| 69 |
Report.Test ("CXA5013", "Check that a discrete random number generator " & |
| 70 |
"will yield each value in its result subtype " & |
| 71 |
"in a finite number of calls"); |
| 72 |
|
| 73 |
Test_Block: |
| 74 |
declare |
| 75 |
|
| 76 |
use Ada.Numerics; |
| 77 |
|
| 78 |
-- The following constant designed into the test creates a high |
| 79 |
-- probability that a random series of numbers will satisfy the |
| 80 |
-- requirements. Occasionally, even a random series of numbers |
| 81 |
-- will fail. In such a case, the test will reset the random |
| 82 |
-- number generator and rerun the test conditions. This constant |
| 83 |
-- determines how many times the random number generator will be |
| 84 |
-- reset before any individual test run is failed. |
| 85 |
|
| 86 |
TC_Max_Random_Test_Runs : constant := 3; |
| 87 |
|
| 88 |
-- The following constant will ensure that multiple attempts of the |
| 89 |
-- complete set of tests are performed in the event of a failure of |
| 90 |
-- a set of test runs. |
| 91 |
|
| 92 |
TC_Finite_Number_Of_Tests : constant := 3; |
| 93 |
|
| 94 |
|
| 95 |
TC_Test_Run : Integer := 0; |
| 96 |
TC_Success : Boolean := False; |
| 97 |
TC_Trials_Per_Test : Integer := 1500; |
| 98 |
|
| 99 |
type Enum_Type is (One, Two, Three, Four, Five, Six, Seven); |
| 100 |
type Discrete_Type is range 1..100; |
| 101 |
|
| 102 |
|
| 103 |
package Enum_Pack is new Discrete_Random(Enum_Type); |
| 104 |
package Discrete_Pack is |
| 105 |
new Discrete_Random(Result_Subtype => Discrete_Type); |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
-- |
| 110 |
-- Definition of generic Random_Test procedure, which will be |
| 111 |
-- instantiated for both an integer type and an enumeration type. |
| 112 |
-- |
| 113 |
|
| 114 |
generic |
| 115 |
with package Gen_Pack is new Ada.Numerics.Discrete_Random (<>); |
| 116 |
procedure Random_Test (Trials_Per_Test : in Integer; |
| 117 |
Success : out Boolean); |
| 118 |
|
| 119 |
|
| 120 |
procedure Random_Test (Trials_Per_Test : in Integer; |
| 121 |
Success : out Boolean) is |
| 122 |
Total_Runs : Integer := 0; |
| 123 |
Total_Trials : Integer := 0; |
| 124 |
Total_Attempts_This_Test : Integer := 0; |
| 125 |
Random_Array : array (Gen_Pack.Result_Subtype) |
| 126 |
of Boolean := (others => False); |
| 127 |
Gen : Gen_Pack.Generator; |
| 128 |
|
| 129 |
function All_Values_Present return Boolean is |
| 130 |
Result : Boolean := True; |
| 131 |
begin |
| 132 |
for i in Gen_Pack.Result_Subtype'Range loop |
| 133 |
if not Random_Array(i) then |
| 134 |
Result := False; |
| 135 |
exit; |
| 136 |
end if; |
| 137 |
end loop; |
| 138 |
return Result; |
| 139 |
end All_Values_Present; |
| 140 |
|
| 141 |
begin |
| 142 |
|
| 143 |
Success := False; -- Initialized to failure prior to test. |
| 144 |
Gen_Pack.Reset(Gen); -- Perform a time-dependent reset. |
| 145 |
|
| 146 |
-- Guarantee that a specific minimum number of trials are performed |
| 147 |
-- prior to the timer being set. |
| 148 |
|
| 149 |
for i in 1..Trials_Per_Test loop |
| 150 |
-- Set array element to True when a particular array |
| 151 |
-- index is generated by the random number generator. |
| 152 |
Random_Array(Gen_Pack.Random(Gen)) := True; |
| 153 |
end loop; |
| 154 |
|
| 155 |
if All_Values_Present then |
| 156 |
|
| 157 |
Success := True; -- Test was successful, exit procedure with no |
| 158 |
-- further testing performed. |
| 159 |
else |
| 160 |
|
| 161 |
-- Initial test above was unsuccessful, so set a timer and perform |
| 162 |
-- additional trials to determine if all values in the discrete |
| 163 |
-- range will be produced. |
| 164 |
|
| 165 |
select |
| 166 |
|
| 167 |
-- This asynchronous select has a triggering statement which |
| 168 |
-- is a delay statement, set to an implementation defined |
| 169 |
-- number of seconds for any particular test to execute. |
| 170 |
-- The point here is to allow the implementation to decide |
| 171 |
-- how long to run this test in order to generate an |
| 172 |
-- appropriate (i.e., correct) sample from the Random Number |
| 173 |
-- Generator. |
| 174 |
|
| 175 |
delay ImpDef.Delay_Per_Random_Test; -- Delay per test. |
| 176 |
|
| 177 |
-- If, after expiration of delay, the random number generator |
| 178 |
-- has generated all values within the range at least once, |
| 179 |
-- then the result is success; otherwise, a comment is output |
| 180 |
-- to indicate that the random number generator was |
| 181 |
-- unsuccessful in this series of test runs. |
| 182 |
|
| 183 |
if All_Values_Present then |
| 184 |
Success := True; |
| 185 |
else |
| 186 |
Total_Attempts_This_Test := |
| 187 |
Total_Runs * Trials_Per_Test + Total_Trials; |
| 188 |
Report.Comment |
| 189 |
("Not all numbers within the Range were produced in " & |
| 190 |
Integer'Image( |
| 191 |
Integer(ImpDef.Delay_Per_Random_Test*1000.0)) & |
| 192 |
" milliseconds or in " & |
| 193 |
Integer'Image(Total_Attempts_This_Test) & |
| 194 |
" trials during this test"); |
| 195 |
end if; |
| 196 |
|
| 197 |
then abort |
| 198 |
|
| 199 |
-- After setting the triggering statement above, the execution |
| 200 |
-- of this abortable part is begun. |
| 201 |
-- This loop continues until either a) every value has been |
| 202 |
-- produced or b) the triggering statement times out. |
| 203 |
|
| 204 |
Total_Runs := 1; |
| 205 |
|
| 206 |
Test_Loop: -- This loop continues until a test run is |
| 207 |
loop -- successful, the test run limit has been reached, |
| 208 |
-- or the triggering statement times-out above. |
| 209 |
|
| 210 |
Total_Trials := 0; |
| 211 |
|
| 212 |
for i in 1..Trials_Per_Test loop |
| 213 |
Total_Trials := i; -- Used above if triggering statement |
| 214 |
-- completes prior to test completion. |
| 215 |
|
| 216 |
-- Set array element to True when a particular array |
| 217 |
-- index is generated by the random number generator. |
| 218 |
|
| 219 |
Random_Array(Gen_Pack.Random(Gen)) := True; |
| 220 |
|
| 221 |
end loop; |
| 222 |
|
| 223 |
-- At the conclusion of a complete series of trials, the |
| 224 |
-- following evaluation is performed to determine whether |
| 225 |
-- the test run was successful, or whether an additional |
| 226 |
-- test run should be re-attempted. |
| 227 |
|
| 228 |
if All_Values_Present then |
| 229 |
Success := True; |
| 230 |
exit Test_Loop; |
| 231 |
elsif Total_Runs = TC_Max_Random_Test_Runs then |
| 232 |
Report.Comment |
| 233 |
("Not all numbers in the Range were produced in " & |
| 234 |
Integer'Image(Total_Runs*Trials_Per_Test) & |
| 235 |
" individual trials during this test"); |
| 236 |
exit Test_Loop; |
| 237 |
else |
| 238 |
Total_Runs := Total_Runs + 1; |
| 239 |
end if; |
| 240 |
|
| 241 |
end loop Test_Loop; |
| 242 |
end select; |
| 243 |
end if; |
| 244 |
end Random_Test; |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
-- Instantiation of test procedures. |
| 249 |
|
| 250 |
procedure Discrete_Random_Test is new Random_Test(Discrete_Pack); |
| 251 |
procedure Enumeration_Random_Test is new Random_Test(Enum_Pack); |
| 252 |
|
| 253 |
|
| 254 |
begin |
| 255 |
|
| 256 |
-- Make a series of test runs, checking to ensure that discrete |
| 257 |
-- random number generators produce each value in their result subtype |
| 258 |
-- within a finite number of calls. In each case, if the first test |
| 259 |
-- is not successful, another attempt is made, after a time-dependent |
| 260 |
-- reset, up to a total of 3 runs. This allows an implementation |
| 261 |
-- multiple opportunities to pass the test successfully. |
| 262 |
-- Note: The odds of getting all 100 integer values in 1500 trials are |
| 263 |
-- greater than 99.997 percent, confirmed by Monte Carlo |
| 264 |
-- simulation. |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
-- Run the Random_Test for an integer discrete random number generator. |
| 269 |
|
| 270 |
TC_Test_Run := 0; |
| 271 |
TC_Success := False; |
| 272 |
while TC_Test_Run < TC_Finite_Number_Of_Tests and |
| 273 |
not TC_Success |
| 274 |
loop |
| 275 |
TC_Test_Run := TC_Test_Run + 1; -- Increment test counter. |
| 276 |
Discrete_Random_Test (TC_Trials_Per_Test, -- Perform test. |
| 277 |
TC_Success); |
| 278 |
-- Increment the number of trials that will be performed |
| 279 |
-- in the next test by 50%. |
| 280 |
TC_Trials_Per_Test := TC_Trials_Per_Test + TC_Trials_Per_Test/2 ; |
| 281 |
end loop; |
| 282 |
|
| 283 |
if not TC_Success then |
| 284 |
Report.Failed("Random_Test was run " & Integer'Image(TC_Test_Run) & |
| 285 |
" times, but a successful result was not recorded " & |
| 286 |
"from any run using the integer discrete random " & |
| 287 |
"number generator"); |
| 288 |
end if; |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
-- Run the Random_Test for an enumeration type random number generator. |
| 293 |
|
| 294 |
-- Note: The odds of getting all seven enumeration values in 100 |
| 295 |
-- trials are greater than 99.997 percent, confirmed by Monte |
| 296 |
-- Carlo simulation. |
| 297 |
|
| 298 |
TC_Test_Run := 0; |
| 299 |
TC_Trials_Per_Test := 100; |
| 300 |
TC_Success := False; |
| 301 |
while TC_Test_Run < TC_Finite_Number_Of_Tests and |
| 302 |
not TC_Success |
| 303 |
loop |
| 304 |
TC_Test_Run := TC_Test_Run + 1; |
| 305 |
Enumeration_Random_Test (TC_Trials_Per_Test, |
| 306 |
TC_Success); |
| 307 |
-- Increment the number of trials that will be performed |
| 308 |
-- in the next test by 50%. |
| 309 |
TC_Trials_Per_Test := TC_Trials_Per_Test + TC_Trials_Per_Test/2 ; |
| 310 |
end loop; |
| 311 |
|
| 312 |
if not TC_Success then |
| 313 |
Report.Failed("Random_Test was run " & Integer'Image(TC_Test_Run) & |
| 314 |
" times, but a successful result was not recorded " & |
| 315 |
"from any run using the enumeration random number " & |
| 316 |
"generator"); |
| 317 |
end if; |
| 318 |
|
| 319 |
|
| 320 |
exception |
| 321 |
when others => Report.Failed ("Exception raised in Test_Block"); |
| 322 |
end Test_Block; |
| 323 |
|
| 324 |
Report.Result; |
| 325 |
|
| 326 |
end CXA5013; |