protobuf/src/google/protobuf/stubs/statusor_test.cc
Go to the documentation of this file.
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:
46  virtual ~Base1() {}
47  int pad;
48 };
49 
50 class Base2 {
51  public:
52  virtual ~Base2() {}
54 };
55 
56 class Derived : public Base1, public Base2 {
57  public:
58  virtual ~Derived() {}
60 };
61 
62 class CopyNoAssign {
63  public:
64  explicit CopyNoAssign(int value) : foo(value) {}
65  CopyNoAssign(const CopyNoAssign& other) : foo(other.foo) {}
66  int foo;
67  private:
68  const CopyNoAssign& operator=(const CopyNoAssign&);
69 };
70 
71 TEST(StatusOr, TestDefaultCtor) {
72  StatusOr<int> thing;
73  EXPECT_FALSE(thing.ok());
74  EXPECT_EQ(util::UnknownError(""), thing.status());
75 }
76 
77 TEST(StatusOr, TestStatusCtor) {
78  StatusOr<int> thing(util::CancelledError(""));
79  EXPECT_FALSE(thing.ok());
80  EXPECT_EQ(util::CancelledError(""), thing.status());
81 }
82 
83 TEST(StatusOr, TestValueCtor) {
84  const int kI = 4;
85  StatusOr<int> thing(kI);
86  EXPECT_TRUE(thing.ok());
87  EXPECT_EQ(kI, thing.value());
88 }
89 
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.value(), copy.value());
96 }
97 
98 TEST(StatusOr, TestCopyCtorStatusNotOk) {
99  StatusOr<int> original(util::CancelledError(""));
100  StatusOr<int> copy(original);
101  EXPECT_EQ(original.status(), copy.status());
102 }
103 
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.value(), copy.value());
110 }
111 
112 TEST(StatusOr, TestCopyCtorStatusNotOkConverting) {
113  StatusOr<int> original(util::CancelledError(""));
114  StatusOr<double> copy(original);
115  EXPECT_EQ(original.status(), copy.status());
116 }
117 
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.value(), target.value());
125 }
126 
127 TEST(StatusOr, TestAssignmentStatusNotOk) {
128  StatusOr<int> source(util::CancelledError(""));
129  StatusOr<int> target;
130  target = source;
131  EXPECT_EQ(source.status(), target.status());
132 }
133 
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.value(), target.value());
141 }
142 
143 TEST(StatusOr, TestAssignmentStatusNotOkConverting) {
144  StatusOr<int> source(util::CancelledError(""));
145  StatusOr<double> target;
146  target = source;
147  EXPECT_EQ(source.status(), target.status());
148 }
149 
150 TEST(StatusOr, TestStatus) {
151  StatusOr<int> good(4);
152  EXPECT_TRUE(good.ok());
153  StatusOr<int> bad(util::CancelledError(""));
154  EXPECT_FALSE(bad.ok());
155  EXPECT_EQ(util::CancelledError(""), bad.status());
156 }
157 
158 TEST(StatusOr, TestValue) {
159  const int kI = 4;
160  StatusOr<int> thing(kI);
161  EXPECT_EQ(kI, thing.value());
162 }
163 
164 TEST(StatusOr, TestValueConst) {
165  const int kI = 4;
166  const StatusOr<int> thing(kI);
167  EXPECT_EQ(kI, thing.value());
168 }
169 
170 TEST(StatusOr, TestPointerDefaultCtor) {
171  StatusOr<int*> thing;
172  EXPECT_FALSE(thing.ok());
173  EXPECT_EQ(util::UnknownError(""), thing.status());
174 }
175 
176 TEST(StatusOr, TestPointerStatusCtor) {
177  StatusOr<int*> thing(util::CancelledError(""));
178  EXPECT_FALSE(thing.ok());
179  EXPECT_EQ(util::CancelledError(""), thing.status());
180 }
181 
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.value());
187 }
188 
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.value(), copy.value());
195 }
196 
197 TEST(StatusOr, TestPointerCopyCtorStatusNotOk) {
198  StatusOr<int*> original(util::CancelledError(""));
199  StatusOr<int*> copy(original);
200  EXPECT_EQ(original.status(), copy.status());
201 }
202 
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.value()), copy.value());
209 }
210 
211 TEST(StatusOr, TestPointerCopyCtorStatusNotOkConverting) {
212  StatusOr<Derived*> original(util::CancelledError(""));
213  StatusOr<Base2*> copy(original);
214  EXPECT_EQ(original.status(), copy.status());
215 }
216 
217 TEST(StatusOr, TestPointerAssignmentStatusOk) {
218  const int kI = 0;
219  StatusOr<const int*> source(&kI);
220  StatusOr<const int*> target;
221  target = source;
222  EXPECT_EQ(source.status(), target.status());
223  EXPECT_EQ(source.value(), target.value());
224 }
225 
226 TEST(StatusOr, TestPointerAssignmentStatusNotOk) {
227  StatusOr<int*> source(util::CancelledError(""));
228  StatusOr<int*> target;
229  target = source;
230  EXPECT_EQ(source.status(), target.status());
231 }
232 
233 TEST(StatusOr, TestPointerAssignmentStatusOKConverting) {
234  Derived derived;
235  StatusOr<Derived*> source(&derived);
236  StatusOr<Base2*> target;
237  target = source;
238  EXPECT_EQ(source.status(), target.status());
239  EXPECT_EQ(static_cast<const Base2*>(source.value()), target.value());
240 }
241 
242 TEST(StatusOr, TestPointerAssignmentStatusNotOkConverting) {
243  StatusOr<Derived*> source(util::CancelledError(""));
244  StatusOr<Base2*> target;
245  target = source;
246  EXPECT_EQ(source.status(), target.status());
247 }
248 
249 TEST(StatusOr, TestPointerStatus) {
250  const int kI = 0;
251  StatusOr<const int*> good(&kI);
252  EXPECT_TRUE(good.ok());
253  StatusOr<const int*> bad(util::CancelledError(""));
254  EXPECT_EQ(util::CancelledError(""), bad.status());
255 }
256 
257 TEST(StatusOr, TestPointerValue) {
258  const int kI = 0;
259  StatusOr<const int*> thing(&kI);
260  EXPECT_EQ(&kI, thing.value());
261 }
262 
263 TEST(StatusOr, TestPointerValueConst) {
264  const int kI = 0;
265  const StatusOr<const int*> thing(&kI);
266  EXPECT_EQ(&kI, thing.value());
267 }
268 
269 } // namespace
270 } // namespace util
271 } // namespace protobuf
272 } // namespace google
EXPECT_FALSE
#define EXPECT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1970
check_banned_filenames.bad
bad
Definition: check_banned_filenames.py:26
google::protobuf::value
const Descriptor::ReservedRange value
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:1954
google::protobuf::util::TEST
TEST(DelimitedMessageUtilTest, DelimitedMessages)
Definition: bloaty/third_party/protobuf/src/google/protobuf/util/delimited_message_util_test.cc:47
absl::StatusOr
ABSL_NAMESPACE_BEGIN class ABSL_MUST_USE_RESULT StatusOr
Definition: abseil-cpp/absl/status/internal/statusor_internal.h:29
absl::CancelledError
Status CancelledError(absl::string_view message)
Definition: third_party/abseil-cpp/absl/status/status.cc:331
copy
static int copy(grpc_slice_buffer *input, grpc_slice_buffer *output)
Definition: message_compress.cc:145
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
foo
Definition: bloaty/third_party/googletest/googletest/test/googletest-output-test_.cc:546
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
yetotherpad
int yetotherpad
Definition: protobuf/src/google/protobuf/stubs/statusor_test.cc:53
pad
int pad
Definition: protobuf/src/google/protobuf/stubs/statusor_test.cc:47
absl::UnknownError
Status UnknownError(absl::string_view message)
Definition: third_party/abseil-cpp/absl/status/status.cc:383
foo
int foo
Definition: protobuf/src/google/protobuf/stubs/statusor_test.cc:66
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
EXPECT_DOUBLE_EQ
#define EXPECT_DOUBLE_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:28
setup.target
target
Definition: third_party/bloaty/third_party/protobuf/python/setup.py:179
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11
errno.h
evenmorepad
int evenmorepad
Definition: protobuf/src/google/protobuf/stubs/statusor_test.cc:59


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:19