From e9758859568d510f8a42131ee0fed29a1d6b8c53 Mon Sep 17 00:00:00 2001 From: Alex Jordan Date: Wed, 10 Jun 2026 23:29:39 -0700 Subject: [PATCH] don't try to remove a Matrix's only row (or only column) --- lib/Value/Matrix.pm | 2 ++ t/math_objects/matrix.t | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/Value/Matrix.pm b/lib/Value/Matrix.pm index 9a6c41f70..277d92f83 100644 --- a/lib/Value/Matrix.pm +++ b/lib/Value/Matrix.pm @@ -1502,6 +1502,7 @@ sub removeRow { my @dim = $self->dimensions; my $degree = scalar @dim; Value::Error("removeRow cannot be used on a Matrix of degree 1") if $degree == 1; + Value::Error("cannot remove a Matrix's only row") if $dim[0] == 1; my @indices = map { [ 1 .. $_ ] } @dim; Value::Error("Can only remove rows 1 through $indices[0][-1]") unless $r =~ /^\d+$/ && $r >= 1 && $r <= $indices[0][-1]; @@ -1529,6 +1530,7 @@ sub removeColumn { my @dim = $self->dimensions; my $degree = scalar @dim; Value::Error("removeColumn cannot be used on a Matrix of degree 1") if $degree == 1; + Value::Error("cannot remove a Matrix's only column") if $dim[1] == 1; my @indices = map { [ 1 .. $_ ] } @dim; Value::Error("Can only remove columns 1 through $indices[1][-1]") unless $r =~ /^\d+$/ && $r >= 1 && $r <= $indices[1][-1]; diff --git a/t/math_objects/matrix.t b/t/math_objects/matrix.t index 0fcd83a4e..c793daa31 100644 --- a/t/math_objects/matrix.t +++ b/t/math_objects/matrix.t @@ -334,6 +334,11 @@ subtest 'Remove row' => sub { like dies { $A->removeRow('a'); }, qr/Can only remove rows 1 through 4/, 'check that error is thrown for bad row specification'; + + my $D = Matrix([ [ 1, 2, 3 ] ]); + like dies { + $D->removeRow(1); + }, qr/cannot remove a Matrix's only row/, 'check that error is thrown for trying to remove the only row'; }; subtest 'Remove column' => sub { @@ -362,6 +367,11 @@ subtest 'Remove column' => sub { like dies { $A->removeColumn('a'); }, qr/Can only remove columns 1 through 4/, 'check that error is thrown for bad column specification'; + + my $D = Matrix([1], [2], [3]); + like dies { + $D->removeColumn(1); + }, qr/cannot remove a Matrix's only column/, 'check that error is thrown for trying to remove the only column'; }; subtest 'Construct an identity matrix' => sub {