blob: 473384a981927fbec8464fc54e6453d79fba59d8 [file] [log] [blame]
Wyatt Hepler421a8102020-01-13 11:30:58 -08001// Copyright 2020 The Pigweed Authors
Wyatt Heplerb6679282019-11-12 22:40:17 -08002//
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
Wyatt Heplerf7078802020-02-25 13:50:05 -080054TEST(StatusWithSize, ConstructFromConstant) {
55 StatusWithSize result(StatusWithSize::ALREADY_EXISTS);
56
Wyatt Hepler50f70772020-02-13 11:25:10 -080057 EXPECT_EQ(Status::ALREADY_EXISTS, result.status());
58 EXPECT_EQ(0u, result.size());
Wyatt Hepler50f70772020-02-13 11:25:10 -080059
Wyatt Heplerf7078802020-02-25 13:50:05 -080060 result = StatusWithSize::NOT_FOUND;
61
62 EXPECT_EQ(Status::NOT_FOUND, result.status());
Wyatt Hepler50f70772020-02-13 11:25:10 -080063 EXPECT_EQ(0u, result.size());
64}
65
Wyatt Heplerb6679282019-11-12 22:40:17 -080066TEST(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 Hepler421a8102020-01-13 11:30:58 -080070 EXPECT_EQ(i, static_cast<int>(result.status()));
Wyatt Heplerb6679282019-11-12 22:40:17 -080071 EXPECT_EQ(0u, result.size());
72 }
73}
74
75TEST(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 Hepler421a8102020-01-13 11:30:58 -080079 EXPECT_EQ(i, static_cast<int>(result.status()));
Wyatt Heplerb6679282019-11-12 22:40:17 -080080 EXPECT_EQ(static_cast<size_t>(i), result.size());
81 }
82}
83
84TEST(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 Hepler421a8102020-01-13 11:30:58 -080089 EXPECT_EQ(i, static_cast<int>(result.status()));
Wyatt Heplerb6679282019-11-12 22:40:17 -080090 EXPECT_EQ(result.max_size(), result.size());
91 }
92}
93
94TEST(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
106TEST(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 Hepler463359c2019-11-13 17:04:21 -0800114} // namespace pw