Debian Codegraph @

alphabet/alphabet_test.go

raw symbols SCIP
1 // Copyright ©2011-2012 The bíogo Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package alphabet
6
7 import (
8 "strings"
9 "testing"
10 "unicode"
11
12 "gopkg.in/check.v1"
13 )
14
15 // Tests
16 func Test(t *testing.T) { check.TestingT(t) }
17
18 type S struct{}
19
20 var _ = check.Suite(&S{})
21
22 func (s *S) TestInterfaces(c *check.C) {
23 var (
24 alpha Alphabet
25 comp Complementor
26 )
27
28 for _, a := range []interface{}{DNA, RNA, Protein} {
29 c.Check(a, check.Implements, &alpha)
30 }
31
32 for _, a := range []interface{}{DNA, DNAredundant, RNA, RNAredundant} {
33 c.Check(a, check.Implements, &comp)
34 }
35
36 c.Check(Protein, check.Not(check.Implements), &comp)
37 }
38
39 type testAlphabets struct {
40 alphabet Alphabet
41 letters string
42 }
43
44 func (s *S) TestIsValid(c *check.C) {
45 for _, t := range []testAlphabets{
46 {DNA, "acgt"},
47 {DNAgapped, "-acgt"},
48 {RNA, "acgu"},
49 {RNAgapped, "-acgu"},
50 {Protein, "-abcdefghijklmnpqrstvwxyz*"},
51 } {
52 for i := 0; i < 256; i++ {
53 c.Check(t.alphabet.IsValid(Letter(i)), check.Equals, strings.ContainsRune(t.letters, unicode.ToUpper(rune(i))) || strings.ContainsRune(t.letters, unicode.ToLower(rune(i))))
54 }
55 }
56 }
57
58 func uc(l Letter) Letter {
59 return Letter(unicode.ToUpper(rune(l)))
60 }
61
62 func (s *S) TestLetter(c *check.C) {
63 for _, t := range []Alphabet{
64 DNA,
65 RNA,
66 Protein,
67 } {
68 for i := 0; i < t.Len(); i++ {
69 c.Check(t.IndexOf(t.Letter(i)), check.Equals, i,
70 check.Commentf("Index %d: %c == %d", i, t.Letter(i), t.IndexOf(t.Letter(i))))
71 c.Check(t.IndexOf(uc(t.Letter(i))), check.Equals, i,
72 check.Commentf("Index %d: %c == %d", i, uc(t.Letter(i)), t.IndexOf(uc(t.Letter(i)))))
73 }
74 }
75 }
76
77 func (s *S) TestComplement(c *check.C) {
78 for _, t := range []Complementor{
79 DNA,
80 RNA,
81 } {
82 for i := 0; i < 256; i++ {
83 if sc, ok := t.Complement(Letter(i)); ok {
84 dc, ok := t.Complement(sc)
85 c.Check(ok, check.Equals, true)
86 c.Check(dc, check.Equals, Letter(i))
87 }
88 }
89 }
90 }
91
92 func (s *S) TestComplementDirect(c *check.C) {
93 for _, t := range []Complementor{
94 DNA,
95 RNA,
96 } {
97 complement := t.ComplementTable()
98 for i := 0; i < 256; i++ {
99 if sc := complement[i]; sc <= unicode.MaxASCII {
100 dc := complement[sc]
101 c.Check(dc <= unicode.MaxASCII, check.Equals, true)
102 c.Check(dc, check.Equals, Letter(i))
103 } else {
104 c.Check(sc&unicode.MaxASCII, check.Equals, Letter(i&unicode.MaxASCII))
105 }
106 }
107 }
108 }
109
110 func (s *S) TestLetters(c *check.C) {
111 for _, t := range []testAlphabets{
112 {DNA, "acgtACGT"},
113 {DNA, "acgtACGT"},
114 {RNAgapped, "-acgu-ACGU"},
115 {RNAgapped, "-acgu-ACGU"},
116 {Protein, "-abcdefghijklmnpqrstvwxyz*-ABCDEFGHIJKLMNPQRSTVWXYZ*"},
117 } {
118 c.Check(t.alphabet.Letters(), check.Equals, t.letters)
119 }
120 }
121
122 func (s *S) TestRangeCheck(c *check.C) {
123 var err error
124 _, err = newAlphabet(string([]rune{256}), 0, 0, 0, !CaseSensitive)
125 c.Check(err, check.Not(check.IsNil))
126 _, err = newAlphabet(string([]rune{0}), 0, 0, 0, !CaseSensitive)
127 c.Check(err, check.IsNil)
128 _, err = newAlphabet(string([]rune{127}), 0, 0, 0, !CaseSensitive)
129 c.Check(err, check.IsNil)
130 _, err = newAlphabet(string([]rune{-1}), 0, 0, 0, !CaseSensitive)
131 c.Check(err, check.Not(check.IsNil))
132 }
133
134 func BenchmarkIsValid(b *testing.B) {
135 g, _ := newAlphabet("-abcdefghijklmnpqrstvwxyz*", 0, 0, 0, !CaseSensitive)
136 b.ResetTimer()
137 for i := 0; i < b.N; i++ {
138 g.IsValid(Letter(i))
139 }
140 }
141
142 func BenchmarkIsValidProtein(b *testing.B) {
143 for i := 0; i < b.N; i++ {
144 Protein.IsValid(Letter(i))
145 }
146 }
147
148 func BenchmarkIsValidDNA(b *testing.B) {
149 for i := 0; i < b.N; i++ {
150 DNA.IsValid(Letter(i))
151 }
152 }
153
154 func BenchmarkIsValidDNADirect(b *testing.B) {
155 valid := DNA.ValidLetters()
156 for i := 0; i < b.N; i++ {
157 _ = valid[byte(i)]
158 }
159 }
160
161 func BenchmarkIndexDNA(b *testing.B) {
162 for i := 0; i < b.N; i++ {
163 DNA.IndexOf(Letter(i))
164 }
165 }
166
167 func BenchmarkIndexDNADirect(b *testing.B) {
168 index := DNA.LetterIndex()
169 for i := 0; i < b.N; i++ {
170 _ = index[byte(i)]
171 }
172 }
173
174 func BenchmarkComplementDNA(b *testing.B) {
175 for i := 0; i < b.N; i++ {
176 DNA.Complement(Letter(i))
177 }
178 }
179
180 func BenchmarkComplementDNADirect(b *testing.B) {
181 comp := DNA.ComplementTable()
182 var c Letter
183 for i := 0; i < b.N; i++ {
184 if c = comp[Letter(i)]; c != 0x80 {
185 }
186 }
187 }