-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup: add tests and docs for Trie methods
- Loading branch information
Showing
2 changed files
with
169 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,85 @@ | ||
module TestTries | ||
|
||
using GATlab.Syntax.Tries | ||
import .Tries: node, leaf | ||
import .Tries: node, leaf, Node, Leaf, Empty, NonEmpty, zipwith, flatten, fold | ||
using Test | ||
|
||
p1 = node(:a => leaf(1), :b => node(:a => leaf(2), :c => leaf(3))) | ||
using MLStyle | ||
|
||
@test p1.a isa AbstractTrie | ||
@test_throws Tries.TrieDerefError p1[] | ||
@test p1.a[] == 1 | ||
@test p1.b.a isa NonEmptyTrie | ||
@test p1.b.a[] == 2 | ||
t1 = node(:a => leaf(1), :b => node(:a => leaf(2), :c => leaf(3))) | ||
|
||
@test sprint(show, p1.a) == "leaf(1)::NonEmptyTrie{Int64}" | ||
@test sprint(show, p1.b) == "NonEmptyTrie{Int64}\n├─ :a ⇒ 2\n└─ :c ⇒ 3\n" | ||
@test t1.a isa AbstractTrie | ||
@test_throws Tries.TrieDerefError t1[] | ||
@test t1.a[] == 1 | ||
@test t1.b.a isa NonEmptyTrie | ||
@test t1.b.a[] == 2 | ||
|
||
@test sprint(show, t1.a) == "leaf(1)::NonEmptyTrie{Int64}" | ||
@test sprint(show, t1.b) == "NonEmptyTrie{Int64}\n├─ :a ⇒ 2\n└─ :c ⇒ 3\n" | ||
|
||
@test ■ == PACKAGE_ROOT | ||
@test ■.a isa TrieVar | ||
@test ■.a.b isa TrieVar | ||
@test_throws Tries.TrieVarNotFound p1[■] | ||
@test_throws Tries.TrieVarNotFound t1[■] | ||
|
||
@test t1[■.a] == 1 | ||
|
||
@test t1[■.b.c] == 3 | ||
|
||
@test filter(x -> x % 2 == 0, t1) == node(:b => node(:a => leaf(2))) | ||
|
||
function int_sqrt(x) | ||
try | ||
Some(Int(sqrt(x))) | ||
catch e | ||
nothing | ||
end | ||
end | ||
|
||
@test filtermap(int_sqrt, Int, t1) == node(:a => leaf(1)) | ||
|
||
@test t1 == @match t1 begin | ||
NonEmpty(net1) => net1 | ||
end | ||
|
||
@test zipwith(+, t1, t1) == node(:a => leaf(2), :b => node(:a => leaf(4), :c => leaf(6))) | ||
@test zip(t1, t1) == node(:a => leaf((1,1)), :b => node(:a => leaf((2,2)), :c => leaf((3,3)))) | ||
|
||
|
||
@test flatten(leaf(t1)) == t1 | ||
@test flatten(leaf(leaf(1))) == leaf(1) | ||
@test flatten(node(:f => leaf(leaf(1)), :g => leaf(t1))) == | ||
node( | ||
:f => leaf(1) | ||
, :g => node( | ||
:a => leaf(1) | ||
, :b => node( | ||
:a => leaf(2) | ||
, :c => leaf(3) | ||
) | ||
) | ||
) | ||
|
||
@test mapwithkey((k, _) -> k, TrieVar, t1) == node(:a => leaf(■.a), :b => node(:a => leaf(■.b.a), :c => leaf(■.b.c))) | ||
|
||
keys = TrieVar[] | ||
|
||
traversewithkey((k, _) -> push!(keys, k), t1) | ||
|
||
@test keys == [■.a, ■.b.a, ■.b.c] | ||
|
||
@test fold(0, identity, d -> sum(values(d)), t1) == 6 | ||
@test fold(0, identity, d -> sum(values(d)), Trie{Int}()) == 0 | ||
|
||
@test all(iseven, t1) == false | ||
@test all(iseven, filter(iseven, t1)) | ||
|
||
@test merge(t1, node(:z => leaf(4))) == node(:a => leaf(1), :b => node(:a => leaf(2), :c => leaf(3)), :z => leaf(4)) | ||
|
||
@test Trie(■.a => 1, ■.b.a => 2, ■.b.c => 3) == t1 | ||
|
||
@test p1[■.a] == 1 | ||
g2 = node(:a => Trie{Int}()) | ||
|
||
@test p1[■.b.c] == 3 | ||
@test t2 == Trie{Int}() | ||
|
||
end |