Caffe2 - C++ API
A deep learning, cross platform ML framework
expanding-array.cpp
1 #include <gtest/gtest.h>
2 
3 #include <torch/expanding_array.h>
4 
5 #include <test/cpp/api/support.h>
6 
7 #include <cstddef>
8 #include <initializer_list>
9 #include <vector>
10 
12 
13 TEST_F(ExpandingArrayTest, CanConstructFromInitializerList) {
14  torch::ExpandingArray<5> e({1, 2, 3, 4, 5});
15  ASSERT_EQ(e.size(), 5);
16  for (size_t i = 0; i < e.size(); ++i) {
17  ASSERT_EQ((*e)[i], i + 1);
18  }
19 }
20 
21 TEST_F(ExpandingArrayTest, CanConstructFromVector) {
22  torch::ExpandingArray<5> e(std::vector<int64_t>{1, 2, 3, 4, 5});
23  ASSERT_EQ(e.size(), 5);
24  for (size_t i = 0; i < e.size(); ++i) {
25  ASSERT_EQ((*e)[i], i + 1);
26  }
27 }
28 
29 TEST_F(ExpandingArrayTest, CanConstructFromArray) {
30  torch::ExpandingArray<5> e(std::array<int64_t, 5>({1, 2, 3, 4, 5}));
31  ASSERT_EQ(e.size(), 5);
32  for (size_t i = 0; i < e.size(); ++i) {
33  ASSERT_EQ((*e)[i], i + 1);
34  }
35 }
36 
37 TEST_F(ExpandingArrayTest, CanConstructFromSingleValue) {
39  ASSERT_EQ(e.size(), 5);
40  for (size_t i = 0; i < e.size(); ++i) {
41  ASSERT_EQ((*e)[i], 5);
42  }
43 }
44 
45 TEST_F(
47  ThrowsWhenConstructedWithIncorrectNumberOfArgumentsInInitializerList) {
48  ASSERT_THROWS_WITH(
49  torch::ExpandingArray<5>({1, 2, 3, 4, 5, 6, 7}),
50  "Expected 5 values, but instead got 7");
51 }
52 
53 TEST_F(
55  ThrowsWhenConstructedWithIncorrectNumberOfArgumentsInVector) {
56  ASSERT_THROWS_WITH(
57  torch::ExpandingArray<5>(std::vector<int64_t>({1, 2, 3, 4, 5, 6, 7})),
58  "Expected 5 values, but instead got 7");
59 }
size_t size() const noexcept
Returns the extent of the ExpandingArray.
A utility class that accepts either a container of D-many values, or a single value, which is internally repeated D times.