• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <pdx/status.h>
2 
3 #include <gtest/gtest.h>
4 
5 #include <memory>
6 
7 using android::pdx::ErrorStatus;
8 using android::pdx::Status;
9 
TEST(Status,DefaultInit)10 TEST(Status, DefaultInit) {
11   Status<int> status;
12   EXPECT_FALSE(status.ok());
13   EXPECT_TRUE(status.empty());
14   EXPECT_EQ(0, status.get());
15   EXPECT_EQ(0, status.error());
16 }
17 
TEST(Status,InitalizeSuccess)18 TEST(Status, InitalizeSuccess) {
19   Status<int> status_int{0};
20   EXPECT_FALSE(status_int.empty());
21   EXPECT_TRUE(status_int.ok());
22   EXPECT_EQ(0, status_int.get());
23   status_int = Status<int>(3);
24   EXPECT_FALSE(status_int.empty());
25   EXPECT_TRUE(status_int.ok());
26   EXPECT_EQ(3, status_int.get());
27   status_int = Status<int>(-3);
28   EXPECT_FALSE(status_int.empty());
29   EXPECT_TRUE(status_int.ok());
30   EXPECT_EQ(-3, status_int.get());
31 
32   Status<std::string> status_str{"foo"};
33   EXPECT_FALSE(status_str.empty());
34   EXPECT_TRUE(status_str.ok());
35   EXPECT_EQ("foo", status_str.get());
36 }
37 
TEST(Status,InitalizeError)38 TEST(Status, InitalizeError) {
39   Status<int> status_int = ErrorStatus(12);
40   EXPECT_FALSE(status_int.empty());
41   EXPECT_FALSE(status_int.ok());
42   EXPECT_EQ(0, status_int.get());
43   EXPECT_EQ(12, status_int.error());
44 
45   Status<std::string> status_str = ErrorStatus(EIO);
46   EXPECT_FALSE(status_str.empty());
47   EXPECT_FALSE(status_str.ok());
48   EXPECT_EQ(EIO, status_str.error());
49 }
50 
TEST(Status,ErrorMessage)51 TEST(Status, ErrorMessage) {
52   Status<int> status = ErrorStatus(EIO);
53   EXPECT_EQ(status.GetErrorMessage(), strerror(EIO));
54 
55   status = ErrorStatus(EINVAL);
56   EXPECT_EQ(status.GetErrorMessage(), strerror(EINVAL));
57 }
58 
TEST(Status,Copy)59 TEST(Status, Copy) {
60   Status<int> status1;
61   Status<int> status2;
62 
63   status1 = Status<int>{12};
64   status2 = ErrorStatus(13);
65   EXPECT_FALSE(status1.empty());
66   EXPECT_FALSE(status2.empty());
67   EXPECT_TRUE(status1.ok());
68   EXPECT_FALSE(status2.ok());
69   EXPECT_EQ(12, status1.get());
70   EXPECT_EQ(0, status1.error());
71   EXPECT_EQ(0, status2.get());
72   EXPECT_EQ(13, status2.error());
73 
74   status1 = status2;
75   EXPECT_FALSE(status1.empty());
76   EXPECT_FALSE(status2.empty());
77   EXPECT_FALSE(status1.ok());
78   EXPECT_FALSE(status2.ok());
79   EXPECT_EQ(0, status1.get());
80   EXPECT_EQ(13, status1.error());
81   EXPECT_EQ(0, status2.get());
82   EXPECT_EQ(13, status2.error());
83 }
84 
TEST(Status,Move)85 TEST(Status, Move) {
86   Status<std::unique_ptr<int>> status1;
87   Status<std::unique_ptr<int>> status2;
88 
89   status1 = Status<std::unique_ptr<int>>{std::make_unique<int>(int{11})};
90   status2 = Status<std::unique_ptr<int>>{std::make_unique<int>(int{12})};
91   EXPECT_FALSE(status1.empty());
92   EXPECT_FALSE(status2.empty());
93   EXPECT_TRUE(status1.ok());
94   EXPECT_TRUE(status2.ok());
95   EXPECT_EQ(11, *status1.get());
96   EXPECT_EQ(12, *status2.get());
97 
98   Status<std::unique_ptr<int>> status3 = std::move(status2);
99   EXPECT_FALSE(status1.empty());
100   EXPECT_TRUE(status2.empty());
101   EXPECT_FALSE(status3.empty());
102   EXPECT_TRUE(status1.ok());
103   EXPECT_FALSE(status2.ok());
104   EXPECT_TRUE(status3.ok());
105   EXPECT_EQ(11, *status1.get());
106   EXPECT_EQ(nullptr, status2.get());
107   EXPECT_EQ(12, *status3.get());
108 
109   std::swap(status1, status3);
110   EXPECT_EQ(12, *status1.get());
111   EXPECT_EQ(11, *status3.get());
112 
113   status3 = std::move(status1);
114   EXPECT_TRUE(status1.empty());
115   EXPECT_EQ(12, *status3.get());
116 }
117 
TEST(Status,Take)118 TEST(Status, Take) {
119   Status<std::unique_ptr<int>> status{std::make_unique<int>(int{123})};
120   EXPECT_FALSE(status.empty());
121   EXPECT_NE(nullptr, status.get());
122 
123   auto data = status.take();
124   EXPECT_TRUE(status.empty());
125   EXPECT_EQ(nullptr, status.get());
126   EXPECT_EQ(123, *data);
127 }
128