<commit_before># Copyright (C) 2010, <NAME>. See accompanying LICENSE file, or
# http://www.opensource.org/licenses/artistic-license-2.0.php for license.

INIT {
    use('UnitTest::Testcase');
    use('UnitTest::Assertions');
}

class Pla::Testcase is UnitTest::Testcase {
    method default_loader() {
    	Pla::Loader.new;
    }

    # A default value which can be set at a particular location and tested
    method defaultvalue() {
        return (1);
    }

    # The null value which is auto-inserted into the matrix on resize.
    method nullvalue() {
        return (0);
    }

    # A novel value which can be used to flag interesting changes in tests.
    method fancyvalue() {
        return (2);
    }

    # Create an empty matrix of the given type
    method matrix() {
        Exception::MethodNotFound.new(
            :message("Must subclass matrix in your test class")
        ).throw;
    }

    # Create a 2x2 matrix of the type with given values row-first
    method matrix2x2($aa, $ab, $ba, $bb) {
        my $m := self.matrix();
        $m{Key.new(0,0)} := $aa;
        $m{Key.new(0,1)} := $ab;
        $m{Key.new(1,0)} := $ba;
        $m{Key.new(1,1)} := $bb;
        return ($m);
    }

    method defaultmatrix2x2() {
        return self.matrix2x2(
            self.defaultvalue(),
            self.defaultvalue(),
            self.defaultvalue(),
            self.defaultvalue()
        );
    }

    # Create a 3x3 matrix of the type with given values row-first
    method matrix3x3($aa, $ab, $ac, $ba, $bb, $bc, $ca, $cb, $cc) {
        my $m := self.matrix();
        $m{Key.new(0,0)} := $aa;
        $m{Key.new(0,1)} := $ab;
        $m{Key.new(0,2)} := $ac;
        $m{Key.new(1,0)} := $ba;
        $m{Key.new(1,1)} := $bb;
        $m{Key.new(1,2)} := $bc;
        $m{Key.new(2,0)} := $ca;
        $m{Key.new(2,1)} := $cb;
        $m{Key.new(2,2)} := $cc;
        return ($m);
    }

    ### COMMON TESTS ###
    # TODO: For some of the stub tests below, we should fail() if a test isn't
    #       provided. This means we're going to have to actually implement these
    #       methods in the various types and enforce the behavior.

    method test_OP_new() {
        assert_throws_nothing("Cannot create ComplexMatrix2D", {
            my $m := self.matrix();
            assert_not_null($m, "Could not create a ComplexMatrix2D");
        });
    }

    method test_OP_does() {
        my $m := self.matrix();
        assert_true(pir::does($m, "matrix"), "Does not do matrix");
        assert_false(pir::does($m, "gobbledegak"), "Does gobbledegak");
    }

    method test_VTABLE_get_pmc_keyed() {
        my $m := self.matrix();
        my $a := self.defaultvalue();
        $m{Key.new(0,0)} := $a;
        my $b := $m{Key.new(0,0)};
        assert_equal($a, $b, "get_pmc_keyed doesn't work");
    }

    method test_VTABLE_set_pmc_keyed() {
        assert_throws_nothing("Cannot set_pmc_keyed", {
            my $m := self.matrix();
            my $a := self.defaultvalue();
            $m{Key.new(0,0)} := $a;
        });
    }

    method test_VTABLE_clone() {
        my $m := self.defaultmatrix2x2();
        my $n := pir::clone($m);
        assert_equal($m, $n, "clones are not equal");
        assert_not_same($m, $n, "clones are the same PMC!");
    }

    method test_VTABLE_is_equal() {
        my $m := self.defaultmatrix2x2();
        my $n := self.defaultmatrix2x2();
        assert_equal($m, $n, "equal matrices are not equal");
    }

    method test_VTABLE_is_equal_SIZEFAIL() {
        my $m := self.defaultmatrix2x2();
        my $n := self.defaultmatrix2x2();
        $n{Key.new(2, 2)} := self.nullvalue();
        assert_not_equal($m, $n, "different sized matrices are equal");
    }

    method test_VTABLE_is_equal_ELEMSFAIL() {
        my $m := self.defaultmatrix2x2();
        my $n := self.defaultmatrix2x2();
        $n{Key.new(1,1)} := self.fancyvalue();
        assert_not_equal($m, $n, "non-equal matrices are equal");
    }

    method test_VTABLE_get_attr_str() {
        my $m := self.matrix();
        $m{Key.new(5,7)} := self.defaultvalue;
        assert_equal(pir::getattribute__PPS($m, "rows"), 6, "matrix does not have right size");
        assert_equal(pir::getattribute__PPS($m, "cols"), 8, "matrix does not have right size");
    }

    method test_VTABLE_get_attr_str_EMPTY() {
        my $m := self.matrix();
        assert_equal(pir::getattribute__PPS($m, "rows"), 0, "empty matrix has non-zero row count");
        assert_equal(pir::getattribute__PPS($m, "cols"), 0, "empty matrix has non-zero col count");
    }

    # TODO: Add tests that get_pmc_keyed_int and set_pmc_keyed_int share a
    #       correct linear relationship with get_pmc_keyed and set_pmc_keyed.

    # Test to show that autoresizing behavior of the type is consistent.
    method test_autoresizing() {
        todo("Tests Needed!");
    }

    method test_METHOD_resize() {
        my $m := self.matrix();
        $m.resize(3,3);
        assert_equal(pir::getattribute__PPS($m, "rows"), 3, "matrix does not have right size");
        assert_equal(pir::getattribute__PPS($m, "cols"), 3, "matrix does not have right size");
    }

    method test_METHOD_resize_SHRINK() {
        my $m := self.matrix();
        $m.resize(3,3);
        $m.resize(1,1);
        assert_equal(pir::getattribute__PPS($m, "rows"), 3, "matrix does not have right size");
        assert_equal(pir::getattribute__PPS($m, "cols"), 3, "matrix does not have right size");
    }

    method test_METHOD_resize_NEGATIVEINDICES() {
        my $m := self.matrix();
        $m.resize(-1, -1);
        assert_equal(pir::getattribute__PPS($m, "cols"), 0, "negative indices silently ignored");
        assert_equal(pir::getattribute__PPS($m, "rows"), 0, "negative indices silently ignored");
    }

    method test_METHOD_fill_RESIZE() {
        my $m := self.defaultmatrix2x2();
        my $n := self.matrix();
        $n.fill(self.defaultvalue(), 2, 2);
        assert_equal($n, $m, "Cannot fill+Resize");
    }

    method test_METHOD_fill() {
        my $m := self.defaultmatrix2x2();
        my $n := self.matrix2x2(
            self.fancyvalue(),
            self.fancyvalue(),
            self.fancyvalue(),
            self.fancyvalue()
        );
        $m.fill(self.fancyvalue());
        assert_equal($n, $m, "Cannot fill");
    }


    # Test transposing square matrices
    method test_METHOD_transpose() {
        todo("Tests Needed!");
    }

    # Test transposing non-square matrices
    method test_METHOD_transpose_DIMCHANGE() {
        todo("Tests Needed!");
    }

    # Test transposing square matrices
    method test_METHOD_mem_transpose() {
        todo("Tests Needed!");
    }
<commit_msg>update CharMatrix2d to use the Test library. Some tests fail.<commit_after># Copyright (C) 2010, <NAME>. See accompanying LICENSE file, or
# http://www.opensource.org/licenses/artistic-license-2.0.php for license.

INIT {
    use('UnitTest::Testcase');
    use('UnitTest::Assertions');
}

class Pla::Testcase is UnitTest::Testcase {
    method default_loader() {
    	Pla::Loader.new;
    }

    # A default value which can be set at a particular location and tested
    method defaultvalue() {
        return (1);
    }

    # The null value which is auto-inserted into the matrix on resize.
    method nullvalue() {
        return (0);
    }

    # A novel value which can be used to flag interesting changes in tests.
    method fancyvalue() {
        return (2);
    }

    # Create an empty matrix of the given type
    method matrix() {
        Exception::MethodNotFound.new(
            :message("Must subclass matrix in your test class")
        ).throw;
    }

    # Create a 2x2 matrix of the type with given values row-first
    method matrix2x2($aa, $ab, $ba, $bb) {
        my $m := self.matrix();
        $m{Key.new(0,0)} := $aa;
        $m{Key.new(0,1)} := $ab;
        $m{Key.new(1,0)} := $ba;
        $m{Key.new(1,1)} := $bb;
        return ($m);
    }

    method defaultmatrix2x2() {
        return self.matrix2x2(
            self.defaultvalue(),
            self.defaultvalue(),
            self.defaultvalue(),
            self.defaultvalue()
        );
    }

    # Create a 3x3 matrix of the type with given values row-first
    method matrix3x3($aa, $ab, $ac, $ba, $bb, $bc, $ca, $cb, $cc) {
        my $m := self.matrix();
        $m{Key.new(0,0)} := $aa;
        $m{Key.new(0,1)} := $ab;
        $m{Key.new(0,2)} := $ac;
        $m{Key.new(1,0)} := $ba;
        $m{Key.new(1,1)} := $bb;
        $m{Key.new(1,2)} := $bc;
        $m{Key.new(2,0)} := $ca;
        $m{Key.new(2,1)} := $cb;
        $m{Key.new(2,2)} := $cc;
        return ($m);
    }

    ### COMMON TESTS ###
    # TODO: For some of the stub tests below, we should fail() if a test isn't
    #       provided. This means we're going to have to actually implement these
    #       methods in the various types and enforce the behavior.

    method test_OP_new() {
        assert_throws_nothing("Cannot create ComplexMatrix2D", {
            my $m := self.matrix();
            assert_not_null($m, "Could not create a ComplexMatrix2D");
        });
    }

    method test_OP_does() {
        my $m := self.matrix();
        assert_true(pir::does($m, "matrix"), "Does not do matrix");
        assert_false(pir::does($m, "gobbledegak"), "Does gobbledegak");
    }

    method test_VTABLE_get_pmc_keyed() {
        my $m := self.matrix();
        my $a := self.defaultvalue();
        $m{Key.new(0,0)} := $a;
        my $b := $m{Key.new(0,0)};
        assert_equal($a, $b, "get_pmc_keyed doesn't work");
    }

    method test_VTABLE_set_pmc_keyed() {
        assert_throws_nothing("Cannot set_pmc_keyed", {
            my $m := self.matrix();
            my $a := self.defaultvalue();
            $m{Key.new(0,0)} := $a;
        });
    }

    method test_VTABLE_clone() {
        my $m := self.defaultmatrix2x2();
        my $n := pir::clone($m);
        assert_equal($m, $n, "clones are not equal");
        assert_not_same($m, $n, "clones are the same PMC!");
    }

    method test_VTABLE_is_equal() {
        my $m := self.defaultmatrix2x2();
        my $n := self.defaultmatrix2x2();
        assert_equal($m, $n, "equal matrices are not equal");
    }

    method test_VTABLE_is_equal_SIZEFAIL() {
        my $m := self.defaultmatrix2x2();
        my $n := self.defaultmatrix2x2();
        $n{Key.new(2, 2)} := self.nullvalue();
        assert_not_equal($m, $n, "different sized matrices are equal");
    }

    method test_VTABLE_is_equal_ELEMSFAIL() {
        my $m := self.defaultmatrix2x2();
        my $n := self.defaultmatrix2x2();
        $n{Key.new(1,1)} := self.fancyvalue();
        assert_not_equal($m, $n, "non-equal matrices are equal");
    }

    method test_VTABLE_get_attr_str() {
        my $m := self.matrix();
        $m{Key.new(5,7)} := self.defaultvalue;
        assert_equal(pir::getattribute__PPS($m, "rows"), 6, "matrix does not have right size");
        assert_equal(pir::getattribute__PPS($m, "cols"), 8, "matrix does not have right size");
    }

    method test_VTABLE_get_attr_str_EMPTY() {
        my $m := self.matrix();
        assert_equal(pir::getattribute__PPS($m, "rows"), 0, "empty matrix has non-zero row count");
        assert_equal(pir::getattribute__PPS($m, "cols"), 0, "empty matrix has non-zero col count");
    }

    # TODO: Add tests that get_pmc_keyed_int and set_pmc_keyed_int share a
    #       correct linear relationship with get_pmc_keyed and set_pmc_keyed.

    # Test to show that autoresizing behavior of the type is consistent.
    method test_autoresizing() {
        todo("Tests Needed!");
    }

    method test_METHOD_resize() {
        my $m := self.matrix();
        $m.resize(3,3);
        assert_equal(pir::getattribute__PPS($m, "rows"), 3, "matrix does not have right size");
        assert_equal(pir::getattribute__PPS($m, "cols"), 3, "matrix does not have right size");
    }

    method test_METHOD_resize_SHRINK() {
        my $m := self.matrix();
        $m.resize(3,3);
        $m.resize(1,1);
        assert_equal(pir::getattribute__PPS($m, "rows"), 3, "matrix does not have right size");
        assert_equal(pir::getattribute__PPS($m, "cols"), 3, "matrix does not have right size");
    }

    method test_METHOD_resize_NEGATIVEINDICES() {
        my $m := self.matrix();
        $m.resize(-1, -1);
        assert_equal(pir::getattribute__PPS($m, "cols"), 0, "negative indices silently ignored");
        assert_equal(pir::getattribute__PPS($m, "rows"), 0, "negative indices silently ignored");
    }

    method test_METHOD_fill_RESIZE() {
        my $m := self.defaultmatrix2x2();
        my $n := self.matrix();
        $n.fill(self.defaultvalue(), 2, 2);
        assert_equal($n, $m, "Cannot fill+Resize");
    }

    method test_METHOD_fill() {
        my $m := self.defaultmatrix2x2();
        my $n := self.matrix2x2(
            self.fancyvalue(),
            self.fancyvalue(),
            self.fancyvalue(),
            self.fancyvalue()
        );
        pir::say($m);
        pir::say($n);
        $m.fill(self.fancyvalue());
        assert_equal($n, $m, "Cannot fill");
    }


    # Test transposing square matrices
    method test_METHOD_transpose() {
        todo("Tests Needed!");
    }

    # Test transposing non-square matrices
    method test_METHOD_transpose_DIMCHANGE() {
        todo("Tests Needed!");
    }

    # Test transposing square matrices
    method test_METHOD_mem_transpose() {
        todo("Tests Needed!");
    }

{'fraction_non_alphanumeric': 0.15141676505312868, 'fraction_numerical': 0.014979338842975207, 'mean_word_length': 2.460929519918284, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4960694', 'n_tokens_mistral': 4588, 'n_tokens_neox': 4461, 'n_words': 1101}