OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/public/bindings/lib/array.h" | 5 #include "mojo/public/bindings/lib/array.h" |
6 #include "mojo/public/tests/bindings/simple_bindings_support.h" | 6 #include "mojo/public/environment/environment.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 | 8 |
9 namespace mojo { | 9 namespace mojo { |
10 namespace test { | 10 namespace test { |
11 | 11 |
12 // Tests that basic Array operations work. | 12 // Tests that basic Array operations work. |
13 TEST(ArrayTest, Basic) { | 13 TEST(ArrayTest, Basic) { |
14 SimpleBindingsSupport bindings_support; | 14 Environment env; |
15 | 15 |
16 // 8 bytes for the array, with 8 bytes left over for elements. | 16 // 8 bytes for the array, with 8 bytes left over for elements. |
17 internal::FixedBuffer buf(8 + 8*sizeof(char)); | 17 internal::FixedBuffer buf(8 + 8*sizeof(char)); |
18 EXPECT_EQ(16u, buf.size()); | 18 EXPECT_EQ(16u, buf.size()); |
19 | 19 |
20 Array<char>::Builder builder(8); | 20 Array<char>::Builder builder(8); |
21 EXPECT_EQ(8u, builder.size()); | 21 EXPECT_EQ(8u, builder.size()); |
22 for (size_t i = 0; i < builder.size(); ++i) { | 22 for (size_t i = 0; i < builder.size(); ++i) { |
23 char val = static_cast<char>(i*2); | 23 char val = static_cast<char>(i*2); |
24 builder[i] = val; | 24 builder[i] = val; |
25 EXPECT_EQ(val, builder.at(i)); | 25 EXPECT_EQ(val, builder.at(i)); |
26 } | 26 } |
27 Array<char> array = builder.Finish(); | 27 Array<char> array = builder.Finish(); |
28 for (size_t i = 0; i < array.size(); ++i) { | 28 for (size_t i = 0; i < array.size(); ++i) { |
29 char val = static_cast<char>(i) * 2; | 29 char val = static_cast<char>(i) * 2; |
30 EXPECT_EQ(val, array[i]); | 30 EXPECT_EQ(val, array[i]); |
31 } | 31 } |
32 } | 32 } |
33 | 33 |
34 // Tests that basic Array<bool> operations work, and that it's packed into 1 | 34 // Tests that basic Array<bool> operations work, and that it's packed into 1 |
35 // bit per element. | 35 // bit per element. |
36 TEST(ArrayTest, Bool) { | 36 TEST(ArrayTest, Bool) { |
37 SimpleBindingsSupport bindings_support; | 37 Environment env; |
38 | 38 |
39 // 8 bytes for the array header, with 8 bytes left over for elements. | 39 // 8 bytes for the array header, with 8 bytes left over for elements. |
40 internal::FixedBuffer buf(8 + 3); | 40 internal::FixedBuffer buf(8 + 3); |
41 EXPECT_EQ(16u, buf.size()); | 41 EXPECT_EQ(16u, buf.size()); |
42 | 42 |
43 // An array of 64 bools can fit into 8 bytes. | 43 // An array of 64 bools can fit into 8 bytes. |
44 Array<bool>::Builder builder(64); | 44 Array<bool>::Builder builder(64); |
45 EXPECT_EQ(64u, builder.size()); | 45 EXPECT_EQ(64u, builder.size()); |
46 for (size_t i = 0; i < builder.size(); ++i) { | 46 for (size_t i = 0; i < builder.size(); ++i) { |
47 bool val = i % 3 == 0; | 47 bool val = i % 3 == 0; |
48 builder[i] = val; | 48 builder[i] = val; |
49 EXPECT_EQ(val, builder.at(i)); | 49 EXPECT_EQ(val, builder.at(i)); |
50 } | 50 } |
51 Array<bool> array = builder.Finish(); | 51 Array<bool> array = builder.Finish(); |
52 for (size_t i = 0; i < array.size(); ++i) { | 52 for (size_t i = 0; i < array.size(); ++i) { |
53 bool val = i % 3 == 0; | 53 bool val = i % 3 == 0; |
54 EXPECT_EQ(val, array[i]); | 54 EXPECT_EQ(val, array[i]); |
55 } | 55 } |
56 } | 56 } |
57 | 57 |
58 // Tests that Array<Handle> supports transferring handles. | 58 // Tests that Array<Handle> supports transferring handles. |
59 TEST(ArrayTest, Handle) { | 59 TEST(ArrayTest, Handle) { |
60 SimpleBindingsSupport bindings_support; | 60 Environment env; |
61 | 61 |
62 AllocationScope scope; | 62 AllocationScope scope; |
63 | 63 |
64 ScopedMessagePipeHandle pipe0, pipe1; | 64 ScopedMessagePipeHandle pipe0, pipe1; |
65 CreateMessagePipe(&pipe0, &pipe1); | 65 CreateMessagePipe(&pipe0, &pipe1); |
66 | 66 |
67 Array<MessagePipeHandle>::Builder handles_builder(2); | 67 Array<MessagePipeHandle>::Builder handles_builder(2); |
68 handles_builder[0] = pipe0.Pass(); | 68 handles_builder[0] = pipe0.Pass(); |
69 handles_builder[1].reset(pipe1.release()); | 69 handles_builder[1].reset(pipe1.release()); |
70 | 70 |
71 EXPECT_FALSE(pipe0.is_valid()); | 71 EXPECT_FALSE(pipe0.is_valid()); |
72 EXPECT_FALSE(pipe1.is_valid()); | 72 EXPECT_FALSE(pipe1.is_valid()); |
73 | 73 |
74 Array<MessagePipeHandle> handles = handles_builder.Finish(); | 74 Array<MessagePipeHandle> handles = handles_builder.Finish(); |
75 EXPECT_TRUE(handles[0].is_valid()); | 75 EXPECT_TRUE(handles[0].is_valid()); |
76 EXPECT_TRUE(handles[1].is_valid()); | 76 EXPECT_TRUE(handles[1].is_valid()); |
77 | 77 |
78 pipe0 = handles[0].Pass(); | 78 pipe0 = handles[0].Pass(); |
79 EXPECT_TRUE(pipe0.is_valid()); | 79 EXPECT_TRUE(pipe0.is_valid()); |
80 EXPECT_FALSE(handles[0].is_valid()); | 80 EXPECT_FALSE(handles[0].is_valid()); |
81 } | 81 } |
82 | 82 |
83 } // namespace test | 83 } // namespace test |
84 } // namespace mojo | 84 } // namespace mojo |
OLD | NEW |