1 // Copyright 2022 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "core/fxcrt/fixed_zeroed_data_vector.h"
6
7 #include <utility>
8
9 #include "core/fxcrt/fixed_try_alloc_zeroed_data_vector.h"
10 #include "core/fxcrt/fixed_uninit_data_vector.h"
11 #include "core/fxcrt/span_util.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/base/containers/span.h"
15
TEST(FixedZeroedDataVector,NoData)16 TEST(FixedZeroedDataVector, NoData) {
17 FixedZeroedDataVector<int> vec;
18 EXPECT_EQ(0u, vec.size());
19 EXPECT_TRUE(vec.empty());
20 EXPECT_TRUE(vec.span().empty());
21 EXPECT_TRUE(vec.writable_span().empty());
22 }
23
TEST(FixedZeroedDataVector,WithData)24 TEST(FixedZeroedDataVector, WithData) {
25 FixedZeroedDataVector<int> vec(4);
26 EXPECT_FALSE(vec.empty());
27 EXPECT_EQ(4u, vec.size());
28 EXPECT_EQ(4u, vec.span().size());
29 EXPECT_EQ(4u, vec.writable_span().size());
30 EXPECT_THAT(vec.span(), testing::ElementsAre(0, 0, 0, 0));
31
32 constexpr int kData[] = {1, 2, 3, 4};
33 fxcrt::spancpy(vec.writable_span(), pdfium::make_span(kData));
34 EXPECT_THAT(vec.span(), testing::ElementsAre(1, 2, 3, 4));
35 }
36
TEST(FixedZeroedDataVector,Move)37 TEST(FixedZeroedDataVector, Move) {
38 FixedZeroedDataVector<int> vec(4);
39 constexpr int kData[] = {1, 2, 3, 4};
40 ASSERT_EQ(4u, vec.writable_span().size());
41 fxcrt::spancpy(vec.writable_span(), pdfium::make_span(kData));
42 const int* const original_data_ptr = vec.span().data();
43
44 FixedZeroedDataVector<int> vec2(std::move(vec));
45 EXPECT_FALSE(vec2.empty());
46 EXPECT_EQ(4u, vec2.size());
47 EXPECT_EQ(4u, vec2.span().size());
48 EXPECT_EQ(4u, vec2.writable_span().size());
49 EXPECT_THAT(vec2.span(), testing::ElementsAre(1, 2, 3, 4));
50 EXPECT_EQ(vec2.span().data(), original_data_ptr);
51
52 EXPECT_EQ(0u, vec.size());
53 EXPECT_TRUE(vec.empty());
54 EXPECT_TRUE(vec.span().empty());
55 EXPECT_TRUE(vec.writable_span().empty());
56
57 vec = std::move(vec2);
58 EXPECT_FALSE(vec.empty());
59 EXPECT_EQ(4u, vec.size());
60 EXPECT_EQ(4u, vec.span().size());
61 EXPECT_EQ(4u, vec.writable_span().size());
62 EXPECT_THAT(vec.span(), testing::ElementsAre(1, 2, 3, 4));
63 EXPECT_EQ(vec.span().data(), original_data_ptr);
64
65 EXPECT_EQ(0u, vec2.size());
66 EXPECT_TRUE(vec2.empty());
67 EXPECT_TRUE(vec2.span().empty());
68 EXPECT_TRUE(vec2.writable_span().empty());
69 }
70
TEST(FixedZeroedDataVector,AssignFromFixedUninitDataVector)71 TEST(FixedZeroedDataVector, AssignFromFixedUninitDataVector) {
72 FixedZeroedDataVector<int> vec;
73
74 FixedUninitDataVector<int> vec2(4);
75 constexpr int kData[] = {1, 2, 3, 4};
76 ASSERT_EQ(4u, vec2.writable_span().size());
77 fxcrt::spancpy(vec2.writable_span(), pdfium::make_span(kData));
78
79 vec = std::move(vec2);
80 EXPECT_TRUE(vec2.empty());
81 EXPECT_EQ(4u, vec.span().size());
82 EXPECT_THAT(vec.span(), testing::ElementsAre(1, 2, 3, 4));
83 }
84
TEST(FixedZeroedDataVector,AssignFromFixedTryAllocZeroedDataVector)85 TEST(FixedZeroedDataVector, AssignFromFixedTryAllocZeroedDataVector) {
86 FixedZeroedDataVector<int> vec;
87
88 FixedTryAllocZeroedDataVector<int> vec2(4);
89 constexpr int kData[] = {1, 2, 3, 4};
90 ASSERT_EQ(4u, vec2.writable_span().size());
91 fxcrt::spancpy(vec2.writable_span(), pdfium::make_span(kData));
92
93 vec = std::move(vec2);
94 EXPECT_TRUE(vec2.empty());
95 EXPECT_EQ(4u, vec.span().size());
96 EXPECT_THAT(vec.span(), testing::ElementsAre(1, 2, 3, 4));
97 }
98