UefiCpuPkg/MtrrLibUnitTest: Change to use static array for CI test
The unit test app supports running in 3 mode:
1. MtrrLibUnitTest generate-random-numbers
     <path to MtrrLib/UnitTest/RandomNumber.c> <random-number count>
   It generates random numbers and writes to RandomNumber.c.
2. MtrrLibUnitTest [<iterations>]
   It tests MtrrLib APIs using configurations generated from static
   numbers generated by mode #1.
   This is the default execution mode running in CI environment.
3. MtrrLibUnitTest <iterations> random
   It tests MtrrLib APIs using configurations generated from random
   numbers.
   This is what developers can use to test MtrrLib for regressions.
Signed-off-by: Ray Ni <ray.ni@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Ming Shao <ming.shao@intel.com>
Cc: Sean Brogan <sean.brogan@microsoft.com>
Cc: Bret Barkelew <Bret.Barkelew@microsoft.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
			
			
This commit is contained in:
		| @@ -1055,8 +1055,6 @@ UnitTestingEntry ( | |||||||
|   GetFirmwareVariableMtrrCountContext.SystemParameter = &mDefaultSystemParameter; |   GetFirmwareVariableMtrrCountContext.SystemParameter = &mDefaultSystemParameter; | ||||||
|   Framework = NULL; |   Framework = NULL; | ||||||
|  |  | ||||||
|   DEBUG ((DEBUG_INFO, "%a v%a\n", UNIT_TEST_APP_NAME, UNIT_TEST_APP_VERSION)); |  | ||||||
|  |  | ||||||
|   // |   // | ||||||
|   // Setup the test framework for running the tests. |   // Setup the test framework for running the tests. | ||||||
|   // |   // | ||||||
| @@ -1100,7 +1098,6 @@ UnitTestingEntry ( | |||||||
|   // |   // | ||||||
|   // Execute the tests. |   // Execute the tests. | ||||||
|   // |   // | ||||||
|   srand ((unsigned int) time (NULL)); |  | ||||||
|   Status = RunAllTestSuites (Framework); |   Status = RunAllTestSuites (Framework); | ||||||
|  |  | ||||||
| EXIT: | EXIT: | ||||||
| @@ -1125,15 +1122,42 @@ main ( | |||||||
|   CHAR8 *Argv[] |   CHAR8 *Argv[] | ||||||
|   ) |   ) | ||||||
| { | { | ||||||
|   UINTN    Iteration; |   UINTN    Count; | ||||||
|  |  | ||||||
|  |   DEBUG ((DEBUG_INFO, "%a v%a\n", UNIT_TEST_APP_NAME, UNIT_TEST_APP_VERSION)); | ||||||
|  |   srand ((unsigned int) time (NULL)); | ||||||
|  |  | ||||||
|   // |   // | ||||||
|   // First parameter specifies the test iterations. |   // MtrrLibUnitTest generate-random-numbers <path to MtrrLib/UnitTest/RandomNumber.c> <random-number count> | ||||||
|   // Default is 10. |  | ||||||
|   // |   // | ||||||
|   Iteration = 10; |   if ((Argc == 4) && (AsciiStriCmp ("generate-random-numbers", Argv[1]) == 0)) { | ||||||
|   if (Argc == 2) { |     Count = atoi (Argv[3]); | ||||||
|     Iteration = atoi (Argv[1]); |     DEBUG ((DEBUG_INFO, "Generate %d random numbers to %a.\n", Count, Argv[2])); | ||||||
|  |     GenerateRandomNumbers (Argv[2], Count); | ||||||
|  |     return 0; | ||||||
|   } |   } | ||||||
|   return UnitTestingEntry (Iteration); |  | ||||||
|  |   // | ||||||
|  |   // MtrrLibUnitTest [<iterations>] | ||||||
|  |   //                 <iterations> [fixed|random] | ||||||
|  |   //   Default <iterations> is 10. | ||||||
|  |   //   Default uses fixed inputs. | ||||||
|  |   // | ||||||
|  |   Count        = 10; | ||||||
|  |   mRandomInput = FALSE; | ||||||
|  |   if ((Argc == 2) || (Argc == 3)) { | ||||||
|  |     Count = atoi (Argv[1]); | ||||||
|  |     if (Argc == 3) { | ||||||
|  |       if (AsciiStriCmp ("fixed", Argv[2]) == 0) { | ||||||
|  |         mRandomInput = FALSE; | ||||||
|  |       } else if (AsciiStriCmp ("random", Argv[2]) == 0) { | ||||||
|  |         mRandomInput = TRUE; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   DEBUG ((DEBUG_INFO, "Iterations = %d\n", Count)); | ||||||
|  |   DEBUG ((DEBUG_INFO, "Input      = %a\n", mRandomInput ? "random" : "fixed")); | ||||||
|  |  | ||||||
|  |   return UnitTestingEntry (Count); | ||||||
| } | } | ||||||
|   | |||||||
| @@ -43,6 +43,7 @@ typedef struct { | |||||||
| } MTRR_LIB_SYSTEM_PARAMETER; | } MTRR_LIB_SYSTEM_PARAMETER; | ||||||
|  |  | ||||||
| extern UINT32                           mFixedMtrrsIndex[]; | extern UINT32                           mFixedMtrrsIndex[]; | ||||||
|  | extern BOOLEAN                          mRandomInput; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|   Initialize the MTRR registers. |   Initialize the MTRR registers. | ||||||
| @@ -179,4 +180,16 @@ Random32 ( | |||||||
|   UINT32  Start, |   UINT32  Start, | ||||||
|   UINT32  Limit |   UINT32  Limit | ||||||
|   ); |   ); | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |   Generate Count random numbers in FilePath. | ||||||
|  |  | ||||||
|  |   @param FilePath  The file path to put the generated random numbers. | ||||||
|  |   @param Count     Count of random numbers. | ||||||
|  | **/ | ||||||
|  | VOID | ||||||
|  | GenerateRandomNumbers ( | ||||||
|  |   CHAR8         *FilePath, | ||||||
|  |   UINTN         Count | ||||||
|  |   ); | ||||||
| #endif | #endif | ||||||
|   | |||||||
| @@ -22,6 +22,7 @@ | |||||||
|   MtrrLibUnitTest.c |   MtrrLibUnitTest.c | ||||||
|   MtrrLibUnitTest.h |   MtrrLibUnitTest.h | ||||||
|   Support.c |   Support.c | ||||||
|  |   RandomNumber.c | ||||||
|  |  | ||||||
| [Packages] | [Packages] | ||||||
|   MdePkg/MdePkg.dec |   MdePkg/MdePkg.dec | ||||||
| @@ -37,3 +38,6 @@ | |||||||
|  |  | ||||||
| [Pcd] | [Pcd] | ||||||
|   gUefiCpuPkgTokenSpaceGuid.PcdCpuNumberOfReservedVariableMtrrs   ## SOMETIMES_CONSUMES |   gUefiCpuPkgTokenSpaceGuid.PcdCpuNumberOfReservedVariableMtrrs   ## SOMETIMES_CONSUMES | ||||||
|  |  | ||||||
|  | [BuildOptions] | ||||||
|  |   MSFT:*_*_*_CC_FLAGS = -D _CRT_SECURE_NO_WARNINGS | ||||||
|   | |||||||
							
								
								
									
										5009
									
								
								UefiCpuPkg/Library/MtrrLib/UnitTest/RandomNumber.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5009
									
								
								UefiCpuPkg/Library/MtrrLib/UnitTest/RandomNumber.c
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| @@ -20,6 +20,70 @@ MSR_IA32_MTRRCAP_REGISTER        mMtrrCapMsr; | |||||||
| CPUID_VERSION_INFO_EDX           mCpuidVersionInfoEdx; | CPUID_VERSION_INFO_EDX           mCpuidVersionInfoEdx; | ||||||
| CPUID_VIR_PHY_ADDRESS_SIZE_EAX   mCpuidVirPhyAddressSizeEax; | CPUID_VIR_PHY_ADDRESS_SIZE_EAX   mCpuidVirPhyAddressSizeEax; | ||||||
|  |  | ||||||
|  | BOOLEAN                          mRandomInput; | ||||||
|  | UINTN                            mNumberIndex = 0; | ||||||
|  | extern UINTN                     mNumbers[]; | ||||||
|  | extern UINTN                     mNumberCount; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |   Return a random number between 0 and RAND_MAX. | ||||||
|  |  | ||||||
|  |   If mRandomInput is TRUE, the routine directly calls rand(). | ||||||
|  |   Otherwise, the routine returns the pre-generated numbers. | ||||||
|  |  | ||||||
|  |   @return a number between 0 and RAND_MAX. | ||||||
|  | **/ | ||||||
|  | UINTN | ||||||
|  | Rand ( | ||||||
|  |   VOID | ||||||
|  |   ) | ||||||
|  | { | ||||||
|  |   if (mRandomInput) { | ||||||
|  |     return rand (); | ||||||
|  |   } else { | ||||||
|  |     DEBUG ((DEBUG_INFO, "random: %d\n", mNumberIndex)); | ||||||
|  |     return mNumbers[mNumberIndex++ % (mNumberCount - 1)]; | ||||||
|  |   } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | CHAR8  mContentTemplate[] = { | ||||||
|  |   "/** @file\n" | ||||||
|  |   "  Pre-generated random number used by MtrrLib test.\n" | ||||||
|  |   "\n" | ||||||
|  |   "  Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>\n" | ||||||
|  |   "  SPDX-License-Identifier: BSD-2-Clause-Patent\n" | ||||||
|  |   "**/\n" | ||||||
|  |   "UINTN mNumberCount = %d;\n" | ||||||
|  |   "UINTN mNumbers[] = {" | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |   Generate Count random numbers in FilePath. | ||||||
|  |  | ||||||
|  |   @param FilePath  The file path to put the generated random numbers. | ||||||
|  |   @param Count     Count of random numbers. | ||||||
|  | **/ | ||||||
|  | VOID | ||||||
|  | GenerateRandomNumbers ( | ||||||
|  |   CHAR8         *FilePath, | ||||||
|  |   UINTN         Count | ||||||
|  |   ) | ||||||
|  | { | ||||||
|  |   FILE   *File; | ||||||
|  |   UINTN  Index; | ||||||
|  |  | ||||||
|  |   File = fopen (FilePath, "w"); | ||||||
|  |   fprintf (File, mContentTemplate, Count); | ||||||
|  |   for (Index = 0; Index < Count; Index++) { | ||||||
|  |     if (Index % 10 == 0) { | ||||||
|  |       fprintf (File, "\n "); | ||||||
|  |     } | ||||||
|  |     fprintf (File, " %d,", rand ()); | ||||||
|  |   } | ||||||
|  |   fprintf (File, "\n};\n"); | ||||||
|  |   fclose (File); | ||||||
|  | } | ||||||
|  |  | ||||||
| /** | /** | ||||||
|   Retrieves CPUID information. |   Retrieves CPUID information. | ||||||
|  |  | ||||||
| @@ -328,7 +392,7 @@ Random32 ( | |||||||
|   UINT32  Limit |   UINT32  Limit | ||||||
|   ) |   ) | ||||||
| { | { | ||||||
|   return (UINT32) (((double) rand () / RAND_MAX) * (Limit - Start)) + Start; |   return (UINT32) (((double) Rand () / RAND_MAX) * (Limit - Start)) + Start; | ||||||
| } | } | ||||||
|  |  | ||||||
| /** | /** | ||||||
| @@ -344,7 +408,7 @@ Random64 ( | |||||||
|   UINT64  Limit |   UINT64  Limit | ||||||
|   ) |   ) | ||||||
| { | { | ||||||
|   return (UINT64) (((double) rand () / RAND_MAX) * (Limit - Start)) + Start; |   return (UINT64) (((double) Rand () / RAND_MAX) * (Limit - Start)) + Start; | ||||||
| } | } | ||||||
|  |  | ||||||
| /** | /** | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user