| Flutter Impeller |

archivist_unittests.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
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 <cstdio>
6 #include <thread>
7
8 #include "flutter/fml/macros.h"
9 #include "flutter/testing/testing.h"
10 #include "impeller/archivist/archive.h"
11 #include "impeller/archivist/archive_location.h"
12 #include "impeller/archivist/archivist_fixture.h"
13
14 // TODO(zanderso): https://github.com/flutter/flutter/issues/127701
15 // NOLINTBEGIN(bugprone-unchecked-optional-access)
16
17 namespace impeller {
18 namespace testing {
19
20 static int64_t LastSample = 0;
21
22 class Sample : public Archivable {
23 public:
24 explicit Sample(uint64_t count = 42) : some_data_(count) {}
25
26 Sample(Sample&&) = default;
27
28 uint64_t GetSomeData() const { return some_data_; }
29
30 // |Archivable|
31 PrimaryKey GetPrimaryKey() const override { return name_; }
32
33 // |Archivable|
34 bool Write(ArchiveLocation& item) const override {
35 return item.Write("some_data", some_data_);
36 };
37
38 // |Archivable|
39 bool Read(ArchiveLocation& item) override {
40 name_ = item.GetPrimaryKey();
41 return item.Read("some_data", some_data_);
42 };
43
44 static const ArchiveDef kArchiveDefinition;
45
46 private:
47 uint64_t some_data_;
48 PrimaryKey name_ = ++LastSample;
49
50 Sample(const Sample&) = delete;
51
52 Sample& operator=(const Sample&) = delete;
53 };
54
55 const ArchiveDef Sample::kArchiveDefinition = {
56 .table_name = "Sample",
57 .members = {"some_data"},
58 };
59
60 class SampleWithVector : public Archivable {
61 public:
62 SampleWithVector() = default;
63
64 // |Archivable|
65 PrimaryKey GetPrimaryKey() const override { return std::nullopt; }
66
67 // |Archivable|
68 bool Write(ArchiveLocation& item) const override {
69 std::vector<Sample> samples;
70 for (size_t i = 0; i < 50u; i++) {
71 samples.emplace_back(Sample{1988 + i});
72 }
73 return item.Write("hello", "world") && item.Write("samples", samples);
74 };
75
76 // |Archivable|
77 bool Read(ArchiveLocation& item) override {
78 std::string str;
79 auto str_result = item.Read("hello", str);
80 std::vector<Sample> samples;
81 auto vec_result = item.Read("samples", samples);
82
83 if (!str_result || str != "world" || !vec_result || samples.size() != 50) {
84 return false;
85 }
86
87 size_t current = 1988;
88 for (const auto& sample : samples) {
89 if (sample.GetSomeData() != current++) {
90 return false;
91 }
92 }
93 return true;
94 };
95
96 static const ArchiveDef kArchiveDefinition;
97
98 private:
99 std::vector<Sample> samples_;
100 SampleWithVector(const SampleWithVector&) = delete;
101
102 SampleWithVector& operator=(const SampleWithVector&) = delete;
103 };
104
105 const ArchiveDef SampleWithVector::kArchiveDefinition = {
106 .table_name = "SampleWithVector",
107 .members = {"hello", "samples"},
108 };
109
110 using ArchiveTest = ArchivistFixture;
111
112 TEST_F(ArchiveTest, SimpleInitialization) {
113 Archive archive(GetArchiveFileName().c_str());
114 ASSERT_TRUE(archive.IsValid());
115 }
116
117 TEST_F(ArchiveTest, AddStorageClass) {
118 Archive archive(GetArchiveFileName().c_str());
119 ASSERT_TRUE(archive.IsValid());
120 }
121
122 TEST_F(ArchiveTest, AddData) {
123 Archive archive(GetArchiveFileName().c_str());
124 ASSERT_TRUE(archive.IsValid());
125 Sample sample;
126 ASSERT_TRUE(archive.Write(sample));
127 }
128
129 TEST_F(ArchiveTest, AddDataMultiple) {
130 Archive archive(GetArchiveFileName().c_str());
131 ASSERT_TRUE(archive.IsValid());
132
133 for (size_t i = 0; i < 100; i++) {
134 Sample sample(i + 1);
135 ASSERT_TRUE(archive.Write(sample));
136 }
137 }
138
139 TEST_F(ArchiveTest, ReadData) {
140 Archive archive(GetArchiveFileName().c_str());
141 ASSERT_TRUE(archive.IsValid());
142
143 size_t count = 50;
144
145 std::vector<PrimaryKey::value_type> keys;
146 std::vector<uint64_t> values;
147
148 for (size_t i = 0; i < count; i++) {
149 Sample sample(i + 1);
150 keys.push_back(sample.GetPrimaryKey().value());
151 values.push_back(sample.GetSomeData());
152 ASSERT_TRUE(archive.Write(sample));
153 }
154
155 for (size_t i = 0; i < count; i++) {
156 Sample sample;
157 ASSERT_TRUE(archive.Read(keys[i], sample));
158 ASSERT_EQ(values[i], sample.GetSomeData());
159 }
160 }
161
162 TEST_F(ArchiveTest, ReadDataWithNames) {
163 Archive archive(GetArchiveFileName().c_str());
164 ASSERT_TRUE(archive.IsValid());
165
166 size_t count = 8;
167
168 std::vector<PrimaryKey::value_type> keys;
169 std::vector<uint64_t> values;
170
171 keys.reserve(count);
172 values.reserve(count);
173
174 for (size_t i = 0; i < count; i++) {
175 Sample sample(i + 1);
176 keys.push_back(sample.GetPrimaryKey().value());
177 values.push_back(sample.GetSomeData());
178 ASSERT_TRUE(archive.Write(sample));
179 }
180
181 for (size_t i = 0; i < count; i++) {
182 Sample sample;
183 ASSERT_TRUE(archive.Read(keys[i], sample));
184 ASSERT_EQ(values[i], sample.GetSomeData());
185 ASSERT_EQ(keys[i], sample.GetPrimaryKey());
186 }
187 }
188
189 TEST_F(ArchiveTest, CanReadWriteVectorOfArchivables) {
190 Archive archive(GetArchiveFileName().c_str());
191 ASSERT_TRUE(archive.IsValid());
192
193 SampleWithVector sample_with_vector;
194 ASSERT_TRUE(archive.Write(sample_with_vector));
195 bool read_success = false;
196 ASSERT_EQ(
197 archive.Read<SampleWithVector>([&](ArchiveLocation& location) -> bool {
198 SampleWithVector other_sample_with_vector;
199 read_success = other_sample_with_vector.Read(location);
200 return true; // Always keep continuing but assert that we only get one.
201 }),
202 1u);
203 ASSERT_TRUE(read_success);
204 }
205
206 } // namespace testing
207 } // namespace impeller
208
209 // NOLINTEND(bugprone-unchecked-optional-access)
impeller::ArchiveLocation::GetPrimaryKey
PrimaryKey GetPrimaryKey() const
Definition: archive_location.cc:21
impeller::testing::LastSample
static int64_t LastSample
Definition: archivist_unittests.cc:20
impeller::Archive
Definition: archive.h:21
impeller::testing::SampleWithVector::Read
bool Read(ArchiveLocation &item) override
Definition: archivist_unittests.cc:77
impeller::testing::Sample::kArchiveDefinition
static const ArchiveDef kArchiveDefinition
Definition: archivist_unittests.cc:42
impeller::Archivable
Instances of Archivables can be read from and written to a persistent archive.
Definition: archivable.h:28
impeller::testing::Sample::GetPrimaryKey
PrimaryKey GetPrimaryKey() const override
Definition: archivist_unittests.cc:31
impeller::ArchiveDef::table_name
const std::string table_name
Definition: archivable.h:16
archive_location.h
impeller::ArchiveDef
Definition: archivable.h:15
impeller::PrimaryKey
std::optional< int64_t > PrimaryKey
Definition: archivable.h:22
impeller::testing::SampleWithVector::Write
bool Write(ArchiveLocation &item) const override
Definition: archivist_unittests.cc:68
impeller::testing::SampleWithVector
Definition: archivist_unittests.cc:60
impeller::testing::SampleWithVector::kArchiveDefinition
static const ArchiveDef kArchiveDefinition
Definition: archivist_unittests.cc:94
impeller::testing::Sample::Read
bool Read(ArchiveLocation &item) override
Definition: archivist_unittests.cc:39
impeller::testing::SampleWithVector::GetPrimaryKey
PrimaryKey GetPrimaryKey() const override
Definition: archivist_unittests.cc:65
impeller::testing::Sample
Definition: archivist_unittests.cc:22
impeller::testing::Sample::GetSomeData
uint64_t GetSomeData() const
Definition: archivist_unittests.cc:28
impeller::testing::Sample::Write
bool Write(ArchiveLocation &item) const override
Definition: archivist_unittests.cc:34
impeller::testing::TEST_F
TEST_F(ArchiveTest, SimpleInitialization)
Definition: archivist_unittests.cc:112
impeller::testing::Sample::Sample
Sample(uint64_t count=42)
Definition: archivist_unittests.cc:24
archivist_fixture.h
impeller::Archive::IsValid
bool IsValid() const
Definition: archive.cc:22
impeller::ArchiveLocation::Read
bool Read(const std::string &member, T &item)
Definition: archive_location.h:79
impeller::ArchiveLocation
Definition: archive_location.h:21
impeller::testing::SampleWithVector::SampleWithVector
SampleWithVector()=default
archive.h
impeller::Archive::Write
bool Write(const T &archivable)
Definition: archive.h:31
impeller::ArchiveLocation::Write
bool Write(const std::string &member, T item)
Definition: archive_location.h:26
impeller::Archive::Read
bool Read(PrimaryKey name, T &archivable)
Definition: archive.h:38
impeller::testing::ArchivistFixture
Definition: archivist_fixture.h:14
impeller
Definition: aiks_context.cc:10