Initial tiling code.
[lightning.git] / taglib_test.go
1 package main
2
3 import (
4         "bufio"
5         "fmt"
6         "io"
7         "math/rand"
8         "os"
9         "strings"
10         "testing"
11         "time"
12
13         "gopkg.in/check.v1"
14 )
15
16 func Test(t *testing.T) { check.TestingT(t) }
17
18 type taglibSuite struct{}
19
20 var _ = check.Suite(&taglibSuite{})
21
22 func (s *taglibSuite) TestFindAllTinyData(c *check.C) {
23         pr, pw, err := os.Pipe()
24         c.Assert(err, check.IsNil)
25         go func() {
26                 defer pw.Close()
27                 fmt.Fprintf(pw, `>0000.00
28 ggagaactgtgctccgccttcaga
29 acacatgctagcgcgtcggggtgg
30 gactctagcagagtggccagccac
31 `)
32         }()
33         var taglib tagLibrary
34         err = taglib.Load(pr)
35         c.Assert(err, check.IsNil)
36         haystack := []byte(`ggagaactgtgctccgccttcagaccccccccccccccccccccacacatgctagcgcgtcggggtgggggggggggggggggggggggggggactctagcagagtggccagccac`)
37         var matches []tagMatch
38         taglib.FindAll(haystack, func(id tagID, pos int) {
39                 matches = append(matches, tagMatch{id, pos})
40         })
41         c.Check(matches, check.DeepEquals, []tagMatch{{0, 0}, {1, 44}, {2, 92}})
42 }
43
44 func (s *taglibSuite) TestFindAllRealisticSize(c *check.C) {
45         start := time.Now()
46         acgt := []byte{'a', 'c', 'g', 't'}
47         haystack := make([]byte, 25000000) // ~1/2 smallest human chromosome
48         c.Logf("@%v haystack", time.Since(start))
49         rand.Read(haystack)
50         for i := range haystack {
51                 haystack[i] = acgt[int(haystack[i]&3)]
52         }
53
54         tagcount := 12500
55         tagsize := 24
56         var tags []string
57         pr, pw := io.Pipe()
58         go func() {
59                 defer pw.Close()
60                 w := bufio.NewWriter(pw)
61                 defer w.Flush()
62                 used := map[string]bool{}
63                 fmt.Fprint(w, ">000\n")
64                 for i := 0; len(tags) < tagcount; i += (len(haystack) - tagsize) / tagcount {
65                         i := i
66                         tag := haystack[i : i+tagsize]
67                         for used[string(tag)] {
68                                 i++
69                                 tag = haystack[i : i+tagsize]
70                         }
71                         used[string(tag)] = true
72                         tags = append(tags, strings.ToLower(string(tag)))
73                         w.Write(tag)
74                         w.Write([]byte{'\n'})
75                 }
76         }()
77         c.Logf("@%v build library", time.Since(start))
78         var taglib tagLibrary
79         err := taglib.Load(pr)
80         c.Assert(err, check.IsNil)
81         c.Logf("@%v find tags in input", time.Since(start))
82         var matches []tagMatch
83         taglib.FindAll(haystack, func(id tagID, pos int) {
84                 matches = append(matches, tagMatch{id, pos})
85         })
86         c.Logf("@%v done", time.Since(start))
87         c.Check(matches[0], check.Equals, tagMatch{0, 0})
88         c.Check(matches[1].id, check.Equals, tagID(1))
89 }