1 // Copyright (C) The Lightning Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
20 func Test(t *testing.T) { check.TestingT(t) }
22 type taglibSuite struct{}
24 var _ = check.Suite(&taglibSuite{})
26 type tagMatch struct {
32 func (s *taglibSuite) TestFindAllTinyData(c *check.C) {
33 pr, pw, err := os.Pipe()
34 c.Assert(err, check.IsNil)
37 fmt.Fprintf(pw, `>0000.00
38 ggagaactgtgctccgccttcaga
39 acacatgctagcgcgtcggggtgg
40 gactctagcagagtggccagccac
45 c.Assert(err, check.IsNil)
46 haystack := []byte(`ggagaactgtgctccgccttcagaccccccccccccccccccccacacatgctagcgcgtcggggtgggggggggggggggggggggggggggactctagcagagtggccagccac`)
47 var matches []tagMatch
48 taglib.FindAll(haystack, func(id tagID, pos, taglen int) {
49 matches = append(matches, tagMatch{id, pos, taglen})
51 c.Check(matches, check.DeepEquals, []tagMatch{{0, 0, 24}, {1, 44, 24}, {2, 92, 24}})
54 func (s *taglibSuite) TestFindAllRealisticSize(c *check.C) {
56 acgt := []byte{'a', 'c', 'g', 't'}
57 haystack := make([]byte, 25000000) // ~1/2 smallest human chromosome
58 c.Logf("@%v haystack", time.Since(start))
60 for i := range haystack {
61 haystack[i] = acgt[int(haystack[i]&3)]
70 w := bufio.NewWriter(pw)
72 used := map[string]bool{}
73 fmt.Fprint(w, ">000\n")
74 for i := 0; len(tags) < tagcount; i += (len(haystack) - tagsize) / tagcount {
76 tag := haystack[i : i+tagsize]
77 for used[string(tag)] {
79 tag = haystack[i : i+tagsize]
81 used[string(tag)] = true
82 tags = append(tags, strings.ToLower(string(tag)))
87 c.Logf("@%v build library", time.Since(start))
89 err := taglib.Load(pr)
90 c.Assert(err, check.IsNil)
91 c.Logf("@%v find tags in input", time.Since(start))
92 var matches []tagMatch
93 taglib.FindAll(haystack, func(id tagID, pos, taglen int) {
94 matches = append(matches, tagMatch{id, pos, taglen})
96 c.Logf("@%v done", time.Since(start))
97 c.Check(matches[0], check.Equals, tagMatch{0, 0, tagsize})
98 c.Check(matches[1].id, check.Equals, tagID(1))