Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 1 | #include "aos/flatbuffer_merge.h" |
| 2 | |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 3 | #include <string_view> |
| 4 | |
| 5 | #include "absl/strings/escaping.h" |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 6 | #include "gtest/gtest.h" |
| 7 | |
| 8 | #include "aos/json_to_flatbuffer.h" |
| 9 | #include "aos/json_to_flatbuffer_generated.h" |
| 10 | #include "flatbuffers/minireflect.h" |
| 11 | |
| 12 | namespace aos { |
| 13 | namespace testing { |
| 14 | |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 15 | std::string_view FromFbb(const flatbuffers::FlatBufferBuilder &fbb) { |
| 16 | return std::string_view( |
| 17 | reinterpret_cast<const char *>(fbb.GetCurrentBufferPointer()), |
| 18 | fbb.GetSize()); |
| 19 | } |
| 20 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 21 | std::string_view FromFbb(const FlatbufferDetachedBuffer<Configuration> &b) { |
| 22 | return std::string_view(reinterpret_cast<const char *>(b.span().data()), |
| 23 | b.span().size()); |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 24 | } |
| 25 | |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 26 | class FlatbufferMerge : public ::testing::Test { |
| 27 | public: |
| 28 | FlatbufferMerge() {} |
| 29 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 30 | void ExpectMergedOutput( |
| 31 | const NonSizePrefixedFlatbuffer<Configuration> &fb_merged, |
| 32 | std::string_view expected_output) { |
| 33 | ASSERT_NE(fb_merged.span().size(), 0u); |
James Kuszmaul | f3a3be2 | 2020-01-04 12:12:00 -0800 | [diff] [blame] | 34 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 35 | const ::std::string merged_output = FlatbufferToJson(fb_merged); |
James Kuszmaul | f3a3be2 | 2020-01-04 12:12:00 -0800 | [diff] [blame] | 36 | EXPECT_EQ(expected_output, merged_output); |
Austin Schuh | 30d7db9 | 2020-01-26 16:45:47 -0800 | [diff] [blame] | 37 | |
| 38 | aos::FlatbufferDetachedBuffer<Configuration> expected_message( |
| 39 | JsonToFlatbuffer(std::string(expected_output).c_str(), |
| 40 | ConfigurationTypeTable())); |
| 41 | EXPECT_TRUE( |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 42 | CompareFlatBuffer(&fb_merged.message(), &expected_message.message())); |
James Kuszmaul | f3a3be2 | 2020-01-04 12:12:00 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 45 | void JsonMerge(const ::std::string in1, const ::std::string in2, |
| 46 | const ::std::string out) { |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 47 | LOG(INFO) << "Merging: " << in1.c_str(); |
| 48 | LOG(INFO) << "Merging: " << in2.c_str(); |
| 49 | const FlatbufferDetachedBuffer<Configuration> fb1 = JsonToFlatbuffer( |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 50 | static_cast<const char *>(in1.c_str()), ConfigurationTypeTable()); |
| 51 | |
| 52 | const ::std::string in1_nested = "{ \"nested_config\": " + in1 + " }"; |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 53 | const FlatbufferDetachedBuffer<Configuration> fb1_nested = |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 54 | JsonToFlatbuffer(static_cast<const char *>(in1_nested.c_str()), |
| 55 | ConfigurationTypeTable()); |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 56 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 57 | const FlatbufferDetachedBuffer<Configuration> fb2 = JsonToFlatbuffer( |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 58 | static_cast<const char *>(in2.c_str()), ConfigurationTypeTable()); |
| 59 | |
| 60 | const ::std::string in2_nested = "{ \"nested_config\": " + in2 + " }"; |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 61 | const FlatbufferDetachedBuffer<Configuration> fb2_nested = |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 62 | JsonToFlatbuffer(static_cast<const char *>(in2_nested.c_str()), |
| 63 | ConfigurationTypeTable()); |
| 64 | |
| 65 | const ::std::string out_nested = "{ \"nested_config\": " + out + " }"; |
| 66 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 67 | const FlatbufferDetachedBuffer<Configuration> empty = |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 68 | JsonToFlatbuffer("{ }", ConfigurationTypeTable()); |
| 69 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 70 | ASSERT_NE(fb1.span().size(), 0u); |
| 71 | ASSERT_NE(fb2.span().size(), 0u); |
| 72 | ASSERT_NE(fb1_nested.span().size(), 0u); |
| 73 | ASSERT_NE(fb2_nested.span().size(), 0u); |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 74 | |
| 75 | // We now want to run 7 tests. |
| 76 | // in1 merged "" -> in1. |
| 77 | // in2 merged "" -> in2. |
| 78 | // "" merged in1 -> in1. |
| 79 | // "" merged in2 -> in2. |
| 80 | // nullptr merged in1 -> in1. |
| 81 | // in1 merged nullptr -> in1. |
| 82 | // in1 merged in2 -> out. |
| 83 | |
| 84 | { |
| 85 | // in1 merged with "" => in1. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 86 | FlatbufferDetachedBuffer<Configuration> fb_merged = |
| 87 | MergeFlatBuffers<Configuration>(fb1, empty); |
| 88 | ASSERT_NE(fb_merged.span().size(), 0u); |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 89 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 90 | EXPECT_EQ(in1, FlatbufferToJson(fb_merged)); |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | { |
| 94 | // in2 merged with "" => in2. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 95 | FlatbufferDetachedBuffer<Configuration> fb_merged = |
| 96 | MergeFlatBuffers<Configuration>(fb2, empty); |
| 97 | ASSERT_NE(fb_merged.span().size(), 0u); |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 98 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 99 | EXPECT_EQ(in2, FlatbufferToJson(fb_merged)); |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | { |
| 103 | // "" merged with in1 => in1. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 104 | FlatbufferDetachedBuffer<Configuration> fb_merged = |
| 105 | MergeFlatBuffers<Configuration>(empty, fb1); |
| 106 | ASSERT_NE(fb_merged.span().size(), 0u); |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 107 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 108 | EXPECT_EQ(in1, FlatbufferToJson(fb_merged)); |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | { |
| 112 | // "" merged with in2 => in2. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 113 | FlatbufferDetachedBuffer<Configuration> fb_merged = |
| 114 | MergeFlatBuffers<Configuration>(empty, fb2); |
| 115 | ASSERT_NE(fb_merged.span().size(), 0u); |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 116 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 117 | EXPECT_EQ(in2, FlatbufferToJson(fb_merged)); |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | { |
| 121 | // nullptr merged with in1 => in1. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 122 | FlatbufferDetachedBuffer<Configuration> fb_merged = |
| 123 | MergeFlatBuffers<Configuration>(nullptr, &fb1.message()); |
| 124 | ASSERT_NE(fb_merged.span().size(), 0u); |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 125 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 126 | EXPECT_EQ(in1, FlatbufferToJson(fb_merged)); |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | { |
| 130 | // in1 merged with nullptr => in1. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 131 | FlatbufferDetachedBuffer<Configuration> fb_merged = |
| 132 | MergeFlatBuffers<Configuration>(&fb1.message(), nullptr); |
| 133 | ASSERT_NE(fb_merged.span().size(), 0u); |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 134 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 135 | EXPECT_EQ(in1, FlatbufferToJson(fb_merged)); |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | { |
| 139 | // in1 merged with in2 => out. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 140 | FlatbufferDetachedBuffer<Configuration> fb_merged = |
| 141 | MergeFlatBuffers<Configuration>(fb1, fb2); |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 142 | |
James Kuszmaul | f3a3be2 | 2020-01-04 12:12:00 -0800 | [diff] [blame] | 143 | ExpectMergedOutput(fb_merged, out); |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 144 | } |
| 145 | |
James Kuszmaul | f3a3be2 | 2020-01-04 12:12:00 -0800 | [diff] [blame] | 146 | // Test all the different merge methods: |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 147 | ExpectMergedOutput(MergeFlatBuffers<Configuration>(fb1, fb2), out); |
James Kuszmaul | f3a3be2 | 2020-01-04 12:12:00 -0800 | [diff] [blame] | 148 | { |
| 149 | flatbuffers::FlatBufferBuilder fbb; |
Austin Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 150 | fbb.ForceDefaults(true); |
James Kuszmaul | f3a3be2 | 2020-01-04 12:12:00 -0800 | [diff] [blame] | 151 | fbb.Finish(MergeFlatBuffers( |
| 152 | ConfigurationTypeTable(), |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 153 | reinterpret_cast<const flatbuffers::Table *>(&fb1.message()), |
| 154 | reinterpret_cast<const flatbuffers::Table *>(&fb2.message()), &fbb)); |
| 155 | FlatbufferDetachedBuffer<Configuration> fb(fbb.Release()); |
| 156 | ExpectMergedOutput(fb, out); |
James Kuszmaul | f3a3be2 | 2020-01-04 12:12:00 -0800 | [diff] [blame] | 157 | } |
| 158 | { |
| 159 | flatbuffers::FlatBufferBuilder fbb; |
Austin Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 160 | fbb.ForceDefaults(true); |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 161 | fbb.Finish(MergeFlatBuffers<Configuration>(&fb1.message(), &fb2.message(), |
| 162 | &fbb)); |
| 163 | FlatbufferDetachedBuffer<Configuration> fb(fbb.Release()); |
| 164 | ExpectMergedOutput(fb, out); |
James Kuszmaul | f3a3be2 | 2020-01-04 12:12:00 -0800 | [diff] [blame] | 165 | } |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 166 | ExpectMergedOutput( |
| 167 | MergeFlatBuffers<Configuration>(&fb1.message(), &fb2.message()), out); |
James Kuszmaul | f3a3be2 | 2020-01-04 12:12:00 -0800 | [diff] [blame] | 168 | ExpectMergedOutput(MergeFlatBuffers<Configuration>(fb1, fb2), out); |
James Kuszmaul | f3a3be2 | 2020-01-04 12:12:00 -0800 | [diff] [blame] | 169 | |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 170 | // Now, to make things extra exciting, nest a config inside a config. And |
| 171 | // run all the tests. This will exercise some fun nested merges and copies. |
| 172 | |
| 173 | { |
| 174 | // in1_nested merged with "" => in1. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 175 | FlatbufferDetachedBuffer<Configuration> fb_merged = |
| 176 | MergeFlatBuffers<Configuration>(fb1_nested, empty); |
| 177 | ASSERT_NE(fb_merged.span().size(), 0u); |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 178 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 179 | EXPECT_EQ(in1_nested, FlatbufferToJson(fb_merged)); |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | { |
| 183 | // in2_nested merged with "" => in2_nested. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 184 | FlatbufferDetachedBuffer<Configuration> fb_merged = |
| 185 | MergeFlatBuffers<Configuration>(fb2_nested, empty); |
| 186 | ASSERT_NE(fb_merged.span().size(), 0u); |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 187 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 188 | EXPECT_EQ(in2_nested, FlatbufferToJson(fb_merged)); |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | { |
| 192 | // "" merged with in1_nested => in1_nested. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 193 | FlatbufferDetachedBuffer<Configuration> fb_merged = |
| 194 | MergeFlatBuffers<Configuration>(empty, fb1_nested); |
| 195 | ASSERT_NE(fb_merged.span().size(), 0u); |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 196 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 197 | EXPECT_EQ(in1_nested, FlatbufferToJson(fb_merged)); |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | { |
| 201 | // "" merged with in2_nested => in2_nested. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 202 | FlatbufferDetachedBuffer<Configuration> fb_merged = |
| 203 | MergeFlatBuffers<Configuration>(empty, fb2_nested); |
| 204 | ASSERT_NE(fb_merged.span().size(), 0u); |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 205 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 206 | EXPECT_EQ(in2_nested, FlatbufferToJson(fb_merged)); |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | { |
| 210 | // nullptr merged with in1_nested => in1_nested. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 211 | FlatbufferDetachedBuffer<Configuration> fb_merged = |
| 212 | MergeFlatBuffers<Configuration>(nullptr, &fb1_nested.message()); |
| 213 | ASSERT_NE(fb_merged.span().size(), 0u); |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 214 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 215 | EXPECT_EQ(in1_nested, FlatbufferToJson(fb_merged)); |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | { |
| 219 | // nullptr merged with in1_nested => in1_nested. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 220 | FlatbufferDetachedBuffer<Configuration> fb_merged( |
| 221 | MergeFlatBuffers<Configuration>(&fb1_nested.message(), nullptr)); |
| 222 | ASSERT_NE(fb_merged.span().size(), 0u); |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 223 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 224 | EXPECT_EQ(in1_nested, FlatbufferToJson(fb_merged)); |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | { |
| 228 | // in1_nested merged with in2_nested => out_nested. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 229 | FlatbufferDetachedBuffer<Configuration> fb_merged( |
| 230 | MergeFlatBuffers<Configuration>(fb1_nested, fb2_nested)); |
| 231 | ASSERT_NE(fb_merged.span().size(), 0u); |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 232 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 233 | EXPECT_EQ(out_nested, FlatbufferToJson(fb_merged)); |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 234 | } |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 235 | |
| 236 | // TODO(austin): Try more flatbuffers... |
| 237 | // Now try copying various flatbuffers and confirming they match. |
| 238 | { |
| 239 | flatbuffers::FlatBufferBuilder aos_flatbuffer_copy_fbb; |
| 240 | aos_flatbuffer_copy_fbb.ForceDefaults(true); |
| 241 | |
| 242 | LOG(INFO) << "Copying " << in1 << " " |
| 243 | << absl::BytesToHexString(FromFbb(fb1)) << " at " |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 244 | << reinterpret_cast<const void *>(fb1.span().data()) << " size " |
| 245 | << fb1.span().size(); |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 246 | aos_flatbuffer_copy_fbb.Finish(CopyFlatBuffer<Configuration>( |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 247 | &fb1.message(), &aos_flatbuffer_copy_fbb)); |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 248 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 249 | const aos::FlatbufferDetachedBuffer<Configuration> fb_copy( |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 250 | aos_flatbuffer_copy_fbb.Release()); |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 251 | ASSERT_NE(fb_copy.span().size(), 0u); |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 252 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 253 | EXPECT_TRUE(fb1.Verify()); |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 254 | |
| 255 | ASSERT_TRUE(fb_copy.Verify()) << in1; |
| 256 | |
| 257 | EXPECT_EQ(in1, FlatbufferToJson(fb_copy)); |
| 258 | } |
| 259 | |
| 260 | { |
| 261 | flatbuffers::FlatBufferBuilder aos_flatbuffer_copy_message_ptr_fbb; |
| 262 | aos_flatbuffer_copy_message_ptr_fbb.ForceDefaults(true); |
| 263 | |
| 264 | aos_flatbuffer_copy_message_ptr_fbb.Finish(CopyFlatBuffer<Configuration>( |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 265 | &fb2.message(), &aos_flatbuffer_copy_message_ptr_fbb)); |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 266 | |
| 267 | aos::FlatbufferDetachedBuffer<Configuration> fb_copy_message_ptr( |
| 268 | aos_flatbuffer_copy_message_ptr_fbb.Release()); |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 269 | ASSERT_NE(fb_copy_message_ptr.span().size(), 0u); |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 270 | |
| 271 | flatbuffers::FlatBufferBuilder aos_flatbuffer_copy_full_fbb; |
| 272 | aos_flatbuffer_copy_full_fbb.ForceDefaults(true); |
| 273 | |
| 274 | aos_flatbuffer_copy_full_fbb.Finish(BlindCopyFlatBuffer<Configuration>( |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 275 | aos::FlatbufferSpan<Configuration>(fb2.span()), |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 276 | &aos_flatbuffer_copy_full_fbb)); |
| 277 | |
| 278 | aos::FlatbufferDetachedBuffer<Configuration> fb_copy_full( |
| 279 | aos_flatbuffer_copy_full_fbb.Release()); |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 280 | ASSERT_NE(fb_copy_full.span().size(), 0u); |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 281 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 282 | EXPECT_TRUE(fb2.Verify()); |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 283 | |
| 284 | LOG(INFO) << "Verifying copy of " << in2; |
| 285 | ASSERT_TRUE(fb_copy_message_ptr.Verify()) << in2; |
| 286 | LOG(INFO) << "Verifying full of " << in2; |
| 287 | ASSERT_TRUE(fb_copy_full.Verify()) << in2; |
| 288 | |
| 289 | EXPECT_EQ(in2, FlatbufferToJson(fb_copy_message_ptr)); |
| 290 | EXPECT_EQ(in2, FlatbufferToJson(fb_copy_full)); |
| 291 | } |
| 292 | |
| 293 | { |
| 294 | flatbuffers::FlatBufferBuilder aos_flatbuffer_copy_fbb; |
| 295 | aos_flatbuffer_copy_fbb.ForceDefaults(true); |
| 296 | |
| 297 | aos_flatbuffer_copy_fbb.Finish(CopyFlatBuffer<Configuration>( |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 298 | &fb1_nested.message(), &aos_flatbuffer_copy_fbb)); |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 299 | |
| 300 | aos::FlatbufferDetachedBuffer<Configuration> fb_copy( |
| 301 | aos_flatbuffer_copy_fbb.Release()); |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 302 | ASSERT_NE(fb_copy.span().size(), 0u); |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 303 | |
| 304 | ASSERT_TRUE(fb_copy.Verify()); |
| 305 | |
| 306 | EXPECT_EQ(in1_nested, FlatbufferToJson(fb_copy)); |
| 307 | } |
| 308 | |
| 309 | { |
| 310 | flatbuffers::FlatBufferBuilder aos_flatbuffer_copy_fbb; |
| 311 | aos_flatbuffer_copy_fbb.ForceDefaults(true); |
| 312 | |
| 313 | aos_flatbuffer_copy_fbb.Finish(CopyFlatBuffer<Configuration>( |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 314 | &fb2_nested.message(), &aos_flatbuffer_copy_fbb)); |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 315 | |
| 316 | aos::FlatbufferDetachedBuffer<Configuration> fb_copy( |
| 317 | aos_flatbuffer_copy_fbb.Release()); |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 318 | ASSERT_NE(fb_copy.span().size(), 0u); |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 319 | |
| 320 | ASSERT_TRUE(fb_copy.Verify()); |
| 321 | |
| 322 | EXPECT_EQ(in2_nested, FlatbufferToJson(fb_copy)); |
| 323 | } |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 324 | } |
| 325 | }; |
| 326 | |
| 327 | // Test the easy ones. Test every type, single, no nesting. |
| 328 | TEST_F(FlatbufferMerge, Basic) { |
James Kuszmaul | f3a3be2 | 2020-01-04 12:12:00 -0800 | [diff] [blame] | 329 | JsonMerge("{ }", "{ }", "{ }"); |
| 330 | |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 331 | JsonMerge("{ \"foo_bool\": true }", "{ \"foo_bool\": false }", |
| 332 | "{ \"foo_bool\": false }"); |
| 333 | |
| 334 | JsonMerge("{ \"foo_bool\": false }", "{ \"foo_bool\": true }", |
| 335 | "{ \"foo_bool\": true }"); |
| 336 | |
| 337 | JsonMerge("{ \"foo_byte\": 5 }", "{ \"foo_byte\": -7 }", |
| 338 | "{ \"foo_byte\": -7 }"); |
| 339 | |
| 340 | JsonMerge("{ \"foo_ubyte\": 5 }", "{ \"foo_ubyte\": 7 }", |
| 341 | "{ \"foo_ubyte\": 7 }"); |
| 342 | |
| 343 | JsonMerge("{ \"foo_short\": 5 }", "{ \"foo_short\": 7 }", |
| 344 | "{ \"foo_short\": 7 }"); |
| 345 | |
| 346 | JsonMerge("{ \"foo_int\": 5 }", "{ \"foo_int\": 7 }", "{ \"foo_int\": 7 }"); |
| 347 | |
| 348 | JsonMerge("{ \"foo_uint\": 5 }", "{ \"foo_uint\": 7 }", |
| 349 | "{ \"foo_uint\": 7 }"); |
| 350 | |
| 351 | JsonMerge("{ \"foo_long\": 5 }", "{ \"foo_long\": 7 }", |
| 352 | "{ \"foo_long\": 7 }"); |
| 353 | |
| 354 | JsonMerge("{ \"foo_ulong\": 5 }", "{ \"foo_ulong\": 7 }", |
| 355 | "{ \"foo_ulong\": 7 }"); |
| 356 | |
| 357 | JsonMerge("{ \"foo_float\": 5.0 }", "{ \"foo_float\": 7.1 }", |
| 358 | "{ \"foo_float\": 7.1 }"); |
| 359 | |
| 360 | JsonMerge("{ \"foo_double\": 5.0 }", "{ \"foo_double\": 7.1 }", |
| 361 | "{ \"foo_double\": 7.1 }"); |
| 362 | } |
| 363 | |
| 364 | // Test arrays of simple types. |
| 365 | TEST_F(FlatbufferMerge, Array) { |
| 366 | JsonMerge("{ \"foo_string\": \"baz\" }", "{ \"foo_string\": \"bar\" }", |
| 367 | "{ \"foo_string\": \"bar\" }"); |
| 368 | |
| 369 | JsonMerge("{ \"vector_foo_bool\": [ true, true, false ] }", |
| 370 | "{ \"vector_foo_bool\": [ false, true, true, false ] }", |
| 371 | "{ \"vector_foo_bool\": [ true, true, false, false, " |
| 372 | "true, true, false ] }"); |
| 373 | |
| 374 | JsonMerge("{ \"vector_foo_byte\": [ 9, 7, 1 ] }", |
| 375 | "{ \"vector_foo_byte\": [ -3, 1, 3, 2 ] }", |
| 376 | "{ \"vector_foo_byte\": [ 9, 7, 1, -3, 1, 3, 2 ] }"); |
| 377 | |
| 378 | JsonMerge("{ \"vector_foo_ubyte\": [ 9, 7, 1 ] }", |
| 379 | "{ \"vector_foo_ubyte\": [ 3, 1, 3, 2 ] }", |
| 380 | "{ \"vector_foo_ubyte\": [ 9, 7, 1, 3, 1, 3, 2 ] }"); |
| 381 | |
| 382 | JsonMerge("{ \"vector_foo_short\": [ 9, 7, 1 ] }", |
| 383 | "{ \"vector_foo_short\": [ -3, 1, 3, 2 ] }", |
| 384 | "{ \"vector_foo_short\": [ 9, 7, 1, -3, 1, 3, 2 ] }"); |
| 385 | |
| 386 | JsonMerge("{ \"vector_foo_ushort\": [ 9, 7, 1 ] }", |
| 387 | "{ \"vector_foo_ushort\": [ 3, 1, 3, 2 ] }", |
| 388 | "{ \"vector_foo_ushort\": [ 9, 7, 1, 3, 1, 3, 2 ] }"); |
| 389 | |
| 390 | JsonMerge("{ \"vector_foo_int\": [ 9, 7, 1 ] }", |
| 391 | "{ \"vector_foo_int\": [ -3, 1, 3, 2 ] }", |
| 392 | "{ \"vector_foo_int\": [ 9, 7, 1, -3, 1, 3, 2 ] }"); |
| 393 | |
| 394 | JsonMerge("{ \"vector_foo_uint\": [ 9, 7, 1 ] }", |
| 395 | "{ \"vector_foo_uint\": [ 3, 1, 3, 2 ] }", |
| 396 | "{ \"vector_foo_uint\": [ 9, 7, 1, 3, 1, 3, 2 ] }"); |
| 397 | |
| 398 | JsonMerge("{ \"vector_foo_long\": [ 9, 7, 1 ] }", |
| 399 | "{ \"vector_foo_long\": [ -3, 1, 3, 2 ] }", |
| 400 | "{ \"vector_foo_long\": [ 9, 7, 1, -3, 1, 3, 2 ] }"); |
| 401 | |
| 402 | JsonMerge("{ \"vector_foo_ulong\": [ 9, 7, 1 ] }", |
| 403 | "{ \"vector_foo_ulong\": [ 3, 1, 3, 2 ] }", |
| 404 | "{ \"vector_foo_ulong\": [ 9, 7, 1, 3, 1, 3, 2 ] }"); |
| 405 | |
| 406 | JsonMerge("{ \"vector_foo_float\": [ 9.0, 7.0, 1.0 ] }", |
| 407 | "{ \"vector_foo_float\": [ -3.0, 1.3, 3.0, 2.0 ] }", |
| 408 | "{ \"vector_foo_float\": [ 9.0, 7.0, 1.0, -3.0, 1.3, 3.0, 2.0 ] }"); |
| 409 | |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 410 | JsonMerge( |
| 411 | "{ \"vector_foo_string\": [ \"9\", \"7\", \"1 \" ] }", |
| 412 | "{ \"vector_foo_string\": [ \"31\", \"32\" ] }", |
| 413 | "{ \"vector_foo_string\": [ \"9\", \"7\", \"1 \", \"31\", \"32\" ] }"); |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | // Test nested messages, and arrays of nested messages. |
| 417 | TEST_F(FlatbufferMerge, NestedStruct) { |
| 418 | JsonMerge( |
| 419 | "{ \"single_application\": { \"name\": \"woot\" } }", |
| 420 | "{ \"single_application\": { \"name\": \"wow\", \"priority\": 7 } }", |
| 421 | "{ \"single_application\": { \"name\": \"wow\", \"priority\": 7 } }"); |
| 422 | |
| 423 | JsonMerge( |
| 424 | "{ \"single_application\": { \"name\": \"wow\", \"priority\": 7 } }", |
| 425 | "{ \"single_application\": { \"name\": \"woot\" } }", |
| 426 | "{ \"single_application\": { \"name\": \"woot\", \"priority\": 7 } }"); |
| 427 | |
| 428 | JsonMerge("{ \"apps\": [ { \"name\": \"woot\" }, { \"name\": \"wow\" } ] }", |
| 429 | "{ \"apps\": [ { \"name\": \"woo2\" }, { \"name\": \"wo3\" } ] }", |
| 430 | "{ \"apps\": [ { \"name\": \"woot\" }, { \"name\": \"wow\" }, { " |
| 431 | "\"name\": \"woo2\" }, { \"name\": \"wo3\" } ] }"); |
| 432 | } |
| 433 | |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 434 | // Test nested messages, and arrays of nested messages. |
| 435 | TEST(FlatbufferCopy, WholesaleCopy) { |
| 436 | flatbuffers::FlatBufferBuilder fbb_expected; |
| 437 | fbb_expected.ForceDefaults(true); |
| 438 | fbb_expected.DedupVtables(false); |
| 439 | |
| 440 | { |
| 441 | flatbuffers::Offset<flatbuffers::String> name1_offset = |
| 442 | fbb_expected.CreateString("wow"); |
| 443 | |
| 444 | std::vector<flatbuffers::Offset<Application>> application_offsets; |
| 445 | Application::Builder a1_builder(fbb_expected); |
| 446 | a1_builder.add_name(name1_offset); |
| 447 | a1_builder.add_priority(7); |
| 448 | a1_builder.add_long_thingy(0x2549713132LL); |
| 449 | application_offsets.emplace_back(a1_builder.Finish()); |
| 450 | |
| 451 | flatbuffers::Offset<flatbuffers::String> name2_offset = |
| 452 | fbb_expected.CreateString("foo"); |
| 453 | Application::Builder a2_builder(fbb_expected); |
| 454 | a2_builder.add_name(name2_offset); |
| 455 | a2_builder.add_priority(3); |
| 456 | a2_builder.add_long_thingy(9); |
| 457 | application_offsets.emplace_back(a2_builder.Finish()); |
| 458 | |
| 459 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>> |
| 460 | applications_offset = fbb_expected.CreateVector(application_offsets); |
| 461 | |
| 462 | Configuration::Builder configuration_builder(fbb_expected); |
| 463 | configuration_builder.add_apps(applications_offset); |
| 464 | fbb_expected.Finish(configuration_builder.Finish()); |
| 465 | } |
| 466 | |
| 467 | LOG(INFO) << "Initial alignment " << fbb_expected.GetBufferMinAlignment(); |
| 468 | |
| 469 | aos::FlatbufferDetachedBuffer<Configuration> expected(fbb_expected.Release()); |
| 470 | |
| 471 | LOG(INFO) << "Expected " |
| 472 | << absl::BytesToHexString(std::string_view( |
| 473 | reinterpret_cast<char *>(expected.span().data()), |
| 474 | expected.span().size())); |
| 475 | |
| 476 | aos::FlatbufferDetachedBuffer<Application> a1 = []() { |
| 477 | flatbuffers::FlatBufferBuilder fbb; |
| 478 | fbb.ForceDefaults(true); |
| 479 | |
| 480 | flatbuffers::Offset<flatbuffers::String> name1_offset = |
| 481 | fbb.CreateString("wow"); |
| 482 | |
| 483 | Application::Builder a1_builder(fbb); |
| 484 | a1_builder.add_name(name1_offset); |
| 485 | a1_builder.add_priority(7); |
| 486 | a1_builder.add_long_thingy(0x2549713132LL); |
| 487 | flatbuffers::Offset<Application> a1 = a1_builder.Finish(); |
| 488 | |
| 489 | fbb.Finish(a1); |
| 490 | return fbb.Release(); |
| 491 | }(); |
| 492 | |
| 493 | aos::FlatbufferDetachedBuffer<Application> a2 = |
| 494 | JsonToFlatbuffer<Application>(static_cast<const char *>( |
| 495 | "{ \"name\": \"foo\", \"priority\": 3, \"long_thingy\": 9 }")); |
| 496 | |
| 497 | aos::FlatbufferDetachedBuffer<Configuration> c1 = [&a1, &a2]() { |
| 498 | flatbuffers::FlatBufferBuilder fbb; |
| 499 | fbb.ForceDefaults(true); |
| 500 | |
| 501 | std::vector<flatbuffers::Offset<Application>> application_offsets; |
| 502 | application_offsets.emplace_back(BlindCopyFlatBuffer(a1, &fbb)); |
| 503 | application_offsets.emplace_back(BlindCopyFlatBuffer(a2, &fbb)); |
| 504 | |
| 505 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>> |
| 506 | applications_offset = fbb.CreateVector(application_offsets); |
| 507 | |
| 508 | Configuration::Builder configuration_builder(fbb); |
| 509 | configuration_builder.add_apps(applications_offset); |
| 510 | fbb.Finish(configuration_builder.Finish()); |
| 511 | |
| 512 | return fbb.Release(); |
| 513 | }(); |
| 514 | |
| 515 | LOG(INFO) << "Got " |
| 516 | << absl::BytesToHexString( |
| 517 | std::string_view(reinterpret_cast<char *>(c1.span().data()), |
| 518 | c1.span().size())); |
| 519 | |
| 520 | aos::FlatbufferDetachedBuffer<Configuration> c2 = [&a1, &a2]() { |
| 521 | flatbuffers::FlatBufferBuilder fbb; |
| 522 | fbb.ForceDefaults(true); |
| 523 | |
| 524 | std::vector<flatbuffers::Offset<Application>> application_offsets; |
| 525 | application_offsets.emplace_back(CopyFlatBuffer(&a1.message(), &fbb)); |
| 526 | application_offsets.emplace_back(CopyFlatBuffer(&a2.message(), &fbb)); |
| 527 | |
| 528 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>> |
| 529 | applications_offset = fbb.CreateVector(application_offsets); |
| 530 | |
| 531 | Configuration::Builder configuration_builder(fbb); |
| 532 | configuration_builder.add_apps(applications_offset); |
| 533 | fbb.Finish(configuration_builder.Finish()); |
| 534 | |
| 535 | return fbb.Release(); |
| 536 | }(); |
| 537 | |
| 538 | LOG(INFO) << "Got " |
| 539 | << absl::BytesToHexString( |
| 540 | std::string_view(reinterpret_cast<char *>(c2.span().data()), |
| 541 | c2.span().size())); |
| 542 | |
| 543 | ASSERT_TRUE(expected.Verify()); |
| 544 | ASSERT_TRUE(c1.Verify()); |
| 545 | ASSERT_TRUE(c2.Verify()); |
| 546 | |
| 547 | LOG(INFO) << FlatbufferToJson(expected); |
| 548 | LOG(INFO) << FlatbufferToJson(c1); |
| 549 | LOG(INFO) << FlatbufferToJson(c2); |
| 550 | EXPECT_EQ(FlatbufferToJson(expected), FlatbufferToJson(c1)); |
| 551 | EXPECT_EQ(FlatbufferToJson(expected), FlatbufferToJson(c2)); |
| 552 | } |
| 553 | |
Austin Schuh | 30d7db9 | 2020-01-26 16:45:47 -0800 | [diff] [blame] | 554 | // Tests a compare of 2 basic (different) messages. |
| 555 | TEST_F(FlatbufferMerge, CompareDifferent) { |
| 556 | aos::FlatbufferDetachedBuffer<Configuration> message1(JsonToFlatbuffer( |
| 557 | "{ \"single_application\": { \"name\": \"wow\", \"priority\": 7 } }", |
| 558 | ConfigurationTypeTable())); |
| 559 | aos::FlatbufferDetachedBuffer<Configuration> message2(JsonToFlatbuffer( |
| 560 | "{ \"single_application\": { \"name\": \"wow\", \"priority\": 8 } }", |
| 561 | ConfigurationTypeTable())); |
| 562 | |
| 563 | EXPECT_FALSE(CompareFlatBuffer(&message1.message(), &message2.message())); |
| 564 | } |
| 565 | |
Austin Schuh | 09d7ffa | 2019-10-03 23:43:34 -0700 | [diff] [blame] | 566 | // TODO(austin): enums |
| 567 | // TODO(austin): unions |
| 568 | // TODO(austin): struct |
| 569 | |
| 570 | } // namespace testing |
| 571 | } // namespace aos |