1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 #include <google/protobuf/stubs/statusor.h>
32
33 #include <errno.h>
34 #include <memory>
35
36 #include <google/protobuf/testing/googletest.h>
37 #include <gtest/gtest.h>
38
39 namespace google {
40 namespace protobuf {
41 namespace util {
42 namespace {
43
44 class Base1 {
45 public:
~Base1()46 virtual ~Base1() {}
47 int pad;
48 };
49
50 class Base2 {
51 public:
~Base2()52 virtual ~Base2() {}
53 int yetotherpad;
54 };
55
56 class Derived : public Base1, public Base2 {
57 public:
~Derived()58 virtual ~Derived() {}
59 int evenmorepad;
60 };
61
62 class CopyNoAssign {
63 public:
CopyNoAssign(int value)64 explicit CopyNoAssign(int value) : foo(value) {}
CopyNoAssign(const CopyNoAssign & other)65 CopyNoAssign(const CopyNoAssign& other) : foo(other.foo) {}
66 int foo;
67 private:
68 const CopyNoAssign& operator=(const CopyNoAssign&);
69 };
70
TEST(StatusOr,TestDefaultCtor)71 TEST(StatusOr, TestDefaultCtor) {
72 StatusOr<int> thing;
73 EXPECT_FALSE(thing.ok());
74 EXPECT_EQ(Status::UNKNOWN, thing.status());
75 }
76
TEST(StatusOr,TestStatusCtor)77 TEST(StatusOr, TestStatusCtor) {
78 StatusOr<int> thing(Status::CANCELLED);
79 EXPECT_FALSE(thing.ok());
80 EXPECT_EQ(Status::CANCELLED, thing.status());
81 }
82
TEST(StatusOr,TestValueCtor)83 TEST(StatusOr, TestValueCtor) {
84 const int kI = 4;
85 StatusOr<int> thing(kI);
86 EXPECT_TRUE(thing.ok());
87 EXPECT_EQ(kI, thing.ValueOrDie());
88 }
89
TEST(StatusOr,TestCopyCtorStatusOk)90 TEST(StatusOr, TestCopyCtorStatusOk) {
91 const int kI = 4;
92 StatusOr<int> original(kI);
93 StatusOr<int> copy(original);
94 EXPECT_EQ(original.status(), copy.status());
95 EXPECT_EQ(original.ValueOrDie(), copy.ValueOrDie());
96 }
97
TEST(StatusOr,TestCopyCtorStatusNotOk)98 TEST(StatusOr, TestCopyCtorStatusNotOk) {
99 StatusOr<int> original(Status::CANCELLED);
100 StatusOr<int> copy(original);
101 EXPECT_EQ(original.status(), copy.status());
102 }
103
TEST(StatusOr,TestCopyCtorStatusOKConverting)104 TEST(StatusOr, TestCopyCtorStatusOKConverting) {
105 const int kI = 4;
106 StatusOr<int> original(kI);
107 StatusOr<double> copy(original);
108 EXPECT_EQ(original.status(), copy.status());
109 EXPECT_EQ(original.ValueOrDie(), copy.ValueOrDie());
110 }
111
TEST(StatusOr,TestCopyCtorStatusNotOkConverting)112 TEST(StatusOr, TestCopyCtorStatusNotOkConverting) {
113 StatusOr<int> original(Status::CANCELLED);
114 StatusOr<double> copy(original);
115 EXPECT_EQ(original.status(), copy.status());
116 }
117
TEST(StatusOr,TestAssignmentStatusOk)118 TEST(StatusOr, TestAssignmentStatusOk) {
119 const int kI = 4;
120 StatusOr<int> source(kI);
121 StatusOr<int> target;
122 target = source;
123 EXPECT_EQ(source.status(), target.status());
124 EXPECT_EQ(source.ValueOrDie(), target.ValueOrDie());
125 }
126
TEST(StatusOr,TestAssignmentStatusNotOk)127 TEST(StatusOr, TestAssignmentStatusNotOk) {
128 StatusOr<int> source(Status::CANCELLED);
129 StatusOr<int> target;
130 target = source;
131 EXPECT_EQ(source.status(), target.status());
132 }
133
TEST(StatusOr,TestAssignmentStatusOKConverting)134 TEST(StatusOr, TestAssignmentStatusOKConverting) {
135 const int kI = 4;
136 StatusOr<int> source(kI);
137 StatusOr<double> target;
138 target = source;
139 EXPECT_EQ(source.status(), target.status());
140 EXPECT_DOUBLE_EQ(source.ValueOrDie(), target.ValueOrDie());
141 }
142
TEST(StatusOr,TestAssignmentStatusNotOkConverting)143 TEST(StatusOr, TestAssignmentStatusNotOkConverting) {
144 StatusOr<int> source(Status::CANCELLED);
145 StatusOr<double> target;
146 target = source;
147 EXPECT_EQ(source.status(), target.status());
148 }
149
TEST(StatusOr,TestStatus)150 TEST(StatusOr, TestStatus) {
151 StatusOr<int> good(4);
152 EXPECT_TRUE(good.ok());
153 StatusOr<int> bad(Status::CANCELLED);
154 EXPECT_FALSE(bad.ok());
155 EXPECT_EQ(Status::CANCELLED, bad.status());
156 }
157
TEST(StatusOr,TestValue)158 TEST(StatusOr, TestValue) {
159 const int kI = 4;
160 StatusOr<int> thing(kI);
161 EXPECT_EQ(kI, thing.ValueOrDie());
162 }
163
TEST(StatusOr,TestValueConst)164 TEST(StatusOr, TestValueConst) {
165 const int kI = 4;
166 const StatusOr<int> thing(kI);
167 EXPECT_EQ(kI, thing.ValueOrDie());
168 }
169
TEST(StatusOr,TestPointerDefaultCtor)170 TEST(StatusOr, TestPointerDefaultCtor) {
171 StatusOr<int*> thing;
172 EXPECT_FALSE(thing.ok());
173 EXPECT_EQ(Status::UNKNOWN, thing.status());
174 }
175
TEST(StatusOr,TestPointerStatusCtor)176 TEST(StatusOr, TestPointerStatusCtor) {
177 StatusOr<int*> thing(Status::CANCELLED);
178 EXPECT_FALSE(thing.ok());
179 EXPECT_EQ(Status::CANCELLED, thing.status());
180 }
181
TEST(StatusOr,TestPointerValueCtor)182 TEST(StatusOr, TestPointerValueCtor) {
183 const int kI = 4;
184 StatusOr<const int*> thing(&kI);
185 EXPECT_TRUE(thing.ok());
186 EXPECT_EQ(&kI, thing.ValueOrDie());
187 }
188
TEST(StatusOr,TestPointerCopyCtorStatusOk)189 TEST(StatusOr, TestPointerCopyCtorStatusOk) {
190 const int kI = 0;
191 StatusOr<const int*> original(&kI);
192 StatusOr<const int*> copy(original);
193 EXPECT_EQ(original.status(), copy.status());
194 EXPECT_EQ(original.ValueOrDie(), copy.ValueOrDie());
195 }
196
TEST(StatusOr,TestPointerCopyCtorStatusNotOk)197 TEST(StatusOr, TestPointerCopyCtorStatusNotOk) {
198 StatusOr<int*> original(Status::CANCELLED);
199 StatusOr<int*> copy(original);
200 EXPECT_EQ(original.status(), copy.status());
201 }
202
TEST(StatusOr,TestPointerCopyCtorStatusOKConverting)203 TEST(StatusOr, TestPointerCopyCtorStatusOKConverting) {
204 Derived derived;
205 StatusOr<Derived*> original(&derived);
206 StatusOr<Base2*> copy(original);
207 EXPECT_EQ(original.status(), copy.status());
208 EXPECT_EQ(static_cast<const Base2*>(original.ValueOrDie()),
209 copy.ValueOrDie());
210 }
211
TEST(StatusOr,TestPointerCopyCtorStatusNotOkConverting)212 TEST(StatusOr, TestPointerCopyCtorStatusNotOkConverting) {
213 StatusOr<Derived*> original(Status::CANCELLED);
214 StatusOr<Base2*> copy(original);
215 EXPECT_EQ(original.status(), copy.status());
216 }
217
TEST(StatusOr,TestPointerAssignmentStatusOk)218 TEST(StatusOr, TestPointerAssignmentStatusOk) {
219 const int kI = 0;
220 StatusOr<const int*> source(&kI);
221 StatusOr<const int*> target;
222 target = source;
223 EXPECT_EQ(source.status(), target.status());
224 EXPECT_EQ(source.ValueOrDie(), target.ValueOrDie());
225 }
226
TEST(StatusOr,TestPointerAssignmentStatusNotOk)227 TEST(StatusOr, TestPointerAssignmentStatusNotOk) {
228 StatusOr<int*> source(Status::CANCELLED);
229 StatusOr<int*> target;
230 target = source;
231 EXPECT_EQ(source.status(), target.status());
232 }
233
TEST(StatusOr,TestPointerAssignmentStatusOKConverting)234 TEST(StatusOr, TestPointerAssignmentStatusOKConverting) {
235 Derived derived;
236 StatusOr<Derived*> source(&derived);
237 StatusOr<Base2*> target;
238 target = source;
239 EXPECT_EQ(source.status(), target.status());
240 EXPECT_EQ(static_cast<const Base2*>(source.ValueOrDie()),
241 target.ValueOrDie());
242 }
243
TEST(StatusOr,TestPointerAssignmentStatusNotOkConverting)244 TEST(StatusOr, TestPointerAssignmentStatusNotOkConverting) {
245 StatusOr<Derived*> source(Status::CANCELLED);
246 StatusOr<Base2*> target;
247 target = source;
248 EXPECT_EQ(source.status(), target.status());
249 }
250
TEST(StatusOr,TestPointerStatus)251 TEST(StatusOr, TestPointerStatus) {
252 const int kI = 0;
253 StatusOr<const int*> good(&kI);
254 EXPECT_TRUE(good.ok());
255 StatusOr<const int*> bad(Status::CANCELLED);
256 EXPECT_EQ(Status::CANCELLED, bad.status());
257 }
258
TEST(StatusOr,TestPointerValue)259 TEST(StatusOr, TestPointerValue) {
260 const int kI = 0;
261 StatusOr<const int*> thing(&kI);
262 EXPECT_EQ(&kI, thing.ValueOrDie());
263 }
264
TEST(StatusOr,TestPointerValueConst)265 TEST(StatusOr, TestPointerValueConst) {
266 const int kI = 0;
267 const StatusOr<const int*> thing(&kI);
268 EXPECT_EQ(&kI, thing.ValueOrDie());
269 }
270
271 } // namespace
272 } // namespace util
273 } // namespace protobuf
274 } // namespace google
275