-
Notifications
You must be signed in to change notification settings - Fork 1
/
overlap_test.go
79 lines (72 loc) · 2.6 KB
/
overlap_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package overlap
import (
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/require"
)
func TestPoint(t *testing.T) {
p := pointImpl{loc: 0.5}
p.addTo("foo")
p.addTo("bar")
// See: https://npf.io/2017/08/lies/
require.Equal(t, len(p.belongsTo()), 2, "expected different segment count")
}
func TestBadFile(t *testing.T) {
f, err := ioutil.TempFile("", "bad.file")
require.Nil(t, err, "could not create temp file")
require.Nil(t, os.Remove(f.Name()), "could not remove temp file")
_, err = Calculate(f.Name(), false)
require.Error(t, err, "expected error")
}
func TestBadLines(t *testing.T) {
f, err := ioutil.TempFile("", "good.file")
require.Nil(t, err, "could not create temp file")
defer os.Remove(f.Name())
type testInput struct {
line []byte
assumeHeaders bool
}
testInputs := []testInput{
testInput{},
testInput{line: []byte("foo,100"), assumeHeaders: false},
testInput{line: []byte("foo,bar,200"), assumeHeaders: false},
testInput{line: []byte("foo,100,bar"), assumeHeaders: false},
testInput{line: []byte("foo,100,bar"), assumeHeaders: true},
}
for _, testInput := range testInputs { // https://stackoverflow.com/a/39806983/2363529
_, err = f.WriteAt(testInput.line, 0)
require.Nil(t, err, "could not write to input file")
_, err = Calculate(f.Name(), testInput.assumeHeaders)
require.Error(t, err, "expected error")
}
}
func TestCalculate(t *testing.T) {
f, err := ioutil.TempFile("", "good.file")
require.Nil(t, err, "could not create temp file")
defer os.Remove(f.Name())
// https://stackoverflow.com/a/39806983/2363529
lines := []byte("id,start,end" + "\n" +
"foo,50.0,150.0" + "\n" +
"bar,100,200.0" + "\n" +
"baz,199,201")
_, err = f.Write(lines)
require.Nil(t, err, "could not write to input file")
res, err := Calculate(f.Name(), true)
require.Nil(t, err, "expected no error")
require.Equal(t, len(res), 3, "expected 2 overlaps") // increment by 1 to account for the header
/* require.Equal(t, res[1], []string{
strconv.FormatFloat(50, 'f', -1, 64), // overlapLength
strconv.FormatFloat(100, 'f', -1, 64), // overlapStart
strconv.FormatFloat(150, 'f', -1, 64), // overlapEnd
strconv.Itoa(2), // segmentCount
strings.Join([]string{"bar", "foo"}, ","), // segmentList
})
require.Equal(t, res[2], []string{
strconv.FormatFloat(1, 'f', -1, 64), // overlapLength
strconv.FormatFloat(199, 'f', -1, 64), // overlapStart
strconv.FormatFloat(200, 'f', -1, 64), // overlapEnd
strconv.Itoa(2), // segmentCount
strings.Join([]string{"bar", "baz"}, ","), // segmentList
}) */
}