Skip to content

Commit

Permalink
Add in a Test.Matcher type for matching matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
Whiteknight committed Feb 1, 2012
1 parent 3cfd14d commit 7924f9a
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 41 deletions.
34 changes: 0 additions & 34 deletions t/testlib/loader.nqp

This file was deleted.

2 changes: 1 addition & 1 deletion t/testlib/matrixtest.nqp
Expand Up @@ -3,6 +3,7 @@

class Pla::MatrixTest is Pla::MatrixTestBase {

# Helper function used when we descend into inline PIR
sub equal($a, $b, $r) {
my $assert := Rosella::construct(Rosella::Test::Asserter);
$assert.equal($a, $b, $r);
Expand Down Expand Up @@ -297,7 +298,6 @@ class Pla::MatrixTest is Pla::MatrixTestBase {
$!assert.HasMethod($m, "convert_to_pmc_matrix");
}


method test_negative_array() {
my $m := $!context.factory.defaultmatrix2x2();

Expand Down
50 changes: 50 additions & 0 deletions t/testlib/matrixtestbase.nqp
Expand Up @@ -66,9 +66,59 @@ module Pla::MatrixTestBase {
$context.set_data("factory_pmc", Pla::MatrixFactory::PMCMatrix2D.new);
$context.set_data("factory_number", Pla::MatrixFactory::NumMatrix2D.new);
my $asserter := Rosella::construct(Pla::MatrixAsserter);
Rosella::Test::add_matcher("matrix", Pla::MatrixTestMatcher.new);
Rosella::Test::test($type, :context($context), :asserter($asserter));
}
}

class Pla::MatrixTestBase { }

class Pla::MatrixTestMatcher is Rosella::Test::Matcher {
method expect_match($a, $b) {
my $a_rows := pir::getattribute__PPS($a, "rows");
my $a_cols := pir::getattribute__PPS($a, "cols");
my $b_rows := pir::getattribute__PPS($b, "rows");
my $b_cols := pir::getattribute__PPS($b, "cols");
if ($a_rows != $b_rows || $a_cols != $b_cols) {
return Rosella::construct(Rosella::Test::FailureResult, "Matrix dimensions [$a_rows, $a_cols] does not equal [$b_rows, $b_cols]");
}
my $i := 0;
while $i < $a_rows {
my $j := 0;
while $j < $a_cols {
my $val_a := $a{$!context.factory.key($i, $j)};
my $val_b := $b{$!context.factory.key($i, $j)};
if ($val_a != $val_b) {
Rosella::construct(Rosella::Test::FailureResult, "Value at [$i, $j] does not match");
}
}
}
Rosella::construct(Rosella::Test::SuccessResult);
}

method expect_no_match($a, $b) {
my $a_rows := pir::getattribute__PPS($a, "rows");
my $a_cols := pir::getattribute__PPS($a, "cols");
my $b_rows := pir::getattribute__PPS($b, "rows");
my $b_cols := pir::getattribute__PPS($b, "cols");
if ($a_rows != $b_rows || $a_cols != $b_cols) {
return Rosella::construct(Rosella::Test::SuccessResult);
}
my $i := 0;
while $i < $a_rows {
my $j := 0;
while $j < $a_cols {
my $val_a := $a{$!context.factory.key($i, $j)};
my $val_b := $b{$!context.factory.key($i, $j)};
if ($val_a != $val_b) {
Rosella::construct(Rosella::Test::SuccessResult);
}
}
}
Rosella::construct(Rosella::Test::FailureResult, "Matrices match (and shouldn't)");
}

method can_match($a, $b) {
return pir::does__ips($a, "matrix") && pir::does__ips($b, "matrix");
}
}
2 changes: 0 additions & 2 deletions t/testlib/methods/initialize_from_args.nqp
Expand Up @@ -30,6 +30,4 @@ class Pla::Methods::InitializeFromArgs is Pla::MatrixTestBase {
$!context.factory.fancyvalue(2), $!context.factory.fancyvalue(3));
$!assert.equal($n, $m, "cannot initialize from args undersized");
}


}
1 change: 0 additions & 1 deletion t/testlib/methods/initialize_from_array.nqp
Expand Up @@ -33,5 +33,4 @@ class Pla::Methods::InitializeFromArray is Pla::MatrixTestBase {
$n.initialize_from_array(1, 1, $a);
$!assert.equal($n, $m, "cannot initialize from array undersized");
}

}
2 changes: 0 additions & 2 deletions t/testlib/methods/item_at.nqp
Expand Up @@ -94,6 +94,4 @@ class Pla::Methods::ItemAt is Pla::MatrixTestBase {
$m.item_at(0, 0, $!context.factory.fancyvalue(0));
});
}


}
1 change: 0 additions & 1 deletion t/testlib/methods/resize.nqp
Expand Up @@ -20,5 +20,4 @@ class Pla::Methods::Resize is Pla::MatrixTestBase {
$m.resize(-1, -1);
$!assert.Size($m, 0, 0);
}

}

0 comments on commit 7924f9a

Please sign in to comment.