OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 // This file tests the C++ Mojo system macros and consists of "positive" tests, | 5 // This file tests the C++ Mojo system macros and consists of "positive" tests, |
6 // i.e., those verifying that things work (without compile errors, or even | 6 // i.e., those verifying that things work (without compile errors, or even |
7 // warnings if warnings are treated as errors). | 7 // warnings if warnings are treated as errors). |
8 // TODO(vtl): Maybe rename "MacrosCppTest" -> "MacrosTest" if/when this gets | 8 // TODO(vtl): Maybe rename "MacrosCppTest" -> "MacrosTest" if/when this gets |
9 // compiled into a different binary from the C API tests. | 9 // compiled into a different binary from the C API tests. |
10 // TODO(vtl): Fix no-compile tests (which are all disabled; crbug.com/105388) | 10 // TODO(vtl): Fix no-compile tests (which are all disabled; crbug.com/105388) |
11 // and write some "negative" tests. | 11 // and write some "negative" tests. |
12 | 12 |
13 #include "mojo/public/cpp/system/macros.h" | |
14 | |
15 #include <assert.h> | 13 #include <assert.h> |
16 #include <stdint.h> | 14 #include <stdint.h> |
17 #include <stdlib.h> | 15 #include <stdlib.h> |
| 16 #include <utility> |
18 | 17 |
| 18 #include "mojo/public/cpp/system/macros.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
20 | 20 |
21 namespace mojo { | 21 namespace mojo { |
22 | 22 |
23 // The test for |MOJO_STATIC_CONST_MEMBER_DEFINITION| is really a compile/link | 23 // The test for |MOJO_STATIC_CONST_MEMBER_DEFINITION| is really a compile/link |
24 // test. To test it fully would really require a header file and multiple .cc | 24 // test. To test it fully would really require a header file and multiple .cc |
25 // files, but we'll just cursorily verify it. | 25 // files, but we'll just cursorily verify it. |
26 // | 26 // |
27 // This is defined outside of an anonymous namespace because | 27 // This is defined outside of an anonymous namespace because |
28 // MOJO_STATIC_CONST_MEMBER_DEFINITION may not be used on internal symbols. | 28 // MOJO_STATIC_CONST_MEMBER_DEFINITION may not be used on internal symbols. |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 // defined in a local scope, so define these globally. | 90 // defined in a local scope, so define these globally. |
91 class MoveOnlyInt { | 91 class MoveOnlyInt { |
92 MOJO_MOVE_ONLY_TYPE(MoveOnlyInt) | 92 MOJO_MOVE_ONLY_TYPE(MoveOnlyInt) |
93 | 93 |
94 public: | 94 public: |
95 MoveOnlyInt() : is_set_(false), value_() {} | 95 MoveOnlyInt() : is_set_(false), value_() {} |
96 explicit MoveOnlyInt(int value) : is_set_(true), value_(value) {} | 96 explicit MoveOnlyInt(int value) : is_set_(true), value_(value) {} |
97 ~MoveOnlyInt() {} | 97 ~MoveOnlyInt() {} |
98 | 98 |
99 // Move-only constructor and operator=. | 99 // Move-only constructor and operator=. |
100 MoveOnlyInt(MoveOnlyInt&& other) { *this = other.Pass(); } | 100 MoveOnlyInt(MoveOnlyInt&& other) { *this = std::move(other); } |
101 MoveOnlyInt& operator=(MoveOnlyInt&& other) { | 101 MoveOnlyInt& operator=(MoveOnlyInt&& other) { |
102 if (&other != this) { | 102 if (&other != this) { |
103 is_set_ = other.is_set_; | 103 is_set_ = other.is_set_; |
104 value_ = other.value_; | 104 value_ = other.value_; |
105 other.is_set_ = false; | 105 other.is_set_ = false; |
106 } | 106 } |
107 return *this; | 107 return *this; |
108 } | 108 } |
109 | 109 |
110 int value() const { | 110 int value() const { |
111 assert(is_set()); | 111 assert(is_set()); |
112 return value_; | 112 return value_; |
113 } | 113 } |
114 bool is_set() const { return is_set_; } | 114 bool is_set() const { return is_set_; } |
115 | 115 |
116 private: | 116 private: |
117 bool is_set_; | 117 bool is_set_; |
118 int value_; | 118 int value_; |
119 }; | 119 }; |
120 | 120 |
121 TEST(MacrosCppTest, MoveOnlyType) { | 121 TEST(MacrosCppTest, MoveOnlyType) { |
122 MoveOnlyInt x(123); | 122 MoveOnlyInt x(123); |
123 EXPECT_TRUE(x.is_set()); | 123 EXPECT_TRUE(x.is_set()); |
124 EXPECT_EQ(123, x.value()); | 124 EXPECT_EQ(123, x.value()); |
125 MoveOnlyInt y; | 125 MoveOnlyInt y; |
126 EXPECT_FALSE(y.is_set()); | 126 EXPECT_FALSE(y.is_set()); |
127 y = x.Pass(); | 127 y = std::move(x); |
128 EXPECT_FALSE(x.is_set()); | 128 EXPECT_FALSE(x.is_set()); |
129 EXPECT_TRUE(y.is_set()); | 129 EXPECT_TRUE(y.is_set()); |
130 EXPECT_EQ(123, y.value()); | 130 EXPECT_EQ(123, y.value()); |
131 MoveOnlyInt z(y.Pass()); | 131 MoveOnlyInt z(std::move(y)); |
132 EXPECT_FALSE(y.is_set()); | 132 EXPECT_FALSE(y.is_set()); |
133 EXPECT_TRUE(z.is_set()); | 133 EXPECT_TRUE(z.is_set()); |
134 EXPECT_EQ(123, z.value()); | 134 EXPECT_EQ(123, z.value()); |
135 z = z.Pass(); | |
136 EXPECT_TRUE(z.is_set()); | |
137 EXPECT_EQ(123, z.value()); | |
138 } | 135 } |
139 | 136 |
140 // Use it, to make sure things get linked in and to avoid any warnings about | 137 // Use it, to make sure things get linked in and to avoid any warnings about |
141 // unused things. | 138 // unused things. |
142 TEST(MacrosCppTest, StaticConstMemberDefinition) { | 139 TEST(MacrosCppTest, StaticConstMemberDefinition) { |
143 EXPECT_EQ(123, StructWithStaticConstMember::kStaticConstMember); | 140 EXPECT_EQ(123, StructWithStaticConstMember::kStaticConstMember); |
144 } | 141 } |
145 | 142 |
146 // The test for |ignore_result()| is also just a compilation test. (Note that | 143 // The test for |ignore_result()| is also just a compilation test. (Note that |
147 // |MOJO_WARN_UNUSED_RESULT| can only be used in the prototype. | 144 // |MOJO_WARN_UNUSED_RESULT| can only be used in the prototype. |
148 int ReturnsIntYouMustUse() MOJO_WARN_UNUSED_RESULT; | 145 int ReturnsIntYouMustUse() MOJO_WARN_UNUSED_RESULT; |
149 | 146 |
150 int ReturnsIntYouMustUse() { | 147 int ReturnsIntYouMustUse() { |
151 return 123; | 148 return 123; |
152 } | 149 } |
153 | 150 |
154 TEST(MacrosCppTest, IgnoreResult) { | 151 TEST(MacrosCppTest, IgnoreResult) { |
155 ignore_result(ReturnsIntYouMustUse()); | 152 ignore_result(ReturnsIntYouMustUse()); |
156 } | 153 } |
157 | 154 |
158 } // namespace | 155 } // namespace |
159 } // namespace mojo | 156 } // namespace mojo |
OLD | NEW |