@@ -68,6 +68,15 @@ TEST(BroadcastIndexesRangeTest, ScalarBroadcastToOneD) {
6868 EXPECT_EQ (expected, actual);
6969}
7070
71+ template <typename Range>
72+ void test_operator_plus (const Range& range) {
73+ size_t idx = 0 ;
74+ for (const auto indexes : range) {
75+ EXPECT_EQ (*(range.begin () + idx), indexes);
76+ idx++;
77+ }
78+ }
79+
7180// [1] -> [H, W]
7281// [W] -> [H, W]
7382// [1, 1] -> [H, W]
@@ -87,14 +96,15 @@ TEST(BroadcastIndexesRangeTest, OneAndTwoDExhaustive) {
8796
8897 Tensor in_not_broadcast = tf.zeros ({3 , 4 });
8998
90- auto actual = range_to_vec ( BroadcastIndexesRange<6 >(
99+ const auto range = BroadcastIndexesRange<6 >(
91100 out,
92101 in_0d_scalar,
93102 in_1d_scalar,
94103 in_2d_scalar,
95104 in_row,
96105 in_col,
97- in_not_broadcast));
106+ in_not_broadcast);
107+ auto actual = range_to_vec (range);
98108 decltype (actual) expected = {
99109 {0 , 0 , 0 , 0 , 0 , 0 , 0 },
100110 {1 , 0 , 0 , 0 , 1 , 0 , 1 },
@@ -110,6 +120,8 @@ TEST(BroadcastIndexesRangeTest, OneAndTwoDExhaustive) {
110120 {11 , 0 , 0 , 0 , 3 , 2 , 11 },
111121 };
112122 EXPECT_EQ (expected, actual);
123+
124+ test_operator_plus (range);
113125}
114126
115127// Make sure nothing is thrown off by a size-1 dim in the output:
@@ -138,20 +150,20 @@ TEST(BroadcastIndexesRangeTest, OneAndTwoDWith1InOutputShapeExhaustive) {
138150 Tensor in_col = tf.zeros ({H, 1 });
139151
140152 size_t idx = 0 ;
153+ const auto range_row = BroadcastIndexesRange<5 >(
154+ out_row,
155+ in_0d_scalar,
156+ in_1d_scalar,
157+ in_2d_scalar,
158+ in_row,
159+ in_leading_one_row);
141160 for (const auto
142161 [out_idx,
143162 in_0d_idx,
144163 in_1d_idx,
145164 in_2d_idx,
146165 in_row_idx,
147- in_leading_one_row_idx] :
148- BroadcastIndexesRange<5 >(
149- out_row,
150- in_0d_scalar,
151- in_1d_scalar,
152- in_2d_scalar,
153- in_row,
154- in_leading_one_row)) {
166+ in_leading_one_row_idx] : range_row) {
155167 EXPECT_EQ (out_idx, idx++);
156168 EXPECT_EQ (in_0d_idx, 0 );
157169 EXPECT_EQ (in_1d_idx, 0 );
@@ -160,16 +172,21 @@ TEST(BroadcastIndexesRangeTest, OneAndTwoDWith1InOutputShapeExhaustive) {
160172 EXPECT_EQ (in_leading_one_row_idx, out_idx);
161173 }
162174
175+ test_operator_plus (range_row);
176+
163177 idx = 0 ;
178+ const auto range_col = BroadcastIndexesRange<4 >(
179+ out_col, in_0d_scalar, in_1d_scalar, in_2d_scalar, in_col);
164180 for (const auto [out_idx, in_0d_idx, in_1d_idx, in_2d_idx, in_col_idx] :
165- BroadcastIndexesRange<4 >(
166- out_col, in_0d_scalar, in_1d_scalar, in_2d_scalar, in_col)) {
181+ range_col) {
167182 EXPECT_EQ (out_idx, idx++);
168183 EXPECT_EQ (in_0d_idx, 0 );
169184 EXPECT_EQ (in_1d_idx, 0 );
170185 EXPECT_EQ (in_2d_idx, 0 );
171186 EXPECT_EQ (in_col_idx, out_idx);
172187 }
188+
189+ test_operator_plus (range_col);
173190}
174191
175192// [1, 1, 1] -> [C, H, W]
@@ -197,16 +214,17 @@ TEST(BroadcastIndexesRangeTest, ThreeDBroadcasting) {
197214 // take the opportunity to mutation test against delinearize_index
198215 // and linearize_access_indexes.
199216 int idx = 0 ;
200- for (const auto indexes : BroadcastIndexesRange<8 >(
201- out,
202- input_tensors[0 ],
203- input_tensors[1 ],
204- input_tensors[2 ],
205- input_tensors[3 ],
206- input_tensors[4 ],
207- input_tensors[5 ],
208- input_tensors[6 ],
209- input_tensors[7 ])) {
217+ const auto range = BroadcastIndexesRange<8 >(
218+ out,
219+ input_tensors[0 ],
220+ input_tensors[1 ],
221+ input_tensors[2 ],
222+ input_tensors[3 ],
223+ input_tensors[4 ],
224+ input_tensors[5 ],
225+ input_tensors[6 ],
226+ input_tensors[7 ]);
227+ for (const auto indexes : range) {
210228 const auto out_idx = indexes[0 ];
211229 EXPECT_EQ (out_idx, idx++);
212230 size_t out_indexes[executorch::runtime::kTensorDimensionLimit ];
@@ -219,6 +237,7 @@ TEST(BroadcastIndexesRangeTest, ThreeDBroadcasting) {
219237 out_indexes, out.dim (), input_tensors[tensor_idx]));
220238 }
221239 }
240+ test_operator_plus (range);
222241}
223242
224243// 4-D should generalize, but we will go ahead and test:
@@ -235,8 +254,9 @@ void four_d_broadcasting_test() {
235254 // take the opportunity to mutation test against delinearize_index
236255 // and linearize_access_indexes.
237256 int idx = 0 ;
238- for (const auto [out_idx, in_cw_idx, in_nh_idx] :
239- BroadcastIndexesRange<2 >(out, in_broadcast_cw, in_broadcast_nh)) {
257+ const auto range =
258+ BroadcastIndexesRange<2 >(out, in_broadcast_cw, in_broadcast_nh);
259+ for (const auto [out_idx, in_cw_idx, in_nh_idx] : range) {
240260 EXPECT_EQ (out_idx, idx++);
241261 size_t out_indexes[executorch::runtime::kTensorDimensionLimit ];
242262 delinearize_index (
@@ -248,6 +268,8 @@ void four_d_broadcasting_test() {
248268 in_nh_idx,
249269 linearize_access_indexes (out_indexes, out.dim (), in_broadcast_nh));
250270 }
271+
272+ test_operator_plus (range);
251273}
252274
253275TEST (BroadcastIndexesRangeTest, FourDBroadcasting) {
0 commit comments