Debian Codegraph @

limio/limit_test.go

raw symbols SCIP
1 package limio
2
3 import (
4 "bytes"
5 "crypto/rand"
6 "io"
7 "testing"
8 )
9
10 func TestLimits(t *testing.T) {
11 LimitTestN(0, t)
12 LimitTestN(1, t)
13 LimitTestN(2, t)
14 LimitTestN(1024, t)
15
16 OverLimitTestN(0, t)
17 OverLimitTestN(1, t)
18 OverLimitTestN(2, t)
19 OverLimitTestN(1024, t)
20 }
21
22 func OverLimitTestN(N int64, t *testing.T) {
23 got := bytes.NewBuffer(nil)
24 exp := bytes.NewBuffer(nil)
25
26 lw := LimitWriter(got, N)
27 w := io.MultiWriter(exp, lw)
28
29 _, err := io.CopyN(w, rand.Reader, 2*N)
30 if err != io.ErrShortWrite && N != 2*N {
31 t.Error("did not throw io.ErrShortWrite!")
32 if err != nil {
33 t.Error(err.Error())
34 }
35 }
36
37 if !bytes.Equal(got.Bytes(), exp.Bytes()[:N]) {
38 t.Errorf("Exp: %s\n Got: %s\n",
39 string(exp.Bytes()[:N]),
40 string(got.Bytes()))
41 }
42
43 if int64(got.Len()) != N {
44 t.Errorf("LimitWriter did not cap at %d", N)
45 }
46 }
47
48 func LimitTestN(N int64, t *testing.T) {
49 got := bytes.NewBuffer(nil)
50 exp := bytes.NewBuffer(nil)
51
52 lw := LimitWriter(got, N)
53 w := io.MultiWriter(lw, exp)
54
55 io.CopyN(w, rand.Reader, N)
56
57 if !bytes.Equal(got.Bytes(), exp.Bytes()) {
58 t.Errorf("Exp: %s\n Got: %s\n",
59 string(exp.Bytes()[:N]),
60 string(got.Bytes()))
61 }
62 }