Debian Codegraph @

mfbt/tests/gtest/TestMozDbg.cpp

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include <iostream>
8 #include <type_traits>
9
10 #include "gtest/gtest.h"
11 #include "mozilla/DbgMacro.h"
12 #include "mozilla/Unused.h"
13
14 using namespace mozilla;
15
16 #define TEST_MOZ_DBG_TYPE_IS(type_, expression_...) \
17 static_assert(std::is_same_v<type_, decltype(MOZ_DBG(expression_))>, \
18 "MOZ_DBG should return the indicated type")
19
20 #define TEST_MOZ_DBG_TYPE_SAME(expression_...) \
21 static_assert( \
22 std::is_same_v<decltype((expression_)), decltype(MOZ_DBG(expression_))>, \
23 "MOZ_DBG should return the same type")
24
25 struct Number {
26 explicit Number(int aValue) : mValue(aValue) {}
27
28 Number(const Number& aOther) = default;
29
30 Number(Number&& aOther) : mValue(aOther.mValue) { aOther.mValue = 0; }
31
32 Number& operator=(const Number& aOther) = default;
33
34 Number& operator=(Number&& aOther) {
35 mValue = aOther.mValue;
36 aOther.mValue = 0;
37 return *this;
38 }
39
40 ~Number() { mValue = -999; }
41
42 int mValue;
43
44 MOZ_DEFINE_DBG(Number, mValue)
45 };
46
47 struct MoveOnly {
48 explicit MoveOnly(int aValue) : mValue(aValue) {}
49
50 MoveOnly(const MoveOnly& aOther) = delete;
51
52 MoveOnly(MoveOnly&& aOther) : mValue(aOther.mValue) { aOther.mValue = 0; }
53
54 MoveOnly& operator=(MoveOnly& aOther) = default;
55
56 MoveOnly& operator=(MoveOnly&& aOther) {
57 mValue = aOther.mValue;
58 aOther.mValue = 0;
59 return *this;
60 }
61
62 int mValue;
63
64 MOZ_DEFINE_DBG(MoveOnly)
65 };
66
67 void StaticAssertions() {
68 int x = 123;
69 Number y(123);
70 Number z(234);
71 MoveOnly w(456);
72
73 // Static assertions.
74
75 // lvalues
76 TEST_MOZ_DBG_TYPE_SAME(x); // int&
77 TEST_MOZ_DBG_TYPE_SAME(y); // Number&
78 TEST_MOZ_DBG_TYPE_SAME(x = 234); // int&
79 TEST_MOZ_DBG_TYPE_SAME(y = z); // Number&
80 TEST_MOZ_DBG_TYPE_SAME(w); // MoveOnly&
81
82 // prvalues (which MOZ_DBG turns into xvalues by creating objects for them)
83 TEST_MOZ_DBG_TYPE_IS(int&&, 123);
84 TEST_MOZ_DBG_TYPE_IS(int&&, 1 + 2);
85 TEST_MOZ_DBG_TYPE_IS(int*&&, &x);
86 TEST_MOZ_DBG_TYPE_IS(int&&, x++);
87 TEST_MOZ_DBG_TYPE_IS(Number&&, Number(123));
88 TEST_MOZ_DBG_TYPE_IS(MoveOnly&&, MoveOnly(123));
89
90 // xvalues
91 TEST_MOZ_DBG_TYPE_SAME(std::move(y)); // int&&
92 TEST_MOZ_DBG_TYPE_SAME(std::move(y)); // Number&&
93 TEST_MOZ_DBG_TYPE_SAME(std::move(w)); // MoveOnly&
94
95 Unused << x;
96 Unused << y;
97 Unused << z;
98 }
99
100 TEST(MozDbg, ObjectValues)
101 {
102 // Test that moves and assignments all operate correctly with MOZ_DBG wrapped
103 // around various parts of the expression.
104
105 Number a(1);
106 Number b(4);
107
108 ASSERT_EQ(a.mValue, 1);
109
110 MOZ_DBG(a.mValue);
111 ASSERT_EQ(a.mValue, 1);
112
113 MOZ_DBG(a.mValue + 1);
114 ASSERT_EQ(a.mValue, 1);
115
116 MOZ_DBG(a.mValue = 2);
117 ASSERT_EQ(a.mValue, 2);
118
119 MOZ_DBG(a).mValue = 3;
120 ASSERT_EQ(a.mValue, 3);
121
122 MOZ_DBG(a = b);
123 ASSERT_EQ(a.mValue, 4);
124 ASSERT_EQ(b.mValue, 4);
125
126 b.mValue = 5;
127 MOZ_DBG(a) = b;
128 ASSERT_EQ(a.mValue, 5);
129 ASSERT_EQ(b.mValue, 5);
130
131 b.mValue = 6;
132 MOZ_DBG(a = std::move(b));
133 ASSERT_EQ(a.mValue, 6);
134 ASSERT_EQ(b.mValue, 0);
135
136 b.mValue = 7;
137 MOZ_DBG(a) = std::move(b);
138 ASSERT_EQ(a.mValue, 7);
139 ASSERT_EQ(b.mValue, 0);
140
141 b.mValue = 8;
142 a = std::move(MOZ_DBG(b));
143 ASSERT_EQ(a.mValue, 8);
144 ASSERT_EQ(b.mValue, 0);
145
146 a = MOZ_DBG(Number(9));
147 ASSERT_EQ(a.mValue, 9);
148
149 MoveOnly c(1);
150 MoveOnly d(2);
151
152 c = std::move(MOZ_DBG(d));
153 ASSERT_EQ(c.mValue, 2);
154 ASSERT_EQ(d.mValue, 0);
155
156 c.mValue = 3;
157 d.mValue = 4;
158 c = MOZ_DBG(std::move(d));
159 ASSERT_EQ(c.mValue, 4);
160 ASSERT_EQ(d.mValue, 0);
161
162 c.mValue = 5;
163 d.mValue = 6;
164 MOZ_DBG(c = std::move(d));
165 ASSERT_EQ(c.mValue, 6);
166 ASSERT_EQ(d.mValue, 0);
167
168 c = MOZ_DBG(MoveOnly(7));
169 ASSERT_EQ(c.mValue, 7);
170 }