1 // Copyright 2014 The Chromium 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.
5 #include "mojo/public/bindings/allocation_scope.h"
6 #include "mojo/public/bindings/array.h"
7 #include "mojo/public/bindings/lib/fixed_buffer.h"
8 #include "mojo/public/bindings/lib/scratch_buffer.h"
9 #include "mojo/public/environment/environment.h"
10 #include "testing/gtest/include/gtest/gtest.h"
16 // Tests that basic Array operations work.
17 TEST(ArrayTest, Basic) {
20 // 8 bytes for the array, with 8 bytes left over for elements.
21 internal::FixedBuffer buf(8 + 8*sizeof(char));
22 EXPECT_EQ(16u, buf.size());
24 Array<char>::Builder builder(8);
25 EXPECT_EQ(8u, builder.size());
26 for (size_t i = 0; i < builder.size(); ++i) {
27 char val = static_cast<char>(i*2);
29 EXPECT_EQ(val, builder.at(i));
31 Array<char> array = builder.Finish();
32 for (size_t i = 0; i < array.size(); ++i) {
33 char val = static_cast<char>(i) * 2;
34 EXPECT_EQ(val, array[i]);
38 // Tests that basic Array<bool> operations work, and that it's packed into 1
40 TEST(ArrayTest, Bool) {
43 // 8 bytes for the array header, with 8 bytes left over for elements.
44 internal::FixedBuffer buf(8 + 3);
45 EXPECT_EQ(16u, buf.size());
47 // An array of 64 bools can fit into 8 bytes.
48 Array<bool>::Builder builder(64);
49 EXPECT_EQ(64u, builder.size());
50 for (size_t i = 0; i < builder.size(); ++i) {
51 bool val = i % 3 == 0;
53 EXPECT_EQ(val, builder.at(i));
55 Array<bool> array = builder.Finish();
56 for (size_t i = 0; i < array.size(); ++i) {
57 bool val = i % 3 == 0;
58 EXPECT_EQ(val, array[i]);
62 // Tests that Array<Handle> supports transferring handles.
63 TEST(ArrayTest, Handle) {
66 AllocationScope scope;
68 ScopedMessagePipeHandle pipe0, pipe1;
69 CreateMessagePipe(&pipe0, &pipe1);
71 Array<MessagePipeHandle>::Builder handles_builder(2);
72 handles_builder[0] = pipe0.Pass();
73 handles_builder[1].reset(pipe1.release());
75 EXPECT_FALSE(pipe0.is_valid());
76 EXPECT_FALSE(pipe1.is_valid());
78 Array<MessagePipeHandle> handles = handles_builder.Finish();
79 EXPECT_TRUE(handles[0].is_valid());
80 EXPECT_TRUE(handles[1].is_valid());
82 pipe0 = handles[0].Pass();
83 EXPECT_TRUE(pipe0.is_valid());
84 EXPECT_FALSE(handles[0].is_valid());