SampleGoogleTest.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /** @file
  2. This is a sample to demonstrates the use of GoogleTest that supports host
  3. execution environments.
  4. Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include <gtest/gtest.h>
  8. extern "C" {
  9. #include <Uefi.h>
  10. #include <Library/BaseLib.h>
  11. #include <Library/DebugLib.h>
  12. }
  13. /**
  14. Sample unit test that verifies the expected result of an unsigned integer
  15. addition operation.
  16. **/
  17. TEST(SimpleMathTests, OnePlusOneShouldEqualTwo) {
  18. UINTN A;
  19. UINTN B;
  20. UINTN C;
  21. A = 1;
  22. B = 1;
  23. C = A + B;
  24. ASSERT_EQ (C, (UINTN)2);
  25. }
  26. /**
  27. Sample unit test that verifies that a global BOOLEAN is updatable.
  28. **/
  29. class GlobalBooleanVarTests : public ::testing::Test {
  30. public:
  31. BOOLEAN SampleGlobalTestBoolean = FALSE;
  32. };
  33. TEST_F(GlobalBooleanVarTests, GlobalBooleanShouldBeChangeable) {
  34. SampleGlobalTestBoolean = TRUE;
  35. ASSERT_TRUE (SampleGlobalTestBoolean);
  36. SampleGlobalTestBoolean = FALSE;
  37. ASSERT_FALSE (SampleGlobalTestBoolean);
  38. }
  39. /**
  40. Sample unit test that logs a warning message and verifies that a global
  41. pointer is updatable.
  42. **/
  43. class GlobalVarTests : public ::testing::Test {
  44. public:
  45. VOID *SampleGlobalTestPointer = NULL;
  46. protected:
  47. void SetUp() override {
  48. ASSERT_EQ ((UINTN)SampleGlobalTestPointer, (UINTN)NULL);
  49. }
  50. void TearDown() {
  51. SampleGlobalTestPointer = NULL;
  52. }
  53. };
  54. TEST_F(GlobalVarTests, GlobalPointerShouldBeChangeable) {
  55. SampleGlobalTestPointer = (VOID *)-1;
  56. ASSERT_EQ ((UINTN)SampleGlobalTestPointer, (UINTN)((VOID *)-1));
  57. }
  58. /**
  59. Set PcdDebugPropertyMask for each MacroTestsAssertsEnabledDisabled test
  60. **/
  61. class MacroTestsAssertsEnabledDisabled : public testing::TestWithParam<UINT8> {
  62. void SetUp() {
  63. PatchPcdSet8 (PcdDebugPropertyMask, GetParam());
  64. }
  65. };
  66. /**
  67. Sample unit test using the ASSERT_TRUE() macro.
  68. **/
  69. TEST_P(MacroTestsAssertsEnabledDisabled, MacroAssertTrue) {
  70. UINT64 Result;
  71. //
  72. // This test passes because expression always evaluated to TRUE.
  73. //
  74. ASSERT_TRUE (TRUE);
  75. //
  76. // This test passes because expression always evaluates to TRUE.
  77. //
  78. Result = LShiftU64 (BIT0, 1);
  79. ASSERT_TRUE (Result == BIT1);
  80. }
  81. /**
  82. Sample unit test using the ASSERT_FALSE() macro.
  83. **/
  84. TEST_P(MacroTestsAssertsEnabledDisabled, MacroAssertFalse) {
  85. UINT64 Result;
  86. //
  87. // This test passes because expression always evaluated to FALSE.
  88. //
  89. ASSERT_FALSE (FALSE);
  90. //
  91. // This test passes because expression always evaluates to FALSE.
  92. //
  93. Result = LShiftU64 (BIT0, 1);
  94. ASSERT_FALSE (Result == BIT0);
  95. }
  96. /**
  97. Sample unit test using the ASSERT_EQ() macro.
  98. **/
  99. TEST_P(MacroTestsAssertsEnabledDisabled, MacroAssertEqual) {
  100. UINT64 Result;
  101. //
  102. // This test passes because both values are always equal.
  103. //
  104. ASSERT_EQ (1, 1);
  105. //
  106. // This test passes because both values are always equal.
  107. //
  108. Result = LShiftU64 (BIT0, 1);
  109. ASSERT_EQ (Result, (UINT64)BIT1);
  110. }
  111. /**
  112. Sample unit test using the ASSERT_STREQ() macro.
  113. **/
  114. TEST_P(MacroTestsAssertsEnabledDisabled, MacroAssertMemEqual) {
  115. CHAR8 *String1;
  116. CHAR8 *String2;
  117. //
  118. // This test passes because String1 and String2 are the same.
  119. //
  120. String1 = (CHAR8 *)"Hello";
  121. String2 = (CHAR8 *)"Hello";
  122. ASSERT_STREQ (String1, String2);
  123. }
  124. /**
  125. Sample unit test using the ASSERT_NE() macro.
  126. **/
  127. TEST_P(MacroTestsAssertsEnabledDisabled, MacroAssertNotEqual) {
  128. UINT64 Result;
  129. //
  130. // This test passes because both values are never equal.
  131. //
  132. ASSERT_NE (0, 1);
  133. //
  134. // This test passes because both values are never equal.
  135. //
  136. Result = LShiftU64 (BIT0, 1);
  137. ASSERT_NE (Result, (UINT64)BIT0);
  138. }
  139. /**
  140. Sample unit test using the ASSERT_TRUE() and ASSERT(FALSE)
  141. and EFI_EFFOR() macros to check status
  142. **/
  143. TEST_P(MacroTestsAssertsEnabledDisabled, MacroAssertNotEfiError) {
  144. //
  145. // This test passes because the status is not an EFI error.
  146. //
  147. ASSERT_FALSE (EFI_ERROR (EFI_SUCCESS));
  148. //
  149. // This test passes because the status is not an EFI error.
  150. //
  151. ASSERT_FALSE (EFI_ERROR (EFI_WARN_BUFFER_TOO_SMALL));
  152. }
  153. /**
  154. Sample unit test using the ASSERT_EQ() macro to compare EFI_STATUS values.
  155. **/
  156. TEST_P(MacroTestsAssertsEnabledDisabled, MacroAssertStatusEqual) {
  157. //
  158. // This test passes because the status value are always equal.
  159. //
  160. ASSERT_EQ (EFI_SUCCESS, EFI_SUCCESS);
  161. }
  162. /**
  163. Sample unit test using ASSERT_NE() macro to make sure a pointer is not NULL.
  164. **/
  165. TEST_P(MacroTestsAssertsEnabledDisabled, MacroAssertNotNull) {
  166. UINT64 Result;
  167. //
  168. // This test passes because the pointer is never NULL.
  169. //
  170. ASSERT_NE (&Result, (UINT64 *)NULL);
  171. }
  172. /**
  173. Sample unit test using that should not generate any ASSERTs()
  174. **/
  175. TEST_P(MacroTestsAssertsEnabledDisabled, MacroExpectNoAssertFailure) {
  176. //
  177. // This test passes because it never triggers an ASSERT().
  178. //
  179. ASSERT (TRUE);
  180. //
  181. // This test passes because DecimalToBcd() does not ASSERT() if the
  182. // value passed in is <= 99.
  183. //
  184. DecimalToBcd8 (99);
  185. }
  186. /**
  187. Sample unit test using the ASSERT_DEATH() macro to test expected ASSERT()s.
  188. **/
  189. TEST_P(MacroTestsAssertsEnabledDisabled, MacroExpectAssertFailure) {
  190. //
  191. // Skip tests that verify an ASSERT() is triggered if ASSERT()s are disabled.
  192. //
  193. if ((PcdGet8 (PcdDebugPropertyMask) & BIT0) == 0x00) {
  194. return;
  195. }
  196. //
  197. // This test passes because it directly triggers an ASSERT().
  198. //
  199. ASSERT_DEATH (ASSERT (FALSE), "");
  200. //
  201. // This test passes because DecimalToBcd() generates an ASSERT() if the
  202. // value passed in is >= 100. The expected ASSERT() is caught by the unit
  203. // test framework and ASSERT_DEATH() returns without an error.
  204. //
  205. ASSERT_DEATH (DecimalToBcd8 (101), "");
  206. }
  207. INSTANTIATE_TEST_SUITE_P(ValidInput,
  208. MacroTestsAssertsEnabledDisabled,
  209. ::testing::Values(PcdGet8 (PcdDebugPropertyMask) | BIT0, PcdGet8 (PcdDebugPropertyMask) & (~BIT0)));
  210. /**
  211. Sample unit test using the SCOPED_TRACE() macro for trace messages.
  212. **/
  213. TEST(MacroTestsMessages, MacroTraceMessage) {
  214. //
  215. // Example of logging.
  216. //
  217. SCOPED_TRACE ("SCOPED_TRACE message\n");
  218. }
  219. int main(int argc, char* argv[]) {
  220. testing::InitGoogleTest(&argc, argv);
  221. return RUN_ALL_TESTS();
  222. }