### ----------------------------------------------------
### -- Math::SparseMatrix
### -- Licenses: Artistic-2.0
### -- Authors: Anton Antonov
### -- File: t/01-CSR-creation.rakutest
### ----------------------------------------------------
use v6.d;

use lib <. lib>;
use Math::SparseMatrix;
use Math::SparseMatrix::CSR;
use Math::SparseMatrix::Utilities;
use Test;

my $nrow = 5;
my $ncol = 8;
my $density = 0.25;
my $tol = 0.01;

plan *;

## 1
ok generate-random-sparse-matrix(:$nrow, :$ncol, :$density, :$tol):!decorated;

## 2
subtest {
    my @rules = (1, 1) => 1, (0, 1) => 3, (3, 4) => -1;
    ok Math::SparseMatrix::CSR.new(:@rules);
    my $matrix = Math::SparseMatrix::CSR.new(:@rules);
    is-deeply $matrix.dimensions, (4, 5);
    my $matrix2 = Math::SparseMatrix::CSR.new(:@rules, :6nrow, :8ncol);
    is-deeply $matrix2.dimensions, (6, 8);
}, "Creation from rules";

## 3
my $matrix3 = generate-random-sparse-matrix(:$nrow, :$ncol, :$density, :$tol):!decorated;
isa-ok
        $matrix3,
        Math::SparseMatrix::CSR,
        "Expected type after creation";

## 4
subtest {
    # Dense matrix creation
    #  my @entries4 = (
    #  (0, 0, 1),
    #  (0, 2, 2),
    #  (1, 1, 3),
    #  (2, 0, 4),
    #  (2, 2, 5),
    #  (3, 2, 7)
    #  );
    #
    #  my @rules4 = @entries4.map({ Pair.new($_.head(2).List, $_.tail) });
    #  my $matrix4 = Math::SparseMatrix::CSR.new(rules => @rules4);
    #  say $matrix4.Array.raku;
    my @mat = [1, 0, 2], [0, 3, 0], [4, 0, 5], [0, 0, 7];
    ok Math::SparseMatrix::CSR.new(dense-matrix => @mat);
    my $matrix = Math::SparseMatrix::CSR.new(dense-matrix => @mat);
    is-deeply $matrix.Array, @mat;
    my $matrix2 = Math::SparseMatrix::CSR.new(dense-matrix => @mat, nrow => 6, ncol => 5);
    is-deeply
            $matrix2.Array,
            @mat.map({ $_.append([0, 0]) }).Array.append(((0 xx 5).Array xx 2).Array);
    is $matrix2.verify, True;
}, "Creation from matrix";


done-testing;


