Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / mojo / public / bindings / tests / array_unittest.cc
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.
4
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"
11
12 namespace mojo {
13 namespace test {
14 namespace {
15
16 // Tests that basic Array operations work.
17 TEST(ArrayTest, Basic) {
18   Environment env;
19
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());
23
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);
28     builder[i] = val;
29     EXPECT_EQ(val, builder.at(i));
30   }
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]);
35   }
36 }
37
38 // Tests that basic Array<bool> operations work, and that it's packed into 1
39 // bit per element.
40 TEST(ArrayTest, Bool) {
41   Environment env;
42
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());
46
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;
52     builder[i] = val;
53     EXPECT_EQ(val, builder.at(i));
54   }
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]);
59   }
60 }
61
62 // Tests that Array<Handle> supports transferring handles.
63 TEST(ArrayTest, Handle) {
64   Environment env;
65
66   AllocationScope scope;
67
68   ScopedMessagePipeHandle pipe0, pipe1;
69   CreateMessagePipe(&pipe0, &pipe1);
70
71   Array<MessagePipeHandle>::Builder handles_builder(2);
72   handles_builder[0] = pipe0.Pass();
73   handles_builder[1].reset(pipe1.release());
74
75   EXPECT_FALSE(pipe0.is_valid());
76   EXPECT_FALSE(pipe1.is_valid());
77
78   Array<MessagePipeHandle> handles = handles_builder.Finish();
79   EXPECT_TRUE(handles[0].is_valid());
80   EXPECT_TRUE(handles[1].is_valid());
81
82   pipe0 = handles[0].Pass();
83   EXPECT_TRUE(pipe0.is_valid());
84   EXPECT_FALSE(handles[0].is_valid());
85 }
86
87 }  // namespace
88 }  // namespace test
89 }  // namespace mojo