blob: 280b8be1326471c965c766c1ad4def6cdec0a9f1 [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
Wyatt Hepler2afeb0e2020-09-23 14:56:59 -070015// Make sure status works if these macros are defined.
Wyatt Hepler352fcbc2020-09-24 11:15:29 -070016// TODO(pwbug/268): Remove these macros after migrating from these aliases.
Wyatt Hepler2afeb0e2020-09-23 14:56:59 -070017#define OK Uh oh, this macro is defined !
18#define CANCELLED Uh oh, this macro is defined !
19#define UNKNOWN Uh oh, this macro is defined !
20#define INVALID_ARGUMENT Uh oh, this macro is defined !
21#define DEADLINE_EXCEEDED Uh oh, this macro is defined !
22#define NOT_FOUND Uh oh, this macro is defined !
23#define ALREADY_EXISTS Uh oh, this macro is defined !
24#define PERMISSION_DENIED Uh oh, this macro is defined !
25#define UNAUTHENTICATED Uh oh, this macro is defined !
26#define RESOURCE_EXHAUSTED Uh oh, this macro is defined !
27#define FAILED_PRECONDITION Uh oh, this macro is defined !
28#define ABORTED Uh oh, this macro is defined !
29#define OUT_OF_RANGE Uh oh, this macro is defined !
30#define UNIMPLEMENTED Uh oh, this macro is defined !
31#define INTERNAL Uh oh, this macro is defined !
32#define UNAVAILABLE Uh oh, this macro is defined !
33#define DATA_LOSS Uh oh, this macro is defined !
34
Wyatt Heplerb6679282019-11-12 22:40:17 -080035#include "pw_status/status.h"
36
37#include "gtest/gtest.h"
38
Wyatt Hepler463359c2019-11-13 17:04:21 -080039namespace pw {
Wyatt Heplerb6679282019-11-12 22:40:17 -080040namespace {
41
42constexpr Status::Code kInvalidCode = static_cast<Status::Code>(30);
43
44TEST(Status, Default) {
Wyatt Hepler352fcbc2020-09-24 11:15:29 -070045 constexpr Status status;
46 static_assert(status.ok());
47 static_assert(Status() == status);
Wyatt Heplerb6679282019-11-12 22:40:17 -080048}
49
50TEST(Status, ConstructWithStatusCode) {
Wyatt Hepler352fcbc2020-09-24 11:15:29 -070051 constexpr Status status(PW_STATUS_ABORTED);
52 static_assert(Status::Aborted() == status);
Wyatt Heplerb6679282019-11-12 22:40:17 -080053}
54
55TEST(Status, AssignFromStatusCode) {
56 Status status;
Wyatt Hepler352fcbc2020-09-24 11:15:29 -070057 status = PW_STATUS_INTERNAL;
58 EXPECT_EQ(Status::Internal(), status);
Wyatt Heplerb6679282019-11-12 22:40:17 -080059}
60
61TEST(Status, Ok_OkIsTrue) {
Wyatt Hepler352fcbc2020-09-24 11:15:29 -070062 static_assert(Status().ok());
63 static_assert(Status(PW_STATUS_OK).ok());
64 static_assert(Status::Ok().ok());
Wyatt Heplerb6679282019-11-12 22:40:17 -080065}
66
67TEST(Status, NotOk_OkIsFalse) {
Wyatt Hepler352fcbc2020-09-24 11:15:29 -070068 static_assert(!Status::DataLoss().ok());
69 static_assert(!Status(kInvalidCode).ok());
Wyatt Heplerb6679282019-11-12 22:40:17 -080070}
71
Wyatt Hepler352fcbc2020-09-24 11:15:29 -070072TEST(Status, EqualCodes) {
73 static_assert(PW_STATUS_OK == Status());
74 static_assert(PW_STATUS_OK == Status::Ok());
75 static_assert(PW_STATUS_CANCELLED == Status::Cancelled());
76 static_assert(PW_STATUS_UNKNOWN == Status::Unknown());
77 static_assert(PW_STATUS_INVALID_ARGUMENT == Status::InvalidArgument());
78 static_assert(PW_STATUS_DEADLINE_EXCEEDED == Status::DeadlineExceeded());
79 static_assert(PW_STATUS_NOT_FOUND == Status::NotFound());
80 static_assert(PW_STATUS_ALREADY_EXISTS == Status::AlreadyExists());
81 static_assert(PW_STATUS_PERMISSION_DENIED == Status::PermissionDenied());
Wyatt Hepler352fcbc2020-09-24 11:15:29 -070082 static_assert(PW_STATUS_RESOURCE_EXHAUSTED == Status::ResourceExhausted());
83 static_assert(PW_STATUS_FAILED_PRECONDITION == Status::FailedPrecondition());
84 static_assert(PW_STATUS_ABORTED == Status::Aborted());
85 static_assert(PW_STATUS_OUT_OF_RANGE == Status::OutOfRange());
86 static_assert(PW_STATUS_UNIMPLEMENTED == Status::Unimplemented());
87 static_assert(PW_STATUS_INTERNAL == Status::Internal());
88 static_assert(PW_STATUS_UNAVAILABLE == Status::Unavailable());
89 static_assert(PW_STATUS_DATA_LOSS == Status::DataLoss());
Wyatt Hepler2cbb3132020-09-30 11:28:46 -070090 static_assert(PW_STATUS_UNAUTHENTICATED == Status::Unauthenticated());
Wyatt Hepler352fcbc2020-09-24 11:15:29 -070091}
92
93TEST(Status, Strings) {
94 EXPECT_STREQ("OK", Status().str());
95 EXPECT_STREQ("OK", Status::Ok().str());
96 EXPECT_STREQ("CANCELLED", Status::Cancelled().str());
97 EXPECT_STREQ("UNKNOWN", Status::Unknown().str());
98 EXPECT_STREQ("INVALID_ARGUMENT", Status::InvalidArgument().str());
99 EXPECT_STREQ("DEADLINE_EXCEEDED", Status::DeadlineExceeded().str());
100 EXPECT_STREQ("NOT_FOUND", Status::NotFound().str());
101 EXPECT_STREQ("ALREADY_EXISTS", Status::AlreadyExists().str());
102 EXPECT_STREQ("PERMISSION_DENIED", Status::PermissionDenied().str());
Wyatt Hepler352fcbc2020-09-24 11:15:29 -0700103 EXPECT_STREQ("RESOURCE_EXHAUSTED", Status::ResourceExhausted().str());
104 EXPECT_STREQ("FAILED_PRECONDITION", Status::FailedPrecondition().str());
105 EXPECT_STREQ("ABORTED", Status::Aborted().str());
106 EXPECT_STREQ("OUT_OF_RANGE", Status::OutOfRange().str());
107 EXPECT_STREQ("UNIMPLEMENTED", Status::Unimplemented().str());
108 EXPECT_STREQ("INTERNAL", Status::Internal().str());
109 EXPECT_STREQ("UNAVAILABLE", Status::Unavailable().str());
110 EXPECT_STREQ("DATA_LOSS", Status::DataLoss().str());
Wyatt Hepler2cbb3132020-09-30 11:28:46 -0700111 EXPECT_STREQ("UNAUTHENTICATED", Status::Unauthenticated().str());
Wyatt Heplerb6679282019-11-12 22:40:17 -0800112}
113
114TEST(Status, UnknownString) {
Wyatt Hepler352fcbc2020-09-24 11:15:29 -0700115 EXPECT_STREQ("INVALID STATUS", Status(kInvalidCode).str());
116}
117
118TEST(Status, DeprecatedAliases) {
119 // TODO(pwbug/268): Remove this test after migrating from these aliases.
120 static_assert(PW_STATUS_OK == Status::OK);
121 static_assert(PW_STATUS_CANCELLED == Status::CANCELLED);
122 static_assert(PW_STATUS_UNKNOWN == Status::UNKNOWN);
123 static_assert(PW_STATUS_INVALID_ARGUMENT == Status::INVALID_ARGUMENT);
124 static_assert(PW_STATUS_DEADLINE_EXCEEDED == Status::DEADLINE_EXCEEDED);
125 static_assert(PW_STATUS_NOT_FOUND == Status::NOT_FOUND);
126 static_assert(PW_STATUS_ALREADY_EXISTS == Status::ALREADY_EXISTS);
127 static_assert(PW_STATUS_PERMISSION_DENIED == Status::PERMISSION_DENIED);
Wyatt Hepler352fcbc2020-09-24 11:15:29 -0700128 static_assert(PW_STATUS_RESOURCE_EXHAUSTED == Status::RESOURCE_EXHAUSTED);
129 static_assert(PW_STATUS_FAILED_PRECONDITION == Status::FAILED_PRECONDITION);
130 static_assert(PW_STATUS_ABORTED == Status::ABORTED);
131 static_assert(PW_STATUS_OUT_OF_RANGE == Status::OUT_OF_RANGE);
132 static_assert(PW_STATUS_UNIMPLEMENTED == Status::UNIMPLEMENTED);
133 static_assert(PW_STATUS_INTERNAL == Status::INTERNAL);
134 static_assert(PW_STATUS_UNAVAILABLE == Status::UNAVAILABLE);
135 static_assert(PW_STATUS_DATA_LOSS == Status::DATA_LOSS);
Wyatt Hepler2cbb3132020-09-30 11:28:46 -0700136 static_assert(PW_STATUS_UNAUTHENTICATED == Status::UNAUTHENTICATED);
Wyatt Heplerb6679282019-11-12 22:40:17 -0800137}
138
Wyatt Hepler421a8102020-01-13 11:30:58 -0800139// Functions for executing the C pw_Status tests.
140extern "C" {
141
142Status::Code PassStatusFromC(Status status);
143
144Status::Code PassStatusFromCpp(Status status) { return status; }
145
146int TestStatusFromC(void);
147
148int TestStatusStringsFromC(void);
149
150} // extern "C"
151
152TEST(StatusCLinkage, CallCFunctionWithStatus) {
Wyatt Hepler352fcbc2020-09-24 11:15:29 -0700153 EXPECT_EQ(Status::Aborted(), PassStatusFromC(PW_STATUS_ABORTED));
154 EXPECT_EQ(Status::Unknown(), PassStatusFromC(Status::Unknown()));
Wyatt Hepler421a8102020-01-13 11:30:58 -0800155
Wyatt Hepler352fcbc2020-09-24 11:15:29 -0700156 EXPECT_EQ(Status::NotFound(), PassStatusFromC(PW_STATUS_NOT_FOUND));
157 EXPECT_EQ(Status::Ok(), PassStatusFromC(Status::Ok()));
Wyatt Hepler421a8102020-01-13 11:30:58 -0800158}
159
160TEST(StatusCLinkage, TestStatusFromC) { EXPECT_EQ(0, TestStatusFromC()); }
161
162TEST(StatusCLinkage, TestStatusStringsFromC) {
163 EXPECT_EQ(0, TestStatusStringsFromC());
164}
165
Wyatt Heplerb6679282019-11-12 22:40:17 -0800166} // namespace
Wyatt Hepler463359c2019-11-13 17:04:21 -0800167} // namespace pw