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