| OLD | NEW |
| (Empty) |
| 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 // 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 | |
| 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) | |
| 11 // and write some "negative" tests. | |
| 12 | |
| 13 #include "mojo/public/cpp/system/macros.h" | |
| 14 | |
| 15 #include <assert.h> | |
| 16 #include <stdint.h> | |
| 17 #include <stdlib.h> | |
| 18 | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 | |
| 21 namespace mojo { | |
| 22 namespace { | |
| 23 | |
| 24 // Note: MSVS is very strict (and arguably buggy) about warnings for classes | |
| 25 // defined in a local scope, so define these globally. | |
| 26 struct TestOverrideBaseClass { | |
| 27 virtual ~TestOverrideBaseClass() {} | |
| 28 virtual void ToBeOverridden() {} | |
| 29 virtual void AlsoToBeOverridden() = 0; | |
| 30 }; | |
| 31 | |
| 32 struct TestOverrideSubclass : public TestOverrideBaseClass { | |
| 33 ~TestOverrideSubclass() override {} | |
| 34 void ToBeOverridden() override {} | |
| 35 void AlsoToBeOverridden() override {} | |
| 36 }; | |
| 37 | |
| 38 TEST(MacrosCppTest, Override) { | |
| 39 TestOverrideSubclass x; | |
| 40 x.ToBeOverridden(); | |
| 41 x.AlsoToBeOverridden(); | |
| 42 } | |
| 43 | |
| 44 // Note: MSVS is very strict (and arguably buggy) about warnings for classes | |
| 45 // defined in a local scope, so define these globally. | |
| 46 class TestDisallowCopyAndAssignClass { | |
| 47 public: | |
| 48 TestDisallowCopyAndAssignClass() {} | |
| 49 explicit TestDisallowCopyAndAssignClass(int) {} | |
| 50 void NoOp() {} | |
| 51 | |
| 52 private: | |
| 53 MOJO_DISALLOW_COPY_AND_ASSIGN(TestDisallowCopyAndAssignClass); | |
| 54 }; | |
| 55 | |
| 56 TEST(MacrosCppTest, DisallowCopyAndAssign) { | |
| 57 TestDisallowCopyAndAssignClass x; | |
| 58 x.NoOp(); | |
| 59 TestDisallowCopyAndAssignClass y(789); | |
| 60 y.NoOp(); | |
| 61 } | |
| 62 | |
| 63 // Test that |MOJO_ARRAYSIZE()| works in a |static_assert()|. | |
| 64 const int kGlobalArray[5] = {1, 2, 3, 4, 5}; | |
| 65 static_assert(MOJO_ARRAYSIZE(kGlobalArray) == 5u, | |
| 66 "MOJO_ARRAY_SIZE() failed in static_assert()"); | |
| 67 | |
| 68 TEST(MacrosCppTest, ArraySize) { | |
| 69 double local_array[4] = {6.7, 7.8, 8.9, 9.0}; | |
| 70 // 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. | |
| 72 MOJO_ALLOW_UNUSED_LOCAL(local_array); | |
| 73 EXPECT_EQ(4u, MOJO_ARRAYSIZE(local_array)); | |
| 74 } | |
| 75 | |
| 76 // Note: MSVS is very strict (and arguably buggy) about warnings for classes | |
| 77 // defined in a local scope, so define these globally. | |
| 78 class MoveOnlyInt { | |
| 79 MOJO_MOVE_ONLY_TYPE(MoveOnlyInt) | |
| 80 | |
| 81 public: | |
| 82 MoveOnlyInt() : is_set_(false), value_() {} | |
| 83 explicit MoveOnlyInt(int value) : is_set_(true), value_(value) {} | |
| 84 ~MoveOnlyInt() {} | |
| 85 | |
| 86 // Move-only constructor and operator=. | |
| 87 MoveOnlyInt(MoveOnlyInt&& other) { *this = other.Pass(); } | |
| 88 MoveOnlyInt& operator=(MoveOnlyInt&& other) { | |
| 89 if (&other != this) { | |
| 90 is_set_ = other.is_set_; | |
| 91 value_ = other.value_; | |
| 92 other.is_set_ = false; | |
| 93 } | |
| 94 return *this; | |
| 95 } | |
| 96 | |
| 97 int value() const { | |
| 98 assert(is_set()); | |
| 99 return value_; | |
| 100 } | |
| 101 bool is_set() const { return is_set_; } | |
| 102 | |
| 103 private: | |
| 104 bool is_set_; | |
| 105 int value_; | |
| 106 }; | |
| 107 | |
| 108 TEST(MacrosCppTest, MoveOnlyTypeForCpp03) { | |
| 109 MoveOnlyInt x(123); | |
| 110 EXPECT_TRUE(x.is_set()); | |
| 111 EXPECT_EQ(123, x.value()); | |
| 112 MoveOnlyInt y; | |
| 113 EXPECT_FALSE(y.is_set()); | |
| 114 y = x.Pass(); | |
| 115 EXPECT_FALSE(x.is_set()); | |
| 116 EXPECT_TRUE(y.is_set()); | |
| 117 EXPECT_EQ(123, y.value()); | |
| 118 MoveOnlyInt z(y.Pass()); | |
| 119 EXPECT_FALSE(y.is_set()); | |
| 120 EXPECT_TRUE(z.is_set()); | |
| 121 EXPECT_EQ(123, z.value()); | |
| 122 z = z.Pass(); | |
| 123 EXPECT_TRUE(z.is_set()); | |
| 124 EXPECT_EQ(123, z.value()); | |
| 125 } | |
| 126 | |
| 127 } // namespace | |
| 128 } // namespace mojo | |
| OLD | NEW |