forked from balacode/go-delta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
func_test.go
101 lines (88 loc) · 2.62 KB
/
func_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// -----------------------------------------------------------------------------
// github.com/balacode/go-delta go-delta/[func_test.go]
// (c) [email protected] License: MIT
// -----------------------------------------------------------------------------
package delta
// to generate a test coverage report for the whole module use:
// go test -coverprofile cover.out
// go tool cover -html=cover.out
import (
"bytes"
"fmt"
"os"
"runtime"
"strings"
"testing"
)
const (
AtoM = "ABCDEFGHIJKLM"
AtoS = "ABCDEFGHIJKLMNOPQRS"
AtoZ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Nums = "0123456789"
atoz = "abcdefghijklmnopqrstuvwxyz"
)
const PrintTestNames = true
var Line = strings.Repeat("#", 70)
// -----------------------------------------------------------------------------
// # Function Unit Tests
// go test --run Test_readHash_
func Test_readHash_(t *testing.T) {
if PrintTestNames {
printTestName()
}
var test = func(input []byte) {
var resultHash []byte
{
buf := bytes.NewBuffer(input)
resultHash = readHash(buf)
}
var expectHash []byte
{
buf := bytes.NewBuffer(input)
expectHash = makeHash(buf.Bytes())
}
if !bytes.Equal(resultHash, expectHash) {
t.Errorf("\n input:\n\t%v\n%s\n expect:%v\n\t result:\n\t%v\n",
input, string(input), expectHash, resultHash)
}
}
TempBufferSize = 100
test(nil)
test([]byte("abc"))
test([]byte(strings.Repeat("abc", 1024)))
} // Test_readHash_
// -----------------------------------------------------------------------------
// # Test Helper Functions
// ab converts s to a byte array.
func ab(s string) []byte {
return []byte(s)
} // ab
// printTestName prints the name of the calling unit test.
func printTestName() {
if !PrintTestNames {
return
}
funcName := func() string {
var (
programCounter, _, _, _ = runtime.Caller(2)
ret = runtime.FuncForPC(programCounter).Name()
i = strings.LastIndex(ret, ".")
)
if i > -1 {
ret = ret[i+1:]
}
ret += "()"
return ret
}
fmt.Println("Running test:", funcName())
} // printTestName
// readData reads 'filename' and returns its contents as an array of bytes.
func readData(filename string) []byte {
ret, err := os.ReadFile(filename)
if err != nil {
PL("File reading error:", err)
return nil
}
return ret
} // readData
// end