Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / mojo / public / tests / bindings / array_unittest.cc
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.
4
5 #include "mojo/public/bindings/lib/array.h"
6 #include "mojo/public/environment/environment.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8
9 namespace mojo {
10 namespace test {
11
12 // Tests that basic Array operations work.
13 TEST(ArrayTest, Basic) {
14   Environment env;
15
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());
19
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);
24     builder[i] = val;
25     EXPECT_EQ(val, builder.at(i));
26   }
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]);
31   }
32 }
33
34 // Tests that basic Array<bool> operations work, and that it's packed into 1
35 // bit per element.
36 TEST(ArrayTest, Bool) {
37   Environment env;
38
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());
42
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;
48     builder[i] = val;
49     EXPECT_EQ(val, builder.at(i));
50   }
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]);
55   }
56 }
57
58 // Tests that Array<Handle> supports transferring handles.
59 TEST(ArrayTest, Handle) {
60   Environment env;
61
62   AllocationScope scope;
63
64   ScopedMessagePipeHandle pipe0, pipe1;
65   CreateMessagePipe(&pipe0, &pipe1);
66
67   Array<MessagePipeHandle>::Builder handles_builder(2);
68   handles_builder[0] = pipe0.Pass();
69   handles_builder[1].reset(pipe1.release());
70
71   EXPECT_FALSE(pipe0.is_valid());
72   EXPECT_FALSE(pipe1.is_valid());
73
74   Array<MessagePipeHandle> handles = handles_builder.Finish();
75   EXPECT_TRUE(handles[0].is_valid());
76   EXPECT_TRUE(handles[1].is_valid());
77
78   pipe0 = handles[0].Pass();
79   EXPECT_TRUE(pipe0.is_valid());
80   EXPECT_FALSE(handles[0].is_valid());
81 }
82
83 }  // namespace test
84 }  // namespace mojo