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
base / types / fixed_array_unittest.cc [blame]
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/types/fixed_array.h"
#include <stddef.h>
#include <cstring>
#include <memory>
#include <type_traits>
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace {
TEST(FixedArrayTest, TriviallyDefaultConstructibleInitializes) {
using T = int;
static_assert(std::is_trivially_default_constructible_v<T>);
using Array = FixedArray<T, 1>;
// First try an array on the stack.
Array stack_array(1);
// This read and the one below are UB if `FixedArray` does not initialize the
// elements, but hopefully even if the compiler chooses to zero memory anyway,
// the test will fail under the memory sanitizer.
EXPECT_EQ(0, stack_array[0]);
// Now try an array on the heap, where we've purposefully written a non-zero
// bitpattern in hopes of increasing the chance of catching incorrect
// behavior.
constexpr size_t kSize = sizeof(Array);
alignas(Array) char storage[kSize];
std::memset(storage, 0xAA, kSize);
Array* placement_new_array = new (storage) Array(1);
EXPECT_EQ(0, (*placement_new_array)[0]);
placement_new_array->~Array();
}
} // namespace
} // namespace base