-
Notifications
You must be signed in to change notification settings - Fork 2
/
util_test.go
65 lines (61 loc) · 1.29 KB
/
util_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
package main
import (
"bytes"
"fmt"
"io"
"strings"
"sync"
"testing"
)
func TestContestStdout(t *testing.T) {
var buf bytes.Buffer
cs := &ContestStdout{Output: &buf}
const N = 10
var wg sync.WaitGroup
token := make(chan bool, 1)
token <- true
expectedOutput := make(chan string, 1)
for i := 0; i < N; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
s := fmt.Sprintf("%d\nfoobarbazblah", i)
w := cs.NewWriter(i)
_, err := io.Copy(w, strings.NewReader(s))
if err != nil {
return
}
select {
case <-token:
default:
t.Errorf("instance %d was also able to write", i)
return
}
expectedOutput <- s
}(i)
}
want := <-expectedOutput
got := buf.String()
if got != want {
t.Errorf("wrong output of ContestStdout: got=%q, want=%q", got, want)
}
}
func TestTagStream(t *testing.T) {
for _, tc := range []struct {
input string
output string
}{
{"foo\n", "PREFIXfoo\n"},
{"foo", "PREFIXfoo\n"},
{"foo\n\nbar", "PREFIXfoo\nPREFIX\nPREFIXbar\n"},
} {
var buf bytes.Buffer
if err := TagStream("PREFIX", &buf, strings.NewReader(tc.input)); err != nil {
t.Errorf("TagStream failed: %v", err)
continue
}
if got := buf.String(); got != tc.output {
t.Errorf("TagStream returned invalid output: got=%q, want=%q", got, tc.output)
}
}
}