### ----------------------------------------------------
### -- ML::TriesWithFrequencies
### -- Licenses: Artistic-2.0
### -- Authors: Anton Antonov
### -- File: t/03-representations.rakutest
### ----------------------------------------------------
use Test;

use lib '.';
use lib './lib';

use ML::TriesWithFrequencies;
use ML::TriesWithFrequencies::Trie;

# The previous test should have checked that these commands work and
# produce expected results:
my @words = <bar barman>;
my $tr0 = trie-create-by-split(@words);

## The commands above should produce the trie:
# TRIEROOT => 2
# └─b => 2
#   └─a => 2
#     └─r => 2
#       └─m => 1
#         └─a => 1
#           └─n => 1

## With node counts:
# {Internal => 6, Leaves => 1, Total => 7}

plan 5;

## 1
my Str $wlFormat =
        '<|$TrieRoot -> <|$TrieValue -> 2, "b" -> <|$TrieValue -> 2, "a" ->
<|$TrieValue -> 2, "r" -> <|$TrieValue -> 2, "m" -> <|$TrieValue ->1,
"a" -> <|$TrieValue -> 1, "n" -> <|$TrieValue -> 1|>|>|>|>|>|>|>|>';

is-deeply
        $tr0.WL.subst(/\s/, ''):g,
        $wlFormat.subst(/\s/, ''):g,
        'Convert WL format';

## 2
my Str $jsonFormat =
        '{"key":"TRIEROOT", "value":2, "children":[{"key":"b", "value":2,
"children":[{"key":"a", "value":2, "children":[{"key":"r", "value":2, "children":[{"key":"m", "value":1,
"children":[{"key":"a", "value":1, "children":[{"key":"n", "value":1, "children":[]}]}]}]}]}]}]}';

is-deeply
        $tr0.JSON.subst(/\s/, ''):g,
        $jsonFormat.subst(/\s/, ''):g,
        'Convert JSON format';

## 3
my Str $xmlFormat = q:to/XMLEND/;
<TRIEROOT>
 <TRIEVALUE>2</TRIEVALUE>
 <b>
  <TRIEVALUE>2</TRIEVALUE>
  <a>
   <TRIEVALUE>2</TRIEVALUE>
   <r>
    <TRIEVALUE>2</TRIEVALUE>
    <m>
     <TRIEVALUE>1</TRIEVALUE>
     <a>
      <TRIEVALUE>1</TRIEVALUE>
      <n>
       <TRIEVALUE>1</TRIEVALUE>
      </n>
     </a>
    </m>
   </r>
  </a>
 </b>
</TRIEROOT>
XMLEND

is-deeply
        $tr0.XML.subst(/\s/, ''):g,
        $xmlFormat.subst(/\s/, ''):g,
        'Convert to XML format';

## 4
my ML::TriesWithFrequencies::Trie $tr4 .= new;
is-deeply
        $tr4.from-map-format($tr0.hash).hash,
        $tr0.hash,
        'Hash conversions from-to equivalence.';

## 5
my @words5 = ['bar' xx 6].append('bark' xx 3).append('cam' xx 3).append('came');
my $tr5 = trie-create-by-split(@words5);

# %repr5 was derived with:
#
# use JSON::Fast;
# my $repr5 = from-json($tr5.JSON);
#
# I did not want to make a dependency on JSON::Fast.

my %repr5 =${:children($[{:children($[{:children($[{:children($[{:children($[]), :key("e"), :value(1)},]), :key("m"), :value(4)},]), :key("a"), :value(4)},]), :key("c"), :value(4)}, {:children($[{:children($[{:children($[{:children($[]), :key("k"), :value(3)},]), :key("r"), :value(9)},]), :key("a"), :value(9)},]), :key("b"), :value(9)}]), :key("TRIEROOT"), :value(13)};

my ML::TriesWithFrequencies::Trie $tr5New .= new;
is-deeply
        $tr5New.from-json-map-format(%repr5).hash,
        $tr5.hash,
        'Hash conversions from-to equivalence.';

done-testing;


