StatusTest.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #include "netdutils/Status.h"
  17. #include "netdutils/StatusOr.h"
  18. #include <sstream>
  19. #include <gtest/gtest.h>
  20. namespace android {
  21. namespace netdutils {
  22. namespace {
  23. TEST(StatusTest, valueSemantics) {
  24. // Default constructor
  25. EXPECT_EQ(status::ok, Status());
  26. // Copy constructor
  27. Status status1(1);
  28. Status status2(status1); // NOLINT(performance-unnecessary-copy-initialization)
  29. EXPECT_EQ(1, status2.code());
  30. // Copy assignment
  31. Status status3;
  32. status3 = status2;
  33. EXPECT_EQ(1, status3.code());
  34. // Same with const objects
  35. const Status status4(4);
  36. const Status status5(status4); // NOLINT(performance-unnecessary-copy-initialization)
  37. Status status6;
  38. status6 = status5;
  39. EXPECT_EQ(4, status6.code());
  40. }
  41. TEST(StatusTest, errorMessages) {
  42. Status s(42, "for tea too");
  43. EXPECT_EQ(42, s.code());
  44. EXPECT_FALSE(s.ok());
  45. EXPECT_EQ(s.msg(), "for tea too");
  46. }
  47. TEST(StatusOrTest, moveSemantics) {
  48. // Status objects should be cheaply movable.
  49. EXPECT_TRUE(std::is_nothrow_move_constructible<Status>::value);
  50. EXPECT_TRUE(std::is_nothrow_move_assignable<Status>::value);
  51. // Should move from a temporary Status (twice)
  52. Status s(Status(Status(42, "move me")));
  53. EXPECT_EQ(42, s.code());
  54. EXPECT_EQ(s.msg(), "move me");
  55. Status s2(666, "EDAEMON");
  56. EXPECT_NE(s, s2);
  57. s = s2; // Invokes the move-assignment operator.
  58. EXPECT_EQ(666, s.code());
  59. EXPECT_EQ(s.msg(), "EDAEMON");
  60. EXPECT_EQ(s, s2);
  61. // A moved-from Status can be re-used.
  62. s2 = s;
  63. // Now both objects are valid.
  64. EXPECT_EQ(666, s.code());
  65. EXPECT_EQ(s.msg(), "EDAEMON");
  66. EXPECT_EQ(s, s2);
  67. }
  68. TEST(StatusTest, ignoredStatus) {
  69. statusFromErrno(ENOTTY, "Not a typewriter, what did you expect?").ignoreError();
  70. }
  71. TEST(StatusOrTest, ostream) {
  72. {
  73. StatusOr<int> so(11);
  74. std::stringstream ss;
  75. ss << so;
  76. // TODO: Fix StatusOr to optionally output "value:".
  77. EXPECT_EQ("StatusOr[status: Status[code: 0, msg: \"\"]]", ss.str());
  78. }
  79. {
  80. StatusOr<int> err(status::undefined);
  81. std::stringstream ss;
  82. ss << err;
  83. EXPECT_EQ("StatusOr[status: Status[code: 2147483647, msg: \"undefined\"]]", ss.str());
  84. }
  85. }
  86. } // namespace
  87. } // namespace netdutils
  88. } // namespace android