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"
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  FML_DISALLOW_COPY_AND_ASSIGN(Sample);
51 };
52 
54  .table_name = "Sample",
55  .members = {"some_data"},
56 };
57 
58 class SampleWithVector : public Archivable {
59  public:
60  SampleWithVector() = default;
61 
62  // |Archivable|
63  PrimaryKey GetPrimaryKey() const override { return std::nullopt; }
64 
65  // |Archivable|
66  bool Write(ArchiveLocation& item) const override {
67  std::vector<Sample> samples;
68  for (size_t i = 0; i < 50u; i++) {
69  samples.emplace_back(Sample{1988 + i});
70  }
71  return item.Write("hello", "world") && item.Write("samples", samples);
72  };
73 
74  // |Archivable|
75  bool Read(ArchiveLocation& item) override {
76  std::string str;
77  auto str_result = item.Read("hello", str);
78  std::vector<Sample> samples;
79  auto vec_result = item.Read("samples", samples);
80 
81  if (!str_result || str != "world" || !vec_result || samples.size() != 50) {
82  return false;
83  }
84 
85  size_t current = 1988;
86  for (const auto& sample : samples) {
87  if (sample.GetSomeData() != current++) {
88  return false;
89  }
90  }
91  return true;
92  };
93 
94  static const ArchiveDef kArchiveDefinition;
95 
96  private:
97  std::vector<Sample> samples_;
98  FML_DISALLOW_COPY_AND_ASSIGN(SampleWithVector);
99 };
100 
102  .table_name = "SampleWithVector",
103  .members = {"hello", "samples"},
104 };
105 
107 
108 TEST_F(ArchiveTest, SimpleInitialization) {
109  Archive archive(GetArchiveFileName().c_str());
110  ASSERT_TRUE(archive.IsValid());
111 }
112 
113 TEST_F(ArchiveTest, AddStorageClass) {
114  Archive archive(GetArchiveFileName().c_str());
115  ASSERT_TRUE(archive.IsValid());
116 }
117 
118 TEST_F(ArchiveTest, AddData) {
119  Archive archive(GetArchiveFileName().c_str());
120  ASSERT_TRUE(archive.IsValid());
121  Sample sample;
122  ASSERT_TRUE(archive.Write(sample));
123 }
124 
125 TEST_F(ArchiveTest, AddDataMultiple) {
126  Archive archive(GetArchiveFileName().c_str());
127  ASSERT_TRUE(archive.IsValid());
128 
129  for (size_t i = 0; i < 100; i++) {
130  Sample sample(i + 1);
131  ASSERT_TRUE(archive.Write(sample));
132  }
133 }
134 
135 TEST_F(ArchiveTest, ReadData) {
136  Archive archive(GetArchiveFileName().c_str());
137  ASSERT_TRUE(archive.IsValid());
138 
139  size_t count = 50;
140 
141  std::vector<PrimaryKey::value_type> keys;
142  std::vector<uint64_t> values;
143 
144  for (size_t i = 0; i < count; i++) {
145  Sample sample(i + 1);
146  keys.push_back(sample.GetPrimaryKey().value());
147  values.push_back(sample.GetSomeData());
148  ASSERT_TRUE(archive.Write(sample));
149  }
150 
151  for (size_t i = 0; i < count; i++) {
152  Sample sample;
153  ASSERT_TRUE(archive.Read(keys[i], sample));
154  ASSERT_EQ(values[i], sample.GetSomeData());
155  }
156 }
157 
158 TEST_F(ArchiveTest, ReadDataWithNames) {
159  Archive archive(GetArchiveFileName().c_str());
160  ASSERT_TRUE(archive.IsValid());
161 
162  size_t count = 8;
163 
164  std::vector<PrimaryKey::value_type> keys;
165  std::vector<uint64_t> values;
166 
167  keys.reserve(count);
168  values.reserve(count);
169 
170  for (size_t i = 0; i < count; i++) {
171  Sample sample(i + 1);
172  keys.push_back(sample.GetPrimaryKey().value());
173  values.push_back(sample.GetSomeData());
174  ASSERT_TRUE(archive.Write(sample));
175  }
176 
177  for (size_t i = 0; i < count; i++) {
178  Sample sample;
179  ASSERT_TRUE(archive.Read(keys[i], sample));
180  ASSERT_EQ(values[i], sample.GetSomeData());
181  ASSERT_EQ(keys[i], sample.GetPrimaryKey());
182  }
183 }
184 
185 TEST_F(ArchiveTest, CanReadWriteVectorOfArchivables) {
186  Archive archive(GetArchiveFileName().c_str());
187  ASSERT_TRUE(archive.IsValid());
188 
189  SampleWithVector sample_with_vector;
190  ASSERT_TRUE(archive.Write(sample_with_vector));
191  bool read_success = false;
192  ASSERT_EQ(
193  archive.Read<SampleWithVector>([&](ArchiveLocation& location) -> bool {
194  SampleWithVector other_sample_with_vector;
195  read_success = other_sample_with_vector.Read(location);
196  return true; // Always keep continuing but assert that we only get one.
197  }),
198  1u);
199  ASSERT_TRUE(read_success);
200 }
201 
202 } // namespace testing
203 } // namespace impeller
204 
205 // 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:22
impeller::testing::SampleWithVector::Read
bool Read(ArchiveLocation &item) override
Definition: archivist_unittests.cc:75
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:27
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:15
archive_location.h
impeller::ArchiveDef
Definition: archivable.h:14
impeller::PrimaryKey
std::optional< int64_t > PrimaryKey
Definition: archivable.h:21
impeller::testing::SampleWithVector::Write
bool Write(ArchiveLocation &item) const override
Definition: archivist_unittests.cc:66
impeller::testing::SampleWithVector
Definition: archivist_unittests.cc:58
impeller::testing::SampleWithVector::kArchiveDefinition
static const ArchiveDef kArchiveDefinition
Definition: archivist_unittests.cc:92
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:63
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:108
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:25
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:32
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:39
impeller::testing::ArchivistFixture
Definition: archivist_fixture.h:13
impeller
Definition: aiks_context.cc:10