Skip to content

Commit a9dedf4

Browse files
committed
Tests for cloning a multi-dim array.
1 parent fac0b5c commit a9dedf4

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

t/moar/02-multidim.t

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#! nqp
22

3-
plan(140);
3+
plan(151);
44

55
sub is-dims(@arr, @expected-dims, $description) {
66
my $got-dims := nqp::dimensions(@arr);
@@ -349,3 +349,30 @@ dies-ok({
349349
nqp::setdimensions($test_2d, nqp::list_i(2, 3));
350350
ok(nqp::elems($test_2d) == 2, 'elems returns first dimension of a 2D array');
351351
}
352+
353+
# can clone
354+
{
355+
my $test_2d := nqp::create($array_type_2d);
356+
nqp::setdimensions($test_2d, nqp::list_i(2, 2));
357+
nqp::bindposnd($test_2d, nqp::list_i(0, 0), 10);
358+
nqp::bindposnd($test_2d, nqp::list_i(0, 1), 11);
359+
nqp::bindposnd($test_2d, nqp::list_i(1, 0), 20);
360+
nqp::bindposnd($test_2d, nqp::list_i(1, 1), 21);
361+
362+
my $copied := nqp::clone($test_2d);
363+
ok(nqp::numdimensions($copied) == 2, 'Cloned MultiDimArray has correct number of dimensions');
364+
is-dims($test_2d, [2,2], 'Cloned MultiDimArray has correct dimensions');
365+
ok(nqp::atposnd($copied, nqp::list_i(0, 0)) == 10, 'Clone gets correct values (1)');
366+
ok(nqp::atposnd($copied, nqp::list_i(0, 1)) == 11, 'Clone gets correct values (2)');
367+
ok(nqp::atposnd($copied, nqp::list_i(1, 0)) == 20, 'Clone gets correct values (3)');
368+
ok(nqp::atposnd($copied, nqp::list_i(1, 1)) == 21, 'Clone gets correct values (4)');
369+
370+
nqp::bindposnd($test_2d, nqp::list_i(0, 0), 42);
371+
nqp::bindposnd($test_2d, nqp::list_i(0, 1), 43);
372+
nqp::bindposnd($test_2d, nqp::list_i(1, 0), 44);
373+
nqp::bindposnd($test_2d, nqp::list_i(1, 1), 45);
374+
ok(nqp::atposnd($copied, nqp::list_i(0, 0)) == 10, 'Modifying original does not affect clone (1)');
375+
ok(nqp::atposnd($copied, nqp::list_i(0, 1)) == 11, 'Modifying original does not affect clone (2)');
376+
ok(nqp::atposnd($copied, nqp::list_i(1, 0)) == 20, 'Modifying original does not affect clone (3)');
377+
ok(nqp::atposnd($copied, nqp::list_i(1, 1)) == 21, 'Modifying original does not affect clone (4)');
378+
}

0 commit comments

Comments
 (0)