Files
pytorch/test/cpp/api/expanding-array.cpp
Peter Goldsborough 825181ea9d 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
2018-09-21 21:28:16 -07:00

60 lines
1.5 KiB
C++

#include <gtest/gtest.h>
#include <torch/expanding_array.h>
#include <test/cpp/api/support.h>
#include <cstddef>
#include <initializer_list>
#include <vector>
struct ExpandingArrayTest : torch::test::SeedingFixture {};
TEST_F(ExpandingArrayTest, CanConstructFromInitializerList) {
torch::ExpandingArray<5> e({1, 2, 3, 4, 5});
ASSERT_EQ(e.size(), 5);
for (size_t i = 0; i < e.size(); ++i) {
ASSERT_EQ((*e)[i], i + 1);
}
}
TEST_F(ExpandingArrayTest, CanConstructFromVector) {
torch::ExpandingArray<5> e(std::vector<int64_t>{1, 2, 3, 4, 5});
ASSERT_EQ(e.size(), 5);
for (size_t i = 0; i < e.size(); ++i) {
ASSERT_EQ((*e)[i], i + 1);
}
}
TEST_F(ExpandingArrayTest, CanConstructFromArray) {
torch::ExpandingArray<5> e(std::array<int64_t, 5>({1, 2, 3, 4, 5}));
ASSERT_EQ(e.size(), 5);
for (size_t i = 0; i < e.size(); ++i) {
ASSERT_EQ((*e)[i], i + 1);
}
}
TEST_F(ExpandingArrayTest, CanConstructFromSingleValue) {
torch::ExpandingArray<5> e(5);
ASSERT_EQ(e.size(), 5);
for (size_t i = 0; i < e.size(); ++i) {
ASSERT_EQ((*e)[i], 5);
}
}
TEST_F(
ExpandingArrayTest,
ThrowsWhenConstructedWithIncorrectNumberOfArgumentsInInitializerList) {
ASSERT_THROWS_WITH(
torch::ExpandingArray<5>({1, 2, 3, 4, 5, 6, 7}),
"Expected 5 values, but instead got 7");
}
TEST_F(
ExpandingArrayTest,
ThrowsWhenConstructedWithIncorrectNumberOfArgumentsInVector) {
ASSERT_THROWS_WITH(
torch::ExpandingArray<5>(std::vector<int64_t>({1, 2, 3, 4, 5, 6, 7})),
"Expected 5 values, but instead got 7");
}