1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#include "tensorflow/compiler/xla/array3d.h"
17
18#include <initializer_list>
19
20#include "tensorflow/compiler/xla/test.h"
21#include "tensorflow/compiler/xla/types.h"
22
23namespace xla {
24namespace {
25
26TEST(Array3dTest, UninitializedDimsCtor) {
27  Array3D<int> uninit(2, 3, 4);
28  EXPECT_EQ(uninit.n1(), 2);
29  EXPECT_EQ(uninit.n2(), 3);
30  EXPECT_EQ(uninit.n3(), 4);
31  EXPECT_EQ(uninit.num_elements(), 24);
32}
33
34TEST(Array3dTest, FillCtor) {
35  Array3D<int> fullof7(2, 3, 4, 7);
36
37  EXPECT_EQ(fullof7.n1(), 2);
38  EXPECT_EQ(fullof7.n2(), 3);
39  EXPECT_EQ(fullof7.n3(), 4);
40
41  for (int64 n1 = 0; n1 < fullof7.n1(); ++n1) {
42    for (int64 n2 = 0; n2 < fullof7.n2(); ++n2) {
43      for (int64 n3 = 0; n3 < fullof7.n3(); ++n3) {
44        EXPECT_EQ(fullof7(n1, n2, n3), 7);
45      }
46    }
47  }
48}
49
50TEST(Array3dTest, InitializerListCtor) {
51  Array3D<int> arr = {{{1, 2}, {3, 4}, {5, 6}, {7, 8}},
52                      {{9, 10}, {11, 12}, {13, 14}, {15, 16}},
53                      {{17, 18}, {19, 20}, {21, 22}, {23, 24}}};
54
55  EXPECT_EQ(arr.n1(), 3);
56  EXPECT_EQ(arr.n2(), 4);
57  EXPECT_EQ(arr.n3(), 2);
58  EXPECT_EQ(arr.num_elements(), 24);
59
60  EXPECT_EQ(arr(0, 0, 0), 1);
61  EXPECT_EQ(arr(0, 0, 1), 2);
62  EXPECT_EQ(arr(0, 1, 0), 3);
63  EXPECT_EQ(arr(0, 3, 1), 8);
64  EXPECT_EQ(arr(1, 0, 0), 9);
65  EXPECT_EQ(arr(1, 1, 1), 12);
66  EXPECT_EQ(arr(2, 0, 0), 17);
67  EXPECT_EQ(arr(2, 1, 1), 20);
68  EXPECT_EQ(arr(2, 2, 0), 21);
69  EXPECT_EQ(arr(2, 3, 1), 24);
70}
71
72TEST(Array3dTest, InitializerListCtorHalf) {
73  Array3D<Eigen::half> arr = {
74      {{1.0f, 2.0f}, {3.0f, 4.0f}, {5.0f, 6.0f}, {7.0f, 8.0f}},
75      {{9.0f, 10.0f}, {11.0f, 12.0f}, {13.0f, 14.0f}, {15.0f, 16.0f}},
76      {{17.0f, 18.0f}, {19.0f, 20.0f}, {21.0f, 22.0f}, {23.0f, 24.0f}}};
77
78  EXPECT_EQ(arr.n1(), 3);
79  EXPECT_EQ(arr.n2(), 4);
80  EXPECT_EQ(arr.n3(), 2);
81  EXPECT_EQ(arr.num_elements(), 24);
82
83  EXPECT_EQ(arr(0, 0, 0), static_cast<Eigen::half>(1));
84  EXPECT_EQ(arr(0, 0, 1), static_cast<Eigen::half>(2));
85  EXPECT_EQ(arr(0, 1, 0), static_cast<Eigen::half>(3));
86  EXPECT_EQ(arr(0, 3, 1), static_cast<Eigen::half>(8));
87  EXPECT_EQ(arr(1, 0, 0), static_cast<Eigen::half>(9));
88  EXPECT_EQ(arr(1, 1, 1), static_cast<Eigen::half>(12));
89  EXPECT_EQ(arr(2, 0, 0), static_cast<Eigen::half>(17));
90  EXPECT_EQ(arr(2, 1, 1), static_cast<Eigen::half>(20));
91  EXPECT_EQ(arr(2, 2, 0), static_cast<Eigen::half>(21));
92  EXPECT_EQ(arr(2, 3, 1), static_cast<Eigen::half>(24));
93}
94
95TEST(Array3dTest, Fill) {
96  Array3D<int> fullof7(2, 3, 4, 7);
97  for (int64 n1 = 0; n1 < fullof7.n1(); ++n1) {
98    for (int64 n2 = 0; n2 < fullof7.n2(); ++n2) {
99      for (int64 n3 = 0; n3 < fullof7.n3(); ++n3) {
100        EXPECT_EQ(fullof7(n1, n2, n3), 7);
101      }
102    }
103  }
104
105  fullof7.Fill(11);
106  for (int64 n1 = 0; n1 < fullof7.n1(); ++n1) {
107    for (int64 n2 = 0; n2 < fullof7.n2(); ++n2) {
108      for (int64 n3 = 0; n3 < fullof7.n3(); ++n3) {
109        EXPECT_EQ(fullof7(n1, n2, n3), 11);
110      }
111    }
112  }
113}
114
115}  // namespace
116}  // namespace xla
117