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
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/performance_manager/graph/node_inline_data.h"
#include <string>
#include "base/test/gtest_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace performance_manager {
template <class T>
struct DataWithDefaultCtor : public NodeInlineData<DataWithDefaultCtor<T>> {
T value = {};
};
template <class T>
struct DataWithExplicitCtor : public NodeInlineData<DataWithExplicitCtor<T>> {
explicit DataWithExplicitCtor(T value) : value(std::move(value)) {}
T value;
};
template <class T>
struct SparseDataWithDefaultCtor
: public SparseNodeInlineData<SparseDataWithDefaultCtor<T>> {
T value = {};
};
template <class T>
struct SparseDataWithExplicitCtor
: public SparseNodeInlineData<SparseDataWithExplicitCtor<T>> {
explicit SparseDataWithExplicitCtor(T value) : value(std::move(value)) {}
T value;
};
// Tests internal::IsSparseNodeInlineData.
static_assert(!internal::IsSparseNodeInlineData<DataWithDefaultCtor<int>>);
static_assert(internal::IsSparseNodeInlineData<SparseDataWithDefaultCtor<int>>);
template <class... Args>
class DummyTestSupportsNodeInlineData : public SupportsNodeInlineData<Args...> {
public:
// Expose DestroyNodeInlineDataStorage().
using SupportsNodeInlineData<Args...>::DestroyNodeInlineDataStorage;
};
TEST(NodeInlineDataTest, MustCreateStorage) {
using TestType = DataWithDefaultCtor<int>;
DummyTestSupportsNodeInlineData<TestType> test_supports_node_data;
EXPECT_FALSE(TestType::Exists(&test_supports_node_data));
TestType::Create(&test_supports_node_data); // Does not check.
EXPECT_TRUE(TestType::Exists(&test_supports_node_data));
test_supports_node_data.DestroyNodeInlineDataStorage();
EXPECT_FALSE(TestType::Exists(&test_supports_node_data));
}
template <class... Args>
class TestSupportsNodeInlineData : public SupportsNodeInlineData<Args...> {
public:
TestSupportsNodeInlineData() = default;
~TestSupportsNodeInlineData() override {
SupportsNodeInlineData<Args...>::DestroyNodeInlineDataStorage();
}
};
const int kTestIntValue1 = 42;
const int kTestIntValue2 = 2024;
const double kTestDoubleValue1 = 91.0;
const double kTestDoubleValue2 = 1024.0;
const char* kTestStringValue1 = "non-default value";
const char* kTestStringValue2 = "other non-default value";
TEST(NodeInlineDataDeathTest, NonSparseDefaultCtor) {
using TestType = DataWithDefaultCtor<std::string>;
TestSupportsNodeInlineData<TestType> test_supports_node_data;
EXPECT_FALSE(TestType::Exists(&test_supports_node_data));
EXPECT_CHECK_DEATH({ TestType::Get(&test_supports_node_data); });
TestType& test_data = TestType::Create(&test_supports_node_data);
EXPECT_TRUE(test_data.value.empty());
test_data.value = kTestStringValue1;
EXPECT_EQ(TestType::Get(&test_supports_node_data).value, kTestStringValue1);
TestType::Destroy(&test_supports_node_data);
EXPECT_FALSE(TestType::Exists(&test_supports_node_data));
}
TEST(NodeInlineDataDeathTest, NonSparseExplicitCtor) {
using TestType = DataWithExplicitCtor<std::string>;
TestSupportsNodeInlineData<TestType> test_supports_node_data;
EXPECT_FALSE(TestType::Exists(&test_supports_node_data));
EXPECT_CHECK_DEATH({ TestType::Get(&test_supports_node_data); });
TestType& test_data =
TestType::Create(&test_supports_node_data, kTestStringValue1);
EXPECT_TRUE(TestType::Exists(&test_supports_node_data));
EXPECT_EQ(test_data.value, kTestStringValue1);
test_data.value.clear();
EXPECT_EQ(TestType::Get(&test_supports_node_data).value, "");
TestType::Destroy(&test_supports_node_data);
EXPECT_FALSE(TestType::Exists(&test_supports_node_data));
}
TEST(NodeInlineDataDeathTest, SparseTypeWithDefaultCtor) {
using TestType = SparseDataWithDefaultCtor<std::string>;
TestSupportsNodeInlineData<TestType> test_supports_node_data;
EXPECT_FALSE(TestType::Exists(&test_supports_node_data));
EXPECT_CHECK_DEATH({ TestType::Get(&test_supports_node_data); });
TestType& test_data = TestType::Create(&test_supports_node_data);
EXPECT_TRUE(TestType::Exists(&test_supports_node_data));
EXPECT_EQ(test_data.value, "");
test_data.value = kTestStringValue1;
EXPECT_EQ(TestType::Get(&test_supports_node_data).value, kTestStringValue1);
TestType::Destroy(&test_supports_node_data);
EXPECT_FALSE(TestType::Exists(&test_supports_node_data));
}
TEST(NodeInlineDataDeathTest, SparseTypeWithExplicitCtor) {
using TestType = SparseDataWithExplicitCtor<std::string>;
TestSupportsNodeInlineData<TestType> test_supports_node_data;
EXPECT_FALSE(TestType::Exists(&test_supports_node_data));
EXPECT_CHECK_DEATH({ TestType::Get(&test_supports_node_data); });
TestType& test_data =
TestType::Create(&test_supports_node_data, kTestStringValue1);
EXPECT_TRUE(TestType::Exists(&test_supports_node_data));
EXPECT_EQ(test_data.value, kTestStringValue1);
test_data.value.clear();
EXPECT_EQ(TestType::Get(&test_supports_node_data).value, "");
TestType::Destroy(&test_supports_node_data);
EXPECT_FALSE(TestType::Exists(&test_supports_node_data));
}
TEST(NodeInlineDataTest, MixMultipleTypesSameStorage) {
using TestTypeInt = DataWithExplicitCtor<int>;
using TestTypeDouble = DataWithExplicitCtor<double>;
using TestTypeString = DataWithExplicitCtor<std::string>;
TestSupportsNodeInlineData<TestTypeInt, TestTypeDouble, TestTypeString>
test_supports_node_data;
EXPECT_FALSE(TestTypeInt::Exists(&test_supports_node_data));
EXPECT_FALSE(TestTypeDouble::Exists(&test_supports_node_data));
EXPECT_FALSE(TestTypeString::Exists(&test_supports_node_data));
TestTypeInt& test_type_int =
TestTypeInt::Create(&test_supports_node_data, kTestIntValue1);
EXPECT_TRUE(TestTypeInt::Exists(&test_supports_node_data));
EXPECT_FALSE(TestTypeDouble::Exists(&test_supports_node_data));
EXPECT_FALSE(TestTypeString::Exists(&test_supports_node_data));
TestTypeDouble& test_type_double =
TestTypeDouble::Create(&test_supports_node_data, kTestDoubleValue1);
EXPECT_TRUE(TestTypeInt::Exists(&test_supports_node_data));
EXPECT_TRUE(TestTypeDouble::Exists(&test_supports_node_data));
EXPECT_FALSE(TestTypeString::Exists(&test_supports_node_data));
TestTypeString& test_type_string =
TestTypeString::Create(&test_supports_node_data, kTestStringValue1);
EXPECT_TRUE(TestTypeInt::Exists(&test_supports_node_data));
EXPECT_TRUE(TestTypeDouble::Exists(&test_supports_node_data));
EXPECT_TRUE(TestTypeString::Exists(&test_supports_node_data));
EXPECT_EQ(test_type_int.value, kTestIntValue1);
EXPECT_EQ(test_type_double.value, kTestDoubleValue1);
EXPECT_EQ(test_type_string.value, kTestStringValue1);
test_type_int.value = kTestIntValue2;
test_type_double.value = kTestDoubleValue2;
test_type_string.value = kTestStringValue2;
EXPECT_EQ(TestTypeInt::Get(&test_supports_node_data).value, kTestIntValue2);
EXPECT_EQ(TestTypeDouble::Get(&test_supports_node_data).value,
kTestDoubleValue2);
EXPECT_EQ(TestTypeString::Get(&test_supports_node_data).value,
kTestStringValue2);
TestTypeInt::Destroy(&test_supports_node_data);
EXPECT_FALSE(TestTypeInt::Exists(&test_supports_node_data));
EXPECT_TRUE(TestTypeDouble::Exists(&test_supports_node_data));
EXPECT_TRUE(TestTypeString::Exists(&test_supports_node_data));
TestTypeDouble::Destroy(&test_supports_node_data);
EXPECT_FALSE(TestTypeInt::Exists(&test_supports_node_data));
EXPECT_FALSE(TestTypeDouble::Exists(&test_supports_node_data));
EXPECT_TRUE(TestTypeString::Exists(&test_supports_node_data));
TestTypeString::Destroy(&test_supports_node_data);
EXPECT_FALSE(TestTypeInt::Exists(&test_supports_node_data));
EXPECT_FALSE(TestTypeDouble::Exists(&test_supports_node_data));
EXPECT_FALSE(TestTypeString::Exists(&test_supports_node_data));
}
TEST(NodeInlineDataTest, MixMultipleTypesDifferentStorage) {
using NonSparseTestType = DataWithExplicitCtor<int>;
using SparseTestType = SparseDataWithExplicitCtor<double>;
TestSupportsNodeInlineData<NonSparseTestType, SparseTestType>
test_supports_node_data;
EXPECT_FALSE(NonSparseTestType::Exists(&test_supports_node_data));
EXPECT_FALSE(SparseTestType::Exists(&test_supports_node_data));
NonSparseTestType& non_sparse_test_type =
NonSparseTestType::Create(&test_supports_node_data, kTestIntValue1);
SparseTestType& sparse_test_type =
SparseTestType::Create(&test_supports_node_data, kTestDoubleValue1);
EXPECT_TRUE(NonSparseTestType::Exists(&test_supports_node_data));
EXPECT_TRUE(SparseTestType::Exists(&test_supports_node_data));
EXPECT_EQ(non_sparse_test_type.value, kTestIntValue1);
EXPECT_EQ(sparse_test_type.value, kTestDoubleValue1);
non_sparse_test_type.value = kTestIntValue2;
sparse_test_type.value = kTestDoubleValue2;
EXPECT_EQ(NonSparseTestType::Get(&test_supports_node_data).value,
kTestIntValue2);
EXPECT_EQ(SparseTestType::Get(&test_supports_node_data).value,
kTestDoubleValue2);
SparseTestType::Destroy(&test_supports_node_data);
EXPECT_TRUE(NonSparseTestType::Exists(&test_supports_node_data));
EXPECT_FALSE(SparseTestType::Exists(&test_supports_node_data));
}
TEST(NodeInlineDataTest, ConstAccessors) {
using NonSparseTestType = DataWithExplicitCtor<std::string>;
using SparseTestType = SparseDataWithExplicitCtor<std::string>;
TestSupportsNodeInlineData<NonSparseTestType, SparseTestType>
test_supports_node_data;
NonSparseTestType::Create(&test_supports_node_data, kTestStringValue1);
SparseTestType::Create(&test_supports_node_data, kTestStringValue2);
// Make sure getters compile when passed a const pointer.
const auto* const_ptr = &test_supports_node_data;
const auto& non_sparse_test_type = NonSparseTestType::Get(const_ptr);
const auto& sparse_test_type = SparseTestType::Get(const_ptr);
EXPECT_EQ(non_sparse_test_type.value, kTestStringValue1);
EXPECT_EQ(sparse_test_type.value, kTestStringValue2);
}
} // namespace performance_manager
|