summaryrefslogtreecommitdiff
path: root/src/flatbuffer_builder_test.cc
diff options
context:
space:
mode:
authorBert Belder <bertbelder@gmail.com>2018-07-09 03:35:34 +0200
committerBert Belder <bertbelder@gmail.com>2018-07-12 21:26:38 +0200
commit24b0e91d8096f84a114f662e3eb42c83d0d3878d (patch)
treeacd9ae306618276020e75450c064ead463e64387 /src/flatbuffer_builder_test.cc
parentbbcd4c8dd33121868d82123a3d36e3df282af45f (diff)
Move buffers between V8 and native
* send()/recv() now operate on TypedArrays rather than ArrayBuffers. * Remove a copy (through ArrayBuffer.slice()) from the send path. * Remove a copy (through v8::ArrayBuffer::New()) from the return path. * After moving a buffer from JS to native, the ArrayBuffer object and it's views are made inaccessible ('neutered'). * `struct deno_buf` now holds two [ptr, length] tuples, one for the actual memory allocation, and one for the logical data contained therein. This is necessary because flatbuffers fills it's buffer bottom-up, so the serialized blob doesn't start at beginning of the buffer, but somewhere in the middle.
Diffstat (limited to 'src/flatbuffer_builder_test.cc')
-rw-r--r--src/flatbuffer_builder_test.cc78
1 files changed, 78 insertions, 0 deletions
diff --git a/src/flatbuffer_builder_test.cc b/src/flatbuffer_builder_test.cc
new file mode 100644
index 000000000..cfa12f0a0
--- /dev/null
+++ b/src/flatbuffer_builder_test.cc
@@ -0,0 +1,78 @@
+// Copyright 2018 Bert Belder <bertbelder@gmail.com>
+// All rights reserved. MIT License.
+
+#include <stdint.h>
+
+#include "testing/gtest/include/gtest/gtest.h"
+
+#include "deno.h"
+#include "flatbuffer_builder.h"
+
+template <typename T, std::size_t N>
+constexpr std::size_t countof(T const (&)[N]) noexcept {
+ return N;
+}
+
+TEST(FlatBufferBuilderTest, ExportBuf) {
+ const uint32_t nums[] = {1, 2, 3};
+ const char str[] = "hello mars";
+ deno_buf nums_buf;
+ deno_buf str_buf;
+ // Use scope so builder gets destroyed after building buffers.
+ {
+ deno::FlatBufferBuilder builder;
+ // Build first flatbuffer.
+ auto nums_fb = builder.CreateVector(nums, countof(nums));
+ builder.Finish(nums_fb);
+ nums_buf = builder.ExportBuf();
+ // Reset builder.
+ builder.Reset();
+ // Build second flatbuffer using the same builder.
+ auto str_fb = builder.CreateString(str);
+ builder.Finish(str_fb);
+ str_buf = builder.ExportBuf();
+ }
+ // Allocations should be different.
+ EXPECT_NE(nums_buf.alloc_ptr, str_buf.alloc_ptr);
+ // Logical buffer data should be contained inside their allocations.
+ EXPECT_GE(nums_buf.data_ptr, nums_buf.alloc_ptr);
+ EXPECT_LE(nums_buf.data_ptr + nums_buf.data_len,
+ nums_buf.alloc_ptr + nums_buf.alloc_len);
+ EXPECT_GE(str_buf.data_ptr, str_buf.alloc_ptr);
+ EXPECT_LE(str_buf.data_ptr + str_buf.data_len,
+ str_buf.alloc_ptr + str_buf.alloc_len);
+ // Since there is no way to parse these buffers without generating code,
+ // just check whether the data is contained in the raw content.
+ // Both the numbers vector and the string start at offset 8 in the flatbuffer.
+ auto nums_data = reinterpret_cast<uint32_t*>(nums_buf.data_ptr + 8);
+ for (size_t i = 0; i < countof(nums); i++) {
+ EXPECT_EQ(nums_data[i], nums[i]);
+ }
+ auto str_data = str_buf.data_ptr + 8;
+ for (size_t i = 0; i < countof(str); i++) {
+ EXPECT_EQ(str_data[i], str[i]);
+ }
+}
+
+TEST(FlatBufferBuilderTest, CanGrowBuffer) {
+ static const size_t kSmallInitialSize = 32;
+ static const char zeroes[1024] = {0};
+ {
+ // Create buffer with small initial size.
+ deno::FlatBufferBuilder builder(kSmallInitialSize);
+ // Write 1 byte and export buffer.
+ builder.Finish(builder.CreateVector(zeroes, 1));
+ auto buf = builder.ExportBuf();
+ // Exported buffer should have initial size.
+ EXPECT_EQ(buf.alloc_len, kSmallInitialSize);
+ }
+ {
+ // Create buffer with small initial size.
+ deno::FlatBufferBuilder builder(kSmallInitialSize);
+ // Write 1024 bytes and export buffer.
+ builder.Finish(builder.CreateVector(zeroes, countof(zeroes)));
+ auto buf = builder.ExportBuf();
+ // Exported buffer have grown.
+ EXPECT_GT(buf.alloc_len, kSmallInitialSize);
+ }
+}