errno_unittest.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. #include <gtest/gtest.h>
  18. #include "butil/errno.h"
  19. class ErrnoTest : public ::testing::Test{
  20. protected:
  21. ErrnoTest(){
  22. };
  23. virtual ~ErrnoTest(){};
  24. virtual void SetUp() {
  25. };
  26. virtual void TearDown() {
  27. };
  28. };
  29. #define ESTOP -114
  30. #define EBREAK -115
  31. #define ESTH -116
  32. #define EOK -117
  33. #define EMYERROR -30
  34. BAIDU_REGISTER_ERRNO(ESTOP, "the thread is stopping")
  35. BAIDU_REGISTER_ERRNO(EBREAK, "the thread is interrupted")
  36. BAIDU_REGISTER_ERRNO(ESTH, "something happened")
  37. BAIDU_REGISTER_ERRNO(EOK, "OK!")
  38. BAIDU_REGISTER_ERRNO(EMYERROR, "my error");
  39. TEST_F(ErrnoTest, system_errno) {
  40. errno = EPIPE;
  41. ASSERT_STREQ("Broken pipe", berror());
  42. ASSERT_STREQ("Interrupted system call", berror(EINTR));
  43. }
  44. TEST_F(ErrnoTest, customized_errno) {
  45. ASSERT_STREQ("the thread is stopping", berror(ESTOP));
  46. ASSERT_STREQ("the thread is interrupted", berror(EBREAK));
  47. ASSERT_STREQ("something happened", berror(ESTH));
  48. ASSERT_STREQ("OK!", berror(EOK));
  49. ASSERT_STREQ("Unknown error 1000", berror(1000));
  50. errno = ESTOP;
  51. printf("Something got wrong, %m\n");
  52. printf("Something got wrong, %s\n", berror());
  53. }