### ----------------------------------------------------
### -- Graph
### -- Licenses: Artistic-2.0
### -- Authors: Anton Antonov
### -- File: t/01-basic-usage.rakutest
### ----------------------------------------------------
use v6.d;

#use lib <. lib>;

use Graph;
use Graph::Classes;
# No need to import these classes since Graph/Makers.rakumod has EXPORT sub.
#`[
use Graph::Circulant;
use Graph::Complete;
use Graph::Cycle;
use Graph::HexagonalGrid;
use Graph::Indexed;
use Graph::Path;
use Graph::Petersen;
use Graph::Star;
use Graph::TriangularGrid;
]

use Test;

my @edges =
        { from => '1', to => '5', weight => 1 },
        { from => '1', to => '7', weight => 1 },
        { from => '2', to => '3', weight => 1 },
        { from => '2', to => '4', weight => 1 },
        { from => '2', to => '6', weight => 1 },
        { from => '2', to => '7', weight => 1 },
        { from => '2', to => '8', weight => 1 },
        { from => '2', to => '10', weight => 1 },
        { from => '2', to => '12', weight => 1 },
        { from => '3', to => '4', weight => 1 },
        { from => '3', to => '8', weight => 1 },
        { from => '4', to => '9', weight => 1 },
        { from => '5', to => '12', weight => 1 },
        { from => '6', to => '7', weight => 1 },
        { from => '9', to => '10', weight => 1 },
        { from => '11', to => '12', weight => 1 };

plan *;

## 1
my $graph1 = Graph.new;
ok $graph1.add-edges(@edges);

## 2
is $graph1.edge-count, @edges.elems;

## 3
is $graph1.vertex-count, 12;

## 4
is-deeply $graph1.vertex-list.sort, (1 .. 12)».Str.sort.List;

## 5
is-deeply $graph1.find-shortest-path('1', '4'), ["1", "7", "2", "4"];

## 6
is-deeply $graph1.find-hamiltonian-path.sort, $graph1.vertex-list.sort;

## 7
ok Graph.new(@edges);

## 8
my $graph8 = Graph.new(@edges);
is $graph8.Str, $graph1.Str;

## 9
is-deeply $graph8.vertex-list.sort, $graph1.vertex-list;

## 10
is-deeply $graph8.find-shortest-path('1', '4'), $graph1.find-shortest-path('1', '4');

## 11
my $graph11 = Graph.new(@edges, :directed);
is $graph11.directed, True;

## 12
is $graph11.vertex-out-degree('1'), 2;

## 13
is $graph11.vertex-in-degree('12'), 3;

## 14
is $graph1.vertex-degree('5'), 2;

## 15
is $graph1.vertex-in-degree('5'), $graph1.vertex-degree('5');

## 16
is-deeply
        Graph::Path.new('a' .. 'h', :directed).vertex-list.Array,
        ('a' ... 'h').Array;

## 17
my $gr17 = Graph::Path.new('a' .. 'h', :directed);
is-deeply
        $gr17.vertex-list,
        Graph.new($gr17, directed => Whatever).vertex-list;

## 18
is-deeply
        $gr17.edge-list.sort,
        Graph.new($gr17).edge-list.sort;

## 19
ok Graph.new('a' .. 'h', [a => 'b', c => 'd']);

## 20
is
        Graph.new('a' .. 'h', [a => 'b', c => 'd']).vertex-list,
        'a' .. 'h';

## 21
is
        Graph::Complete.new(5, :!directed).edge-count,
        10;

## 22
is
        Graph::Complete.new(5, :directed).edge-count,
        20;

## 23
my @edges23 = <1 2 3 4> X=> <1 2 3 4>;
my $g23 = Graph.new(@edges23, :directed);
is
        $g23.edge-count,
        16;

## 24
my $g24 = Graph.new(@edges23, :!directed);
is
        $g24.edge-count,
        $g23.vertex-list.combinations(2).elems + $g23.vertex-count;

## 25
my $g25 = $g24.clone;
subtest {
    ok $g25.vertex-add('a');
    ok $g25.vertex-add(<a b c>);
    is $g25.vertex-list.sort, <1 2 3 4 a b c>;
}, 'Adding vertexes';

## 26
is
        Graph::Star.new(5, prefix => 'st_', :!directed).edge-count,
        5;

## 27
isa-ok
        Graph::Petersen.new.vertex-coordinates,
        Map:D;

## 28
isa-ok
        Graph::Indexed.new($g24, 10),
        Graph;

## 29
isa-ok
        Graph::HexagonalGrid.new(3, 4),
        Graph;

## 30
isa-ok
        Graph::TriangularGrid.new(3, 4),
        Graph;

## 31
subtest {
    isa-ok Graph::Circulant.new(7, 2), Graph;
    isa-ok Graph::Circulant.new(7, [2, 4]), Graph;
    isa-ok Graph::Circulant.new(n => 10, jump => 4), Graph;
    isa-ok Graph::Circulant.new(n => 10, jumps => [2, 4]), Graph;
    is Graph::Circulant.new(10, 3).vertex-count, 10, 'number of vertexes';

    my @d = Graph::Circulant.new(10, 3).vertex-degree;
    is @d.min == @d.max, True, 'circulant graph is regular graph';
}, 'Circulant graph';

## 32
subtest {
    is
            Graph::Complete.new(4).eqv(Graph::Complete.new(4)),
            True,
            'Equivalence of Complete(4) graphs';

    is
            Graph::Complete.new(4).eqv(Graph::Star.new(4)),
            False,
            'Non-equivalence of Complete(4) and Star(4) graphs';

    is
            Graph::Circulant.new(6, 1).eqv(Graph::Cycle.new(6)),
            True,
            'Cicrculant graph Ci_n(1) gives Cycle(n)';

    # Cicrculant graph Ci_n(1, 2, ... ⎣n/2⎦) is a complete graph.
    is
            Graph::Circulant.new(6, [1, 2, 3]).is-complete,
            True,
            'Expected complete graph';

}, 'Equivalence tests';

done-testing;


