### ----------------------------------------------------
### -- Graph
### -- Licenses: Artistic-2.0
### -- Authors: Anton Antonov
### -- File: t/07-subgraphs.rakutest
### ----------------------------------------------------
use v6.d;
use Test;

#use lib <. lib>;

use Graph;
use Graph::Complete;
use Graph::Grid;

## 1
my $g1 = Graph::Grid.new(4, 6);
my @vs1 = ((^3) X (^2)).map({ $_.join('_') });
isa-ok $g1.subgraph(@vs1), Graph;

## 2
my $g2 = $g1.subgraph(@vs1);

isa-ok $g1.subgraph($g2.edges), Graph;

subtest {
    my $g = Graph::Grid.new(20, 30);

    my $vset = $g.vertex-list (-) $g.neighborhood-graph($g.vertex-list.pick(40), d => 1).vertex-list;

    my $g2 = $g.subgraph($vset.keys);

    is
            $g2.vertex-count < $g.vertex-count,
            True,
            'Less vertexes';

    is
            $g2.vertex-coordinates.keys.elems,
            $g2.vertex-count,
            'Same number of vertexes';

}, 'Expected vertex coordinates';

## 3
subtest {

    my $g = Graph::Complete(8);
    #say $g.wl(VertexLabels => "Name");

    my $g2 = $g.subgraph((1..5)».Str);
    #say $g2.wl(VertexLabels => "Name");

    is $g2.is-complete, True;

}, 'The subgraph of a complete graph is a complete graph';

## 4
subtest {

    my @edges =
            { from => 'Rock',     to => 'Scissors',  weight => 1},
            { from => 'Scissors', to => 'Paper',     weight => 2},
            { from => 'Paper',    to => 'Rock',      weight => 3},
            { from => 'Rock',     to => 'Lizard',    weight => 4},
            { from => 'Lizard',   to => 'Spock',     weight => 5},
            { from => 'Spock',    to => 'Scissors',  weight => 6},
            { from => 'Scissors', to => 'Lizard',    weight => 7},
            { from => 'Lizard',   to => 'Paper',     weight => 8},
            { from => 'Paper',    to => 'Spock',     weight => 9},
            { from => 'Spock',    to => 'Rock',      weight => 10}
            ;

    my $g = Graph.new(@edges, :directed);

    my $g1 = $g.subgraph( ['Rock' => 'Scissors', 'Scissors' => 'Paper', 'Paper' => 'Rock']);

    my @edges1 = $g1.edges(:dataset);
    is @edges1.map(*<weight>).sort, [1, 2, 3], 'expected weights';

}, 'Subgraph of weighted graph';

done-testing;


