The Perl Toolchain Summit 2025 Needs You: You can help 🙏 Learn more

#!perl
use strict;
use Test::More tests => 41;
my ($x, $y);
###############################################################################
# new() as a class method.
note("\n\$x = Math::Matrix -> new([1, 2, 3], [4, 5, 6]);\n\n");
$x = Math::Matrix -> new([1, 2, 3], [4, 5, 6]);
is(ref($x), 'Math::Matrix', '$x is a Math::Matrix');
is_deeply([ @$x ], [[1, 2, 3], [4, 5, 6]], '$x has the right values');
note("\n\$x = Math::Matrix -> new([[1, 2, 3], [4, 5, 6]]);\n\n");
$x = Math::Matrix -> new([[1, 2, 3], [4, 5, 6]]);
is(ref($x), 'Math::Matrix', '$x is a Math::Matrix');
is_deeply([ @$x ], [[1, 2, 3], [4, 5, 6]], '$x has the right values');
note("\n\$x = Math::Matrix -> new([1, 2, 3]);\n\n");
$x = Math::Matrix -> new([1, 2, 3]);
is(ref($x), 'Math::Matrix', '$x is a Math::Matrix');
is_deeply([ @$x ], [[1, 2, 3]], '$x has the right values');
note("\n\$x = Math::Matrix -> new([[1, 2, 3]]);\n\n");
$x = Math::Matrix -> new([[1, 2, 3]]);
is(ref($x), 'Math::Matrix', '$x is a Math::Matrix');
is_deeply([ @$x ], [[1, 2, 3]], '$x has the right values');
note("\n\$x = Math::Matrix -> new(3);\n\n");
$x = Math::Matrix -> new(3);
is(ref($x), 'Math::Matrix', '$x is a Math::Matrix');
is_deeply([ @$x ], [[3]], '$x has the right values');
note("\n\$x = Math::Matrix -> new([3]);\n\n");
$x = Math::Matrix -> new([3]);
is(ref($x), 'Math::Matrix', '$x is a Math::Matrix');
is_deeply([ @$x ], [[3]], '$x has the right values');
note("\n\$x = Math::Matrix -> new([[3]]);\n\n");
$x = Math::Matrix -> new([[3]]);
is(ref($x), 'Math::Matrix', '$x is a Math::Matrix');
is_deeply([ @$x ], [[3]], '$x has the right values');
note("\n\$x = Math::Matrix -> new();\n\n");
$x = Math::Matrix -> new();
is(ref($x), 'Math::Matrix', '$x is a Math::Matrix');
is_deeply([ @$x ], [], '$x has the right values');
note("\n\$x = Math::Matrix -> new([]);\n\n");
$x = Math::Matrix -> new([]);
is(ref($x), 'Math::Matrix', '$x is a Math::Matrix');
is_deeply([ @$x ], [], '$x has the right values');
note("\n\$x = Math::Matrix -> new([[]]);\n\n");
$x = Math::Matrix -> new([[]]);
is(ref($x), 'Math::Matrix', '$x is a Math::Matrix');
is_deeply([ @$x ], [], '$x has the right values');
###############################################################################
# new() as an instance method with input argument(s)
note("\n\$x -> new([1, 2, 3], [4, 5, 6]); \$y = \$x -> new([9, 8, 7], [6, 5, 4])\n\n");
$x = Math::Matrix -> new([1, 2, 3], [4, 5, 6]);
$y = $x -> new([9, 8, 7], [6, 5, 4]);
is_deeply([ @$x ], [[1, 2, 3], [4, 5, 6]], '$x is unmodified');
is(ref($y), 'Math::Matrix', '$y is a Math::Matrix');
is_deeply([ @$y ], [[9, 8, 7], [6, 5, 4]], '$y has the right values');
note("\n\$x -> new([1, 2, 3], [4, 5, 6]); \$y = \$x -> new([9, 8, 7]);\n\n");
$x = Math::Matrix -> new([1, 2, 3], [4, 5, 6]);
$y = $x -> new([9, 8, 7]);
is_deeply([ @$x ], [[1, 2, 3], [4, 5, 6]], '$x is unmodified');
is(ref($y), 'Math::Matrix', '$y is a Math::Matrix');
is_deeply([ @$y ], [[9, 8, 7]], '$y has the right values');
note("\n\$x -> new([1, 2, 3], [4, 5, 6]); \$y = \$x -> new(9);\n\n");
$x = Math::Matrix -> new([1, 2, 3], [4, 5, 6]);
$y = $x -> new(9);
is_deeply([ @$x ], [[1, 2, 3], [4, 5, 6]], '$x is unmodified');
is(ref($y), 'Math::Matrix', '$y is a Math::Matrix');
is_deeply([ @$y ], [[9]], '$y has the right values');
###############################################################################
# new() as instance method with no input argument(s)
note("\n\$x = Math::Matrix -> new([1, 2, 3], [4, 5, 6]); \$y = \$x -> new();\n\n");
$x = Math::Matrix -> new([1, 2, 3], [4, 5, 6]);
$y = $x -> new();
is_deeply([ @$x ], [[1, 2, 3], [4, 5, 6]], '$x is unmodified');
is(ref($y), 'Math::Matrix', '$y is a Math::Matrix');
is_deeply([ @$y ], [[0, 0, 0], [0, 0, 0]], '$y has the right values');
note("\n\$x = Math::Matrix -> new([1, 2, 3]); \$y = \$x -> new();\n\n");
$x = Math::Matrix -> new([1, 2, 3]);
$y = $x -> new();
is_deeply([ @$x ], [[1, 2, 3]], '$x is unmodified');
is(ref($y), 'Math::Matrix', '$y is a Math::Matrix');
is_deeply([ @$y ], [[0, 0, 0]], '$y has the right values');
note("\n\$x = Math::Matrix -> new(1); \$y = \$x -> new();\n\n");
$x = Math::Matrix -> new(1);
$y = $x -> new();
is_deeply([ @$x ], [[1]], '$x is unmodified');
is(ref($y), 'Math::Matrix', '$y is a Math::Matrix');
is_deeply([ @$y ], [[0]], '$y has the right values');
note("\n\$x = Math::Matrix -> new([]); \$y = \$x -> new();\n\n");
$x = Math::Matrix -> new([]);
$y = $x -> new();
is_deeply([ @$x ], [], '$x is unmodified');
is(ref($y), 'Math::Matrix', '$y is a Math::Matrix');
is_deeply([ @$y ], [], '$y has the right values');