mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 00:21:07 +01:00
Summary: One of the purposes of the C++ API tests in `test/cpp/api/` should be to check that including `torch/torch.h` is a sufficient prerequisite for using all C++ frontend features. This PR change ensures that. Pull Request resolved: https://github.com/pytorch/pytorch/pull/27067 Differential Revision: D17856815 Pulled By: yf225 fbshipit-source-id: 49c057bd807b003e4a00f6ba73131d763a0f277a
60 lines
1.5 KiB
C++
60 lines
1.5 KiB
C++
#include <gtest/gtest.h>
|
|
|
|
#include <torch/torch.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");
|
|
}
|