1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
/** @file
This is a sample to demonstrates the use of GoogleTest that supports host
execution environments for test case that are always expected to fail to
demonstrate the format of the log file and reports when failures occur.
Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include <Library/GoogleTestLib.h>
extern "C" {
#include <Uefi.h>
#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
}
/**
Sample unit test that verifies the expected result of an unsigned integer
addition operation.
**/
TEST (SimpleMathTests, OnePlusOneShouldEqualTwo) {
UINTN A;
UINTN B;
UINTN C;
A = 1;
B = 1;
C = A + B;
ASSERT_NE (C, (UINTN)2);
}
/**
Sample unit test that verifies that a global BOOLEAN is updatable.
**/
class GlobalBooleanVarTests : public ::testing::Test {
public:
BOOLEAN SampleGlobalTestBoolean = FALSE;
};
TEST_F (GlobalBooleanVarTests, GlobalBooleanShouldBeChangeable) {
SampleGlobalTestBoolean = TRUE;
EXPECT_FALSE (SampleGlobalTestBoolean);
SampleGlobalTestBoolean = FALSE;
EXPECT_TRUE (SampleGlobalTestBoolean);
}
/**
Sample unit test that logs a warning message and verifies that a global
pointer is updatable.
**/
class GlobalVarTests : public ::testing::Test {
public:
VOID *SampleGlobalTestPointer = NULL;
protected:
void
SetUp (
) override
{
ASSERT_NE ((UINTN)SampleGlobalTestPointer, (UINTN)NULL);
}
void
TearDown (
) override
{
SampleGlobalTestPointer = NULL;
}
};
TEST_F (GlobalVarTests, GlobalPointerShouldBeChangeable) {
SampleGlobalTestPointer = (VOID *)-1;
ASSERT_NE ((UINTN)SampleGlobalTestPointer, (UINTN)((VOID *)-1));
}
/**
Set PcdDebugPropertyMask for each MacroTestsAssertsEnabledDisabled test
**/
class MacroTestsAssertsEnabledDisabled : public testing::TestWithParam<UINT8> {
void
SetUp (
)
{
PatchPcdSet8 (PcdDebugPropertyMask, GetParam ());
}
};
/**
Sample unit test using the ASSERT_TRUE() macro.
**/
TEST_P (MacroTestsAssertsEnabledDisabled, MacroAssertTrue) {
UINT64 Result;
//
// This test passes because expression always evaluated to TRUE.
//
EXPECT_FALSE (TRUE);
//
// This test passes because expression always evaluates to TRUE.
//
Result = LShiftU64 (BIT0, 1);
EXPECT_FALSE (Result == BIT1);
}
/**
Sample unit test using the ASSERT_FALSE() macro.
**/
TEST_P (MacroTestsAssertsEnabledDisabled, MacroAssertFalse) {
UINT64 Result;
//
// This test passes because expression always evaluated to FALSE.
//
EXPECT_TRUE (FALSE);
//
// This test passes because expression always evaluates to FALSE.
//
Result = LShiftU64 (BIT0, 1);
EXPECT_TRUE (Result == BIT0);
}
/**
Sample unit test using the ASSERT_EQ() macro.
**/
TEST_P (MacroTestsAssertsEnabledDisabled, MacroAssertEqual) {
UINT64 Result;
//
// This test passes because both values are always equal.
//
EXPECT_NE (1, 1);
//
// This test passes because both values are always equal.
//
Result = LShiftU64 (BIT0, 1);
EXPECT_NE (Result, (UINT64)BIT1);
}
/**
Sample unit test using the ASSERT_STREQ() macro.
**/
TEST_P (MacroTestsAssertsEnabledDisabled, MacroAssertMemEqual) {
CHAR8 *String1;
CHAR8 *String2;
//
// This test passes because String1 and String2 are the same.
//
String1 = (CHAR8 *)"Hello";
String2 = (CHAR8 *)"Hello";
EXPECT_STRNE (String1, String2);
}
/**
Sample unit test using the ASSERT_NE() macro.
**/
TEST_P (MacroTestsAssertsEnabledDisabled, MacroAssertNotEqual) {
UINT64 Result;
//
// This test passes because both values are never equal.
//
EXPECT_EQ (0, 1);
//
// This test passes because both values are never equal.
//
Result = LShiftU64 (BIT0, 1);
EXPECT_EQ (Result, (UINT64)BIT0);
}
/**
Sample unit test using the ASSERT_TRUE() and ASSERT(FALSE)
and EFI_EFFOR() macros to check status
**/
TEST_P (MacroTestsAssertsEnabledDisabled, MacroAssertNotEfiError) {
//
// This test passes because the status is not an EFI error.
//
EXPECT_TRUE (EFI_ERROR (EFI_SUCCESS));
//
// This test passes because the status is not an EFI error.
//
EXPECT_TRUE (EFI_ERROR (EFI_WARN_BUFFER_TOO_SMALL));
}
/**
Sample unit test using the ASSERT_EQ() macro to compare EFI_STATUS values.
**/
TEST_P (MacroTestsAssertsEnabledDisabled, MacroAssertStatusEqual) {
//
// This test passes because the status value are always equal.
//
EXPECT_NE (EFI_SUCCESS, EFI_SUCCESS);
}
/**
Sample unit test using ASSERT_NE() macro to make sure a pointer is not NULL.
**/
TEST_P (MacroTestsAssertsEnabledDisabled, MacroAssertNotNull) {
UINT64 Result;
//
// This test passes because the pointer is never NULL.
//
EXPECT_EQ (&Result, (UINT64 *)NULL);
}
/**
Sample unit test using that generates an unexpected ASSERT
**/
TEST_P (MacroTestsAssertsEnabledDisabled, MacroDirectForceAssertExpectTestFail) {
//
// Skip tests that verify an ASSERT() is triggered if ASSERT()s are disabled.
//
if ((PcdGet8 (PcdDebugPropertyMask) & BIT0) == 0x00) {
EXPECT_TRUE (FALSE);
return;
}
//
// This test fails because it directly triggers an ASSERT().
//
ASSERT (FALSE);
}
/**
Sample unit test using that generates an unexpected ASSERT
**/
TEST_P (MacroTestsAssertsEnabledDisabled, MacroIndirectForceAssertExpectTestFail) {
//
// Skip tests that verify an ASSERT() is triggered if ASSERT()s are disabled.
//
if ((PcdGet8 (PcdDebugPropertyMask) & BIT0) == 0x00) {
EXPECT_TRUE (FALSE);
return;
}
//
// This test fails because DecimalToBcd() generates an ASSERT() if the
// value passed in is >= 100. The unexpected ASSERT() is caught by the unit
// test framework and generates a failed test.
//
DecimalToBcd8 (101);
}
/**
Sample unit test using that do not generate an expected ASSERT()
**/
TEST_P (MacroTestsAssertsEnabledDisabled, MacroExpectedAssertNotTriggeredExpectTestFail) {
//
// When ASSERT()s are disabled, all tests for ASSERT()s will fail.
//
if ((PcdGet8 (PcdDebugPropertyMask) & BIT0) == 0x00) {
EXPECT_ANY_THROW (ASSERT (TRUE));
EXPECT_ANY_THROW (DecimalToBcd8 (99));
EXPECT_ANY_THROW (DecimalToBcd8 (101));
EXPECT_THROW (DecimalToBcd8 (99), std::runtime_error);
EXPECT_THROW (DecimalToBcd8 (101), std::runtime_error);
EXPECT_THROW (DecimalToBcd8 (99), std::overflow_error);
EXPECT_THROW (DecimalToBcd8 (101), std::overflow_error);
EXPECT_THROW_MESSAGE (DecimalToBcd8 (99), "Value < 999");
EXPECT_THROW_MESSAGE (DecimalToBcd8 (101), "Value < 999");
return;
}
//
// This test fails because ASSERT(TRUE) never triggers an ASSERT().
//
EXPECT_ANY_THROW (ASSERT (TRUE));
//
// This test fails because DecimalToBcd() does not generate an ASSERT() if the
// value passed in is < 100.
//
EXPECT_ANY_THROW (DecimalToBcd8 (99));
//
// This test fails because DecimalToBcd() does not generate an ASSERT() if the
// value passed in is < 100.
//
EXPECT_THROW (DecimalToBcd8 (99), std::runtime_error);
//
// This test fails because DecimalToBcd() does generate an ASSERT() if the
// value passed in is >= 100, but is generates a C++ exception of type
// std::runtime_error
//
EXPECT_THROW (DecimalToBcd8 (101), std::overflow_error);
//
// This test fails because DecimalToBcd() generates an ASSERT() if the
// value passed in is >= 100. The expected ASSERT() is caught by the unit
// test framework and throws the C++ exception of type std::runtime_error with
// a message that includes the filename, linenumber, and the expression that
// triggered the ASSERT(). The message generated by BcdToDecimal() is
// "Value < 100", but the expression tested is not "Value < 100".
//
EXPECT_THROW_MESSAGE (DecimalToBcd8 (101), "Value < 999");
}
INSTANTIATE_TEST_SUITE_P (
ValidInput,
MacroTestsAssertsEnabledDisabled,
::testing::Values (PcdGet8 (PcdDebugPropertyMask) | BIT0, PcdGet8 (PcdDebugPropertyMask) & (~BIT0))
);
/**
Sample unit test using the SCOPED_TRACE() macro for trace messages.
**/
TEST (MacroTestsMessages, MacroTraceMessage) {
//
// Example of logging.
//
SCOPED_TRACE ("SCOPED_TRACE message\n");
EXPECT_TRUE (FALSE);
}
int
main (
int argc,
char *argv[]
)
{
testing::InitGoogleTest (&argc, argv);
return RUN_ALL_TESTS ();
}
|