Wyatt Hepler | 421a810 | 2020-01-13 11:30:58 -0800 | [diff] [blame] | 1 | // Copyright 2020 The Pigweed Authors |
Wyatt Hepler | b667928 | 2019-11-12 22:40:17 -0800 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
Wyatt Hepler | 1a96094 | 2019-11-26 14:13:38 -0800 | [diff] [blame] | 4 | // use this file except in compliance with the License. You may obtain a copy of |
| 5 | // the License at |
Wyatt Hepler | b667928 | 2019-11-12 22:40:17 -0800 | [diff] [blame] | 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 |
Wyatt Hepler | 1a96094 | 2019-11-26 14:13:38 -0800 | [diff] [blame] | 12 | // License for the specific language governing permissions and limitations under |
| 13 | // the License. |
Wyatt Hepler | b667928 | 2019-11-12 22:40:17 -0800 | [diff] [blame] | 14 | |
| 15 | #include "pw_status/status_with_size.h" |
| 16 | |
| 17 | #include "gtest/gtest.h" |
| 18 | |
Wyatt Hepler | 463359c | 2019-11-13 17:04:21 -0800 | [diff] [blame] | 19 | namespace pw { |
Wyatt Hepler | b667928 | 2019-11-12 22:40:17 -0800 | [diff] [blame] | 20 | namespace { |
| 21 | |
| 22 | static_assert(StatusWithSize::max_size() == |
Wyatt Hepler | 463359c | 2019-11-13 17:04:21 -0800 | [diff] [blame] | 23 | (static_cast<size_t>(1) << (sizeof(size_t) * 8 - 5)) - 1, |
| 24 | "max_size() should use all but the top 5 bits of a size_t."); |
Wyatt Hepler | b667928 | 2019-11-12 22:40:17 -0800 | [diff] [blame] | 25 | |
| 26 | TEST(StatusWithSize, Default) { |
| 27 | StatusWithSize result; |
| 28 | EXPECT_TRUE(result.ok()); |
Wyatt Hepler | 352fcbc | 2020-09-24 11:15:29 -0700 | [diff] [blame] | 29 | EXPECT_EQ(Status::Ok(), result.status()); |
Wyatt Hepler | b667928 | 2019-11-12 22:40:17 -0800 | [diff] [blame] | 30 | EXPECT_EQ(0u, result.size()); |
| 31 | } |
| 32 | |
| 33 | TEST(StatusWithSize, ConstructWithSize) { |
| 34 | StatusWithSize result = StatusWithSize(456); |
| 35 | EXPECT_TRUE(result.ok()); |
Wyatt Hepler | 352fcbc | 2020-09-24 11:15:29 -0700 | [diff] [blame] | 36 | EXPECT_EQ(Status::Ok(), result.status()); |
Wyatt Hepler | b667928 | 2019-11-12 22:40:17 -0800 | [diff] [blame] | 37 | EXPECT_EQ(456u, result.size()); |
| 38 | } |
| 39 | |
| 40 | TEST(StatusWithSize, ConstructWithError) { |
Wyatt Hepler | 352fcbc | 2020-09-24 11:15:29 -0700 | [diff] [blame] | 41 | StatusWithSize result(Status::ResourceExhausted(), 123); |
Wyatt Hepler | b667928 | 2019-11-12 22:40:17 -0800 | [diff] [blame] | 42 | EXPECT_FALSE(result.ok()); |
Wyatt Hepler | 352fcbc | 2020-09-24 11:15:29 -0700 | [diff] [blame] | 43 | EXPECT_EQ(Status::ResourceExhausted(), result.status()); |
Wyatt Hepler | b667928 | 2019-11-12 22:40:17 -0800 | [diff] [blame] | 44 | EXPECT_EQ(123u, result.size()); |
| 45 | } |
| 46 | |
| 47 | TEST(StatusWithSize, ConstructWithOkAndSize) { |
Wyatt Hepler | 352fcbc | 2020-09-24 11:15:29 -0700 | [diff] [blame] | 48 | StatusWithSize result(Status::Ok(), 99); |
Wyatt Hepler | b667928 | 2019-11-12 22:40:17 -0800 | [diff] [blame] | 49 | EXPECT_TRUE(result.ok()); |
Wyatt Hepler | 352fcbc | 2020-09-24 11:15:29 -0700 | [diff] [blame] | 50 | EXPECT_EQ(Status::Ok(), result.status()); |
Wyatt Hepler | b667928 | 2019-11-12 22:40:17 -0800 | [diff] [blame] | 51 | EXPECT_EQ(99u, result.size()); |
| 52 | } |
| 53 | |
Wyatt Hepler | f707880 | 2020-02-25 13:50:05 -0800 | [diff] [blame] | 54 | TEST(StatusWithSize, ConstructFromConstant) { |
Wyatt Hepler | 352fcbc | 2020-09-24 11:15:29 -0700 | [diff] [blame] | 55 | StatusWithSize result(StatusWithSize::AlreadyExists()); |
Wyatt Hepler | f707880 | 2020-02-25 13:50:05 -0800 | [diff] [blame] | 56 | |
Wyatt Hepler | 352fcbc | 2020-09-24 11:15:29 -0700 | [diff] [blame] | 57 | EXPECT_EQ(Status::AlreadyExists(), result.status()); |
Wyatt Hepler | 50f7077 | 2020-02-13 11:25:10 -0800 | [diff] [blame] | 58 | EXPECT_EQ(0u, result.size()); |
Wyatt Hepler | 50f7077 | 2020-02-13 11:25:10 -0800 | [diff] [blame] | 59 | |
Wyatt Hepler | 352fcbc | 2020-09-24 11:15:29 -0700 | [diff] [blame] | 60 | result = StatusWithSize::NotFound(); |
Wyatt Hepler | f707880 | 2020-02-25 13:50:05 -0800 | [diff] [blame] | 61 | |
Wyatt Hepler | 352fcbc | 2020-09-24 11:15:29 -0700 | [diff] [blame] | 62 | EXPECT_EQ(Status::NotFound(), result.status()); |
Wyatt Hepler | 50f7077 | 2020-02-13 11:25:10 -0800 | [diff] [blame] | 63 | EXPECT_EQ(0u, result.size()); |
| 64 | } |
| 65 | |
Wyatt Hepler | b667928 | 2019-11-12 22:40:17 -0800 | [diff] [blame] | 66 | TEST(StatusWithSize, AllStatusValues_ZeroSize) { |
| 67 | for (int i = 0; i < 32; ++i) { |
| 68 | StatusWithSize result(static_cast<Status::Code>(i), 0); |
| 69 | EXPECT_EQ(result.ok(), i == 0); |
Wyatt Hepler | 421a810 | 2020-01-13 11:30:58 -0800 | [diff] [blame] | 70 | EXPECT_EQ(i, static_cast<int>(result.status())); |
Wyatt Hepler | b667928 | 2019-11-12 22:40:17 -0800 | [diff] [blame] | 71 | EXPECT_EQ(0u, result.size()); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | TEST(StatusWithSize, AllStatusValues_SameSize) { |
| 76 | for (int i = 0; i < 32; ++i) { |
| 77 | StatusWithSize result(static_cast<Status::Code>(i), i); |
| 78 | EXPECT_EQ(result.ok(), i == 0); |
Wyatt Hepler | 421a810 | 2020-01-13 11:30:58 -0800 | [diff] [blame] | 79 | EXPECT_EQ(i, static_cast<int>(result.status())); |
Wyatt Hepler | b667928 | 2019-11-12 22:40:17 -0800 | [diff] [blame] | 80 | EXPECT_EQ(static_cast<size_t>(i), result.size()); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | TEST(StatusWithSize, AllStatusValues_MaxSize) { |
| 85 | for (int i = 0; i < 32; ++i) { |
| 86 | StatusWithSize result(static_cast<Status::Code>(i), |
| 87 | StatusWithSize::max_size()); |
| 88 | EXPECT_EQ(result.ok(), i == 0); |
Wyatt Hepler | 421a810 | 2020-01-13 11:30:58 -0800 | [diff] [blame] | 89 | EXPECT_EQ(i, static_cast<int>(result.status())); |
Wyatt Hepler | b667928 | 2019-11-12 22:40:17 -0800 | [diff] [blame] | 90 | EXPECT_EQ(result.max_size(), result.size()); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | TEST(StatusWithSize, Assignment) { |
Wyatt Hepler | 352fcbc | 2020-09-24 11:15:29 -0700 | [diff] [blame] | 95 | StatusWithSize result = StatusWithSize(Status::Internal(), 0x123); |
Wyatt Hepler | b667928 | 2019-11-12 22:40:17 -0800 | [diff] [blame] | 96 | EXPECT_FALSE(result.ok()); |
Wyatt Hepler | 352fcbc | 2020-09-24 11:15:29 -0700 | [diff] [blame] | 97 | EXPECT_EQ(Status::Internal(), result.status()); |
Wyatt Hepler | b667928 | 2019-11-12 22:40:17 -0800 | [diff] [blame] | 98 | EXPECT_EQ(0x123u, result.size()); |
| 99 | |
| 100 | result = StatusWithSize(300); |
| 101 | EXPECT_TRUE(result.ok()); |
Wyatt Hepler | 352fcbc | 2020-09-24 11:15:29 -0700 | [diff] [blame] | 102 | EXPECT_EQ(Status::Ok(), result.status()); |
Wyatt Hepler | b667928 | 2019-11-12 22:40:17 -0800 | [diff] [blame] | 103 | EXPECT_EQ(300u, result.size()); |
| 104 | } |
| 105 | |
| 106 | TEST(StatusWithSize, Constexpr) { |
Wyatt Hepler | 352fcbc | 2020-09-24 11:15:29 -0700 | [diff] [blame] | 107 | constexpr StatusWithSize result(Status::Cancelled(), 1234); |
| 108 | static_assert(Status::Cancelled() == result.status()); |
Wyatt Hepler | b667928 | 2019-11-12 22:40:17 -0800 | [diff] [blame] | 109 | static_assert(!result.ok()); |
| 110 | static_assert(1234u == result.size()); |
| 111 | } |
| 112 | |
Wyatt Hepler | 352fcbc | 2020-09-24 11:15:29 -0700 | [diff] [blame] | 113 | TEST(Status, Functions_Status) { |
| 114 | // clang-format off |
| 115 | static_assert(StatusWithSize::Ok(0).status() == Status::Ok()); |
| 116 | static_assert(StatusWithSize::Cancelled().status() == Status::Cancelled()); |
| 117 | static_assert(StatusWithSize::Unknown().status() == Status::Unknown()); |
| 118 | static_assert(StatusWithSize::InvalidArgument().status() == Status::InvalidArgument()); |
| 119 | static_assert(StatusWithSize::DeadlineExceeded().status() == Status::DeadlineExceeded()); |
| 120 | static_assert(StatusWithSize::NotFound().status() == Status::NotFound()); |
| 121 | static_assert(StatusWithSize::AlreadyExists().status() == Status::AlreadyExists()); |
| 122 | static_assert(StatusWithSize::PermissionDenied().status() == Status::PermissionDenied()); |
| 123 | static_assert(StatusWithSize::Unauthenticated().status() == Status::Unauthenticated()); |
| 124 | static_assert(StatusWithSize::ResourceExhausted().status() == Status::ResourceExhausted()); |
| 125 | static_assert(StatusWithSize::FailedPrecondition().status() == Status::FailedPrecondition()); |
| 126 | static_assert(StatusWithSize::Aborted().status() == Status::Aborted()); |
| 127 | static_assert(StatusWithSize::OutOfRange().status() == Status::OutOfRange()); |
| 128 | static_assert(StatusWithSize::Unimplemented().status() == Status::Unimplemented()); |
| 129 | static_assert(StatusWithSize::Internal().status() == Status::Internal()); |
| 130 | static_assert(StatusWithSize::Unavailable().status() == Status::Unavailable()); |
| 131 | static_assert(StatusWithSize::DataLoss().status() == Status::DataLoss()); |
| 132 | |
| 133 | static_assert(StatusWithSize::Ok(123).status() == Status::Ok()); |
| 134 | static_assert(StatusWithSize::Cancelled(123).status() == Status::Cancelled()); |
| 135 | static_assert(StatusWithSize::Unknown(123).status() == Status::Unknown()); |
| 136 | static_assert(StatusWithSize::InvalidArgument(123).status() == Status::InvalidArgument()); |
| 137 | static_assert(StatusWithSize::DeadlineExceeded(123).status() == Status::DeadlineExceeded()); |
| 138 | static_assert(StatusWithSize::NotFound(123).status() == Status::NotFound()); |
| 139 | static_assert(StatusWithSize::AlreadyExists(123).status() == Status::AlreadyExists()); |
| 140 | static_assert(StatusWithSize::PermissionDenied(123).status() == Status::PermissionDenied()); |
| 141 | static_assert(StatusWithSize::Unauthenticated(123).status() == Status::Unauthenticated()); |
| 142 | static_assert(StatusWithSize::ResourceExhausted(123).status() == Status::ResourceExhausted()); |
| 143 | static_assert(StatusWithSize::FailedPrecondition(123).status() == Status::FailedPrecondition()); |
| 144 | static_assert(StatusWithSize::Aborted(123).status() == Status::Aborted()); |
| 145 | static_assert(StatusWithSize::OutOfRange(123).status() == Status::OutOfRange()); |
| 146 | static_assert(StatusWithSize::Unimplemented(123).status() == Status::Unimplemented()); |
| 147 | static_assert(StatusWithSize::Internal(123).status() == Status::Internal()); |
| 148 | static_assert(StatusWithSize::Unavailable(123).status() == Status::Unavailable()); |
| 149 | static_assert(StatusWithSize::DataLoss(123).status() == Status::DataLoss()); |
| 150 | // clang-format on |
| 151 | } |
| 152 | |
| 153 | TEST(Status, Functions_DefaultSize) { |
| 154 | static_assert(StatusWithSize::Cancelled().size() == 0u); |
| 155 | static_assert(StatusWithSize::Unknown().size() == 0u); |
| 156 | static_assert(StatusWithSize::InvalidArgument().size() == 0u); |
| 157 | static_assert(StatusWithSize::DeadlineExceeded().size() == 0u); |
| 158 | static_assert(StatusWithSize::NotFound().size() == 0u); |
| 159 | static_assert(StatusWithSize::AlreadyExists().size() == 0u); |
| 160 | static_assert(StatusWithSize::PermissionDenied().size() == 0u); |
| 161 | static_assert(StatusWithSize::Unauthenticated().size() == 0u); |
| 162 | static_assert(StatusWithSize::ResourceExhausted().size() == 0u); |
| 163 | static_assert(StatusWithSize::FailedPrecondition().size() == 0u); |
| 164 | static_assert(StatusWithSize::Aborted().size() == 0u); |
| 165 | static_assert(StatusWithSize::OutOfRange().size() == 0u); |
| 166 | static_assert(StatusWithSize::Unimplemented().size() == 0u); |
| 167 | static_assert(StatusWithSize::Internal().size() == 0u); |
| 168 | static_assert(StatusWithSize::Unavailable().size() == 0u); |
| 169 | static_assert(StatusWithSize::DataLoss().size() == 0u); |
| 170 | } |
| 171 | |
| 172 | TEST(Status, Functions_SpecifiedSize) { |
| 173 | static_assert(StatusWithSize::Ok(123).size() == 123u); |
| 174 | static_assert(StatusWithSize::Cancelled(123).size() == 123u); |
| 175 | static_assert(StatusWithSize::Unknown(123).size() == 123u); |
| 176 | static_assert(StatusWithSize::InvalidArgument(123).size() == 123u); |
| 177 | static_assert(StatusWithSize::DeadlineExceeded(123).size() == 123u); |
| 178 | static_assert(StatusWithSize::NotFound(123).size() == 123u); |
| 179 | static_assert(StatusWithSize::AlreadyExists(123).size() == 123u); |
| 180 | static_assert(StatusWithSize::PermissionDenied(123).size() == 123u); |
| 181 | static_assert(StatusWithSize::Unauthenticated(123).size() == 123u); |
| 182 | static_assert(StatusWithSize::ResourceExhausted(123).size() == 123u); |
| 183 | static_assert(StatusWithSize::FailedPrecondition(123).size() == 123u); |
| 184 | static_assert(StatusWithSize::Aborted(123).size() == 123u); |
| 185 | static_assert(StatusWithSize::OutOfRange(123).size() == 123u); |
| 186 | static_assert(StatusWithSize::Unimplemented(123).size() == 123u); |
| 187 | static_assert(StatusWithSize::Internal(123).size() == 123u); |
| 188 | static_assert(StatusWithSize::Unavailable(123).size() == 123u); |
| 189 | static_assert(StatusWithSize::DataLoss(123).size() == 123u); |
| 190 | } |
| 191 | |
Wyatt Hepler | b667928 | 2019-11-12 22:40:17 -0800 | [diff] [blame] | 192 | } // namespace |
Wyatt Hepler | 463359c | 2019-11-13 17:04:21 -0800 | [diff] [blame] | 193 | } // namespace pw |