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 consists of "positive" tests, i.e., those verifying that things | |
6 // work (without compile errors, or even warnings if warnings are treated as | |
7 // errors). | |
8 // TODO(vtl): Fix no-compile tests (which are all disabled; crbug.com/105388) | |
9 // and write some "negative" tests. | |
10 | |
11 #include "mojo/public/c/system/macros.h" | |
12 | |
13 #include <assert.h> | |
14 #include <stdint.h> | |
15 #include <stdlib.h> | |
16 | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 | |
19 namespace mojo { | |
20 namespace { | |
21 | |
22 TEST(MacrosTest, AllowUnused) { | |
23 // Test that no warning/error is issued even though |x| is unused. | |
24 int x MOJO_ALLOW_UNUSED = 123; | |
25 } | |
26 | |
27 int MustUseReturnedResult() MOJO_WARN_UNUSED_RESULT; | |
28 int MustUseReturnedResult() { | |
29 return 456; | |
30 } | |
31 | |
32 TEST(MacrosTest, WarnUnusedResult) { | |
33 if (!MustUseReturnedResult()) | |
34 abort(); | |
35 } | |
36 | |
37 // Note: MSVS is very strict (and arguably buggy) about warnings for classes | |
38 // defined in a local scope, so define these globally. | |
39 struct TestOverrideBaseClass { | |
40 virtual ~TestOverrideBaseClass() {} | |
41 virtual void ToBeOverridden() {} | |
42 virtual void AlsoToBeOverridden() = 0; | |
43 }; | |
44 | |
45 struct TestOverrideSubclass : public TestOverrideBaseClass { | |
46 virtual ~TestOverrideSubclass() {} | |
47 virtual void ToBeOverridden() MOJO_OVERRIDE {} | |
48 virtual void AlsoToBeOverridden() MOJO_OVERRIDE {} | |
49 }; | |
50 | |
51 TEST(MacrosTest, Override) { | |
52 TestOverrideSubclass x; | |
53 x.ToBeOverridden(); | |
54 x.AlsoToBeOverridden(); | |
55 } | |
56 | |
57 // Note: MSVS is very strict (and arguably buggy) about warnings for classes | |
58 // defined in a local scope, so define these globally. | |
59 class TestDisallowCopyAndAssignClass { | |
60 public: | |
61 TestDisallowCopyAndAssignClass() {} | |
62 explicit TestDisallowCopyAndAssignClass(int) {} | |
63 void NoOp() {} | |
64 | |
65 private: | |
66 MOJO_DISALLOW_COPY_AND_ASSIGN(TestDisallowCopyAndAssignClass); | |
67 }; | |
68 | |
69 TEST(MacrosTest, DisallowCopyAndAssign) { | |
70 TestDisallowCopyAndAssignClass x; | |
71 x.NoOp(); | |
72 TestDisallowCopyAndAssignClass y(789); | |
73 y.NoOp(); | |
74 } | |
75 | |
76 // First test |MOJO_COMPILE_ASSERT()| in a global scope. | |
77 MOJO_COMPILE_ASSERT(sizeof(int64_t) == 2 * sizeof(int32_t), | |
78 bad_compile_assert_failure_in_global_scope); | |
79 | |
80 TEST(MacrosTest, CompileAssert) { | |
81 // Then in a local scope. | |
82 MOJO_COMPILE_ASSERT(sizeof(int32_t) == 2 * sizeof(int16_t), | |
83 bad_compile_assert_failure); | |
84 } | |
85 | |
86 // Test that |MOJO_ARRAYSIZE()| works in a |MOJO_COMPILE_ASSERT()|. | |
87 const int kGlobalArray[5] = { 1, 2, 3, 4, 5 }; | |
88 MOJO_COMPILE_ASSERT(MOJO_ARRAYSIZE(kGlobalArray) == 5u, | |
89 mojo_array_size_failed_in_compile_assert); | |
90 | |
91 TEST(MacrosTest, ArraySize) { | |
92 double local_array[4] = { 6.7, 7.8, 8.9, 9.0 }; | |
93 EXPECT_EQ(4u, MOJO_ARRAYSIZE(local_array)); | |
94 } | |
95 | |
96 // Note: MSVS is very strict (and arguably buggy) about warnings for classes | |
97 // defined in a local scope, so define these globally. | |
98 class MoveOnlyInt { | |
99 MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(MoveOnlyInt, RValue) | |
100 | |
101 public: | |
102 MoveOnlyInt() : is_set_(false), value_() {} | |
103 explicit MoveOnlyInt(int value) : is_set_(true), value_(value) {} | |
104 ~MoveOnlyInt() {} | |
105 | |
106 // Move-only constructor and operator=. | |
107 MoveOnlyInt(RValue other) { *this = other; } | |
108 MoveOnlyInt& operator=(RValue other) { | |
109 if (other.object != this) { | |
110 is_set_ = other.object->is_set_; | |
111 value_ = other.object->value_; | |
112 other.object->is_set_ = false; | |
113 } | |
114 return *this; | |
115 } | |
116 | |
117 int value() const { | |
118 assert(is_set()); | |
119 return value_; | |
120 } | |
121 bool is_set() const { return is_set_; } | |
122 | |
123 private: | |
124 bool is_set_; | |
125 int value_; | |
126 }; | |
127 | |
128 TEST(MacrosTest, MoveOnlyTypeForCpp03) { | |
129 MoveOnlyInt x(123); | |
130 EXPECT_TRUE(x.is_set()); | |
131 EXPECT_EQ(123, x.value()); | |
132 MoveOnlyInt y; | |
133 EXPECT_FALSE(y.is_set()); | |
134 y = x.Pass(); | |
135 EXPECT_FALSE(x.is_set()); | |
136 EXPECT_TRUE(y.is_set()); | |
137 EXPECT_EQ(123, y.value()); | |
138 MoveOnlyInt z(y.Pass()); | |
139 EXPECT_FALSE(y.is_set()); | |
140 EXPECT_TRUE(z.is_set()); | |
141 EXPECT_EQ(123, z.value()); | |
142 z = z.Pass(); | |
143 EXPECT_TRUE(z.is_set()); | |
144 EXPECT_EQ(123, z.value()); | |
145 } | |
146 | |
147 } // namespace | |
148 } // namespace mojo | |
OLD | NEW |