-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
103 lines (88 loc) · 2.58 KB
/
main.go
File metadata and controls
103 lines (88 loc) · 2.58 KB
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
102
103
// Package assert provides functions for making assertions in tests.
package assert
import (
"math"
"reflect"
"runtime"
"testing"
)
// Close asserts that the distance between two scalars or the uniform distance
// between two vectors is less than the given value.
func Close(actual, expected interface{}, ε interface{}, t *testing.T) {
typo := reflect.TypeOf(actual)
if typo != reflect.TypeOf(expected) {
raise(t, "got %v instead of %v", actual, expected)
}
kind := typo.Kind()
if kind != reflect.TypeOf(expected).Kind() {
raise(t, "got %T instead of %T", actual, expected)
}
avalue, evalue := reflect.ValueOf(actual), reflect.ValueOf(expected)
if kind == reflect.Slice {
kind = typo.Elem().Kind()
} else {
avalue = reflect.Append(reflect.MakeSlice(reflect.SliceOf(typo), 0, 1), avalue)
evalue = reflect.Append(reflect.MakeSlice(reflect.SliceOf(typo), 0, 1), evalue)
}
if reflect.TypeOf(ε).Kind() != kind {
raise(t, "got %T instead of %T", actual, ε)
}
if avalue.Len() != evalue.Len() {
raise(t, "got %v instead of %v", actual, expected)
}
actual, expected = avalue.Interface(), evalue.Interface()
switch kind {
case reflect.Float64:
actual, expected, ε := actual.([]float64), expected.([]float64), ε.(float64)
max := 0.0
for i := range actual {
max = math.Max(max, math.Abs(actual[i]-expected[i]))
}
if max > ε {
raise(t, "got distance %v instead of %v", max, ε)
}
default:
panic("the type is not supported")
}
}
// Equal asserts that two objects are equal.
func Equal(actual, expected interface{}, t *testing.T) {
atype, etype := reflect.TypeOf(actual), reflect.TypeOf(expected)
if atype == nil && etype == nil {
return
} else if (atype == nil) != (etype == nil) {
raise(t, "got %T instead of %T", actual, expected)
}
kind := atype.Kind()
if kind != etype.Kind() {
raise(t, "got %T instead of %T", actual, expected)
}
switch kind {
case reflect.Slice, reflect.Struct, reflect.Ptr:
if !reflect.DeepEqual(actual, expected) {
raise(t, "got %v instead of %v", actual, expected)
}
default:
if actual != expected {
raise(t, "got %v instead of %v", actual, expected)
}
}
}
// Success asserts that the error is not nil.
func Failure(err error, t *testing.T) {
if err == nil {
raise(t, "expected an error")
}
}
// Success asserts that the error is nil.
func Success(err error, t *testing.T) {
if err != nil {
raise(t, "got an error '%v'", err)
}
}
func raise(t *testing.T, format string, arguments ...interface{}) {
if _, file, line, ok := runtime.Caller(2); ok {
t.Errorf("%s:%d", file, line)
}
t.Fatalf(format, arguments...)
}