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