15 #include <tensorflow/compiler/xla/array2d.h>
26 class NetworkioTest :
public ::testing::Test {
28 void SetUp()
override {
29 std::locale::global(std::locale(
""));
34 std::unique_ptr<xla::Array2D<int>> SetupArray(
int ysize,
int xsize,
int start) {
35 std::unique_ptr<xla::Array2D<int>> a(
new xla::Array2D<int>(ysize, xsize));
37 for (
int y = 0; y < ysize; ++y) {
38 for (
int x = 0; x < xsize; ++x) {
46 std::vector<std::unique_ptr<xla::Array2D<int>>> arrays;
47 arrays.push_back(SetupArray(3, 4, 0));
48 arrays.push_back(SetupArray(4, 5, 12));
49 std::vector<std::pair<int, int>> h_w_sizes;
50 for (
size_t i = 0; i < arrays.size(); ++i) {
51 h_w_sizes.emplace_back(arrays[i].get()->height(),
52 arrays[i].get()->width());
58 StrideMap::Index index(stride_map);
62 nio->
SetPixel(index.t(), 0, 128 + value, 0.0f, 128.0f);
63 nio->
SetPixel(index.t(), 1, 128 - value, 0.0f, 128.0f);
64 }
while (index.Increment());
70 TEST_F(NetworkioTest, InitWithZeroFill) {
73 int width = nio.
Width();
74 for (
int t = 0; t < width; ++t) {
87 int value = nio.
i(t)[0];
88 EXPECT_EQ(value, pos);
90 EXPECT_EQ(value, -pos);
93 EXPECT_EQ(nio.
i(next_t)[0], 0);
94 EXPECT_EQ(nio.
i(next_t)[1], 0);
99 }
while (index.Increment());
101 EXPECT_EQ(next_t, 40);
105 TEST_F(NetworkioTest, CopyWithYReversal) {
107 SetupNetworkIO(&nio);
113 std::vector<int> expected_values = {
114 8, 9, 10, 11, 4, 5, 6, 7, 0, 1, 2, 3, 27, 28, 29, 30,
115 31, 22, 23, 24, 25, 26, 17, 18, 19, 20, 21, 12, 13, 14, 15, 16};
119 int value = copy.
i(t)[0];
120 EXPECT_EQ(value, expected_values[pos]);
121 value = copy.
i(t)[1];
122 EXPECT_EQ(value, -expected_values[pos]);
125 EXPECT_EQ(copy.
i(next_t)[0], 0) <<
"Failure t = " << next_t;
126 EXPECT_EQ(copy.
i(next_t)[1], 0) <<
"Failure t = " << next_t;
131 }
while (index.Increment());
133 EXPECT_EQ(next_t, 40);
137 TEST_F(NetworkioTest, CopyWithXReversal) {
139 SetupNetworkIO(&nio);
145 std::vector<int> expected_values = {
146 3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 16, 15, 14, 13,
147 12, 21, 20, 19, 18, 17, 26, 25, 24, 23, 22, 31, 30, 29, 28, 27};
151 int value = copy.
i(t)[0];
152 EXPECT_EQ(value, expected_values[pos]);
153 value = copy.
i(t)[1];
154 EXPECT_EQ(value, -expected_values[pos]);
157 EXPECT_EQ(copy.
i(next_t)[0], 0) <<
"Failure t = " << next_t;
158 EXPECT_EQ(copy.
i(next_t)[1], 0) <<
"Failure t = " << next_t;
163 }
while (index.Increment());
165 EXPECT_EQ(next_t, 40);
169 TEST_F(NetworkioTest, CopyWithXYTranspose) {
171 SetupNetworkIO(&nio);
177 std::vector<int> expected_values = {
178 0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11, 12, 17, 22, 27,
179 13, 18, 23, 28, 14, 19, 24, 29, 15, 20, 25, 30, 16, 21, 26, 31};
183 int value = copy.
i(t)[0];
184 EXPECT_EQ(value, expected_values[pos]);
185 value = copy.
i(t)[1];
186 EXPECT_EQ(value, -expected_values[pos]);
189 EXPECT_EQ(copy.
i(next_t)[0], 0);
190 EXPECT_EQ(copy.
i(next_t)[1], 0);
195 }
while (index.Increment());
197 EXPECT_EQ(next_t, 40);