| 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" | 13 #include "mojo/public/cpp/system/macros.h" |
| 14 | 14 |
| 15 #include <assert.h> | 15 #include <assert.h> |
| 16 #include <stdint.h> | 16 #include <stdint.h> |
| 17 #include <stdlib.h> | 17 #include <stdlib.h> |
| 18 #include <utility> |
| 18 | 19 |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 21 |
| 21 namespace mojo { | 22 namespace mojo { |
| 22 | 23 |
| 23 // The test for |MOJO_STATIC_CONST_MEMBER_DEFINITION| is really a compile/link | 24 // 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 | 25 // test. To test it fully would really require a header file and multiple .cc |
| 25 // files, but we'll just cursorily verify it. | 26 // files, but we'll just cursorily verify it. |
| 26 // | 27 // |
| 27 // This is defined outside of an anonymous namespace because | 28 // This is defined outside of an anonymous namespace because |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 // defined in a local scope, so define these globally. | 91 // defined in a local scope, so define these globally. |
| 91 class MoveOnlyInt { | 92 class MoveOnlyInt { |
| 92 MOJO_MOVE_ONLY_TYPE(MoveOnlyInt) | 93 MOJO_MOVE_ONLY_TYPE(MoveOnlyInt) |
| 93 | 94 |
| 94 public: | 95 public: |
| 95 MoveOnlyInt() : is_set_(false), value_() {} | 96 MoveOnlyInt() : is_set_(false), value_() {} |
| 96 explicit MoveOnlyInt(int value) : is_set_(true), value_(value) {} | 97 explicit MoveOnlyInt(int value) : is_set_(true), value_(value) {} |
| 97 ~MoveOnlyInt() {} | 98 ~MoveOnlyInt() {} |
| 98 | 99 |
| 99 // Move-only constructor and operator=. | 100 // Move-only constructor and operator=. |
| 100 MoveOnlyInt(MoveOnlyInt&& other) { *this = other.Pass(); } | 101 MoveOnlyInt(MoveOnlyInt&& other) { *this = std::move(other); } |
| 101 MoveOnlyInt& operator=(MoveOnlyInt&& other) { | 102 MoveOnlyInt& operator=(MoveOnlyInt&& other) { |
| 102 if (&other != this) { | 103 if (&other != this) { |
| 103 is_set_ = other.is_set_; | 104 is_set_ = other.is_set_; |
| 104 value_ = other.value_; | 105 value_ = other.value_; |
| 105 other.is_set_ = false; | 106 other.is_set_ = false; |
| 106 } | 107 } |
| 107 return *this; | 108 return *this; |
| 108 } | 109 } |
| 109 | 110 |
| 110 int value() const { | 111 int value() const { |
| 111 assert(is_set()); | 112 assert(is_set()); |
| 112 return value_; | 113 return value_; |
| 113 } | 114 } |
| 114 bool is_set() const { return is_set_; } | 115 bool is_set() const { return is_set_; } |
| 115 | 116 |
| 116 private: | 117 private: |
| 117 bool is_set_; | 118 bool is_set_; |
| 118 int value_; | 119 int value_; |
| 119 }; | 120 }; |
| 120 | 121 |
| 121 TEST(MacrosCppTest, MoveOnlyType) { | 122 TEST(MacrosCppTest, MoveOnlyType) { |
| 122 MoveOnlyInt x(123); | 123 MoveOnlyInt x(123); |
| 123 EXPECT_TRUE(x.is_set()); | 124 EXPECT_TRUE(x.is_set()); |
| 124 EXPECT_EQ(123, x.value()); | 125 EXPECT_EQ(123, x.value()); |
| 125 MoveOnlyInt y; | 126 MoveOnlyInt y; |
| 126 EXPECT_FALSE(y.is_set()); | 127 EXPECT_FALSE(y.is_set()); |
| 127 y = x.Pass(); | 128 y = std::move(x); |
| 128 EXPECT_FALSE(x.is_set()); | 129 EXPECT_FALSE(x.is_set()); |
| 129 EXPECT_TRUE(y.is_set()); | 130 EXPECT_TRUE(y.is_set()); |
| 130 EXPECT_EQ(123, y.value()); | 131 EXPECT_EQ(123, y.value()); |
| 131 MoveOnlyInt z(y.Pass()); | 132 MoveOnlyInt z(std::move(y)); |
| 132 EXPECT_FALSE(y.is_set()); | 133 EXPECT_FALSE(y.is_set()); |
| 133 EXPECT_TRUE(z.is_set()); | 134 EXPECT_TRUE(z.is_set()); |
| 134 EXPECT_EQ(123, z.value()); | 135 EXPECT_EQ(123, z.value()); |
| 135 z = z.Pass(); | |
| 136 EXPECT_TRUE(z.is_set()); | |
| 137 EXPECT_EQ(123, z.value()); | |
| 138 } | 136 } |
| 139 | 137 |
| 140 // Use it, to make sure things get linked in and to avoid any warnings about | 138 // Use it, to make sure things get linked in and to avoid any warnings about |
| 141 // unused things. | 139 // unused things. |
| 142 TEST(MacrosCppTest, StaticConstMemberDefinition) { | 140 TEST(MacrosCppTest, StaticConstMemberDefinition) { |
| 143 EXPECT_EQ(123, StructWithStaticConstMember::kStaticConstMember); | 141 EXPECT_EQ(123, StructWithStaticConstMember::kStaticConstMember); |
| 144 } | 142 } |
| 145 | 143 |
| 146 // The test for |ignore_result()| is also just a compilation test. (Note that | 144 // 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. | 145 // |MOJO_WARN_UNUSED_RESULT| can only be used in the prototype. |
| 148 int ReturnsIntYouMustUse() MOJO_WARN_UNUSED_RESULT; | 146 int ReturnsIntYouMustUse() MOJO_WARN_UNUSED_RESULT; |
| 149 | 147 |
| 150 int ReturnsIntYouMustUse() { | 148 int ReturnsIntYouMustUse() { |
| 151 return 123; | 149 return 123; |
| 152 } | 150 } |
| 153 | 151 |
| 154 TEST(MacrosCppTest, IgnoreResult) { | 152 TEST(MacrosCppTest, IgnoreResult) { |
| 155 ignore_result(ReturnsIntYouMustUse()); | 153 ignore_result(ReturnsIntYouMustUse()); |
| 156 } | 154 } |
| 157 | 155 |
| 158 } // namespace | 156 } // namespace |
| 159 } // namespace mojo | 157 } // namespace mojo |
| OLD | NEW |