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

View File

@ -1,4 +1,4 @@
#include "catch_utils.hpp"
#include <gtest/gtest.h>
#include <ATen/Context.h>
#include <ATen/DeviceGuard.h>
@ -9,19 +9,20 @@
using namespace at;
// A macro so we don't lose location information when an assertion fails.
#define REQUIRE_OPTIONS(device_, index_, type_, layout_) \
CATCH_REQUIRE(options.device().type() == Device((device_), (index_)).type()); \
CATCH_REQUIRE(options.device().index() == Device((device_), (index_)).index()); \
CATCH_REQUIRE(options.dtype() == (type_)); \
CATCH_REQUIRE(options.layout() == (layout_))
#define REQUIRE_OPTIONS(device_, index_, type_, layout_) \
ASSERT_EQ(options.device().type(), Device((device_), (index_)).type()); \
ASSERT_TRUE( \
options.device().index() == Device((device_), (index_)).index()); \
ASSERT_EQ(options.dtype(), (type_)); \
ASSERT_TRUE(options.layout() == (layout_))
#define REQUIRE_TENSOR_OPTIONS(device_, index_, type_, layout_) \
CATCH_REQUIRE(tensor.device().type() == Device((device_), (index_)).type()); \
CATCH_REQUIRE(tensor.device().index() == Device((device_), (index_)).index()); \
CATCH_REQUIRE(tensor.type().scalarType() == (type_)); \
CATCH_REQUIRE(tensor.type().layout() == (layout_))
#define REQUIRE_TENSOR_OPTIONS(device_, index_, type_, layout_) \
ASSERT_EQ(tensor.device().type(), Device((device_), (index_)).type()); \
ASSERT_EQ(tensor.device().index(), Device((device_), (index_)).index()); \
ASSERT_EQ(tensor.type().scalarType(), (type_)); \
ASSERT_TRUE(tensor.type().layout() == (layout_))
CATCH_TEST_CASE("TensorOptions/ConstructsWellFromCUDATypes", "[cuda]") {
TEST(TensorOptionsTest, ConstructsWellFromCUDATypes_CUDA) {
auto options = CUDA(kFloat).options();
REQUIRE_OPTIONS(kCUDA, -1, kFloat, kStrided);
@ -37,11 +38,12 @@ CATCH_TEST_CASE("TensorOptions/ConstructsWellFromCUDATypes", "[cuda]") {
options = CUDA(kFloat).options(/*device=*/5);
REQUIRE_OPTIONS(kCUDA, 5, kFloat, kStrided);
options = getNonVariableType(Backend::SparseCUDA, kFloat).options(/*device=*/5);
options =
getNonVariableType(Backend::SparseCUDA, kFloat).options(/*device=*/5);
REQUIRE_OPTIONS(kCUDA, 5, kFloat, kSparse);
}
CATCH_TEST_CASE("TensorOptions/ConstructsWellFromCUDATensors", "[multi-cuda]") {
TEST(TensorOptionsTest, ConstructsWellFromCUDATensors_MultiCUDA) {
auto options = empty(5, device(kCUDA).dtype(kDouble)).options();
REQUIRE_OPTIONS(kCUDA, 0, kDouble, kStrided);
@ -66,7 +68,7 @@ CATCH_TEST_CASE("TensorOptions/ConstructsWellFromCUDATensors", "[multi-cuda]") {
}
}
CATCH_TEST_CASE("OptionsGuardCUDA", "[multi-cuda]") {
TEST(OptionsGuardTest, TestFunctionality_CUDA) {
Tensor tensor;
{
OptionsGuard guard(device(kCUDA));
@ -87,7 +89,7 @@ CATCH_TEST_CASE("OptionsGuardCUDA", "[multi-cuda]") {
REQUIRE_TENSOR_OPTIONS(kCUDA, 0, kInt, kStrided);
}
CATCH_TEST_CASE("DeviceGuardOptionsGuardInteraction", "[multi-cuda]") {
TEST(OptionsGuardTest, DeviceGuardOptionsGuardInteraction_MultiCUDA) {
Tensor tensor;
{
// Check that OptionsGuard respects any active device before construction.
@ -112,17 +114,17 @@ CATCH_TEST_CASE("DeviceGuardOptionsGuardInteraction", "[multi-cuda]") {
}
}
CATCH_TEST_CASE("DeviceGuardIsMovable", "[cuda]") {
TEST(DeviceGuardTest, IsMovable_CUDA) {
DeviceGuard first(1);
CATCH_REQUIRE(first.original_index() == 0);
CATCH_REQUIRE(first.last_index() == 1);
ASSERT_EQ(first.original_index(), 0);
ASSERT_EQ(first.last_index(), 1);
DeviceGuard second(std::move(first));
CATCH_REQUIRE(second.original_index() == 0);
CATCH_REQUIRE(second.last_index() == 1);
CATCH_REQUIRE(first.original_index() == -1);
ASSERT_EQ(second.original_index(), 0);
ASSERT_EQ(second.last_index(), 1);
ASSERT_EQ(first.original_index(), -1);
DeviceGuard third;
third = std::move(second);
CATCH_REQUIRE(third.original_index() == 0);
CATCH_REQUIRE(third.last_index() == 1);
CATCH_REQUIRE(second.original_index() == -1);
ASSERT_EQ(third.original_index(), 0);
ASSERT_EQ(third.last_index(), 1);
ASSERT_EQ(second.original_index(), -1);
}