blob: f37af53f1dbe9d6a8849334fad1cf47d894fb76a [file] [log] [blame]
Austin Schuh977a5ed2020-12-02 23:20:04 -08001#include "aos/flatbuffers.h"
2
davidjevans8b9b52f2021-09-17 08:57:30 -07003#include "absl/strings/str_cat.h"
Austin Schuh977a5ed2020-12-02 23:20:04 -08004#include "aos/json_to_flatbuffer.h"
5#include "aos/json_to_flatbuffer_generated.h"
davidjevans8b9b52f2021-09-17 08:57:30 -07006#include "aos/testing/tmpdir.h"
7#include "gtest/gtest.h"
Austin Schuh977a5ed2020-12-02 23:20:04 -08008
9namespace aos {
10namespace testing {
11
12// Tests that Verify works.
13TEST(FlatbufferTest, Verify) {
14 FlatbufferDetachedBuffer<Configuration> fb =
15 JsonToFlatbuffer<Configuration>("{}");
16 FlatbufferSpan<Configuration> fb_span(fb);
17 EXPECT_TRUE(fb.Verify());
18 EXPECT_TRUE(fb_span.Verify());
19
20 // Now confirm it works on an empty flatbuffer.
21 FlatbufferSpan<Configuration> empty(absl::Span<const uint8_t>(nullptr, 0));
22 EXPECT_FALSE(empty.Verify());
23}
24
davidjevans8b9b52f2021-09-17 08:57:30 -070025// Tests the ability to map a flatbuffer on disk to memory
26TEST(FlatbufferMMapTest, Verify) {
27 FlatbufferDetachedBuffer<Configuration> fb =
28 JsonToFlatbuffer<Configuration>("{\"foo_int\": 3}");
29
30 const std::string fb_path = absl::StrCat(TestTmpDir(), "/fb.bfbs");
31 WriteFlatbufferToFile(fb_path, fb);
32
33 FlatbufferMMap<Configuration> fb_mmap(fb_path);
34 EXPECT_TRUE(fb.Verify());
35 EXPECT_TRUE(fb_mmap.Verify());
36 ASSERT_EQ(fb_mmap.message().foo_int(), 3);
37
38 // Verify that copying works
39 {
40 FlatbufferMMap<Configuration> fb_mmap2(fb_path);
41 fb_mmap2 = fb_mmap;
42 EXPECT_TRUE(fb_mmap.Verify());
43 EXPECT_TRUE(fb_mmap2.Verify());
44 ASSERT_EQ(fb_mmap2.message().foo_int(), 3);
45 ASSERT_EQ(fb_mmap.message().foo_int(), 3);
46 }
47 EXPECT_TRUE(fb_mmap.Verify());
48 ASSERT_EQ(fb_mmap.message().foo_int(), 3);
49
50 // Verify that moving works
51 {
52 FlatbufferMMap<Configuration> fb_mmap3(fb_path);
53 fb_mmap3 = std::move(fb_mmap);
54 EXPECT_TRUE(fb_mmap3.Verify());
55 ASSERT_EQ(fb_mmap3.message().foo_int(), 3);
56 }
57}
Austin Schuh977a5ed2020-12-02 23:20:04 -080058} // namespace testing
59} // namespace aos