folly copyright 2015 -> copyright 2016
[folly.git] / folly / test / ExceptionTest.cpp
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <folly/Exception.h>
18
19 #include <cstdio>
20 #include <memory>
21
22 #include <glog/logging.h>
23 #include <gtest/gtest.h>
24
25 namespace folly { namespace test {
26
27 #define EXPECT_SYSTEM_ERROR(statement, err, msg) \
28   try { \
29     statement; \
30     ADD_FAILURE() << "Didn't throw"; \
31   } catch (const std::system_error& e) { \
32     std::system_error expected(err, std::system_category(), msg); \
33     EXPECT_STREQ(expected.what(), e.what()); \
34   } catch (...) { \
35     ADD_FAILURE() << "Threw a different type"; \
36   }
37
38
39 TEST(ExceptionTest, Simple) {
40   // Make sure errno isn't used when we don't want it to, set it to something
41   // else than what we set when we call Explicit functions
42   errno = ERANGE;
43   EXPECT_SYSTEM_ERROR({throwSystemErrorExplicit(EIO, "hello");},
44                       EIO, "hello");
45   errno = ERANGE;
46   EXPECT_SYSTEM_ERROR({throwSystemErrorExplicit(EIO, "hello", " world");},
47                       EIO, "hello world");
48   errno = ERANGE;
49   EXPECT_SYSTEM_ERROR({throwSystemError("hello", " world");},
50                       ERANGE, "hello world");
51
52   EXPECT_NO_THROW({checkPosixError(0, "hello", " world");});
53   errno = ERANGE;
54   EXPECT_SYSTEM_ERROR({checkPosixError(EIO, "hello", " world");},
55                       EIO, "hello world");
56
57   EXPECT_NO_THROW({checkKernelError(0, "hello", " world");});
58   EXPECT_NO_THROW({checkKernelError(EIO, "hello", " world");});
59   errno = ERANGE;
60   EXPECT_SYSTEM_ERROR({checkKernelError(-EIO, "hello", " world");},
61                       EIO, "hello world");
62
63   EXPECT_NO_THROW({checkUnixError(0, "hello", " world");});
64   EXPECT_NO_THROW({checkUnixError(1, "hello", " world");});
65   errno = ERANGE;
66   EXPECT_SYSTEM_ERROR({checkUnixError(-1, "hello", " world");},
67                       ERANGE, "hello world");
68
69   EXPECT_NO_THROW({checkUnixErrorExplicit(0, EIO, "hello", " world");});
70   EXPECT_NO_THROW({checkUnixErrorExplicit(1, EIO, "hello", " world");});
71   errno = ERANGE;
72   EXPECT_SYSTEM_ERROR({checkUnixErrorExplicit(-1, EIO, "hello", " world");},
73                       EIO, "hello world");
74
75   std::shared_ptr<FILE> fp(tmpfile(), fclose);
76   ASSERT_TRUE(fp != nullptr);
77
78   EXPECT_NO_THROW({checkFopenError(fp.get(), "hello", " world");});
79   errno = ERANGE;
80   EXPECT_SYSTEM_ERROR({checkFopenError(nullptr, "hello", " world");},
81                       ERANGE, "hello world");
82
83   EXPECT_NO_THROW({checkFopenErrorExplicit(fp.get(), EIO, "hello", " world");});
84   errno = ERANGE;
85   EXPECT_SYSTEM_ERROR({checkFopenErrorExplicit(nullptr, EIO,
86                                                "hello", " world");},
87                       EIO, "hello world");
88 }
89
90 }}  // namespaces
91
92 int main(int argc, char *argv[]) {
93   testing::InitGoogleTest(&argc, argv);
94   gflags::ParseCommandLineFlags(&argc, &argv, true);
95   return RUN_ALL_TESTS();
96 }