| 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 | |
| 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) | 8 // TODO(vtl): Fix no-compile tests (which are all disabled; crbug.com/105388) |
| 11 // and write some "negative" tests. | 9 // and write some "negative" tests. |
| 12 | 10 |
| 13 #include "mojo/public/cpp/system/macros.h" | 11 #include "mojo/public/cpp/system/macros.h" |
| 14 | 12 |
| 15 #include <assert.h> | 13 #include <assert.h> |
| 16 #include <stdint.h> | 14 #include <stdint.h> |
| 17 #include <stdlib.h> | 15 #include <stdlib.h> |
| 18 | 16 |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 18 |
| 21 namespace mojo { | 19 namespace mojo { |
| 22 namespace { | 20 namespace { |
| 23 | 21 |
| 24 // Note: MSVS is very strict (and arguably buggy) about warnings for classes | 22 // Note: MSVS is very strict (and arguably buggy) about warnings for classes |
| 25 // defined in a local scope, so define these globally. | 23 // defined in a local scope, so define these globally. |
| 26 struct TestOverrideBaseClass { | 24 struct TestOverrideBaseClass { |
| 27 virtual ~TestOverrideBaseClass() {} | 25 virtual ~TestOverrideBaseClass() {} |
| 28 virtual void ToBeOverridden() {} | 26 virtual void ToBeOverridden() {} |
| 29 virtual void AlsoToBeOverridden() = 0; | 27 virtual void AlsoToBeOverridden() = 0; |
| 30 }; | 28 }; |
| 31 | 29 |
| 32 struct TestOverrideSubclass : public TestOverrideBaseClass { | 30 struct TestOverrideSubclass : public TestOverrideBaseClass { |
| 33 ~TestOverrideSubclass() override {} | 31 ~TestOverrideSubclass() override {} |
| 34 void ToBeOverridden() override {} | 32 void ToBeOverridden() override {} |
| 35 void AlsoToBeOverridden() override {} | 33 void AlsoToBeOverridden() override {} |
| 36 }; | 34 }; |
| 37 | 35 |
| 38 TEST(MacrosCppTest, Override) { | 36 TEST(MacrosTest, Override) { |
| 39 TestOverrideSubclass x; | 37 TestOverrideSubclass x; |
| 40 x.ToBeOverridden(); | 38 x.ToBeOverridden(); |
| 41 x.AlsoToBeOverridden(); | 39 x.AlsoToBeOverridden(); |
| 42 } | 40 } |
| 43 | 41 |
| 44 // Note: MSVS is very strict (and arguably buggy) about warnings for classes | 42 // Note: MSVS is very strict (and arguably buggy) about warnings for classes |
| 45 // defined in a local scope, so define these globally. | 43 // defined in a local scope, so define these globally. |
| 46 class TestDisallowCopyAndAssignClass { | 44 class TestDisallowCopyAndAssignClass { |
| 47 public: | 45 public: |
| 48 TestDisallowCopyAndAssignClass() {} | 46 TestDisallowCopyAndAssignClass() {} |
| 49 explicit TestDisallowCopyAndAssignClass(int) {} | 47 explicit TestDisallowCopyAndAssignClass(int) {} |
| 50 void NoOp() {} | 48 void NoOp() {} |
| 51 | 49 |
| 52 private: | 50 private: |
| 53 MOJO_DISALLOW_COPY_AND_ASSIGN(TestDisallowCopyAndAssignClass); | 51 MOJO_DISALLOW_COPY_AND_ASSIGN(TestDisallowCopyAndAssignClass); |
| 54 }; | 52 }; |
| 55 | 53 |
| 56 TEST(MacrosCppTest, DisallowCopyAndAssign) { | 54 TEST(MacrosTest, DisallowCopyAndAssign) { |
| 57 TestDisallowCopyAndAssignClass x; | 55 TestDisallowCopyAndAssignClass x; |
| 58 x.NoOp(); | 56 x.NoOp(); |
| 59 TestDisallowCopyAndAssignClass y(789); | 57 TestDisallowCopyAndAssignClass y(789); |
| 60 y.NoOp(); | 58 y.NoOp(); |
| 61 } | 59 } |
| 62 | 60 |
| 63 // Test that |MOJO_ARRAYSIZE()| works in a |static_assert()|. | 61 // Test that |MOJO_ARRAYSIZE()| works in a |static_assert()|. |
| 64 const int kGlobalArray[5] = {1, 2, 3, 4, 5}; | 62 const int kGlobalArray[5] = {1, 2, 3, 4, 5}; |
| 65 static_assert(MOJO_ARRAYSIZE(kGlobalArray) == 5u, | 63 static_assert(MOJO_ARRAYSIZE(kGlobalArray) == 5u, |
| 66 "MOJO_ARRAY_SIZE() failed in static_assert()"); | 64 "MOJO_ARRAY_SIZE() failed in static_assert()"); |
| 67 | 65 |
| 68 TEST(MacrosCppTest, ArraySize) { | 66 TEST(MacrosTest, ArraySize) { |
| 69 double local_array[4] = {6.7, 7.8, 8.9, 9.0}; | 67 double local_array[4] = {6.7, 7.8, 8.9, 9.0}; |
| 70 // MSVS considers this local variable unused since MOJO_ARRAYSIZE only takes | 68 // MSVS considers this local variable unused since MOJO_ARRAYSIZE only takes |
| 71 // the size of the type of the local and not the values itself. | 69 // the size of the type of the local and not the values itself. |
| 72 MOJO_ALLOW_UNUSED_LOCAL(local_array); | 70 MOJO_ALLOW_UNUSED_LOCAL(local_array); |
| 73 EXPECT_EQ(4u, MOJO_ARRAYSIZE(local_array)); | 71 EXPECT_EQ(4u, MOJO_ARRAYSIZE(local_array)); |
| 74 | 72 |
| 75 // Prevent gcc unneeded-internal-declaration warning. | 73 // Prevent gcc unneeded-internal-declaration warning. |
| 76 MOJO_ALLOW_UNUSED_LOCAL(kGlobalArray); | 74 MOJO_ALLOW_UNUSED_LOCAL(kGlobalArray); |
| 77 } | 75 } |
| 78 | 76 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 101 } | 99 } |
| 102 bool is_set() const { return is_set_; } | 100 bool is_set() const { return is_set_; } |
| 103 | 101 |
| 104 private: | 102 private: |
| 105 bool is_set_; | 103 bool is_set_; |
| 106 int value_; | 104 int value_; |
| 107 | 105 |
| 108 MOJO_MOVE_ONLY_TYPE(MoveOnlyInt); | 106 MOJO_MOVE_ONLY_TYPE(MoveOnlyInt); |
| 109 }; | 107 }; |
| 110 | 108 |
| 111 TEST(MacrosCppTest, MoveOnlyType) { | 109 TEST(MacrosTest, MoveOnlyType) { |
| 112 MoveOnlyInt x(123); | 110 MoveOnlyInt x(123); |
| 113 EXPECT_TRUE(x.is_set()); | 111 EXPECT_TRUE(x.is_set()); |
| 114 EXPECT_EQ(123, x.value()); | 112 EXPECT_EQ(123, x.value()); |
| 115 MoveOnlyInt y; | 113 MoveOnlyInt y; |
| 116 EXPECT_FALSE(y.is_set()); | 114 EXPECT_FALSE(y.is_set()); |
| 117 y = x.Pass(); | 115 y = x.Pass(); |
| 118 EXPECT_FALSE(x.is_set()); | 116 EXPECT_FALSE(x.is_set()); |
| 119 EXPECT_TRUE(y.is_set()); | 117 EXPECT_TRUE(y.is_set()); |
| 120 EXPECT_EQ(123, y.value()); | 118 EXPECT_EQ(123, y.value()); |
| 121 MoveOnlyInt z(y.Pass()); | 119 MoveOnlyInt z(y.Pass()); |
| 122 EXPECT_FALSE(y.is_set()); | 120 EXPECT_FALSE(y.is_set()); |
| 123 EXPECT_TRUE(z.is_set()); | 121 EXPECT_TRUE(z.is_set()); |
| 124 EXPECT_EQ(123, z.value()); | 122 EXPECT_EQ(123, z.value()); |
| 125 z = z.Pass(); | 123 z = z.Pass(); |
| 126 EXPECT_TRUE(z.is_set()); | 124 EXPECT_TRUE(z.is_set()); |
| 127 EXPECT_EQ(123, z.value()); | 125 EXPECT_EQ(123, z.value()); |
| 128 } | 126 } |
| 129 | 127 |
| 130 // The test for |ignore_result()| is also just a compilation test. (Note that | 128 // The test for |ignore_result()| is also just a compilation test. (Note that |
| 131 // |MOJO_WARN_UNUSED_RESULT| can only be used in the prototype. | 129 // |MOJO_WARN_UNUSED_RESULT| can only be used in the prototype. |
| 132 int ReturnsIntYouMustUse() MOJO_WARN_UNUSED_RESULT; | 130 int ReturnsIntYouMustUse() MOJO_WARN_UNUSED_RESULT; |
| 133 | 131 |
| 134 int ReturnsIntYouMustUse() { | 132 int ReturnsIntYouMustUse() { |
| 135 return 123; | 133 return 123; |
| 136 } | 134 } |
| 137 | 135 |
| 138 TEST(MacrosCppTest, IgnoreResult) { | 136 TEST(MacrosTest, IgnoreResult) { |
| 139 ignore_result(ReturnsIntYouMustUse()); | 137 ignore_result(ReturnsIntYouMustUse()); |
| 140 } | 138 } |
| 141 | 139 |
| 142 } // namespace | 140 } // namespace |
| 143 } // namespace mojo | 141 } // namespace mojo |
| OLD | NEW |