### ----------------------------------------------------
### -- Math::SparseMatrix
### -- Licenses: Artistic-2.0
### -- Authors: Anton Antonov
### -- File: t/21-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
my $matrix1 = generate-random-sparse-matrix(:$nrow, :$ncol, :$density, :$tol):!decorated;
isa-ok
        $matrix1,
        Math::SparseMatrix::CSR,
        "Expected type after creation";

## 2
ok Math::SparseMatrix.new($matrix1);

## 3
isa-ok Math::SparseMatrix.new($matrix1), Math::SparseMatrix:D;

## 4
isa-ok Math::SparseMatrix.new($matrix1, row-names => Whatever, column-names => ('A' .. 'Z').head($ncol)),
        Math::SparseMatrix:D;

## 5
subtest {
        ok Math::SparseMatrix.new(core-matrix => $matrix1, row-names => 'a' .. 'e', column-names => 'A' .. 'H');
        my $m = Math::SparseMatrix.new(core-matrix => $matrix1, row-names => 'a' .. 'e', column-names => 'A' .. 'H');
        isa-ok $m, Math::SparseMatrix:D;
        is $m.dimensions, ($nrow, $ncol), "Expected dimensions";
}
## 6
my $smat9 = Math::SparseMatrix.new($matrix1);
isa-ok
        Math::SparseMatrix.new(core-matrix => $smat9, row-names => 'a' .. 'e', column-names => 'A' .. 'H'),
        Math::SparseMatrix:D;

## 7
subtest {
        my $m = Math::SparseMatrix.new(core-matrix => $matrix1, row-names => 'a' .. 'e');
        isa-ok Math::SparseMatrix.new($m, column-names => 'A' .. 'H'), Math::SparseMatrix:D;
        my $m2 = Math::SparseMatrix.new($m, column-names => 'A' .. 'H');
        is $m2.row-names, ('a'...'e');
        is $m2.column-names, ('A'...'H');
}

## 8
my @edge-dataset =
        { from => 'A', to => 'B', weight => 1 },
        { from => 'B', to => 'C', weight => 2 },
        { from => 'C', to => 'A', weight => 3 };

ok
        Math::SparseMatrix.new(:@edge-dataset, :directed),
        'Object created successfully for directed graph with default parameters';

## 9
ok
        Math::SparseMatrix.new(:@edge-dataset, :!directed),
        'Object created successfully for undirected graph with default parameters';

## 10
subtest {
        ok
                Math::SparseMatrix.new(:@edge-dataset, :directed, row-names => <X A B C Y>, column-names => <A B C>),
                'Object created successfully for directed graph with custom row and column names';

        my $m = Math::SparseMatrix.new(:@edge-dataset, :directed, row-names => <X A B C Y>, column-names => <A B C>);
        is $m.dimensions, (5, 3), "Expected number of rows";
        is $m.row-names, <X A B C Y>, "Expected rows names";
}

## 11
ok
        Math::SparseMatrix.new(:@edge-dataset, :!directed, :implicit-value(5)),
        'Object created successfully for undirected graph with implicit value';

## 12
subtest {
    my $obj12 = Math::SparseMatrix.new(:@edge-dataset, :!directed, row-names => <B A C E D>.map(*.Str));
    is-deeply
            $obj12.row-names.Array,
            'BACED'.comb.Array,
            'Matrix has row-names as provided';

    is-deeply
            $obj12.column-names.Array,
            'BACED'.comb.Array,
            'Matrix has column-names as provided';

}

done-testing;


