1 // Copyright 2018 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
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,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "absl/base/internal/errno_saver.h"
16
17 #include <cerrno>
18
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
21
22 namespace {
23 using ::testing::Eq;
24
25 struct ErrnoPrinter {
26 int no;
27 };
operator <<(std::ostream & os,ErrnoPrinter ep)28 std::ostream &operator<<(std::ostream &os, ErrnoPrinter ep) {
29 return os << strerror(ep.no) << " [" << ep.no << "]";
30 }
operator ==(ErrnoPrinter one,ErrnoPrinter two)31 bool operator==(ErrnoPrinter one, ErrnoPrinter two) { return one.no == two.no; }
32
TEST(ErrnoSaverTest,Works)33 TEST(ErrnoSaverTest, Works) {
34 errno = EDOM;
35 {
36 absl::base_internal::ErrnoSaver errno_saver;
37 EXPECT_THAT(ErrnoPrinter{errno}, Eq(ErrnoPrinter{EDOM}));
38 errno = ERANGE;
39 EXPECT_THAT(ErrnoPrinter{errno}, Eq(ErrnoPrinter{ERANGE}));
40 EXPECT_THAT(ErrnoPrinter{errno_saver()}, Eq(ErrnoPrinter{EDOM}));
41 }
42 EXPECT_THAT(ErrnoPrinter{errno}, Eq(ErrnoPrinter{EDOM}));
43 }
44 } // namespace
45