| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "Shape.hpp" | ||
| 2 | #include "Verbose.hpp" | ||
| 3 | #include "compat/gsl14.hpp" | ||
| 4 | #include <functional> | ||
| 5 | #include <gmock/gmock.h> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | using namespace testing; | ||
| 9 | using namespace testing::internal; | ||
| 10 | |||
| 11 | // Anonymous namespace for definitions that are local to this file. | ||
| 12 | namespace { | ||
| 13 | template <typename T> | ||
| 14 | using RefContainer = std::vector<std::reference_wrapper<T>>; | ||
| 15 | |||
| 16 | 6 | bool less(const Verbose<>& left, const Verbose<>& right) { | |
| 17 | 6 | return left.name() < right.name(); | |
| 18 | } | ||
| 19 | |||
| 20 | 30 | const std::string& name(const Verbose<>& v) { return v.name(); } | |
| 21 | |||
| 22 | // Print the elements of a container of smart references. | ||
| 23 | template <typename T> | ||
| 24 | 3 | void print( | |
| 25 | const RefContainer<Verbose<T>>& container, | ||
| 26 | const std::string& prefix = "print: ") { | ||
| 27 |
2/2✓ Branch 9 taken 9 times.
✓ Branch 10 taken 3 times.
|
12 | for (const auto& elem : container) { |
| 28 |
3/6✓ Branch 3 taken 9 times.
✗ Branch 4 not taken.
✓ Branch 11 taken 9 times.
✗ Branch 12 not taken.
✓ Branch 17 taken 9 times.
✗ Branch 18 not taken.
|
9 | std::cout << prefix << name(elem) << std::endl; |
| 29 |
5/16✓ Branch 5 taken 9 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 9 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 9 times.
✗ Branch 12 not taken.
✓ Branch 19 taken 9 times.
✗ Branch 20 not taken.
✗ Branch 28 not taken.
✓ Branch 29 taken 9 times.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✗ Branch 38 not taken.
✗ Branch 39 not taken.
✗ Branch 42 not taken.
✗ Branch 43 not taken.
|
9 | EXPECT_THAT(name(elem), Not(HasSubstr("copy"))); |
| 30 |
5/16✓ Branch 5 taken 9 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 9 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 9 times.
✗ Branch 12 not taken.
✓ Branch 19 taken 9 times.
✗ Branch 20 not taken.
✗ Branch 28 not taken.
✓ Branch 29 taken 9 times.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✗ Branch 38 not taken.
✗ Branch 39 not taken.
✗ Branch 42 not taken.
✗ Branch 43 not taken.
|
9 | EXPECT_THAT(name(elem), Not(HasSubstr("move"))); |
| 31 | } | ||
| 32 | 3 | } | |
| 33 | } // anonymous namespace | ||
| 34 | |||
| 35 | 8 | TEST(RawRef, container) { | |
| 36 | 2 | auto container = std::vector<Verbose<>>{}; | |
| 37 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | container.reserve(3); // avoid reallocation |
| 38 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | container.emplace_back("one"); |
| 39 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | container.emplace_back("two"); |
| 40 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | container.emplace_back("three"); |
| 41 | |||
| 42 | 2 | auto refContainer = | |
| 43 |
1/2✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
|
6 | RefContainer<Verbose<>>(container.begin(), container.end()); |
| 44 | |||
| 45 |
2/4✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
|
6 | print(refContainer, "ref: "); |
| 46 |
2/10✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✗ Branch 11 not taken.
✓ Branch 12 taken 1 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
|
2 | EXPECT_EQ("two", name(refContainer[1])); |
| 47 | |||
| 48 | // Copying a container of references | ||
| 49 | // does not copy or move the referenced objects. | ||
| 50 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | auto container_copy = refContainer; |
| 51 |
2/4✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
|
6 | print(container_copy, "copy: "); |
| 52 | |||
| 53 | // Sorting a container of references | ||
| 54 | // does not affect the original container. | ||
| 55 | #if __cplusplus >= 202002L // since C++20 | ||
| 56 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | std::ranges::sort(refContainer, less); |
| 57 | #else // until C++20 | ||
| 58 | std::sort(refContainer.begin(), refContainer.end(), less); | ||
| 59 | #endif // C++20 | ||
| 60 | |||
| 61 |
2/4✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
|
6 | print(refContainer, "sorted: "); |
| 62 |
2/10✓ Branch 12 taken 1 times.
✗ Branch 13 not taken.
✗ Branch 15 not taken.
✓ Branch 16 taken 1 times.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
|
2 | EXPECT_EQ(name(container[2]), name(refContainer[1])); |
| 63 | 4 | } | |
| 64 |