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