OLD | NEW |
| (Empty) |
1 // Copyright 2015 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 // Tests of the static thread annotation macros. These fall into two categories, | |
6 // positive tests (testing that correct code compiles and works) and negative | |
7 // tests (testing that incorrect code does not compile). | |
8 // | |
9 // Unfortunately, we don't have systematic/automated negative compilation tests. | |
10 // So instead we have some cheesy macros that you can define to enable | |
11 // individual compilation failures. | |
12 | |
13 #include "mojo/edk/system/thread_annotations.h" | |
14 | |
15 #include "mojo/edk/system/mutex.h" | |
16 #include "mojo/public/cpp/system/macros.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 | |
19 // Uncomment these to enable particular compilation failure tests. | |
20 // #define NC_GUARDED_BY | |
21 // TODO(vtl): |ACQUIRED_{BEFORE,AFTER}()| are currently unimplemented in clang | |
22 // as of 2015-07-06 ("To be fixed in a future update."). So this actually | |
23 // compiles! | |
24 // #define NC_ACQUIRED_BEFORE | |
25 | |
26 namespace mojo { | |
27 namespace system { | |
28 namespace { | |
29 | |
30 // Test MOJO_GUARDED_BY -------------------------------------------------------- | |
31 | |
32 class GuardedByClass { | |
33 public: | |
34 GuardedByClass() : x_() {} | |
35 ~GuardedByClass() {} | |
36 | |
37 void GoodSet(int x) { | |
38 mu_.Lock(); | |
39 x_ = x; | |
40 mu_.Unlock(); | |
41 } | |
42 | |
43 #ifdef NC_GUARDED_BY | |
44 void BadSet(int x) { x_ = x; } | |
45 #endif | |
46 | |
47 private: | |
48 Mutex mu_; | |
49 int x_ MOJO_GUARDED_BY(mu_); | |
50 | |
51 MOJO_DISALLOW_COPY_AND_ASSIGN(GuardedByClass); | |
52 }; | |
53 | |
54 TEST(ThreadAnnotationsTest, GuardedBy) { | |
55 GuardedByClass c; | |
56 c.GoodSet(123); | |
57 } | |
58 | |
59 // Test MOJO_ACQUIRED_BEFORE --------------------------------------------------- | |
60 | |
61 class AcquiredBeforeClass2; | |
62 | |
63 class AcquiredBeforeClass1 { | |
64 public: | |
65 AcquiredBeforeClass1() {} | |
66 ~AcquiredBeforeClass1() {} | |
67 | |
68 void NoOp() { | |
69 mu_.Lock(); | |
70 mu_.Unlock(); | |
71 } | |
72 | |
73 #ifdef NC_ACQUIRED_BEFORE | |
74 void BadMethod(AcquiredBeforeClass2* c2) { | |
75 mu_.Lock(); | |
76 c2->NoOp(); | |
77 mu_.Unlock(); | |
78 } | |
79 #endif | |
80 | |
81 private: | |
82 friend class AcquiredBeforeClass2; | |
83 | |
84 Mutex mu_; | |
85 | |
86 MOJO_DISALLOW_COPY_AND_ASSIGN(AcquiredBeforeClass1); | |
87 }; | |
88 | |
89 class AcquiredBeforeClass2 { | |
90 public: | |
91 AcquiredBeforeClass2() {} | |
92 ~AcquiredBeforeClass2() {} | |
93 | |
94 void NoOp() { | |
95 mu_.Lock(); | |
96 mu_.Unlock(); | |
97 } | |
98 | |
99 void GoodMethod(AcquiredBeforeClass1* c1) { | |
100 mu_.Lock(); | |
101 c1->NoOp(); | |
102 mu_.Unlock(); | |
103 } | |
104 | |
105 private: | |
106 Mutex mu_ MOJO_ACQUIRED_BEFORE(AcquiredBeforeClass1::mu_); | |
107 | |
108 MOJO_DISALLOW_COPY_AND_ASSIGN(AcquiredBeforeClass2); | |
109 }; | |
110 | |
111 TEST(ThreadAnnotationsTest, AcquiredBefore) { | |
112 AcquiredBeforeClass1 c1; | |
113 AcquiredBeforeClass2 c2; | |
114 c2.GoodMethod(&c1); | |
115 #ifdef NC_ACQUIRED_BEFORE | |
116 c1.BadMethod(&c2); | |
117 #endif | |
118 } | |
119 | |
120 // TODO(vtl): Test more things. | |
121 | |
122 } // namespace | |
123 } // namespace system | |
124 } // namespace mojo | |
OLD | NEW |