Update googletest to 1.18.0 and SIMDe to 0.8.2.
Googletest 1.18 asks for CMake 3.13 or newer, so configuring the tests no longer warns that CMake compatibility below 3.10 is deprecated. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
e4e666f459
commit
3f10e05176
912 changed files with 224964 additions and 11502 deletions
|
|
@ -3,7 +3,7 @@
|
|||
<!--* toc_depth: 3 *-->
|
||||
|
||||
This page lists the facilities provided by GoogleTest for writing test programs.
|
||||
To use them, include the header `gtest/gtest.h`.
|
||||
To use them, add `#include <gtest/gtest.h>`.
|
||||
|
||||
## Macros
|
||||
|
||||
|
|
@ -94,7 +94,8 @@ Instantiates the value-parameterized test suite *`TestSuiteName`* (defined with
|
|||
The argument *`InstantiationName`* is a unique name for the instantiation of the
|
||||
test suite, to distinguish between multiple instantiations. In test output, the
|
||||
instantiation name is added as a prefix to the test suite name
|
||||
*`TestSuiteName`*.
|
||||
*`TestSuiteName`*. If *`InstantiationName`* is empty
|
||||
(`INSTANTIATE_TEST_SUITE_P(, ...)`), no prefix is added.
|
||||
|
||||
The argument *`param_generator`* is one of the following GoogleTest-provided
|
||||
functions that generate the test parameters, all defined in the `::testing`
|
||||
|
|
@ -109,7 +110,8 @@ namespace:
|
|||
| `ValuesIn(container)` or `ValuesIn(begin,end)` | Yields values from a C-style array, an STL-style container, or an iterator range `[begin, end)`. |
|
||||
| `Bool()` | Yields sequence `{false, true}`. |
|
||||
| `Combine(g1, g2, ..., gN)` | Yields as `std::tuple` *n*-tuples all combinations (Cartesian product) of the values generated by the given *n* generators `g1`, `g2`, ..., `gN`. |
|
||||
| `ConvertGenerator<T>(g)` | Yields values generated by generator `g`, `static_cast` to `T`. |
|
||||
| `ConvertGenerator<T>(g)` or `ConvertGenerator(g, func)` | Yields values generated by generator `g`, `static_cast` from `T`. (Note: `T` might not be what you expect. See [*Using ConvertGenerator*](#using-convertgenerator) below.) The second overload uses `func` to perform the conversion. |
|
||||
|
||||
The optional last argument *`name_generator`* is a function or functor that
|
||||
generates custom test name suffixes based on the test parameters. The function
|
||||
must accept an argument of type
|
||||
|
|
@ -121,8 +123,8 @@ custom function can be used for more control:
|
|||
```cpp
|
||||
INSTANTIATE_TEST_SUITE_P(
|
||||
MyInstantiation, MyTestSuite,
|
||||
::testing::Values(...),
|
||||
[](const ::testing::TestParamInfo<MyTestSuite::ParamType>& info) {
|
||||
testing::Values(...),
|
||||
[](const testing::TestParamInfo<MyTestSuite::ParamType>& info) {
|
||||
// Can use info.param here to generate the test suffix
|
||||
std::string name = ...
|
||||
return name;
|
||||
|
|
@ -135,9 +137,107 @@ For more information, see
|
|||
See also
|
||||
[`GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST`](#GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST).
|
||||
|
||||
###### Using `ConvertGenerator`
|
||||
|
||||
The functions listed in the table above appear to return generators that create
|
||||
values of the desired types, but this is not generally the case. Rather, they
|
||||
typically return factory objects that convert to the the desired generators.
|
||||
This affords some flexibility in allowing you to specify values of types that
|
||||
are different from, yet implicitly convertible to, the actual parameter type
|
||||
required by your fixture class.
|
||||
|
||||
For example, you can do the following with a fixture that requires an `int`
|
||||
parameter:
|
||||
|
||||
```cpp
|
||||
INSTANTIATE_TEST_SUITE_P(MyInstantiation, MyTestSuite,
|
||||
testing::Values(1, 1.2)); // Yes, Values() supports heterogeneous argument types.
|
||||
```
|
||||
|
||||
It might seem obvious that `1.2` — a `double` — will be converted to
|
||||
an `int` but in actuality it requires some template gymnastics involving the
|
||||
indirection described in the previous paragraph.
|
||||
|
||||
What if your parameter type is not implicitly convertible from the generated
|
||||
type but is *explicitly* convertible? There will be no automatic conversion, but
|
||||
you can force it by applying `ConvertGenerator<T>`. The compiler can
|
||||
automatically deduce the target type (your fixture's parameter type), but
|
||||
because of the aforementioned indirection it cannot decide what the generated
|
||||
type should be. You need to tell it, by providing the type `T` explicitly. Thus
|
||||
`T` should not be your fixture's parameter type, but rather an intermediate type
|
||||
that is supported by the factory object, and which can be `static_cast` to the
|
||||
fixture's parameter type:
|
||||
|
||||
```cpp
|
||||
// The fixture's parameter type.
|
||||
class MyParam {
|
||||
public:
|
||||
// Explicit converting ctor.
|
||||
explicit MyParam(const std::tuple<int, bool>& t);
|
||||
...
|
||||
};
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(MyInstantiation, MyTestSuite,
|
||||
ConvertGenerator<std::tuple<int, bool>>(Combine(Values(0.1, 1.2), Bool())));
|
||||
```
|
||||
|
||||
In this example `Combine` supports the generation of `std::tuple<int, bool>>`
|
||||
objects (even though the provided values for the first tuple element are
|
||||
`double`s) and those `tuple`s get converted into `MyParam` objects by virtue of
|
||||
the call to `ConvertGenerator`.
|
||||
|
||||
For parameter types that are not convertible from the generated types you can
|
||||
provide a callable that does the conversion. The callable accepts an object of
|
||||
the generated type and returns an object of the fixture's parameter type. The
|
||||
generated type can often be deduced by the compiler from the callable's call
|
||||
signature so you do not usually need specify it explicitly (but see a caveat
|
||||
below).
|
||||
|
||||
```cpp
|
||||
// The fixture's parameter type.
|
||||
class MyParam {
|
||||
public:
|
||||
MyParam(int, bool);
|
||||
...
|
||||
};
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(MyInstantiation, MyTestSuite,
|
||||
ConvertGenerator(Combine(Values(1, 1.2), Bool()),
|
||||
[](const std::tuple<int, bool>& t){
|
||||
const auto [i, b] = t;
|
||||
return MyParam(i, b);
|
||||
}));
|
||||
```
|
||||
|
||||
The callable may be anything that can be used to initialize a `std::function`
|
||||
with the appropriate call signature. Note the callable's return object gets
|
||||
`static_cast` to the fixture's parameter type, so it does not have to be of that
|
||||
exact type, only convertible to it.
|
||||
|
||||
**Caveat:** Consider the following example.
|
||||
|
||||
```cpp
|
||||
INSTANTIATE_TEST_SUITE_P(MyInstantiation, MyTestSuite,
|
||||
ConvertGenerator(Values(std::string("s")), [](std::string_view s) { ... }));
|
||||
```
|
||||
|
||||
The `string` argument gets copied into the factory object returned by `Values`.
|
||||
Then, because the generated type deduced from the lambda is `string_view`, the
|
||||
factory object spawns a generator that holds a `string_view` referencing that
|
||||
`string`. Unfortunately, by the time this generator gets invoked, the factory
|
||||
object is gone and the `string_view` is dangling.
|
||||
|
||||
To overcome this problem you can specify the generated type explicitly:
|
||||
`ConvertGenerator<std::string>(Values(std::string("s")), [](std::string_view s)
|
||||
{ ... })`. Alternatively, you can change the lambda's signature to take a
|
||||
`std::string` or a `const std::string&` (the latter will not leave you with a
|
||||
dangling reference because the type deduction strips off the reference and the
|
||||
`const`).
|
||||
|
||||
### TYPED_TEST_SUITE {#TYPED_TEST_SUITE}
|
||||
|
||||
`TYPED_TEST_SUITE(`*`TestFixtureName`*`,`*`Types`*`)`
|
||||
`TYPED_TEST_SUITE(`*`TestFixtureName`*`,`*`Types`*`,`*`NameGenerator`*`)`
|
||||
|
||||
Defines a typed test suite based on the test fixture *`TestFixtureName`*. The
|
||||
test suite name is *`TestFixtureName`*.
|
||||
|
|
@ -147,7 +247,7 @@ type, for example:
|
|||
|
||||
```cpp
|
||||
template <typename T>
|
||||
class MyFixture : public ::testing::Test {
|
||||
class MyFixture : public testing::Test {
|
||||
public:
|
||||
...
|
||||
using List = std::list<T>;
|
||||
|
|
@ -167,6 +267,22 @@ TYPED_TEST_SUITE(MyFixture, MyTypes);
|
|||
The type alias (`using` or `typedef`) is necessary for the `TYPED_TEST_SUITE`
|
||||
macro to parse correctly.
|
||||
|
||||
The optional third argument *`NameGenerator`* allows specifying a class that
|
||||
exposes a templated static function `GetName(int)`. For example:
|
||||
|
||||
```cpp
|
||||
class NameGenerator {
|
||||
public:
|
||||
template <typename T>
|
||||
static std::string GetName(int) {
|
||||
if constexpr (std::is_same_v<T, char>) return "char";
|
||||
if constexpr (std::is_same_v<T, int>) return "int";
|
||||
if constexpr (std::is_same_v<T, unsigned int>) return "unsignedInt";
|
||||
}
|
||||
};
|
||||
TYPED_TEST_SUITE(MyFixture, MyTypes, NameGenerator);
|
||||
```
|
||||
|
||||
See also [`TYPED_TEST`](#TYPED_TEST) and
|
||||
[Typed Tests](../advanced.md#typed-tests) for more information.
|
||||
|
||||
|
|
@ -276,7 +392,8 @@ must be registered with
|
|||
The argument *`InstantiationName`* is a unique name for the instantiation of the
|
||||
test suite, to distinguish between multiple instantiations. In test output, the
|
||||
instantiation name is added as a prefix to the test suite name
|
||||
*`TestSuiteName`*.
|
||||
*`TestSuiteName`*. If *`InstantiationName`* is empty
|
||||
(`INSTANTIATE_TYPED_TEST_SUITE_P(, ...)`), no prefix is added.
|
||||
|
||||
The argument *`Types`* is a [`Types`](#Types) object representing the list of
|
||||
types to run the tests on, for example:
|
||||
|
|
@ -323,7 +440,7 @@ Then the test code should look like:
|
|||
```cpp
|
||||
namespace my_namespace {
|
||||
|
||||
class MyClassTest : public ::testing::Test {
|
||||
class MyClassTest : public testing::Test {
|
||||
...
|
||||
};
|
||||
|
||||
|
|
@ -386,7 +503,7 @@ GoogleTest defines the following classes and types to help with writing tests.
|
|||
|
||||
### AssertionResult {#AssertionResult}
|
||||
|
||||
`::testing::AssertionResult`
|
||||
`testing::AssertionResult`
|
||||
|
||||
A class for indicating whether an assertion was successful.
|
||||
|
||||
|
|
@ -400,14 +517,14 @@ To create an instance of this class, use one of the factory functions
|
|||
|
||||
### AssertionException {#AssertionException}
|
||||
|
||||
`::testing::AssertionException`
|
||||
`testing::AssertionException`
|
||||
|
||||
Exception which can be thrown from
|
||||
[`TestEventListener::OnTestPartResult`](#TestEventListener::OnTestPartResult).
|
||||
|
||||
### EmptyTestEventListener {#EmptyTestEventListener}
|
||||
|
||||
`::testing::EmptyTestEventListener`
|
||||
`testing::EmptyTestEventListener`
|
||||
|
||||
Provides an empty implementation of all methods in the
|
||||
[`TestEventListener`](#TestEventListener) interface, such that a subclass only
|
||||
|
|
@ -415,7 +532,7 @@ needs to override the methods it cares about.
|
|||
|
||||
### Environment {#Environment}
|
||||
|
||||
`::testing::Environment`
|
||||
`testing::Environment`
|
||||
|
||||
Represents a global test environment. See
|
||||
[Global Set-Up and Tear-Down](../advanced.md#global-set-up-and-tear-down).
|
||||
|
|
@ -436,7 +553,7 @@ Override this to define how to tear down the environment.
|
|||
|
||||
### ScopedTrace {#ScopedTrace}
|
||||
|
||||
`::testing::ScopedTrace`
|
||||
`testing::ScopedTrace`
|
||||
|
||||
An instance of this class causes a trace to be included in every test failure
|
||||
message generated by code in the scope of the lifetime of the `ScopedTrace`
|
||||
|
|
@ -452,7 +569,7 @@ ScopedTrace(const char* file, int line, const T& message)
|
|||
Example usage:
|
||||
|
||||
```cpp
|
||||
::testing::ScopedTrace trace("file.cc", 123, "message");
|
||||
testing::ScopedTrace trace("file.cc", 123, "message");
|
||||
```
|
||||
|
||||
The resulting trace includes the given source file path and line number, and the
|
||||
|
|
@ -463,7 +580,7 @@ See also [`SCOPED_TRACE`](#SCOPED_TRACE).
|
|||
|
||||
### Test {#Test}
|
||||
|
||||
`::testing::Test`
|
||||
`testing::Test`
|
||||
|
||||
The abstract class that all tests inherit from. `Test` is not copyable.
|
||||
|
||||
|
|
@ -551,7 +668,7 @@ after running each individual test.
|
|||
|
||||
### TestWithParam {#TestWithParam}
|
||||
|
||||
`::testing::TestWithParam<T>`
|
||||
`testing::TestWithParam<T>`
|
||||
|
||||
A convenience class which inherits from both [`Test`](#Test) and
|
||||
[`WithParamInterface<T>`](#WithParamInterface).
|
||||
|
|
@ -671,7 +788,7 @@ during execution of `SetUpTestSuite` and `TearDownTestSuite`.
|
|||
|
||||
### TestInfo {#TestInfo}
|
||||
|
||||
`::testing::TestInfo`
|
||||
`testing::TestInfo`
|
||||
|
||||
Stores information about a test.
|
||||
|
||||
|
|
@ -750,7 +867,7 @@ Returns the result of the test. See [`TestResult`](#TestResult).
|
|||
|
||||
### TestParamInfo {#TestParamInfo}
|
||||
|
||||
`::testing::TestParamInfo<T>`
|
||||
`testing::TestParamInfo<T>`
|
||||
|
||||
Describes a parameter to a value-parameterized test. The type `T` is the type of
|
||||
the parameter.
|
||||
|
|
@ -760,7 +877,7 @@ and its integer index respectively.
|
|||
|
||||
### UnitTest {#UnitTest}
|
||||
|
||||
`::testing::UnitTest`
|
||||
`testing::UnitTest`
|
||||
|
||||
This class contains information about the test program.
|
||||
|
||||
|
|
@ -928,7 +1045,7 @@ GoogleTest. See [`TestEventListeners`](#TestEventListeners).
|
|||
|
||||
### TestEventListener {#TestEventListener}
|
||||
|
||||
`::testing::TestEventListener`
|
||||
`testing::TestEventListener`
|
||||
|
||||
The interface for tracing execution of tests. The methods below are listed in
|
||||
the order the corresponding events are fired.
|
||||
|
|
@ -1026,7 +1143,7 @@ Fired after all test activities have ended.
|
|||
|
||||
### TestEventListeners {#TestEventListeners}
|
||||
|
||||
`::testing::TestEventListeners`
|
||||
`testing::TestEventListeners`
|
||||
|
||||
Lets users add listeners to track events in GoogleTest.
|
||||
|
||||
|
|
@ -1071,7 +1188,7 @@ the caller and makes this function return `NULL` the next time.
|
|||
|
||||
### TestPartResult {#TestPartResult}
|
||||
|
||||
`::testing::TestPartResult`
|
||||
`testing::TestPartResult`
|
||||
|
||||
A copyable object representing the result of a test part (i.e. an assertion or
|
||||
an explicit `FAIL()`, `ADD_FAILURE()`, or `SUCCESS()`).
|
||||
|
|
@ -1153,7 +1270,7 @@ Returns true if and only if the test part failed.
|
|||
|
||||
### TestProperty {#TestProperty}
|
||||
|
||||
`::testing::TestProperty`
|
||||
`testing::TestProperty`
|
||||
|
||||
A copyable object representing a user-specified test property which can be
|
||||
output as a key/value string pair.
|
||||
|
|
@ -1180,7 +1297,7 @@ Sets a new value, overriding the previous one.
|
|||
|
||||
### TestResult {#TestResult}
|
||||
|
||||
`::testing::TestResult`
|
||||
`testing::TestResult`
|
||||
|
||||
Contains information about the result of a single test.
|
||||
|
||||
|
|
@ -1261,20 +1378,20 @@ range, aborts the program.
|
|||
|
||||
### TimeInMillis {#TimeInMillis}
|
||||
|
||||
`::testing::TimeInMillis`
|
||||
`testing::TimeInMillis`
|
||||
|
||||
An integer type representing time in milliseconds.
|
||||
|
||||
### Types {#Types}
|
||||
|
||||
`::testing::Types<T...>`
|
||||
`testing::Types<T...>`
|
||||
|
||||
Represents a list of types for use in typed tests and type-parameterized tests.
|
||||
|
||||
The template argument `T...` can be any number of types, for example:
|
||||
|
||||
```
|
||||
::testing::Types<char, int, unsigned int>
|
||||
testing::Types<char, int, unsigned int>
|
||||
```
|
||||
|
||||
See [Typed Tests](../advanced.md#typed-tests) and
|
||||
|
|
@ -1283,7 +1400,7 @@ information.
|
|||
|
||||
### WithParamInterface {#WithParamInterface}
|
||||
|
||||
`::testing::WithParamInterface<T>`
|
||||
`testing::WithParamInterface<T>`
|
||||
|
||||
The pure interface class that all value-parameterized tests inherit from.
|
||||
|
||||
|
|
@ -1309,14 +1426,16 @@ tests.
|
|||
|
||||
### InitGoogleTest {#InitGoogleTest}
|
||||
|
||||
`void ::testing::InitGoogleTest(int* argc, char** argv)` \
|
||||
`void ::testing::InitGoogleTest(int* argc, wchar_t** argv)` \
|
||||
`void ::testing::InitGoogleTest()`
|
||||
`void testing::InitGoogleTest(int* argc, char** argv)` \
|
||||
`void testing::InitGoogleTest(int* argc, wchar_t** argv)` \
|
||||
`void testing::InitGoogleTest()`
|
||||
|
||||
Initializes GoogleTest. This must be called before calling
|
||||
[`RUN_ALL_TESTS()`](#RUN_ALL_TESTS). In particular, it parses the command line
|
||||
for the flags that GoogleTest recognizes. Whenever a GoogleTest flag is seen, it
|
||||
is removed from `argv`, and `*argc` is decremented.
|
||||
is removed from `argv`, and `*argc` is decremented. Keep in mind that `argv`
|
||||
must terminate with a `NULL` pointer (i.e. `argv[argc]` is `NULL`), which is
|
||||
already the case with the default `argv` passed to `main`.
|
||||
|
||||
No value is returned. Instead, the GoogleTest flag variables are updated.
|
||||
|
||||
|
|
@ -1328,7 +1447,7 @@ platforms where there is no `argc`/`argv`.
|
|||
|
||||
### AddGlobalTestEnvironment {#AddGlobalTestEnvironment}
|
||||
|
||||
`Environment* ::testing::AddGlobalTestEnvironment(Environment* env)`
|
||||
`Environment* testing::AddGlobalTestEnvironment(Environment* env)`
|
||||
|
||||
Adds a test environment to the test program. Must be called before
|
||||
[`RUN_ALL_TESTS()`](#RUN_ALL_TESTS) is called. See
|
||||
|
|
@ -1341,7 +1460,7 @@ See also [`Environment`](#Environment).
|
|||
|
||||
```cpp
|
||||
template <typename Factory>
|
||||
TestInfo* ::testing::RegisterTest(const char* test_suite_name, const char* test_name,
|
||||
TestInfo* testing::RegisterTest(const char* test_suite_name, const char* test_name,
|
||||
const char* type_param, const char* value_param,
|
||||
const char* file, int line, Factory factory)
|
||||
```
|
||||
|
|
@ -1380,27 +1499,27 @@ an all-caps name.
|
|||
|
||||
### AssertionSuccess {#AssertionSuccess}
|
||||
|
||||
`AssertionResult ::testing::AssertionSuccess()`
|
||||
`AssertionResult testing::AssertionSuccess()`
|
||||
|
||||
Creates a successful assertion result. See
|
||||
[`AssertionResult`](#AssertionResult).
|
||||
|
||||
### AssertionFailure {#AssertionFailure}
|
||||
|
||||
`AssertionResult ::testing::AssertionFailure()`
|
||||
`AssertionResult testing::AssertionFailure()`
|
||||
|
||||
Creates a failed assertion result. Use the `<<` operator to store a failure
|
||||
message:
|
||||
|
||||
```cpp
|
||||
::testing::AssertionFailure() << "My failure message";
|
||||
testing::AssertionFailure() << "My failure message";
|
||||
```
|
||||
|
||||
See [`AssertionResult`](#AssertionResult).
|
||||
|
||||
### StaticAssertTypeEq {#StaticAssertTypeEq}
|
||||
|
||||
`::testing::StaticAssertTypeEq<T1, T2>()`
|
||||
`testing::StaticAssertTypeEq<T1, T2>()`
|
||||
|
||||
Compile-time assertion for type equality. Compiles if and only if `T1` and `T2`
|
||||
are the same type. The value it returns is irrelevant.
|
||||
|
|
@ -1409,7 +1528,7 @@ See [Type Assertions](../advanced.md#type-assertions) for more information.
|
|||
|
||||
### PrintToString {#PrintToString}
|
||||
|
||||
`std::string ::testing::PrintToString(x)`
|
||||
`std::string testing::PrintToString(x)`
|
||||
|
||||
Prints any value `x` using GoogleTest's value printer.
|
||||
|
||||
|
|
@ -1419,7 +1538,7 @@ for more information.
|
|||
|
||||
### PrintToStringParamName {#PrintToStringParamName}
|
||||
|
||||
`std::string ::testing::PrintToStringParamName(TestParamInfo<T>& info)`
|
||||
`std::string testing::PrintToStringParamName(TestParamInfo<T>& info)`
|
||||
|
||||
A built-in parameterized test name generator which returns the result of
|
||||
[`PrintToString`](#PrintToString) called on `info.param`. Does not work when the
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue