\section{Testing}
Our testing framework has hooks for imports and the testing logic.
package nwk
import (
"testing"
//<<Testing imports>>
)
func TestNewick(t *testing.T) {
//<<Testing>>
}
We open the file \ty{test1.nwk} and scan the two trees it
contains. They should both be identical to the tree we want.
in := "test1.nwk"
f, err := os.Open(in)
if err != nil {
t.Errorf("couldn't open %q", in)
}
defer f.Close()
sc := NewScanner(f)
want := `(((A:0.2,B:0.3):0.3,(D:0.5,E:0.3):0.2):0.3,F:0.7);`
//<<Scan trees>>
We compare the trees we get with the one we want.
var root *Node
for sc.Scan() {
root = sc.Tree()
get := root.String()
if get != want {
t.Errorf("get:\n%s\nwant:\n%s\n",
get, want)
}
}
We copy the tree and check the identity between original and copy.
root2 := root.CopyClade()
get := root2.String()
if get != want {
t.Errorf("get:\n%s\nwant\n%s\n", get, want)
}
We label the tree uniformly to get Figure~\ref{fig:nwk}. Then we check
the newly labeled tree.
\begin{figure}
\begin{center}
\includegraphics{nwk}
\end{center}
\caption{Uniformly labeled version of the testing
tree.}\label{fig:nwk}
\end{figure}
root.UniformLabels("n")
want = `(((n13:0.2,n14:0.3)n12:0.3,` +
`(n16:0.5,n17:0.3)n15:0.2)n11` +
`:0.3,n18:0.7)n10;`
get = root.String()
if get != want {
t.Errorf("get:\n%s\nwant:\n%s",
get, want)
}
We find a lowest common ancestor of n13 and n16 in
Figure~\ref{fig:nwk}.
n1 := root.Child.Child.Child
n2 := root.Child.Child.Sib.Child
l1 := n1.LCA(n2)
l2 := n2.LCA(n1)
if l1.Id != l2.Id || l1.Id != 11 {
t.Errorf("get:\n%d\nwant:\n%d",
l1.Id, 11)
}
We measure the \ty{UpDistance} between n13 and the root.
ud := n1.UpDistance(root)
if ud != 0.8 {
t.Errorf("get:\n%g\nwant:\n%g", ud, 0.8)
}
We make a new node and add it as a child to the root.
ch := NewNode()
ch.Label = "new"
root.AddChild(ch)
get = root.String()
ot := want
want = `(((n13:0.2,n14:0.3)n12:0.3,` +
`(n16:0.5,n17:0.3)n15:0.2)n11` +
`:0.3,n18:0.7,new)n10;`
if get != want {
t.Errorf("get:\n%s\nwant:\n%s", get, want)
}
We remove the child again.
root.RemoveChild(ch)
get = root.String()
want = ot
if get != want {
t.Errorf("get:\n%s\nwant:\n%s", get, want)
}
get = root.Print()
want = "n10\n n18\n n11\n n15\n n17\n" +
" n16\n n12\n n14\n" +
" n13\n"
if get != want {
t.Errorf("get:\n%s\nwant:\n%s", get, want)
}
We test the \ty{Key} method on the root.
want = "n10$n11$n12$n13$n14$n15$n16$n17$n18"
get = root.Key("$")
if get != want {
t.Errorf("get:\n%s\nwant:\n%s", get, want)
}
For testing \ty{RemoveClade}, we read the tree shown in
Figure~\ref{fig:tt}A from \ty{test2.nwk}. Then we remove nodes 3 and 2.
\begin{figure}
\begin{center}
\begin{tabular}{ccc}
\textbf{A} & \textbf{B} & \textbf{C}\\
\scalebox{0.95}{\includegraphics{tt1}} &
\scalebox{0.95}{\includegraphics{tt2}} &
\scalebox{0.95}{\includegraphics{tt3}}
\end{tabular}
\end{center}
\caption{We remove the clade rooted on node 3 from tree \textbf{A}
to get tree \textbf{B} and the clade rooted on node 2 to get tree
\textbf{C}.}\label{fig:tt}
\end{figure}
//<<Read \ty{test2.nwk}>>
//<<Remove node 3>>
//<<Remove node 2>>
We read the tree with a scanner and save an original and a copy of it
so we can run several tests on the original tree.
in = "test2.nwk"
f, err = os.Open(in)
if err != nil {
t.Errorf("couldn't open %q", in)
}
defer f.Close()
sc = NewScanner(f)
sc.Scan()
origRoot := sc.Tree()
copyRoot := origRoot.CopyClade()
Removing node 3 from Figure~\ref{fig:tt}A should give us
Figure\ref{fig:tt}B, so that's the tree we want. We reach node 3 by
following the appropriate child and sibling links.
want = `((T1:47)4:1,((T6:15,T7:11)5:20,` +
`((T8:34,T9:37)6:3,T10:41)7:2)8:4)9;`
v := copyRoot.Child.Child.Sib
v.RemoveClade()
get = copyRoot.String()
if get != want {
t.Errorf("get\n%s\nwant:\n%s\n", get, want)
}
Removing node 2 from Figure~\ref{fig:tt}A should give
Figure~\ref{fig:tt}C, so that's the tree we want. Before we can remove
node 2, however, we generate a pristine copy of the original tree.
want = `((T1:47,(T5:31)3:10)4:1,((T6:15,T7:11)5:20,` +
`((T8:34,T9:37)6:3,T10:41)7:2)8:4)9;`
copyRoot = origRoot.CopyClade()
v = copyRoot.Child.Child.Sib.Child
v.RemoveClade()
get = copyRoot.String()
if get != want {
t.Errorf("get:\n%s\nwant:\n%s\n", get, want)
}