Rewrite C++ API tests in gtest (#11953)

Summary:
This PR is a large codemod to rewrite all C++ API tests with GoogleTest (gtest) instead of Catch.

You can largely trust me to have correctly code-modded the tests, so it's not required to review every of the 2000+ changed lines. However, additional things I changed were:

1. Moved the cmake parts for these tests into their own `CMakeLists.txt` under `test/cpp/api` and calling `add_subdirectory` from `torch/CMakeLists.txt`
2. Fixing DataParallel tests which weren't being compiled because `USE_CUDA` wasn't correctly being set at all.
3. Updated README

ezyang ebetica
Pull Request resolved: https://github.com/pytorch/pytorch/pull/11953

Differential Revision: D9998883

Pulled By: goldsborough

fbshipit-source-id: affe3f320b0ca63e7e0019926a59076bb943db80
This commit is contained in:
Peter Goldsborough
2018-09-21 21:12:37 -07:00
committed by Facebook Github Bot
parent d0db23e95a
commit 825181ea9d
27 changed files with 2154 additions and 2182 deletions
+27 -25
View File
@@ -1,5 +1,4 @@
#include "gtest/gtest.h"
#include <gtest/gtest.h>
#include <torch/detail/static.h>
#include <torch/nn/module.h>
@@ -23,35 +22,38 @@ torch::detail::enable_if_module_t<T, bool> f(T&& m) {
return true;
}
TEST(TestStatic, All_Of){
EXPECT_TRUE(torch::all_of<>::value);
EXPECT_TRUE(torch::all_of<true>::value);
EXPECT_TRUE((torch::all_of<true, true, true>::value));
EXPECT_FALSE(torch::all_of<false>::value);
EXPECT_FALSE((torch::all_of<false, false, false>::value));
EXPECT_FALSE((torch::all_of<true, true, false>::value));
TEST(TestStatic, AllOf) {
ASSERT_TRUE(torch::all_of<>::value);
ASSERT_TRUE(torch::all_of<true>::value);
ASSERT_TRUE((torch::all_of<true, true, true>::value));
ASSERT_FALSE(torch::all_of<false>::value);
ASSERT_FALSE((torch::all_of<false, false, false>::value));
ASSERT_FALSE((torch::all_of<true, true, false>::value));
}
TEST(TestStatic, Any_Of){
EXPECT_FALSE(torch::any_of<>::value);
EXPECT_TRUE(bool((torch::any_of<true>::value)));
EXPECT_TRUE(bool((torch::any_of<true, true, true>::value)));
EXPECT_FALSE(bool((torch::any_of<false>::value)));
TEST(TestStatic, AnyOf) {
ASSERT_FALSE(torch::any_of<>::value);
ASSERT_TRUE(bool((torch::any_of<true>::value)));
ASSERT_TRUE(bool((torch::any_of<true, true, true>::value)));
ASSERT_FALSE(bool((torch::any_of<false>::value)));
}
TEST(TestStatic, Enable_If_Module){
EXPECT_TRUE(f(torch::nn::LinearImpl(1, 2)));
EXPECT_FALSE(f(5));
EXPECT_TRUE(torch::detail::check_not_lvalue_references<int>());
EXPECT_TRUE((torch::detail::check_not_lvalue_references<float, int, char>()));
EXPECT_FALSE(
TEST(TestStatic, EnableIfModule) {
ASSERT_TRUE(f(torch::nn::LinearImpl(1, 2)));
ASSERT_FALSE(f(5));
ASSERT_TRUE(torch::detail::check_not_lvalue_references<int>());
ASSERT_TRUE((torch::detail::check_not_lvalue_references<float, int, char>()));
ASSERT_FALSE(
(torch::detail::check_not_lvalue_references<float, int&, char>()));
EXPECT_TRUE(torch::detail::check_not_lvalue_references<std::string>());
EXPECT_FALSE(torch::detail::check_not_lvalue_references<std::string&>());
ASSERT_TRUE(torch::detail::check_not_lvalue_references<std::string>());
ASSERT_FALSE(torch::detail::check_not_lvalue_references<std::string&>());
}
TEST(TestStatic, Apply){
TEST(TestStatic, Apply) {
std::vector<int> v;
torch::apply([&v](int x) { v.push_back(x); }, 1, 2, 3, 4, 5);
EXPECT_EQ(v.size(), 5);
ASSERT_EQ(v.size(), 5);
for (size_t i = 0; i < v.size(); ++i) {
EXPECT_EQ(v.at(i), i + 1);
ASSERT_EQ(v.at(i), i + 1);
}
}