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()); |
| 29 | EXPECT_EQ(Status::OK, result.status()); |
| 30 | EXPECT_EQ(0u, result.size()); |
| 31 | } |
| 32 | |
| 33 | TEST(StatusWithSize, ConstructWithSize) { |
| 34 | StatusWithSize result = StatusWithSize(456); |
| 35 | EXPECT_TRUE(result.ok()); |
| 36 | EXPECT_EQ(Status::OK, result.status()); |
| 37 | EXPECT_EQ(456u, result.size()); |
| 38 | } |
| 39 | |
| 40 | TEST(StatusWithSize, ConstructWithError) { |
| 41 | StatusWithSize result(Status::RESOURCE_EXHAUSTED, 123); |
| 42 | EXPECT_FALSE(result.ok()); |
| 43 | EXPECT_EQ(Status::RESOURCE_EXHAUSTED, result.status()); |
| 44 | EXPECT_EQ(123u, result.size()); |
| 45 | } |
| 46 | |
| 47 | TEST(StatusWithSize, ConstructWithOkAndSize) { |
| 48 | StatusWithSize result(Status::OK, 99); |
| 49 | EXPECT_TRUE(result.ok()); |
| 50 | EXPECT_EQ(Status::OK, result.status()); |
| 51 | EXPECT_EQ(99u, result.size()); |
| 52 | } |
| 53 | |
Wyatt Hepler | f707880 | 2020-02-25 13:50:05 -0800 | [diff] [blame] | 54 | TEST(StatusWithSize, ConstructFromConstant) { |
| 55 | StatusWithSize result(StatusWithSize::ALREADY_EXISTS); |
| 56 | |
Wyatt Hepler | 50f7077 | 2020-02-13 11:25:10 -0800 | [diff] [blame] | 57 | EXPECT_EQ(Status::ALREADY_EXISTS, result.status()); |
| 58 | EXPECT_EQ(0u, result.size()); |
Wyatt Hepler | 50f7077 | 2020-02-13 11:25:10 -0800 | [diff] [blame] | 59 | |
Wyatt Hepler | f707880 | 2020-02-25 13:50:05 -0800 | [diff] [blame] | 60 | result = StatusWithSize::NOT_FOUND; |
| 61 | |
| 62 | EXPECT_EQ(Status::NOT_FOUND, 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) { |
| 95 | StatusWithSize result = StatusWithSize(Status::INTERNAL, 0x123); |
| 96 | EXPECT_FALSE(result.ok()); |
| 97 | EXPECT_EQ(Status::INTERNAL, result.status()); |
| 98 | EXPECT_EQ(0x123u, result.size()); |
| 99 | |
| 100 | result = StatusWithSize(300); |
| 101 | EXPECT_TRUE(result.ok()); |
| 102 | EXPECT_EQ(Status::OK, result.status()); |
| 103 | EXPECT_EQ(300u, result.size()); |
| 104 | } |
| 105 | |
| 106 | TEST(StatusWithSize, Constexpr) { |
| 107 | constexpr StatusWithSize result(Status::CANCELLED, 1234); |
| 108 | static_assert(Status::CANCELLED == result.status()); |
| 109 | static_assert(!result.ok()); |
| 110 | static_assert(1234u == result.size()); |
| 111 | } |
| 112 | |
| 113 | } // namespace |
Wyatt Hepler | 463359c | 2019-11-13 17:04:21 -0800 | [diff] [blame] | 114 | } // namespace pw |