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 #include "absl/base/internal/strerror.h"
22 
23 namespace {
24 using ::testing::Eq;
25 
26 struct ErrnoPrinter {
27   int no;
28 };
operator <<(std::ostream & os,ErrnoPrinter ep)29 std::ostream &operator<<(std::ostream &os, ErrnoPrinter ep) {
30   return os << absl::base_internal::StrError(ep.no) << " [" << ep.no << "]";
31 }
operator ==(ErrnoPrinter one,ErrnoPrinter two)32 bool operator==(ErrnoPrinter one, ErrnoPrinter two) { return one.no == two.no; }
33 
TEST(ErrnoSaverTest,Works)34 TEST(ErrnoSaverTest, Works) {
35   errno = EDOM;
36   {
37     absl::base_internal::ErrnoSaver errno_saver;
38     EXPECT_THAT(ErrnoPrinter{errno}, Eq(ErrnoPrinter{EDOM}));
39     errno = ERANGE;
40     EXPECT_THAT(ErrnoPrinter{errno}, Eq(ErrnoPrinter{ERANGE}));
41     EXPECT_THAT(ErrnoPrinter{errno_saver()}, Eq(ErrnoPrinter{EDOM}));
42   }
43   EXPECT_THAT(ErrnoPrinter{errno}, Eq(ErrnoPrinter{EDOM}));
44 }
45 }  // namespace
46