1 // Copyright (C) The Lightning Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
21 func Test(t *testing.T) { check.TestingT(t) }
23 type taglibSuite struct{}
25 var _ = check.Suite(&taglibSuite{})
27 type tagMatch struct {
33 func (s *taglibSuite) TestFindAllTinyData(c *check.C) {
34 pr, pw, err := os.Pipe()
35 c.Assert(err, check.IsNil)
38 fmt.Fprintf(pw, `>0000.00
39 ggagaactgtgctccgccttcaga
40 acacatgctagcgcgtcggggtgg
41 gactctagcagagtggccagccac
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})
52 c.Check(matches, check.DeepEquals, []tagMatch{{0, 0, 24}, {1, 44, 24}, {2, 92, 24}})
55 func (s *taglibSuite) TestFindAllRealisticSize(c *check.C) {
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))
61 for i := range haystack {
62 haystack[i] = acgt[int(haystack[i]&3)]
71 w := bufio.NewWriter(pw)
73 used := map[string]bool{}
74 fmt.Fprint(w, ">000\n")
75 for i := 0; len(tags) < tagcount; i += (len(haystack) - tagsize) / tagcount {
77 tag := haystack[i : i+tagsize]
78 for used[string(tag)] {
80 tag = haystack[i : i+tagsize]
82 used[string(tag)] = true
83 tags = append(tags, strings.ToLower(string(tag)))
88 c.Logf("@%v build library", time.Since(start))
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})
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))