blob: 772f8fe45be4a0db6606e2f9ca8cf9f140a4b287 [file] [log] [blame]
Wyatt Heplerb6679282019-11-12 22:40:17 -08001// Copyright 2019 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
Wyatt Hepler1a960942019-11-26 14:13:38 -08004// use this file except in compliance with the License. You may obtain a copy of
5// the License at
Wyatt Heplerb6679282019-11-12 22:40:17 -08006//
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 Hepler1a960942019-11-26 14:13:38 -080012// License for the specific language governing permissions and limitations under
13// the License.
Wyatt Heplerb6679282019-11-12 22:40:17 -080014
15#include "pw_status/status_with_size.h"
16
17#include "gtest/gtest.h"
18
Wyatt Hepler463359c2019-11-13 17:04:21 -080019namespace pw {
Wyatt Heplerb6679282019-11-12 22:40:17 -080020namespace {
21
22static_assert(StatusWithSize::max_size() ==
Wyatt Hepler463359c2019-11-13 17:04:21 -080023 (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 Heplerb6679282019-11-12 22:40:17 -080025
26TEST(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
33TEST(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
40TEST(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
47TEST(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
54TEST(StatusWithSize, AllStatusValues_ZeroSize) {
55 for (int i = 0; i < 32; ++i) {
56 StatusWithSize result(static_cast<Status::Code>(i), 0);
57 EXPECT_EQ(result.ok(), i == 0);
58 EXPECT_EQ(i, static_cast<int>(result.status().code()));
59 EXPECT_EQ(0u, result.size());
60 }
61}
62
63TEST(StatusWithSize, AllStatusValues_SameSize) {
64 for (int i = 0; i < 32; ++i) {
65 StatusWithSize result(static_cast<Status::Code>(i), i);
66 EXPECT_EQ(result.ok(), i == 0);
67 EXPECT_EQ(i, static_cast<int>(result.status().code()));
68 EXPECT_EQ(static_cast<size_t>(i), result.size());
69 }
70}
71
72TEST(StatusWithSize, AllStatusValues_MaxSize) {
73 for (int i = 0; i < 32; ++i) {
74 StatusWithSize result(static_cast<Status::Code>(i),
75 StatusWithSize::max_size());
76 EXPECT_EQ(result.ok(), i == 0);
77 EXPECT_EQ(i, static_cast<int>(result.status().code()));
78 EXPECT_EQ(result.max_size(), result.size());
79 }
80}
81
82TEST(StatusWithSize, Assignment) {
83 StatusWithSize result = StatusWithSize(Status::INTERNAL, 0x123);
84 EXPECT_FALSE(result.ok());
85 EXPECT_EQ(Status::INTERNAL, result.status());
86 EXPECT_EQ(0x123u, result.size());
87
88 result = StatusWithSize(300);
89 EXPECT_TRUE(result.ok());
90 EXPECT_EQ(Status::OK, result.status());
91 EXPECT_EQ(300u, result.size());
92}
93
94TEST(StatusWithSize, Constexpr) {
95 constexpr StatusWithSize result(Status::CANCELLED, 1234);
96 static_assert(Status::CANCELLED == result.status());
97 static_assert(!result.ok());
98 static_assert(1234u == result.size());
99}
100
101} // namespace
Wyatt Hepler463359c2019-11-13 17:04:21 -0800102} // namespace pw