pytorch/test/cpp/api/expanding-array.cpp
Nikita Shulga a9b0a921d5 Disable avoid-non-const-global-variables lint check (#62008)
Summary:
As GoogleTest `TEST` macro is non-compliant with it as well as `DEFINE_DISPATCH`

All changes but the ones to `.clang-tidy` are generated using following script:
```
for i in `find . -type f -iname "*.c*" -or -iname "*.h"|xargs grep cppcoreguidelines-avoid-non-const-global-variables|cut -f1 -d:|sort|uniq`;  do sed -i "/\/\/ NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)/d" $i; done
```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/62008

Reviewed By: driazati, r-barnes

Differential Revision: D29838584

Pulled By: malfet

fbshipit-source-id: 1b2f8602c945bd4ce50a9bfdd204755556e31d13
2021-07-22 18:04:40 -07:00

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");
}