• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 #include "netdutils/Status.h"
18 #include "netdutils/StatusOr.h"
19 
20 #include <sstream>
21 
22 #include <gtest/gtest.h>
23 
24 namespace android {
25 namespace netdutils {
26 namespace {
27 
TEST(StatusTest,valueSemantics)28 TEST(StatusTest, valueSemantics) {
29     // Default constructor
30     EXPECT_EQ(status::ok, Status());
31 
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());
48 }
49 
TEST(StatusTest,errorMessages)50 TEST(StatusTest, errorMessages) {
51     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 
TEST(StatusOrTest,moveSemantics)57 TEST(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 
TEST(StatusTest,ignoredStatus)83 TEST(StatusTest, ignoredStatus) {
84     statusFromErrno(ENOTTY, "Not a typewriter, what did you expect?").ignoreError();
85 }
86 
TEST(StatusOrTest,ostream)87 TEST(StatusOrTest, ostream) {
88     {
89       StatusOr<int> so(11);
90       std::stringstream ss;
91       ss << so;
92       // TODO: Fix StatusOr to optionally output "value:".
93       EXPECT_EQ("StatusOr[status: Status[code: 0, msg: \"\"]]", ss.str());
94     }
95     {
96       StatusOr<int> err(status::undefined);
97       std::stringstream ss;
98       ss << err;
99       EXPECT_EQ("StatusOr[status: Status[code: 2147483647, msg: \"undefined\"]]", ss.str());
100     }
101 }
102 
103 }  // namespace
104 }  // namespace netdutils
105 }  // namespace android
106