Skip to content

Commit 474607e

Browse files
committed
TEST: Adding new tests for CPP assign operators
1 parent 57faf93 commit 474607e

1 file changed

Lines changed: 126 additions & 0 deletions

File tree

test/assign.cpp

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,129 @@ TEST(ArrayAssign, CPP)
246246

247247
delete[] outData;
248248
}
249+
250+
TEST(ArrayAssign, CPP_END)
251+
{
252+
using af::array;
253+
254+
const int n = 5;
255+
const int m = 5;
256+
const int end_off = 2;
257+
258+
array a = af::randu(n, m);
259+
array b = af::randu(1, m);
260+
a(af::end - end_off, af::span) = b;
261+
262+
float *hA = a.host<float>();
263+
float *hB = b.host<float>();
264+
265+
for (int i = 0; i < m; i++) {
266+
ASSERT_EQ(hA[i * n + end_off], hB[i]);
267+
}
268+
269+
270+
delete[] hA;
271+
delete[] hB;
272+
}
273+
274+
TEST(ArrayAssign, CPP_END_SEQ)
275+
{
276+
using af::array;
277+
278+
const int num = 20;
279+
const int end_begin = 10;
280+
const int end_end = 0;
281+
const int len = end_begin - end_end + 1;
282+
283+
array a = af::randu(num);
284+
array b = af::randu(len);
285+
a(af::seq(af::end - end_begin, af::end - end_end)) = b;
286+
287+
float *hA = a.host<float>();
288+
float *hB = b.host<float>();
289+
290+
for (int i = 0; i < len; i++) {
291+
ASSERT_EQ(hA[i + end_begin - 1], hB[i]);
292+
}
293+
294+
delete[] hA;
295+
delete[] hB;
296+
}
297+
298+
TEST(ArrayAssign, CPP_COPY_ON_WRITE)
299+
{
300+
using af::array;
301+
302+
const int num = 20;
303+
const int len = 10;
304+
305+
array a = af::randu(num);
306+
float *hAO = a.host<float>();
307+
308+
array a_copy = a;
309+
array b = af::randu(len);
310+
a(af::seq(len)) = b;
311+
312+
float *hA = a.host<float>();
313+
float *hB = b.host<float>();
314+
float *hAC = a_copy.host<float>();
315+
316+
// first half should be from B
317+
for (int i = 0; i < len; i++) {
318+
ASSERT_EQ(hA[i], hB[i]);
319+
}
320+
321+
// Second half should be same as original
322+
for (int i = 0; i < num - len; i++) {
323+
ASSERT_EQ(hA[i + len], hAO[i + len]);
324+
}
325+
326+
// hAC should not be modified, i.e. same as original
327+
for (int i = 0; i < num; i++) {
328+
ASSERT_EQ(hAO[i], hAC[i]);
329+
}
330+
331+
delete[] hA;
332+
delete[] hB;
333+
delete[] hAC;
334+
delete[] hAO;
335+
}
336+
337+
TEST(ArrayAssign, CPP_ASSIGN_BINOP)
338+
{
339+
using af::array;
340+
341+
const int num = 20;
342+
const int len = 10;
343+
344+
array a = af::randu(num);
345+
float *hAO = a.host<float>();
346+
347+
array a_copy = a;
348+
array b = af::randu(len);
349+
a(af::seq(len)) += b;
350+
351+
float *hA = a.host<float>();
352+
float *hB = b.host<float>();
353+
float *hAC = a_copy.host<float>();
354+
355+
// first half should be hAO + hB
356+
for (int i = 0; i < len; i++) {
357+
ASSERT_EQ(hA[i], hAO[i] + hB[i]);
358+
}
359+
360+
// Second half should be same as original
361+
for (int i = 0; i < num - len; i++) {
362+
ASSERT_EQ(hA[i + len], hAO[i + len]);
363+
}
364+
365+
// hAC should not be modified, i.e. same as original
366+
for (int i = 0; i < num; i++) {
367+
ASSERT_EQ(hAO[i], hAC[i]);
368+
}
369+
370+
delete[] hA;
371+
delete[] hB;
372+
delete[] hAC;
373+
delete[] hAO;
374+
}

0 commit comments

Comments
 (0)