• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/cpp/bindings/array.h"
6 
7 #include "mojo/public/cpp/bindings/lib/serialization.h"
8 #include "mojo/public/cpp/bindings/tests/array_common_test.h"
9 #include "mojo/public/cpp/bindings/tests/container_test_util.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 
12 namespace mojo {
13 namespace test {
14 namespace {
15 
16 using ArrayTest = testing::Test;
17 
ARRAY_COMMON_TEST(Array,NullAndEmpty)18 ARRAY_COMMON_TEST(Array, NullAndEmpty)
19 ARRAY_COMMON_TEST(Array, Basic)
20 ARRAY_COMMON_TEST(Array, Bool)
21 ARRAY_COMMON_TEST(Array, Handle)
22 ARRAY_COMMON_TEST(Array, HandlesAreClosed)
23 ARRAY_COMMON_TEST(Array, Clone)
24 ARRAY_COMMON_TEST(Array, Serialization_ArrayOfPOD)
25 ARRAY_COMMON_TEST(Array, Serialization_EmptyArrayOfPOD)
26 ARRAY_COMMON_TEST(Array, Serialization_ArrayOfArrayOfPOD)
27 ARRAY_COMMON_TEST(Array, Serialization_ArrayOfBool)
28 ARRAY_COMMON_TEST(Array, Serialization_ArrayOfString)
29 ARRAY_COMMON_TEST(Array, Resize_Copyable)
30 ARRAY_COMMON_TEST(Array, Resize_MoveOnly)
31 
32 TEST_F(ArrayTest, PushBack_Copyable) {
33   ASSERT_EQ(0u, CopyableType::num_instances());
34   Array<CopyableType> array(2);
35   array = nullptr;
36   std::vector<CopyableType*> value_ptrs;
37   size_t capacity = array.storage().capacity();
38   for (size_t i = 0; i < capacity; i++) {
39     CopyableType value;
40     value_ptrs.push_back(value.ptr());
41     array.push_back(value);
42     ASSERT_EQ(i + 1, array.size());
43     ASSERT_EQ(i + 1, value_ptrs.size());
44     EXPECT_EQ(array.size() + 1, CopyableType::num_instances());
45     EXPECT_TRUE(array[i].copied());
46     EXPECT_EQ(value_ptrs[i], array[i].ptr());
47     array[i].ResetCopied();
48     EXPECT_TRUE(array);
49   }
50   {
51     CopyableType value;
52     value_ptrs.push_back(value.ptr());
53     array.push_back(value);
54     EXPECT_EQ(array.size() + 1, CopyableType::num_instances());
55   }
56   ASSERT_EQ(capacity + 1, array.size());
57   EXPECT_EQ(array.size(), CopyableType::num_instances());
58 
59   for (size_t i = 0; i < array.size(); i++) {
60     EXPECT_TRUE(array[i].copied());
61     EXPECT_EQ(value_ptrs[i], array[i].ptr());
62   }
63   array = nullptr;
64   EXPECT_EQ(0u, CopyableType::num_instances());
65 }
66 
TEST_F(ArrayTest,PushBack_MoveOnly)67 TEST_F(ArrayTest, PushBack_MoveOnly) {
68   ASSERT_EQ(0u, MoveOnlyType::num_instances());
69   Array<MoveOnlyType> array(2);
70   array = nullptr;
71   std::vector<MoveOnlyType*> value_ptrs;
72   size_t capacity = array.storage().capacity();
73   for (size_t i = 0; i < capacity; i++) {
74     MoveOnlyType value;
75     value_ptrs.push_back(value.ptr());
76     array.push_back(std::move(value));
77     ASSERT_EQ(i + 1, array.size());
78     ASSERT_EQ(i + 1, value_ptrs.size());
79     EXPECT_EQ(array.size() + 1, MoveOnlyType::num_instances());
80     EXPECT_TRUE(array[i].moved());
81     EXPECT_EQ(value_ptrs[i], array[i].ptr());
82     array[i].ResetMoved();
83     EXPECT_TRUE(array);
84   }
85   {
86     MoveOnlyType value;
87     value_ptrs.push_back(value.ptr());
88     array.push_back(std::move(value));
89     EXPECT_EQ(array.size() + 1, MoveOnlyType::num_instances());
90   }
91   ASSERT_EQ(capacity + 1, array.size());
92   EXPECT_EQ(array.size(), MoveOnlyType::num_instances());
93 
94   for (size_t i = 0; i < array.size(); i++) {
95     EXPECT_TRUE(array[i].moved());
96     EXPECT_EQ(value_ptrs[i], array[i].ptr());
97   }
98   array = nullptr;
99   EXPECT_EQ(0u, MoveOnlyType::num_instances());
100 }
101 
TEST_F(ArrayTest,MoveFromAndToSTLVector_Copyable)102 TEST_F(ArrayTest, MoveFromAndToSTLVector_Copyable) {
103   std::vector<CopyableType> vec1(1);
104   Array<CopyableType> arr(std::move(vec1));
105   ASSERT_EQ(1u, arr.size());
106   ASSERT_FALSE(arr[0].copied());
107 
108   std::vector<CopyableType> vec2(arr.PassStorage());
109   ASSERT_EQ(1u, vec2.size());
110   ASSERT_FALSE(vec2[0].copied());
111 
112   ASSERT_EQ(0u, arr.size());
113   ASSERT_TRUE(arr.is_null());
114 }
115 
TEST_F(ArrayTest,MoveFromAndToSTLVector_MoveOnly)116 TEST_F(ArrayTest, MoveFromAndToSTLVector_MoveOnly) {
117   std::vector<MoveOnlyType> vec1(1);
118   Array<MoveOnlyType> arr(std::move(vec1));
119 
120   ASSERT_EQ(1u, arr.size());
121 
122   std::vector<MoveOnlyType> vec2(arr.PassStorage());
123   ASSERT_EQ(1u, vec2.size());
124 
125   ASSERT_EQ(0u, arr.size());
126   ASSERT_TRUE(arr.is_null());
127 }
128 
129 }  // namespace
130 }  // namespace test
131 }  // namespace mojo
132