go-countlines/countlines_test.go

60 lines
1.8 KiB
Go

package countlines
import (
"testing"
"testing/quick"
)
type testVector struct {
n uint64
b []byte
}
func repTestVector(expected uint64, s []byte, repeatTimes int) testVector {
b := make([]byte, repeatTimes*len(s))
for i := 0; i < repeatTimes; i++ {
for j, v := range s {
b[i*len(s)+j] = v
}
}
return testVector{expected * uint64(repeatTimes), b}
}
var testVectors = []testVector{
repTestVector(1, []byte{0xa, 0x5e, 0x74, 0x15, 0x4e, 0xf3, 0xeb, 0xa6, 0x66, 0x83, 0x78, 0xfc, 0xfe, 0xd, 0x3e, 0xbd, 0xa8, 0x57, 0x93, 0x9e, 0x2b, 0x3d, 0xed, 0x99, 0xc9, 0xf9, 0x81, 0x10, 0x7f, 0xb0, 0xb0,
0xad, 0x1e, 0x2a, 0x84, 0xd0}, 1),
repTestVector(1, []byte{0x01, 0x02, 0x03, 0x0A}, 128),
repTestVector(0, []byte{0xf5, 0xc, 0x36, 0x9e, 0x86, 0xca, 0xf9}, 1),
repTestVector(0, []byte{0xf1, 0x35, 0xe5, 0xa3, 0x3c, 0x9f, 0x2c, 0x93, 0xbd, 0x72, 0xcf, 0x95, 0x16, 0x34, 0x37, 0xc5, 0xfd, 0xe4, 0x5d, 0x75, 0xb8, 0x2f, 0x5f, 0x53, 0x19, 0x2d, 0x6, 0xc3, 0xdb, 0x6d, 0xd4,
0xb5, 0xc0, 0x24, 0x95, 0x8e, 0x8d, 0x76, 0x20, 0xc5, 0x2b, 0x92, 0xc0, 0xa1, 0x3d, 0xee}, 1),
}
func testCountNewlines(t *testing.T, count func(s []byte) uint64) {
for _, tc := range testVectors {
if n := count(tc.b); n != tc.n {
t.Errorf("Expected %d, got %d", tc.n, n)
}
}
}
func TestCountNewlines(t *testing.T) {
testCountNewlines(t, CountNewlines)
}
func TestCountNewlinesGo(t *testing.T) {
testCountNewlines(t, countNewlinesGo)
}
func TestCountNewlinesCompare(t *testing.T) {
for _, tc := range testVectors {
if a, b := CountNewlines(tc.b), countNewlinesGo(tc.b); a != b {
t.Errorf("CountNewlines(%[1]v) = %[2]d; countNewlinesGo(%[1]v) = %[3]d", tc.b, a, b)
}
}
if err := quick.CheckEqual(countNewlinesGo, CountNewlines, &quick.Config{
MaxCountScale: 1000,
}); err != nil {
t.Error(err)
}
}