blob: 4cfc3bbbc3a436eeee0d99f874a81e2ea292e1cd [file] [log] [blame]
Joel Scherpelzf3fa5cc2017-05-22 12:30:03 +09001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joel Scherpelzf3fa5cc2017-05-22 12:30:03 +090017#include "netdutils/Status.h"
18#include "netdutils/StatusOr.h"
19
Bernie Innocenti6f9fd902018-10-11 20:50:23 +090020#include <sstream>
21
22#include <gtest/gtest.h>
23
Joel Scherpelzf3fa5cc2017-05-22 12:30:03 +090024namespace android {
25namespace netdutils {
Bernie Innocenti6f9fd902018-10-11 20:50:23 +090026namespace {
Joel Scherpelzf3fa5cc2017-05-22 12:30:03 +090027
Bernie Innocentib9a17542018-10-17 17:37:23 +090028TEST(StatusTest, valueSemantics) {
Joel Scherpelzf3fa5cc2017-05-22 12:30:03 +090029 // Default constructor
30 EXPECT_EQ(status::ok, Status());
Bernie Innocentib9a17542018-10-17 17:37:23 +090031
32 // Copy constructor
33 Status status1(1);
34 Status status2(status1); // NOLINT(performance-unnecessary-copy-initialization)
35 EXPECT_EQ(1, status2.code());
36
37 // Copy assignment
38 Status status3;
39 status3 = status2;
40 EXPECT_EQ(1, status3.code());
41
42 // Same with const objects
43 const Status status4(4);
44 const Status status5(status4); // NOLINT(performance-unnecessary-copy-initialization)
45 Status status6;
46 status6 = status5;
47 EXPECT_EQ(4, status6.code());
Joel Scherpelzf3fa5cc2017-05-22 12:30:03 +090048}
49
Bernie Innocentib9a17542018-10-17 17:37:23 +090050TEST(StatusTest, errorMessages) {
Bernie Innocenti6f9fd902018-10-11 20:50:23 +090051 Status s(42, "for tea too");
52 EXPECT_EQ(42, s.code());
53 EXPECT_FALSE(s.ok());
54 EXPECT_EQ(s.msg(), "for tea too");
55}
56
57TEST(StatusOrTest, moveSemantics) {
58 // Status objects should be cheaply movable.
59 EXPECT_TRUE(std::is_nothrow_move_constructible<Status>::value);
60 EXPECT_TRUE(std::is_nothrow_move_assignable<Status>::value);
61
62 // Should move from a temporary Status (twice)
63 Status s(Status(Status(42, "move me")));
64 EXPECT_EQ(42, s.code());
65 EXPECT_EQ(s.msg(), "move me");
66
67 Status s2(666, "EDAEMON");
68 EXPECT_NE(s, s2);
69 s = s2; // Invokes the move-assignment operator.
70 EXPECT_EQ(666, s.code());
71 EXPECT_EQ(s.msg(), "EDAEMON");
72 EXPECT_EQ(s, s2);
73
74 // A moved-from Status can be re-used.
75 s2 = s;
76
77 // Now both objects are valid.
78 EXPECT_EQ(666, s.code());
79 EXPECT_EQ(s.msg(), "EDAEMON");
80 EXPECT_EQ(s, s2);
81}
82
83TEST(StatusTest, ignoredStatus) {
84 statusFromErrno(ENOTTY, "Not a typewriter, what did you expect?").ignoreError();
85}
86
Joel Scherpelzf3fa5cc2017-05-22 12:30:03 +090087TEST(StatusOrTest, ostream) {
88 {
89 StatusOr<int> so(11);
90 std::stringstream ss;
91 ss << so;
Erik Klineb1157dd2018-05-30 20:19:08 +090092 // TODO: Fix StatusOr to optionally output "value:".
93 EXPECT_EQ("StatusOr[status: Status[code: 0, msg: \"\"]]", ss.str());
Joel Scherpelzf3fa5cc2017-05-22 12:30:03 +090094 }
95 {
96 StatusOr<int> err(status::undefined);
97 std::stringstream ss;
98 ss << err;
Erik Klineb1157dd2018-05-30 20:19:08 +090099 EXPECT_EQ("StatusOr[status: Status[code: 2147483647, msg: \"undefined\"]]", ss.str());
Joel Scherpelzf3fa5cc2017-05-22 12:30:03 +0900100 }
101}
102
Bernie Innocenti6f9fd902018-10-11 20:50:23 +0900103} // namespace
Joel Scherpelzf3fa5cc2017-05-22 12:30:03 +0900104} // namespace netdutils
105} // namespace android