Fixes RCU test cases error (loads should use Consume ordering)
[folly.git] / folly / test / ExceptionTest.cpp
index 6fc0d397e43c090f03abcc18786e37438923fb65..826df625b80e5d29d446ebab5071226ccaf2039e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2013-present Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 #include <folly/Exception.h>
 
+#include <folly/experimental/TestUtil.h>
+#include <folly/portability/GTest.h>
+
 #include <cstdio>
 #include <memory>
 
-#include <glog/logging.h>
-#include <gtest/gtest.h>
-
 namespace folly { namespace test {
 
 #define EXPECT_SYSTEM_ERROR(statement, err, msg) \
@@ -72,25 +72,52 @@ TEST(ExceptionTest, Simple) {
   EXPECT_SYSTEM_ERROR({checkUnixErrorExplicit(-1, EIO, "hello", " world");},
                       EIO, "hello world");
 
-  std::shared_ptr<FILE> fp(tmpfile(), fclose);
+  TemporaryDirectory tmpdir;
+  auto exnpath = tmpdir.path() / "ExceptionTest";
+  auto fp = fopen(exnpath.string().c_str(), "w+b");
   ASSERT_TRUE(fp != nullptr);
+  SCOPE_EXIT { fclose(fp); };
 
-  EXPECT_NO_THROW({checkFopenError(fp.get(), "hello", " world");});
+  EXPECT_NO_THROW({ checkFopenError(fp, "hello", " world"); });
   errno = ERANGE;
   EXPECT_SYSTEM_ERROR({checkFopenError(nullptr, "hello", " world");},
                       ERANGE, "hello world");
 
-  EXPECT_NO_THROW({checkFopenErrorExplicit(fp.get(), EIO, "hello", " world");});
+  EXPECT_NO_THROW({ checkFopenErrorExplicit(fp, EIO, "hello", " world"); });
   errno = ERANGE;
   EXPECT_SYSTEM_ERROR({checkFopenErrorExplicit(nullptr, EIO,
                                                "hello", " world");},
                       EIO, "hello world");
 }
 
-}}  // namespaces
-
-int main(int argc, char *argv[]) {
-  testing::InitGoogleTest(&argc, argv);
-  gflags::ParseCommandLineFlags(&argc, &argv, true);
-  return RUN_ALL_TESTS();
+TEST(ExceptionTest, makeSystemError) {
+  errno = ENOENT;
+  auto ex = makeSystemErrorExplicit(EDEADLK, "stuck");
+  EXPECT_EQ(EDEADLK, ex.code().value());
+  EXPECT_EQ(std::system_category(), ex.code().category());
+  EXPECT_TRUE(StringPiece{ex.what()}.contains("stuck"))
+      << "what() string missing input message: " << ex.what();
+
+  ex = makeSystemErrorExplicit(EDOM, 300, " is bigger than max=", 255);
+  EXPECT_EQ(EDOM, ex.code().value());
+  EXPECT_EQ(std::system_category(), ex.code().category());
+  EXPECT_TRUE(StringPiece{ex.what()}.contains("300 is bigger than max=255"))
+      << "what() string missing input message: " << ex.what();
+
+  errno = EINVAL;
+  ex = makeSystemError("bad argument ", 1234, ": bogus");
+  EXPECT_EQ(EINVAL, ex.code().value());
+  EXPECT_EQ(std::system_category(), ex.code().category());
+  EXPECT_TRUE(StringPiece{ex.what()}.contains("bad argument 1234: bogus"))
+      << "what() string missing input message: " << ex.what();
+
+  errno = 0;
+  ex = makeSystemError("unexpected success");
+  EXPECT_EQ(0, ex.code().value());
+  EXPECT_EQ(std::system_category(), ex.code().category());
+  EXPECT_TRUE(StringPiece{ex.what()}.contains("unexpected success"))
+      << "what() string missing input message: " << ex.what();
 }
+
+} // namespace test
+} // namespace folly