-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtestsuite_test.go
145 lines (124 loc) · 2.76 KB
/
testsuite_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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
// Copyright (c) 2019-2021 Markku Rossi
//
// All rights reserved.
//
package compiler
import (
"fmt"
"math/big"
"os"
"path"
"regexp"
"runtime/pprof"
"strings"
"testing"
"github.com/markkurossi/mpc/compiler/utils"
)
const (
testsuite = "tests"
)
var (
reWhitespace = regexp.MustCompilePOSIX(`[[:space:]]+`)
)
func TestSuite(t *testing.T) {
dir, err := os.Open(testsuite)
if err != nil {
t.Fatalf("Failed to open tests directory: %s", err)
}
defer dir.Close()
files, err := dir.Readdirnames(-1)
if err != nil {
t.Fatalf("Failed to list tests directory: %s", err)
}
compiler := New(utils.NewParams())
loop:
for _, file := range files {
if !strings.HasSuffix(file, ".mpcl") {
continue
}
name := path.Join(testsuite, file)
circ, annotations, err := compiler.CompileFile(name)
if err != nil {
t.Errorf("failed to compile '%s': %s", file, err)
continue
}
var cpuprof bool
base := 10
for _, annotation := range annotations {
ann := strings.TrimSpace(annotation)
if strings.HasPrefix(ann, "@heavy") && testing.Short() {
fmt.Printf("Skipping heavy test %s\n", file)
continue loop
}
if strings.HasPrefix(ann, "@pprof") {
cpuprof = true
continue
}
if strings.HasPrefix(ann, "@Hex") {
base = 16
continue
}
if !strings.HasPrefix(ann, "@Test ") {
continue
}
parts := reWhitespace.Split(ann, -1)
var inputs []*big.Int
var outputs []*big.Int
var sep bool
for i := 1; i < len(parts); i++ {
part := parts[i]
if part == "=" {
sep = true
continue
}
v := new(big.Int)
_, ok := v.SetString(parts[i], 0)
if !ok {
t.Errorf("%s: invalid argument '%s'", file, parts[i])
continue loop
}
if sep {
outputs = append(outputs, v)
} else {
inputs = append(inputs, v)
}
}
// Wrap inputs to args.
var prof *os.File
if cpuprof {
prof, err = os.Create(fmt.Sprintf("%s.cpu.prof", file))
if err != nil {
t.Errorf("%s: failed to create cpu.prof: %s", file, err)
continue loop
}
err = pprof.StartCPUProfile(prof)
if err != nil {
t.Errorf("%s: failed to start CPU profile: %s", file, err)
continue loop
}
}
results, err := circ.Compute(inputs)
if cpuprof {
pprof.StopCPUProfile()
prof.Close()
}
if err != nil {
t.Errorf("%s: compute failed: %s", file, err)
continue loop
}
if len(results) != len(outputs) {
t.Errorf("%s: unexpected return values: got %v, expected %v",
file, results, outputs)
continue loop
}
for idx, result := range results {
if result.Cmp(outputs[idx]) != 0 {
t.Errorf("%s: result %d mismatch: got %v, expected %v",
file, idx, result.Text(base), outputs[idx].Text(base))
}
}
_ = results
}
}
}