2 This is a sample to demonstrates the use of GoogleTest that supports host
3 execution environments.
5 Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <gtest/gtest.h>
13 #include <Library/BaseLib.h>
14 #include <Library/DebugLib.h>
18 Sample unit test that verifies the expected result of an unsigned integer
21 TEST(SimpleMathTests
, OnePlusOneShouldEqualTwo
) {
30 ASSERT_EQ (C
, (UINTN
)2);
34 Sample unit test that verifies that a global BOOLEAN is updatable.
36 class GlobalBooleanVarTests
: public ::testing::Test
{
38 BOOLEAN SampleGlobalTestBoolean
= FALSE
;
41 TEST_F(GlobalBooleanVarTests
, GlobalBooleanShouldBeChangeable
) {
42 SampleGlobalTestBoolean
= TRUE
;
43 ASSERT_TRUE (SampleGlobalTestBoolean
);
45 SampleGlobalTestBoolean
= FALSE
;
46 ASSERT_FALSE (SampleGlobalTestBoolean
);
50 Sample unit test that logs a warning message and verifies that a global
53 class GlobalVarTests
: public ::testing::Test
{
55 VOID
*SampleGlobalTestPointer
= NULL
;
58 void SetUp() override
{
59 ASSERT_EQ ((UINTN
)SampleGlobalTestPointer
, (UINTN
)NULL
);
62 SampleGlobalTestPointer
= NULL
;
66 TEST_F(GlobalVarTests
, GlobalPointerShouldBeChangeable
) {
67 SampleGlobalTestPointer
= (VOID
*)-1;
68 ASSERT_EQ ((UINTN
)SampleGlobalTestPointer
, (UINTN
)((VOID
*)-1));
73 Set PcdDebugPropertyMask for each MacroTestsAssertsEnabledDisabled test
75 class MacroTestsAssertsEnabledDisabled
: public testing::TestWithParam
<UINT8
> {
77 PatchPcdSet8 (PcdDebugPropertyMask
, GetParam());
82 Sample unit test using the ASSERT_TRUE() macro.
84 TEST_P(MacroTestsAssertsEnabledDisabled
, MacroAssertTrue
) {
88 // This test passes because expression always evaluated to TRUE.
93 // This test passes because expression always evaluates to TRUE.
95 Result
= LShiftU64 (BIT0
, 1);
96 ASSERT_TRUE (Result
== BIT1
);
100 Sample unit test using the ASSERT_FALSE() macro.
102 TEST_P(MacroTestsAssertsEnabledDisabled
, MacroAssertFalse
) {
106 // This test passes because expression always evaluated to FALSE.
108 ASSERT_FALSE (FALSE
);
111 // This test passes because expression always evaluates to FALSE.
113 Result
= LShiftU64 (BIT0
, 1);
114 ASSERT_FALSE (Result
== BIT0
);
118 Sample unit test using the ASSERT_EQ() macro.
120 TEST_P(MacroTestsAssertsEnabledDisabled
, MacroAssertEqual
) {
124 // This test passes because both values are always equal.
129 // This test passes because both values are always equal.
131 Result
= LShiftU64 (BIT0
, 1);
132 ASSERT_EQ (Result
, (UINT64
)BIT1
);
136 Sample unit test using the ASSERT_STREQ() macro.
138 TEST_P(MacroTestsAssertsEnabledDisabled
, MacroAssertMemEqual
) {
143 // This test passes because String1 and String2 are the same.
145 String1
= (CHAR8
*)"Hello";
146 String2
= (CHAR8
*)"Hello";
147 ASSERT_STREQ (String1
, String2
);
151 Sample unit test using the ASSERT_NE() macro.
153 TEST_P(MacroTestsAssertsEnabledDisabled
, MacroAssertNotEqual
) {
157 // This test passes because both values are never equal.
162 // This test passes because both values are never equal.
164 Result
= LShiftU64 (BIT0
, 1);
165 ASSERT_NE (Result
, (UINT64
)BIT0
);
169 Sample unit test using the ASSERT_TRUE() and ASSERT(FALSE)
170 and EFI_EFFOR() macros to check status
172 TEST_P(MacroTestsAssertsEnabledDisabled
, MacroAssertNotEfiError
) {
174 // This test passes because the status is not an EFI error.
176 ASSERT_FALSE (EFI_ERROR (EFI_SUCCESS
));
179 // This test passes because the status is not an EFI error.
181 ASSERT_FALSE (EFI_ERROR (EFI_WARN_BUFFER_TOO_SMALL
));
185 Sample unit test using the ASSERT_EQ() macro to compare EFI_STATUS values.
187 TEST_P(MacroTestsAssertsEnabledDisabled
, MacroAssertStatusEqual
) {
189 // This test passes because the status value are always equal.
191 ASSERT_EQ (EFI_SUCCESS
, EFI_SUCCESS
);
195 Sample unit test using ASSERT_NE() macro to make sure a pointer is not NULL.
197 TEST_P(MacroTestsAssertsEnabledDisabled
, MacroAssertNotNull
) {
201 // This test passes because the pointer is never NULL.
203 ASSERT_NE (&Result
, (UINT64
*)NULL
);
207 Sample unit test using that should not generate any ASSERTs()
209 TEST_P(MacroTestsAssertsEnabledDisabled
, MacroExpectNoAssertFailure
) {
211 // This test passes because it never triggers an ASSERT().
216 // This test passes because DecimalToBcd() does not ASSERT() if the
217 // value passed in is <= 99.
223 Sample unit test using the ASSERT_DEATH() macro to test expected ASSERT()s.
225 TEST_P(MacroTestsAssertsEnabledDisabled
, MacroExpectAssertFailure
) {
227 // Skip tests that verify an ASSERT() is triggered if ASSERT()s are disabled.
229 if ((PcdGet8 (PcdDebugPropertyMask
) & BIT0
) == 0x00) {
234 // This test passes because it directly triggers an ASSERT().
236 ASSERT_DEATH (ASSERT (FALSE
), "");
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.
243 ASSERT_DEATH (DecimalToBcd8 (101), "");
246 INSTANTIATE_TEST_SUITE_P(ValidInput
,
247 MacroTestsAssertsEnabledDisabled
,
248 ::testing::Values(PcdGet8 (PcdDebugPropertyMask
) | BIT0
, PcdGet8 (PcdDebugPropertyMask
) & (~BIT0
)));
251 Sample unit test using the SCOPED_TRACE() macro for trace messages.
253 TEST(MacroTestsMessages
, MacroTraceMessage
) {
255 // Example of logging.
257 SCOPED_TRACE ("SCOPED_TRACE message\n");
260 int main(int argc
, char* argv
[]) {
261 testing::InitGoogleTest(&argc
, argv
);
262 return RUN_ALL_TESTS();