David Rogers | b6b14b8 | 2020-09-11 16:27:27 -0700 | [diff] [blame] | 1 | // Copyright 2020 The Pigweed Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | // use this file except in compliance with the License. You may obtain a copy of |
| 5 | // the License at |
| 6 | // |
| 7 | // https://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, WITHOUT |
| 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | // License for the specific language governing permissions and limitations under |
| 13 | // the License. |
| 14 | |
| 15 | #include "pw_status/try.h" |
| 16 | |
| 17 | #include "gtest/gtest.h" |
| 18 | |
| 19 | namespace pw { |
| 20 | namespace { |
| 21 | |
| 22 | Status ReturnStatus(Status status) { return status; } |
| 23 | StatusWithSize ReturnStatusWithSize(StatusWithSize status) { return status; } |
| 24 | |
| 25 | Status TryStatus(Status status) { |
David Rogers | c4dc864 | 2020-09-14 10:52:36 -0700 | [diff] [blame] | 26 | PW_TRY(ReturnStatus(status)); |
David Rogers | b6b14b8 | 2020-09-11 16:27:27 -0700 | [diff] [blame] | 27 | |
| 28 | // Any status other than OK should have already returned. |
| 29 | EXPECT_EQ(status, Status::OK); |
| 30 | return status; |
| 31 | } |
| 32 | |
| 33 | Status TryStatus(StatusWithSize status) { |
David Rogers | c4dc864 | 2020-09-14 10:52:36 -0700 | [diff] [blame] | 34 | PW_TRY(ReturnStatusWithSize(status)); |
David Rogers | b6b14b8 | 2020-09-11 16:27:27 -0700 | [diff] [blame] | 35 | |
| 36 | // Any status other than OK should have already returned. |
| 37 | EXPECT_EQ(status.status(), Status::OK); |
| 38 | return status.status(); |
| 39 | } |
| 40 | |
| 41 | TEST(Status, Try_Status) { |
| 42 | EXPECT_EQ(TryStatus(Status::OK), Status::OK); |
| 43 | |
| 44 | // Don't need all the status types, just pick a few not-ok ones. |
| 45 | EXPECT_EQ(TryStatus(Status::CANCELLED), Status::CANCELLED); |
| 46 | EXPECT_EQ(TryStatus(Status::DATA_LOSS), Status::DATA_LOSS); |
| 47 | EXPECT_EQ(TryStatus(Status::UNIMPLEMENTED), Status::UNIMPLEMENTED); |
| 48 | } |
| 49 | |
| 50 | TEST(Status, Try_StatusWithSizeOk) { |
| 51 | for (size_t i = 0; i < 32; ++i) { |
| 52 | StatusWithSize val(Status::OK, 0); |
| 53 | EXPECT_EQ(TryStatus(val), Status::OK); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | TEST(Status, Try_StatusWithSizeError) { |
| 58 | for (size_t i = 0; i < 32; ++i) { |
| 59 | StatusWithSize val(Status::DATA_LOSS, i); |
| 60 | EXPECT_EQ(TryStatus(val), Status::DATA_LOSS); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | TEST(Status, Try_StatusWithSizeFromConstant) { |
| 65 | // Don't need all the status types, just pick a few not-ok ones. |
| 66 | EXPECT_EQ(TryStatus(StatusWithSize::CANCELLED), Status::CANCELLED); |
| 67 | EXPECT_EQ(TryStatus(StatusWithSize::DATA_LOSS), Status::DATA_LOSS); |
| 68 | EXPECT_EQ(TryStatus(StatusWithSize::UNIMPLEMENTED), Status::UNIMPLEMENTED); |
| 69 | } |
| 70 | |
| 71 | Status TryStatusAssign(size_t& size_val, StatusWithSize status) { |
| 72 | PW_TRY_ASSIGN(size_val, ReturnStatusWithSize(status)); |
| 73 | |
| 74 | // Any status other than OK should have already returned. |
| 75 | EXPECT_EQ(status.status(), Status::OK); |
| 76 | EXPECT_EQ(size_val, status.size()); |
| 77 | return status.status(); |
| 78 | } |
| 79 | |
| 80 | TEST(Status, TryAssignOk) { |
| 81 | size_t size_val = 0; |
| 82 | |
| 83 | for (size_t i = 1; i < 32; ++i) { |
| 84 | StatusWithSize val(Status::OK, i); |
| 85 | EXPECT_EQ(TryStatusAssign(size_val, val), Status::OK); |
| 86 | EXPECT_EQ(size_val, i); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | TEST(Status, TryAssignError) { |
| 91 | size_t size_val = 0u; |
| 92 | |
| 93 | for (size_t i = 1; i < 32; ++i) { |
| 94 | StatusWithSize val(Status::OUT_OF_RANGE, i); |
| 95 | EXPECT_EQ(TryStatusAssign(size_val, val), Status::OUT_OF_RANGE); |
| 96 | EXPECT_EQ(size_val, 0u); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | StatusWithSize TryStatusWithSize(StatusWithSize status) { |
David Rogers | c4dc864 | 2020-09-14 10:52:36 -0700 | [diff] [blame] | 101 | PW_TRY_WITH_SIZE(ReturnStatusWithSize(status)); |
David Rogers | b6b14b8 | 2020-09-11 16:27:27 -0700 | [diff] [blame] | 102 | |
| 103 | // Any status other than OK should have already returned. |
| 104 | EXPECT_TRUE(status.ok()); |
| 105 | return status; |
| 106 | } |
| 107 | |
| 108 | StatusWithSize TryStatusWithSize(Status status) { |
David Rogers | c4dc864 | 2020-09-14 10:52:36 -0700 | [diff] [blame] | 109 | PW_TRY_WITH_SIZE(ReturnStatus(status)); |
David Rogers | b6b14b8 | 2020-09-11 16:27:27 -0700 | [diff] [blame] | 110 | |
| 111 | // Any status other than OK should have already returned. |
| 112 | EXPECT_EQ(status, Status::OK); |
| 113 | |
| 114 | StatusWithSize return_val(status, 0u); |
| 115 | return return_val; |
| 116 | } |
| 117 | |
| 118 | TEST(Status, TryWithSize_StatusOk) { |
| 119 | StatusWithSize result = TryStatusWithSize(Status::OK); |
| 120 | EXPECT_EQ(result.status(), Status::OK); |
| 121 | EXPECT_EQ(result.size(), 0u); |
| 122 | } |
| 123 | |
| 124 | TEST(Status, TryWithSize_StatusError) { |
| 125 | StatusWithSize result = TryStatusWithSize(Status::PERMISSION_DENIED); |
| 126 | EXPECT_EQ(result.status(), Status::PERMISSION_DENIED); |
| 127 | EXPECT_EQ(result.size(), 0u); |
| 128 | } |
| 129 | |
| 130 | TEST(Status, TryWithSize_StatusWithSizeOk) { |
| 131 | for (size_t i = 0; i < 32; ++i) { |
| 132 | StatusWithSize val(Status::OK, i); |
| 133 | EXPECT_EQ(TryStatusWithSize(val).status(), Status::OK); |
| 134 | EXPECT_EQ(TryStatusWithSize(val).size(), i); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | TEST(Status, TryWithSize_StatusWithSizeError) { |
| 139 | for (size_t i = 0; i < 32; ++i) { |
| 140 | StatusWithSize val(Status::DATA_LOSS, i); |
| 141 | StatusWithSize result = TryStatusWithSize(val); |
| 142 | EXPECT_EQ(result.status(), Status::DATA_LOSS); |
| 143 | EXPECT_EQ(result.size(), i); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | TEST(Status, TryWithSize_StatusWithSizeConst) { |
| 148 | StatusWithSize result = TryStatusWithSize(StatusWithSize::DATA_LOSS); |
| 149 | EXPECT_EQ(result.status(), Status::DATA_LOSS); |
| 150 | EXPECT_EQ(result.size(), 0u); |
| 151 | |
| 152 | result = TryStatusWithSize(StatusWithSize::NOT_FOUND); |
| 153 | EXPECT_EQ(result.status(), Status::NOT_FOUND); |
| 154 | EXPECT_EQ(result.size(), 0u); |
| 155 | |
| 156 | result = TryStatusWithSize(StatusWithSize::UNIMPLEMENTED); |
| 157 | EXPECT_EQ(result.status(), Status::UNIMPLEMENTED); |
| 158 | EXPECT_EQ(result.size(), 0u); |
| 159 | } |
| 160 | |
| 161 | } // namespace |
| 162 | } // namespace pw |