mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-25 08:11:06 +08:00
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/66743 Modified loops in files under fbsource/fbcode/caffe2/ from the format `for(TYPE var=x0;var<x_max;x++)` to the format `for(const auto var: irange(xmax))` This was achieved by running r-barnes's loop upgrader script (D28874212) with some modification to exclude all files under /torch/jit and a number of reversions or unused variable suppression warnings added by hand. Test Plan: Sandcastle Reviewed By: malfet Differential Revision: D31705359 fbshipit-source-id: c9ea2fbc0f9cd29e97a52dcb203addc5f2abb09b
61 lines
1.6 KiB
C++
61 lines
1.6 KiB
C++
#include <gtest/gtest.h>
|
|
|
|
#include <c10/util/irange.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 (const auto i : c10::irange(e.size())) {
|
|
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 (const auto i : c10::irange(e.size())) {
|
|
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 (const auto i : c10::irange(e.size())) {
|
|
ASSERT_EQ((*e)[i], i + 1);
|
|
}
|
|
}
|
|
|
|
TEST_F(ExpandingArrayTest, CanConstructFromSingleValue) {
|
|
torch::ExpandingArray<5> e(5);
|
|
ASSERT_EQ(e.size(), 5);
|
|
for (const auto i : c10::irange(e.size())) {
|
|
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");
|
|
}
|