1 // Copyright 2013 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/lib/array.h"
6 #include "mojo/public/environment/environment.h"
7 #include "testing/gtest/include/gtest/gtest.h"
12 // Tests that basic Array operations work.
13 TEST(ArrayTest, Basic) {
16 // 8 bytes for the array, with 8 bytes left over for elements.
17 internal::FixedBuffer buf(8 + 8*sizeof(char));
18 EXPECT_EQ(16u, buf.size());
20 Array<char>::Builder builder(8);
21 EXPECT_EQ(8u, builder.size());
22 for (size_t i = 0; i < builder.size(); ++i) {
23 char val = static_cast<char>(i*2);
25 EXPECT_EQ(val, builder.at(i));
27 Array<char> array = builder.Finish();
28 for (size_t i = 0; i < array.size(); ++i) {
29 char val = static_cast<char>(i) * 2;
30 EXPECT_EQ(val, array[i]);
34 // Tests that basic Array<bool> operations work, and that it's packed into 1
36 TEST(ArrayTest, Bool) {
39 // 8 bytes for the array header, with 8 bytes left over for elements.
40 internal::FixedBuffer buf(8 + 3);
41 EXPECT_EQ(16u, buf.size());
43 // An array of 64 bools can fit into 8 bytes.
44 Array<bool>::Builder builder(64);
45 EXPECT_EQ(64u, builder.size());
46 for (size_t i = 0; i < builder.size(); ++i) {
47 bool val = i % 3 == 0;
49 EXPECT_EQ(val, builder.at(i));
51 Array<bool> array = builder.Finish();
52 for (size_t i = 0; i < array.size(); ++i) {
53 bool val = i % 3 == 0;
54 EXPECT_EQ(val, array[i]);
58 // Tests that Array<Handle> supports transferring handles.
59 TEST(ArrayTest, Handle) {
62 AllocationScope scope;
64 ScopedMessagePipeHandle pipe0, pipe1;
65 CreateMessagePipe(&pipe0, &pipe1);
67 Array<MessagePipeHandle>::Builder handles_builder(2);
68 handles_builder[0] = pipe0.Pass();
69 handles_builder[1].reset(pipe1.release());
71 EXPECT_FALSE(pipe0.is_valid());
72 EXPECT_FALSE(pipe1.is_valid());
74 Array<MessagePipeHandle> handles = handles_builder.Finish();
75 EXPECT_TRUE(handles[0].is_valid());
76 EXPECT_TRUE(handles[1].is_valid());
78 pipe0 = handles[0].Pass();
79 EXPECT_TRUE(pipe0.is_valid());
80 EXPECT_FALSE(handles[0].is_valid());