1f92157deSopenharmony_ci# Assertions Reference 2f92157deSopenharmony_ci 3f92157deSopenharmony_ciThis page lists the assertion macros provided by GoogleTest for verifying code 4f92157deSopenharmony_cibehavior. To use them, include the header `gtest/gtest.h`. 5f92157deSopenharmony_ci 6f92157deSopenharmony_ciThe majority of the macros listed below come as a pair with an `EXPECT_` variant 7f92157deSopenharmony_ciand an `ASSERT_` variant. Upon failure, `EXPECT_` macros generate nonfatal 8f92157deSopenharmony_cifailures and allow the current function to continue running, while `ASSERT_` 9f92157deSopenharmony_cimacros generate fatal failures and abort the current function. 10f92157deSopenharmony_ci 11f92157deSopenharmony_ciAll assertion macros support streaming a custom failure message into them with 12f92157deSopenharmony_cithe `<<` operator, for example: 13f92157deSopenharmony_ci 14f92157deSopenharmony_ci```cpp 15f92157deSopenharmony_ciEXPECT_TRUE(my_condition) << "My condition is not true"; 16f92157deSopenharmony_ci``` 17f92157deSopenharmony_ci 18f92157deSopenharmony_ciAnything that can be streamed to an `ostream` can be streamed to an assertion 19f92157deSopenharmony_cimacro—in particular, C strings and string objects. If a wide string (`wchar_t*`, 20f92157deSopenharmony_ci`TCHAR*` in `UNICODE` mode on Windows, or `std::wstring`) is streamed to an 21f92157deSopenharmony_ciassertion, it will be translated to UTF-8 when printed. 22f92157deSopenharmony_ci 23f92157deSopenharmony_ci## Explicit Success and Failure {#success-failure} 24f92157deSopenharmony_ci 25f92157deSopenharmony_ciThe assertions in this section generate a success or failure directly instead of 26f92157deSopenharmony_citesting a value or expression. These are useful when control flow, rather than a 27f92157deSopenharmony_ciBoolean expression, determines the test's success or failure, as shown by the 28f92157deSopenharmony_cifollowing example: 29f92157deSopenharmony_ci 30f92157deSopenharmony_ci```c++ 31f92157deSopenharmony_ciswitch(expression) { 32f92157deSopenharmony_ci case 1: 33f92157deSopenharmony_ci ... some checks ... 34f92157deSopenharmony_ci case 2: 35f92157deSopenharmony_ci ... some other checks ... 36f92157deSopenharmony_ci default: 37f92157deSopenharmony_ci FAIL() << "We shouldn't get here."; 38f92157deSopenharmony_ci} 39f92157deSopenharmony_ci``` 40f92157deSopenharmony_ci 41f92157deSopenharmony_ci### SUCCEED {#SUCCEED} 42f92157deSopenharmony_ci 43f92157deSopenharmony_ci`SUCCEED()` 44f92157deSopenharmony_ci 45f92157deSopenharmony_ciGenerates a success. This *does not* make the overall test succeed. A test is 46f92157deSopenharmony_ciconsidered successful only if none of its assertions fail during its execution. 47f92157deSopenharmony_ci 48f92157deSopenharmony_ciThe `SUCCEED` assertion is purely documentary and currently doesn't generate any 49f92157deSopenharmony_ciuser-visible output. However, we may add `SUCCEED` messages to GoogleTest output 50f92157deSopenharmony_ciin the future. 51f92157deSopenharmony_ci 52f92157deSopenharmony_ci### FAIL {#FAIL} 53f92157deSopenharmony_ci 54f92157deSopenharmony_ci`FAIL()` 55f92157deSopenharmony_ci 56f92157deSopenharmony_ciGenerates a fatal failure, which returns from the current function. 57f92157deSopenharmony_ci 58f92157deSopenharmony_ciCan only be used in functions that return `void`. See 59f92157deSopenharmony_ci[Assertion Placement](../advanced.md#assertion-placement) for more information. 60f92157deSopenharmony_ci 61f92157deSopenharmony_ci### ADD_FAILURE {#ADD_FAILURE} 62f92157deSopenharmony_ci 63f92157deSopenharmony_ci`ADD_FAILURE()` 64f92157deSopenharmony_ci 65f92157deSopenharmony_ciGenerates a nonfatal failure, which allows the current function to continue 66f92157deSopenharmony_cirunning. 67f92157deSopenharmony_ci 68f92157deSopenharmony_ci### ADD_FAILURE_AT {#ADD_FAILURE_AT} 69f92157deSopenharmony_ci 70f92157deSopenharmony_ci`ADD_FAILURE_AT(`*`file_path`*`,`*`line_number`*`)` 71f92157deSopenharmony_ci 72f92157deSopenharmony_ciGenerates a nonfatal failure at the file and line number specified. 73f92157deSopenharmony_ci 74f92157deSopenharmony_ci## Generalized Assertion {#generalized} 75f92157deSopenharmony_ci 76f92157deSopenharmony_ciThe following assertion allows [matchers](matchers.md) to be used to verify 77f92157deSopenharmony_civalues. 78f92157deSopenharmony_ci 79f92157deSopenharmony_ci### EXPECT_THAT {#EXPECT_THAT} 80f92157deSopenharmony_ci 81f92157deSopenharmony_ci`EXPECT_THAT(`*`value`*`,`*`matcher`*`)` \ 82f92157deSopenharmony_ci`ASSERT_THAT(`*`value`*`,`*`matcher`*`)` 83f92157deSopenharmony_ci 84f92157deSopenharmony_ciVerifies that *`value`* matches the [matcher](matchers.md) *`matcher`*. 85f92157deSopenharmony_ci 86f92157deSopenharmony_ciFor example, the following code verifies that the string `value1` starts with 87f92157deSopenharmony_ci`"Hello"`, `value2` matches a regular expression, and `value3` is between 5 and 88f92157deSopenharmony_ci10: 89f92157deSopenharmony_ci 90f92157deSopenharmony_ci```cpp 91f92157deSopenharmony_ci#include "gmock/gmock.h" 92f92157deSopenharmony_ci 93f92157deSopenharmony_ciusing ::testing::AllOf; 94f92157deSopenharmony_ciusing ::testing::Gt; 95f92157deSopenharmony_ciusing ::testing::Lt; 96f92157deSopenharmony_ciusing ::testing::MatchesRegex; 97f92157deSopenharmony_ciusing ::testing::StartsWith; 98f92157deSopenharmony_ci 99f92157deSopenharmony_ci... 100f92157deSopenharmony_ciEXPECT_THAT(value1, StartsWith("Hello")); 101f92157deSopenharmony_ciEXPECT_THAT(value2, MatchesRegex("Line \\d+")); 102f92157deSopenharmony_ciASSERT_THAT(value3, AllOf(Gt(5), Lt(10))); 103f92157deSopenharmony_ci``` 104f92157deSopenharmony_ci 105f92157deSopenharmony_ciMatchers enable assertions of this form to read like English and generate 106f92157deSopenharmony_ciinformative failure messages. For example, if the above assertion on `value1` 107f92157deSopenharmony_cifails, the resulting message will be similar to the following: 108f92157deSopenharmony_ci 109f92157deSopenharmony_ci``` 110f92157deSopenharmony_ciValue of: value1 111f92157deSopenharmony_ci Actual: "Hi, world!" 112f92157deSopenharmony_ciExpected: starts with "Hello" 113f92157deSopenharmony_ci``` 114f92157deSopenharmony_ci 115f92157deSopenharmony_ciGoogleTest provides a built-in library of matchers—see the 116f92157deSopenharmony_ci[Matchers Reference](matchers.md). It is also possible to write your own 117f92157deSopenharmony_cimatchers—see [Writing New Matchers Quickly](../gmock_cook_book.md#NewMatchers). 118f92157deSopenharmony_ciThe use of matchers makes `EXPECT_THAT` a powerful, extensible assertion. 119f92157deSopenharmony_ci 120f92157deSopenharmony_ci*The idea for this assertion was borrowed from Joe Walnes' Hamcrest project, 121f92157deSopenharmony_ciwhich adds `assertThat()` to JUnit.* 122f92157deSopenharmony_ci 123f92157deSopenharmony_ci## Boolean Conditions {#boolean} 124f92157deSopenharmony_ci 125f92157deSopenharmony_ciThe following assertions test Boolean conditions. 126f92157deSopenharmony_ci 127f92157deSopenharmony_ci### EXPECT_TRUE {#EXPECT_TRUE} 128f92157deSopenharmony_ci 129f92157deSopenharmony_ci`EXPECT_TRUE(`*`condition`*`)` \ 130f92157deSopenharmony_ci`ASSERT_TRUE(`*`condition`*`)` 131f92157deSopenharmony_ci 132f92157deSopenharmony_ciVerifies that *`condition`* is true. 133f92157deSopenharmony_ci 134f92157deSopenharmony_ci### EXPECT_FALSE {#EXPECT_FALSE} 135f92157deSopenharmony_ci 136f92157deSopenharmony_ci`EXPECT_FALSE(`*`condition`*`)` \ 137f92157deSopenharmony_ci`ASSERT_FALSE(`*`condition`*`)` 138f92157deSopenharmony_ci 139f92157deSopenharmony_ciVerifies that *`condition`* is false. 140f92157deSopenharmony_ci 141f92157deSopenharmony_ci## Binary Comparison {#binary-comparison} 142f92157deSopenharmony_ci 143f92157deSopenharmony_ciThe following assertions compare two values. The value arguments must be 144f92157deSopenharmony_cicomparable by the assertion's comparison operator, otherwise a compiler error 145f92157deSopenharmony_ciwill result. 146f92157deSopenharmony_ci 147f92157deSopenharmony_ciIf an argument supports the `<<` operator, it will be called to print the 148f92157deSopenharmony_ciargument when the assertion fails. Otherwise, GoogleTest will attempt to print 149f92157deSopenharmony_cithem in the best way it can—see 150f92157deSopenharmony_ci[Teaching GoogleTest How to Print Your Values](../advanced.md#teaching-googletest-how-to-print-your-values). 151f92157deSopenharmony_ci 152f92157deSopenharmony_ciArguments are always evaluated exactly once, so it's OK for the arguments to 153f92157deSopenharmony_cihave side effects. However, the argument evaluation order is undefined and 154f92157deSopenharmony_ciprograms should not depend on any particular argument evaluation order. 155f92157deSopenharmony_ci 156f92157deSopenharmony_ciThese assertions work with both narrow and wide string objects (`string` and 157f92157deSopenharmony_ci`wstring`). 158f92157deSopenharmony_ci 159f92157deSopenharmony_ciSee also the [Floating-Point Comparison](#floating-point) assertions to compare 160f92157deSopenharmony_cifloating-point numbers and avoid problems caused by rounding. 161f92157deSopenharmony_ci 162f92157deSopenharmony_ci### EXPECT_EQ {#EXPECT_EQ} 163f92157deSopenharmony_ci 164f92157deSopenharmony_ci`EXPECT_EQ(`*`val1`*`,`*`val2`*`)` \ 165f92157deSopenharmony_ci`ASSERT_EQ(`*`val1`*`,`*`val2`*`)` 166f92157deSopenharmony_ci 167f92157deSopenharmony_ciVerifies that *`val1`*`==`*`val2`*. 168f92157deSopenharmony_ci 169f92157deSopenharmony_ciDoes pointer equality on pointers. If used on two C strings, it tests if they 170f92157deSopenharmony_ciare in the same memory location, not if they have the same value. Use 171f92157deSopenharmony_ci[`EXPECT_STREQ`](#EXPECT_STREQ) to compare C strings (e.g. `const char*`) by 172f92157deSopenharmony_civalue. 173f92157deSopenharmony_ci 174f92157deSopenharmony_ciWhen comparing a pointer to `NULL`, use `EXPECT_EQ(`*`ptr`*`, nullptr)` instead 175f92157deSopenharmony_ciof `EXPECT_EQ(`*`ptr`*`, NULL)`. 176f92157deSopenharmony_ci 177f92157deSopenharmony_ci### EXPECT_NE {#EXPECT_NE} 178f92157deSopenharmony_ci 179f92157deSopenharmony_ci`EXPECT_NE(`*`val1`*`,`*`val2`*`)` \ 180f92157deSopenharmony_ci`ASSERT_NE(`*`val1`*`,`*`val2`*`)` 181f92157deSopenharmony_ci 182f92157deSopenharmony_ciVerifies that *`val1`*`!=`*`val2`*. 183f92157deSopenharmony_ci 184f92157deSopenharmony_ciDoes pointer equality on pointers. If used on two C strings, it tests if they 185f92157deSopenharmony_ciare in different memory locations, not if they have different values. Use 186f92157deSopenharmony_ci[`EXPECT_STRNE`](#EXPECT_STRNE) to compare C strings (e.g. `const char*`) by 187f92157deSopenharmony_civalue. 188f92157deSopenharmony_ci 189f92157deSopenharmony_ciWhen comparing a pointer to `NULL`, use `EXPECT_NE(`*`ptr`*`, nullptr)` instead 190f92157deSopenharmony_ciof `EXPECT_NE(`*`ptr`*`, NULL)`. 191f92157deSopenharmony_ci 192f92157deSopenharmony_ci### EXPECT_LT {#EXPECT_LT} 193f92157deSopenharmony_ci 194f92157deSopenharmony_ci`EXPECT_LT(`*`val1`*`,`*`val2`*`)` \ 195f92157deSopenharmony_ci`ASSERT_LT(`*`val1`*`,`*`val2`*`)` 196f92157deSopenharmony_ci 197f92157deSopenharmony_ciVerifies that *`val1`*`<`*`val2`*. 198f92157deSopenharmony_ci 199f92157deSopenharmony_ci### EXPECT_LE {#EXPECT_LE} 200f92157deSopenharmony_ci 201f92157deSopenharmony_ci`EXPECT_LE(`*`val1`*`,`*`val2`*`)` \ 202f92157deSopenharmony_ci`ASSERT_LE(`*`val1`*`,`*`val2`*`)` 203f92157deSopenharmony_ci 204f92157deSopenharmony_ciVerifies that *`val1`*`<=`*`val2`*. 205f92157deSopenharmony_ci 206f92157deSopenharmony_ci### EXPECT_GT {#EXPECT_GT} 207f92157deSopenharmony_ci 208f92157deSopenharmony_ci`EXPECT_GT(`*`val1`*`,`*`val2`*`)` \ 209f92157deSopenharmony_ci`ASSERT_GT(`*`val1`*`,`*`val2`*`)` 210f92157deSopenharmony_ci 211f92157deSopenharmony_ciVerifies that *`val1`*`>`*`val2`*. 212f92157deSopenharmony_ci 213f92157deSopenharmony_ci### EXPECT_GE {#EXPECT_GE} 214f92157deSopenharmony_ci 215f92157deSopenharmony_ci`EXPECT_GE(`*`val1`*`,`*`val2`*`)` \ 216f92157deSopenharmony_ci`ASSERT_GE(`*`val1`*`,`*`val2`*`)` 217f92157deSopenharmony_ci 218f92157deSopenharmony_ciVerifies that *`val1`*`>=`*`val2`*. 219f92157deSopenharmony_ci 220f92157deSopenharmony_ci## String Comparison {#c-strings} 221f92157deSopenharmony_ci 222f92157deSopenharmony_ciThe following assertions compare two **C strings**. To compare two `string` 223f92157deSopenharmony_ciobjects, use [`EXPECT_EQ`](#EXPECT_EQ) or [`EXPECT_NE`](#EXPECT_NE) instead. 224f92157deSopenharmony_ci 225f92157deSopenharmony_ciThese assertions also accept wide C strings (`wchar_t*`). If a comparison of two 226f92157deSopenharmony_ciwide strings fails, their values will be printed as UTF-8 narrow strings. 227f92157deSopenharmony_ci 228f92157deSopenharmony_ciTo compare a C string with `NULL`, use `EXPECT_EQ(`*`c_string`*`, nullptr)` or 229f92157deSopenharmony_ci`EXPECT_NE(`*`c_string`*`, nullptr)`. 230f92157deSopenharmony_ci 231f92157deSopenharmony_ci### EXPECT_STREQ {#EXPECT_STREQ} 232f92157deSopenharmony_ci 233f92157deSopenharmony_ci`EXPECT_STREQ(`*`str1`*`,`*`str2`*`)` \ 234f92157deSopenharmony_ci`ASSERT_STREQ(`*`str1`*`,`*`str2`*`)` 235f92157deSopenharmony_ci 236f92157deSopenharmony_ciVerifies that the two C strings *`str1`* and *`str2`* have the same contents. 237f92157deSopenharmony_ci 238f92157deSopenharmony_ci### EXPECT_STRNE {#EXPECT_STRNE} 239f92157deSopenharmony_ci 240f92157deSopenharmony_ci`EXPECT_STRNE(`*`str1`*`,`*`str2`*`)` \ 241f92157deSopenharmony_ci`ASSERT_STRNE(`*`str1`*`,`*`str2`*`)` 242f92157deSopenharmony_ci 243f92157deSopenharmony_ciVerifies that the two C strings *`str1`* and *`str2`* have different contents. 244f92157deSopenharmony_ci 245f92157deSopenharmony_ci### EXPECT_STRCASEEQ {#EXPECT_STRCASEEQ} 246f92157deSopenharmony_ci 247f92157deSopenharmony_ci`EXPECT_STRCASEEQ(`*`str1`*`,`*`str2`*`)` \ 248f92157deSopenharmony_ci`ASSERT_STRCASEEQ(`*`str1`*`,`*`str2`*`)` 249f92157deSopenharmony_ci 250f92157deSopenharmony_ciVerifies that the two C strings *`str1`* and *`str2`* have the same contents, 251f92157deSopenharmony_ciignoring case. 252f92157deSopenharmony_ci 253f92157deSopenharmony_ci### EXPECT_STRCASENE {#EXPECT_STRCASENE} 254f92157deSopenharmony_ci 255f92157deSopenharmony_ci`EXPECT_STRCASENE(`*`str1`*`,`*`str2`*`)` \ 256f92157deSopenharmony_ci`ASSERT_STRCASENE(`*`str1`*`,`*`str2`*`)` 257f92157deSopenharmony_ci 258f92157deSopenharmony_ciVerifies that the two C strings *`str1`* and *`str2`* have different contents, 259f92157deSopenharmony_ciignoring case. 260f92157deSopenharmony_ci 261f92157deSopenharmony_ci## Floating-Point Comparison {#floating-point} 262f92157deSopenharmony_ci 263f92157deSopenharmony_ciThe following assertions compare two floating-point values. 264f92157deSopenharmony_ci 265f92157deSopenharmony_ciDue to rounding errors, it is very unlikely that two floating-point values will 266f92157deSopenharmony_cimatch exactly, so `EXPECT_EQ` is not suitable. In general, for floating-point 267f92157deSopenharmony_cicomparison to make sense, the user needs to carefully choose the error bound. 268f92157deSopenharmony_ci 269f92157deSopenharmony_ciGoogleTest also provides assertions that use a default error bound based on 270f92157deSopenharmony_ciUnits in the Last Place (ULPs). To learn more about ULPs, see the article 271f92157deSopenharmony_ci[Comparing Floating Point Numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/). 272f92157deSopenharmony_ci 273f92157deSopenharmony_ci### EXPECT_FLOAT_EQ {#EXPECT_FLOAT_EQ} 274f92157deSopenharmony_ci 275f92157deSopenharmony_ci`EXPECT_FLOAT_EQ(`*`val1`*`,`*`val2`*`)` \ 276f92157deSopenharmony_ci`ASSERT_FLOAT_EQ(`*`val1`*`,`*`val2`*`)` 277f92157deSopenharmony_ci 278f92157deSopenharmony_ciVerifies that the two `float` values *`val1`* and *`val2`* are approximately 279f92157deSopenharmony_ciequal, to within 4 ULPs from each other. 280f92157deSopenharmony_ci 281f92157deSopenharmony_ci### EXPECT_DOUBLE_EQ {#EXPECT_DOUBLE_EQ} 282f92157deSopenharmony_ci 283f92157deSopenharmony_ci`EXPECT_DOUBLE_EQ(`*`val1`*`,`*`val2`*`)` \ 284f92157deSopenharmony_ci`ASSERT_DOUBLE_EQ(`*`val1`*`,`*`val2`*`)` 285f92157deSopenharmony_ci 286f92157deSopenharmony_ciVerifies that the two `double` values *`val1`* and *`val2`* are approximately 287f92157deSopenharmony_ciequal, to within 4 ULPs from each other. 288f92157deSopenharmony_ci 289f92157deSopenharmony_ci### EXPECT_NEAR {#EXPECT_NEAR} 290f92157deSopenharmony_ci 291f92157deSopenharmony_ci`EXPECT_NEAR(`*`val1`*`,`*`val2`*`,`*`abs_error`*`)` \ 292f92157deSopenharmony_ci`ASSERT_NEAR(`*`val1`*`,`*`val2`*`,`*`abs_error`*`)` 293f92157deSopenharmony_ci 294f92157deSopenharmony_ciVerifies that the difference between *`val1`* and *`val2`* does not exceed the 295f92157deSopenharmony_ciabsolute error bound *`abs_error`*. 296f92157deSopenharmony_ci 297f92157deSopenharmony_ci## Exception Assertions {#exceptions} 298f92157deSopenharmony_ci 299f92157deSopenharmony_ciThe following assertions verify that a piece of code throws, or does not throw, 300f92157deSopenharmony_cian exception. Usage requires exceptions to be enabled in the build environment. 301f92157deSopenharmony_ci 302f92157deSopenharmony_ciNote that the piece of code under test can be a compound statement, for example: 303f92157deSopenharmony_ci 304f92157deSopenharmony_ci```cpp 305f92157deSopenharmony_ciEXPECT_NO_THROW({ 306f92157deSopenharmony_ci int n = 5; 307f92157deSopenharmony_ci DoSomething(&n); 308f92157deSopenharmony_ci}); 309f92157deSopenharmony_ci``` 310f92157deSopenharmony_ci 311f92157deSopenharmony_ci### EXPECT_THROW {#EXPECT_THROW} 312f92157deSopenharmony_ci 313f92157deSopenharmony_ci`EXPECT_THROW(`*`statement`*`,`*`exception_type`*`)` \ 314f92157deSopenharmony_ci`ASSERT_THROW(`*`statement`*`,`*`exception_type`*`)` 315f92157deSopenharmony_ci 316f92157deSopenharmony_ciVerifies that *`statement`* throws an exception of type *`exception_type`*. 317f92157deSopenharmony_ci 318f92157deSopenharmony_ci### EXPECT_ANY_THROW {#EXPECT_ANY_THROW} 319f92157deSopenharmony_ci 320f92157deSopenharmony_ci`EXPECT_ANY_THROW(`*`statement`*`)` \ 321f92157deSopenharmony_ci`ASSERT_ANY_THROW(`*`statement`*`)` 322f92157deSopenharmony_ci 323f92157deSopenharmony_ciVerifies that *`statement`* throws an exception of any type. 324f92157deSopenharmony_ci 325f92157deSopenharmony_ci### EXPECT_NO_THROW {#EXPECT_NO_THROW} 326f92157deSopenharmony_ci 327f92157deSopenharmony_ci`EXPECT_NO_THROW(`*`statement`*`)` \ 328f92157deSopenharmony_ci`ASSERT_NO_THROW(`*`statement`*`)` 329f92157deSopenharmony_ci 330f92157deSopenharmony_ciVerifies that *`statement`* does not throw any exception. 331f92157deSopenharmony_ci 332f92157deSopenharmony_ci## Predicate Assertions {#predicates} 333f92157deSopenharmony_ci 334f92157deSopenharmony_ciThe following assertions enable more complex predicates to be verified while 335f92157deSopenharmony_ciprinting a more clear failure message than if `EXPECT_TRUE` were used alone. 336f92157deSopenharmony_ci 337f92157deSopenharmony_ci### EXPECT_PRED* {#EXPECT_PRED} 338f92157deSopenharmony_ci 339f92157deSopenharmony_ci`EXPECT_PRED1(`*`pred`*`,`*`val1`*`)` \ 340f92157deSopenharmony_ci`EXPECT_PRED2(`*`pred`*`,`*`val1`*`,`*`val2`*`)` \ 341f92157deSopenharmony_ci`EXPECT_PRED3(`*`pred`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`)` \ 342f92157deSopenharmony_ci`EXPECT_PRED4(`*`pred`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`,`*`val4`*`)` \ 343f92157deSopenharmony_ci`EXPECT_PRED5(`*`pred`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`,`*`val4`*`,`*`val5`*`)` 344f92157deSopenharmony_ci 345f92157deSopenharmony_ci`ASSERT_PRED1(`*`pred`*`,`*`val1`*`)` \ 346f92157deSopenharmony_ci`ASSERT_PRED2(`*`pred`*`,`*`val1`*`,`*`val2`*`)` \ 347f92157deSopenharmony_ci`ASSERT_PRED3(`*`pred`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`)` \ 348f92157deSopenharmony_ci`ASSERT_PRED4(`*`pred`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`,`*`val4`*`)` \ 349f92157deSopenharmony_ci`ASSERT_PRED5(`*`pred`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`,`*`val4`*`,`*`val5`*`)` 350f92157deSopenharmony_ci 351f92157deSopenharmony_ciVerifies that the predicate *`pred`* returns `true` when passed the given values 352f92157deSopenharmony_cias arguments. 353f92157deSopenharmony_ci 354f92157deSopenharmony_ciThe parameter *`pred`* is a function or functor that accepts as many arguments 355f92157deSopenharmony_cias the corresponding macro accepts values. If *`pred`* returns `true` for the 356f92157deSopenharmony_cigiven arguments, the assertion succeeds, otherwise the assertion fails. 357f92157deSopenharmony_ci 358f92157deSopenharmony_ciWhen the assertion fails, it prints the value of each argument. Arguments are 359f92157deSopenharmony_cialways evaluated exactly once. 360f92157deSopenharmony_ci 361f92157deSopenharmony_ciAs an example, see the following code: 362f92157deSopenharmony_ci 363f92157deSopenharmony_ci```cpp 364f92157deSopenharmony_ci// Returns true if m and n have no common divisors except 1. 365f92157deSopenharmony_cibool MutuallyPrime(int m, int n) { ... } 366f92157deSopenharmony_ci... 367f92157deSopenharmony_ciconst int a = 3; 368f92157deSopenharmony_ciconst int b = 4; 369f92157deSopenharmony_ciconst int c = 10; 370f92157deSopenharmony_ci... 371f92157deSopenharmony_ciEXPECT_PRED2(MutuallyPrime, a, b); // Succeeds 372f92157deSopenharmony_ciEXPECT_PRED2(MutuallyPrime, b, c); // Fails 373f92157deSopenharmony_ci``` 374f92157deSopenharmony_ci 375f92157deSopenharmony_ciIn the above example, the first assertion succeeds, and the second fails with 376f92157deSopenharmony_cithe following message: 377f92157deSopenharmony_ci 378f92157deSopenharmony_ci``` 379f92157deSopenharmony_ciMutuallyPrime(b, c) is false, where 380f92157deSopenharmony_cib is 4 381f92157deSopenharmony_cic is 10 382f92157deSopenharmony_ci``` 383f92157deSopenharmony_ci 384f92157deSopenharmony_ciNote that if the given predicate is an overloaded function or a function 385f92157deSopenharmony_citemplate, the assertion macro might not be able to determine which version to 386f92157deSopenharmony_ciuse, and it might be necessary to explicitly specify the type of the function. 387f92157deSopenharmony_ciFor example, for a Boolean function `IsPositive()` overloaded to take either a 388f92157deSopenharmony_cisingle `int` or `double` argument, it would be necessary to write one of the 389f92157deSopenharmony_cifollowing: 390f92157deSopenharmony_ci 391f92157deSopenharmony_ci```cpp 392f92157deSopenharmony_ciEXPECT_PRED1(static_cast<bool (*)(int)>(IsPositive), 5); 393f92157deSopenharmony_ciEXPECT_PRED1(static_cast<bool (*)(double)>(IsPositive), 3.14); 394f92157deSopenharmony_ci``` 395f92157deSopenharmony_ci 396f92157deSopenharmony_ciWriting simply `EXPECT_PRED1(IsPositive, 5);` would result in a compiler error. 397f92157deSopenharmony_ciSimilarly, to use a template function, specify the template arguments: 398f92157deSopenharmony_ci 399f92157deSopenharmony_ci```cpp 400f92157deSopenharmony_citemplate <typename T> 401f92157deSopenharmony_cibool IsNegative(T x) { 402f92157deSopenharmony_ci return x < 0; 403f92157deSopenharmony_ci} 404f92157deSopenharmony_ci... 405f92157deSopenharmony_ciEXPECT_PRED1(IsNegative<int>, -5); // Must specify type for IsNegative 406f92157deSopenharmony_ci``` 407f92157deSopenharmony_ci 408f92157deSopenharmony_ciIf a template has multiple parameters, wrap the predicate in parentheses so the 409f92157deSopenharmony_cimacro arguments are parsed correctly: 410f92157deSopenharmony_ci 411f92157deSopenharmony_ci```cpp 412f92157deSopenharmony_ciASSERT_PRED2((MyPredicate<int, int>), 5, 0); 413f92157deSopenharmony_ci``` 414f92157deSopenharmony_ci 415f92157deSopenharmony_ci### EXPECT_PRED_FORMAT* {#EXPECT_PRED_FORMAT} 416f92157deSopenharmony_ci 417f92157deSopenharmony_ci`EXPECT_PRED_FORMAT1(`*`pred_formatter`*`,`*`val1`*`)` \ 418f92157deSopenharmony_ci`EXPECT_PRED_FORMAT2(`*`pred_formatter`*`,`*`val1`*`,`*`val2`*`)` \ 419f92157deSopenharmony_ci`EXPECT_PRED_FORMAT3(`*`pred_formatter`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`)` \ 420f92157deSopenharmony_ci`EXPECT_PRED_FORMAT4(`*`pred_formatter`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`,`*`val4`*`)` 421f92157deSopenharmony_ci\ 422f92157deSopenharmony_ci`EXPECT_PRED_FORMAT5(`*`pred_formatter`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`,`*`val4`*`,`*`val5`*`)` 423f92157deSopenharmony_ci 424f92157deSopenharmony_ci`ASSERT_PRED_FORMAT1(`*`pred_formatter`*`,`*`val1`*`)` \ 425f92157deSopenharmony_ci`ASSERT_PRED_FORMAT2(`*`pred_formatter`*`,`*`val1`*`,`*`val2`*`)` \ 426f92157deSopenharmony_ci`ASSERT_PRED_FORMAT3(`*`pred_formatter`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`)` \ 427f92157deSopenharmony_ci`ASSERT_PRED_FORMAT4(`*`pred_formatter`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`,`*`val4`*`)` 428f92157deSopenharmony_ci\ 429f92157deSopenharmony_ci`ASSERT_PRED_FORMAT5(`*`pred_formatter`*`,`*`val1`*`,`*`val2`*`,`*`val3`*`,`*`val4`*`,`*`val5`*`)` 430f92157deSopenharmony_ci 431f92157deSopenharmony_ciVerifies that the predicate *`pred_formatter`* succeeds when passed the given 432f92157deSopenharmony_civalues as arguments. 433f92157deSopenharmony_ci 434f92157deSopenharmony_ciThe parameter *`pred_formatter`* is a *predicate-formatter*, which is a function 435f92157deSopenharmony_cior functor with the signature: 436f92157deSopenharmony_ci 437f92157deSopenharmony_ci```cpp 438f92157deSopenharmony_citesting::AssertionResult PredicateFormatter(const char* expr1, 439f92157deSopenharmony_ci const char* expr2, 440f92157deSopenharmony_ci ... 441f92157deSopenharmony_ci const char* exprn, 442f92157deSopenharmony_ci T1 val1, 443f92157deSopenharmony_ci T2 val2, 444f92157deSopenharmony_ci ... 445f92157deSopenharmony_ci Tn valn); 446f92157deSopenharmony_ci``` 447f92157deSopenharmony_ci 448f92157deSopenharmony_ciwhere *`val1`*, *`val2`*, ..., *`valn`* are the values of the predicate 449f92157deSopenharmony_ciarguments, and *`expr1`*, *`expr2`*, ..., *`exprn`* are the corresponding 450f92157deSopenharmony_ciexpressions as they appear in the source code. The types `T1`, `T2`, ..., `Tn` 451f92157deSopenharmony_cican be either value types or reference types; if an argument has type `T`, it 452f92157deSopenharmony_cican be declared as either `T` or `const T&`, whichever is appropriate. For more 453f92157deSopenharmony_ciabout the return type `testing::AssertionResult`, see 454f92157deSopenharmony_ci[Using a Function That Returns an AssertionResult](../advanced.md#using-a-function-that-returns-an-assertionresult). 455f92157deSopenharmony_ci 456f92157deSopenharmony_ciAs an example, see the following code: 457f92157deSopenharmony_ci 458f92157deSopenharmony_ci```cpp 459f92157deSopenharmony_ci// Returns the smallest prime common divisor of m and n, 460f92157deSopenharmony_ci// or 1 when m and n are mutually prime. 461f92157deSopenharmony_ciint SmallestPrimeCommonDivisor(int m, int n) { ... } 462f92157deSopenharmony_ci 463f92157deSopenharmony_ci// Returns true if m and n have no common divisors except 1. 464f92157deSopenharmony_cibool MutuallyPrime(int m, int n) { ... } 465f92157deSopenharmony_ci 466f92157deSopenharmony_ci// A predicate-formatter for asserting that two integers are mutually prime. 467f92157deSopenharmony_citesting::AssertionResult AssertMutuallyPrime(const char* m_expr, 468f92157deSopenharmony_ci const char* n_expr, 469f92157deSopenharmony_ci int m, 470f92157deSopenharmony_ci int n) { 471f92157deSopenharmony_ci if (MutuallyPrime(m, n)) return testing::AssertionSuccess(); 472f92157deSopenharmony_ci 473f92157deSopenharmony_ci return testing::AssertionFailure() << m_expr << " and " << n_expr 474f92157deSopenharmony_ci << " (" << m << " and " << n << ") are not mutually prime, " 475f92157deSopenharmony_ci << "as they have a common divisor " << SmallestPrimeCommonDivisor(m, n); 476f92157deSopenharmony_ci} 477f92157deSopenharmony_ci 478f92157deSopenharmony_ci... 479f92157deSopenharmony_ciconst int a = 3; 480f92157deSopenharmony_ciconst int b = 4; 481f92157deSopenharmony_ciconst int c = 10; 482f92157deSopenharmony_ci... 483f92157deSopenharmony_ciEXPECT_PRED_FORMAT2(AssertMutuallyPrime, a, b); // Succeeds 484f92157deSopenharmony_ciEXPECT_PRED_FORMAT2(AssertMutuallyPrime, b, c); // Fails 485f92157deSopenharmony_ci``` 486f92157deSopenharmony_ci 487f92157deSopenharmony_ciIn the above example, the final assertion fails and the predicate-formatter 488f92157deSopenharmony_ciproduces the following failure message: 489f92157deSopenharmony_ci 490f92157deSopenharmony_ci``` 491f92157deSopenharmony_cib and c (4 and 10) are not mutually prime, as they have a common divisor 2 492f92157deSopenharmony_ci``` 493f92157deSopenharmony_ci 494f92157deSopenharmony_ci## Windows HRESULT Assertions {#HRESULT} 495f92157deSopenharmony_ci 496f92157deSopenharmony_ciThe following assertions test for `HRESULT` success or failure. For example: 497f92157deSopenharmony_ci 498f92157deSopenharmony_ci```cpp 499f92157deSopenharmony_ciCComPtr<IShellDispatch2> shell; 500f92157deSopenharmony_ciASSERT_HRESULT_SUCCEEDED(shell.CoCreateInstance(L"Shell.Application")); 501f92157deSopenharmony_ciCComVariant empty; 502f92157deSopenharmony_ciASSERT_HRESULT_SUCCEEDED(shell->ShellExecute(CComBSTR(url), empty, empty, empty, empty)); 503f92157deSopenharmony_ci``` 504f92157deSopenharmony_ci 505f92157deSopenharmony_ciThe generated output contains the human-readable error message associated with 506f92157deSopenharmony_cithe returned `HRESULT` code. 507f92157deSopenharmony_ci 508f92157deSopenharmony_ci### EXPECT_HRESULT_SUCCEEDED {#EXPECT_HRESULT_SUCCEEDED} 509f92157deSopenharmony_ci 510f92157deSopenharmony_ci`EXPECT_HRESULT_SUCCEEDED(`*`expression`*`)` \ 511f92157deSopenharmony_ci`ASSERT_HRESULT_SUCCEEDED(`*`expression`*`)` 512f92157deSopenharmony_ci 513f92157deSopenharmony_ciVerifies that *`expression`* is a success `HRESULT`. 514f92157deSopenharmony_ci 515f92157deSopenharmony_ci### EXPECT_HRESULT_FAILED {#EXPECT_HRESULT_FAILED} 516f92157deSopenharmony_ci 517f92157deSopenharmony_ci`EXPECT_HRESULT_FAILED(`*`expression`*`)` \ 518f92157deSopenharmony_ci`EXPECT_HRESULT_FAILED(`*`expression`*`)` 519f92157deSopenharmony_ci 520f92157deSopenharmony_ciVerifies that *`expression`* is a failure `HRESULT`. 521f92157deSopenharmony_ci 522f92157deSopenharmony_ci## Death Assertions {#death} 523f92157deSopenharmony_ci 524f92157deSopenharmony_ciThe following assertions verify that a piece of code causes the process to 525f92157deSopenharmony_citerminate. For context, see [Death Tests](../advanced.md#death-tests). 526f92157deSopenharmony_ci 527f92157deSopenharmony_ciThese assertions spawn a new process and execute the code under test in that 528f92157deSopenharmony_ciprocess. How that happens depends on the platform and the variable 529f92157deSopenharmony_ci`::testing::GTEST_FLAG(death_test_style)`, which is initialized from the 530f92157deSopenharmony_cicommand-line flag `--gtest_death_test_style`. 531f92157deSopenharmony_ci 532f92157deSopenharmony_ci* On POSIX systems, `fork()` (or `clone()` on Linux) is used to spawn the 533f92157deSopenharmony_ci child, after which: 534f92157deSopenharmony_ci * If the variable's value is `"fast"`, the death test statement is 535f92157deSopenharmony_ci immediately executed. 536f92157deSopenharmony_ci * If the variable's value is `"threadsafe"`, the child process re-executes 537f92157deSopenharmony_ci the unit test binary just as it was originally invoked, but with some 538f92157deSopenharmony_ci extra flags to cause just the single death test under consideration to 539f92157deSopenharmony_ci be run. 540f92157deSopenharmony_ci* On Windows, the child is spawned using the `CreateProcess()` API, and 541f92157deSopenharmony_ci re-executes the binary to cause just the single death test under 542f92157deSopenharmony_ci consideration to be run - much like the `"threadsafe"` mode on POSIX. 543f92157deSopenharmony_ci 544f92157deSopenharmony_ciOther values for the variable are illegal and will cause the death test to fail. 545f92157deSopenharmony_ciCurrently, the flag's default value is 546f92157deSopenharmony_ci**`"fast"`**. 547f92157deSopenharmony_ci 548f92157deSopenharmony_ciIf the death test statement runs to completion without dying, the child process 549f92157deSopenharmony_ciwill nonetheless terminate, and the assertion fails. 550f92157deSopenharmony_ci 551f92157deSopenharmony_ciNote that the piece of code under test can be a compound statement, for example: 552f92157deSopenharmony_ci 553f92157deSopenharmony_ci```cpp 554f92157deSopenharmony_ciEXPECT_DEATH({ 555f92157deSopenharmony_ci int n = 5; 556f92157deSopenharmony_ci DoSomething(&n); 557f92157deSopenharmony_ci}, "Error on line .* of DoSomething()"); 558f92157deSopenharmony_ci``` 559f92157deSopenharmony_ci 560f92157deSopenharmony_ci### EXPECT_DEATH {#EXPECT_DEATH} 561f92157deSopenharmony_ci 562f92157deSopenharmony_ci`EXPECT_DEATH(`*`statement`*`,`*`matcher`*`)` \ 563f92157deSopenharmony_ci`ASSERT_DEATH(`*`statement`*`,`*`matcher`*`)` 564f92157deSopenharmony_ci 565f92157deSopenharmony_ciVerifies that *`statement`* causes the process to terminate with a nonzero exit 566f92157deSopenharmony_cistatus and produces `stderr` output that matches *`matcher`*. 567f92157deSopenharmony_ci 568f92157deSopenharmony_ciThe parameter *`matcher`* is either a [matcher](matchers.md) for a `const 569f92157deSopenharmony_cistd::string&`, or a regular expression (see 570f92157deSopenharmony_ci[Regular Expression Syntax](../advanced.md#regular-expression-syntax))—a bare 571f92157deSopenharmony_cistring *`s`* (with no matcher) is treated as 572f92157deSopenharmony_ci[`ContainsRegex(s)`](matchers.md#string-matchers), **not** 573f92157deSopenharmony_ci[`Eq(s)`](matchers.md#generic-comparison). 574f92157deSopenharmony_ci 575f92157deSopenharmony_ciFor example, the following code verifies that calling `DoSomething(42)` causes 576f92157deSopenharmony_cithe process to die with an error message that contains the text `My error`: 577f92157deSopenharmony_ci 578f92157deSopenharmony_ci```cpp 579f92157deSopenharmony_ciEXPECT_DEATH(DoSomething(42), "My error"); 580f92157deSopenharmony_ci``` 581f92157deSopenharmony_ci 582f92157deSopenharmony_ci### EXPECT_DEATH_IF_SUPPORTED {#EXPECT_DEATH_IF_SUPPORTED} 583f92157deSopenharmony_ci 584f92157deSopenharmony_ci`EXPECT_DEATH_IF_SUPPORTED(`*`statement`*`,`*`matcher`*`)` \ 585f92157deSopenharmony_ci`ASSERT_DEATH_IF_SUPPORTED(`*`statement`*`,`*`matcher`*`)` 586f92157deSopenharmony_ci 587f92157deSopenharmony_ciIf death tests are supported, behaves the same as 588f92157deSopenharmony_ci[`EXPECT_DEATH`](#EXPECT_DEATH). Otherwise, verifies nothing. 589f92157deSopenharmony_ci 590f92157deSopenharmony_ci### EXPECT_DEBUG_DEATH {#EXPECT_DEBUG_DEATH} 591f92157deSopenharmony_ci 592f92157deSopenharmony_ci`EXPECT_DEBUG_DEATH(`*`statement`*`,`*`matcher`*`)` \ 593f92157deSopenharmony_ci`ASSERT_DEBUG_DEATH(`*`statement`*`,`*`matcher`*`)` 594f92157deSopenharmony_ci 595f92157deSopenharmony_ciIn debug mode, behaves the same as [`EXPECT_DEATH`](#EXPECT_DEATH). When not in 596f92157deSopenharmony_cidebug mode (i.e. `NDEBUG` is defined), just executes *`statement`*. 597f92157deSopenharmony_ci 598f92157deSopenharmony_ci### EXPECT_EXIT {#EXPECT_EXIT} 599f92157deSopenharmony_ci 600f92157deSopenharmony_ci`EXPECT_EXIT(`*`statement`*`,`*`predicate`*`,`*`matcher`*`)` \ 601f92157deSopenharmony_ci`ASSERT_EXIT(`*`statement`*`,`*`predicate`*`,`*`matcher`*`)` 602f92157deSopenharmony_ci 603f92157deSopenharmony_ciVerifies that *`statement`* causes the process to terminate with an exit status 604f92157deSopenharmony_cithat satisfies *`predicate`*, and produces `stderr` output that matches 605f92157deSopenharmony_ci*`matcher`*. 606f92157deSopenharmony_ci 607f92157deSopenharmony_ciThe parameter *`predicate`* is a function or functor that accepts an `int` exit 608f92157deSopenharmony_cistatus and returns a `bool`. GoogleTest provides two predicates to handle common 609f92157deSopenharmony_cicases: 610f92157deSopenharmony_ci 611f92157deSopenharmony_ci```cpp 612f92157deSopenharmony_ci// Returns true if the program exited normally with the given exit status code. 613f92157deSopenharmony_ci::testing::ExitedWithCode(exit_code); 614f92157deSopenharmony_ci 615f92157deSopenharmony_ci// Returns true if the program was killed by the given signal. 616f92157deSopenharmony_ci// Not available on Windows. 617f92157deSopenharmony_ci::testing::KilledBySignal(signal_number); 618f92157deSopenharmony_ci``` 619f92157deSopenharmony_ci 620f92157deSopenharmony_ciThe parameter *`matcher`* is either a [matcher](matchers.md) for a `const 621f92157deSopenharmony_cistd::string&`, or a regular expression (see 622f92157deSopenharmony_ci[Regular Expression Syntax](../advanced.md#regular-expression-syntax))—a bare 623f92157deSopenharmony_cistring *`s`* (with no matcher) is treated as 624f92157deSopenharmony_ci[`ContainsRegex(s)`](matchers.md#string-matchers), **not** 625f92157deSopenharmony_ci[`Eq(s)`](matchers.md#generic-comparison). 626f92157deSopenharmony_ci 627f92157deSopenharmony_ciFor example, the following code verifies that calling `NormalExit()` causes the 628f92157deSopenharmony_ciprocess to print a message containing the text `Success` to `stderr` and exit 629f92157deSopenharmony_ciwith exit status code 0: 630f92157deSopenharmony_ci 631f92157deSopenharmony_ci```cpp 632f92157deSopenharmony_ciEXPECT_EXIT(NormalExit(), testing::ExitedWithCode(0), "Success"); 633f92157deSopenharmony_ci``` 634