errno_saver_test.cc 1.4 KB

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