status_tests.cpp 3.3 KB

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