statusor_test.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "statusor.h"
  15. #include <memory>
  16. #include <sstream>
  17. #include <string>
  18. #include "absl/status/status.h"
  19. #include <gtest/gtest.h>
  20. namespace rlwe {
  21. namespace {
  22. class NoDefault {
  23. public:
  24. explicit NoDefault(int dummy) { dummy_ = dummy; }
  25. ~NoDefault() = default;
  26. int GetDummy() { return dummy_; }
  27. private:
  28. NoDefault() = delete;
  29. int dummy_;
  30. };
  31. class NoDefaultNoCopy {
  32. public:
  33. explicit NoDefaultNoCopy(int dummy) { dummy_ = dummy; }
  34. ~NoDefaultNoCopy() = default;
  35. int GetDummy() { return dummy_; }
  36. NoDefaultNoCopy(NoDefaultNoCopy&& other) = default;
  37. NoDefaultNoCopy& operator=(NoDefaultNoCopy&& other) = default;
  38. private:
  39. NoDefaultNoCopy() = delete;
  40. NoDefaultNoCopy(const NoDefaultNoCopy& other) = delete;
  41. NoDefaultNoCopy& operator=(const NoDefaultNoCopy& other) = delete;
  42. int dummy_;
  43. };
  44. TEST(StatusOrTest, StatusUnknown) {
  45. StatusOr<std::string> statusor;
  46. EXPECT_FALSE(statusor.ok());
  47. EXPECT_EQ(absl::UnknownError(""), statusor.status());
  48. }
  49. TEST(StatusOrTest, CopyCtors) {
  50. NoDefault no_default(42);
  51. StatusOr<NoDefault> statusor(no_default);
  52. EXPECT_TRUE(statusor.ok());
  53. EXPECT_EQ(42, statusor.value().GetDummy());
  54. statusor = StatusOr<NoDefault>(no_default);
  55. EXPECT_TRUE(statusor.ok());
  56. EXPECT_EQ(42, statusor.value().GetDummy());
  57. }
  58. TEST(StatusOrTest, MoveCtors) {
  59. StatusOr<NoDefaultNoCopy> statusor(NoDefaultNoCopy(42));
  60. EXPECT_TRUE(statusor.ok());
  61. EXPECT_EQ(42, statusor.value().GetDummy());
  62. statusor = NoDefaultNoCopy(42);
  63. EXPECT_TRUE(statusor.ok());
  64. EXPECT_EQ(42, statusor.value().GetDummy());
  65. }
  66. TEST(StatusOrTest, DiesWithNotOkStatus) {
  67. StatusOr<NoDefault> statusor(absl::CancelledError(""));
  68. EXPECT_DEATH_IF_SUPPORTED(statusor.value(), "");
  69. }
  70. TEST(StatusOrTest, Pointers) {
  71. std::unique_ptr<NoDefault> no_default(new NoDefault(42));
  72. StatusOr<NoDefault*> statusor(no_default.get());
  73. EXPECT_TRUE(statusor.ok());
  74. EXPECT_EQ(42, statusor.value()->GetDummy());
  75. }
  76. TEST(StatusOrTest, TestStatusOrCopyCtors) {
  77. NoDefault no_default(42);
  78. StatusOr<NoDefault> statusor(no_default);
  79. StatusOr<NoDefault> statusor_wrap(statusor);
  80. EXPECT_TRUE(statusor_wrap.ok());
  81. EXPECT_EQ(42, statusor_wrap.value().GetDummy());
  82. }
  83. TEST(StatusOrTest, TestStatusOrCopyAssignment) {
  84. NoDefault no_default(42);
  85. StatusOr<NoDefault> statusor(no_default);
  86. StatusOr<NoDefault> statusor_wrap = statusor;
  87. EXPECT_TRUE(statusor_wrap.ok());
  88. EXPECT_EQ(42, statusor_wrap.value().GetDummy());
  89. }
  90. } // namespace
  91. } // namespace rlwe