OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The LUCI Authors. All rights reserved. | |
2 // Use of this source code is governed under the Apache License, Version 2.0 | |
3 // that can be found in the LICENSE file. | |
4 | |
5 package main | |
6 | |
7 import ( | |
8 "io" | |
9 "math/rand" | |
10 ) | |
11 | |
12 // RandomBinaryGenerator is an io.Reader which produces truly random binary | |
13 // data (totally uncompressible). | |
14 func RandomBinaryGenerator(r *rand.Rand) io.Reader { | |
15 // rand.Rand already produces random binary data via Read() | |
16 return r | |
17 } | |
18 | |
19 // io.Reader which produces a random text. | |
20 type textRandomGenerator struct { | |
21 r *rand.Rand | |
22 } | |
23 | |
24 func (g *textRandomGenerator) Read(p []byte) (n int, err error) { | |
25 i := 0 | |
26 for { | |
27 bytes := []byte(string(randChar(g.r, textChars))) | |
28 if i+len(bytes) > len(p) { | |
29 break | |
30 } | |
31 | |
32 for j := range bytes { | |
33 p[i+j] = bytes[j] | |
34 } | |
35 i += len(bytes) | |
36 } | |
37 return i, nil | |
38 } | |
39 | |
40 // RandomTextGenerator is an io.Reader which produces truly random text data | |
41 // (mostly uncompressible). | |
42 func RandomTextGenerator(r *rand.Rand) io.Reader { | |
43 reader := textRandomGenerator{r: r} | |
mcgreevy_g
2017/06/27 03:29:17
return &textRandomGenerator{r: r}
| |
44 return &reader | |
45 } | |
46 | |
47 // Repeated sequence size range | |
48 const ( | |
49 SequenceMinSize uint64 = 16 | |
50 SequenceMaxSize uint64 = 4 * 1024 | |
51 ) | |
52 | |
53 // io.Reader which produces the given byte array repetitively. | |
54 type repeatedByteGenerator struct { | |
55 data []byte | |
56 index int | |
57 } | |
58 | |
59 func (g *repeatedByteGenerator) Read(p []byte) (n int, err error) { | |
60 for i := range p { | |
61 p[i] = g.data[g.index] | |
62 g.index = (g.index + 1) % len(g.data) | |
63 } | |
64 return len(p), nil | |
65 } | |
66 | |
67 // RepeatedBinaryGenerator is an io.Reader which produces repeated binary data | |
68 // (some what compressible). | |
69 func RepeatedBinaryGenerator(r *rand.Rand) io.Reader { | |
70 // Figure out how big the repeated sequence will be | |
71 sequenceSize := randBetween(r, SequenceMinSize, SequenceMaxSize) | |
72 | |
73 repeater := repeatedByteGenerator{index: 0, data: make([]byte, sequenceS ize)} | |
74 r.Read(repeater.data) | |
75 | |
76 return &repeater | |
77 } | |
78 | |
79 // RepeatedTextGenerator is an io.Reader which produces repeated text data | |
80 // (very compressible). | |
81 func RepeatedTextGenerator(r *rand.Rand) io.Reader { | |
82 // Figure out how big the repeated sequence will be | |
83 sequenceSize := randBetween(r, SequenceMinSize, SequenceMaxSize) | |
84 | |
85 repeater := repeatedByteGenerator{index: 0, data: []byte(randStr(r, sequ enceSize, textChars))} | |
86 | |
87 return &repeater | |
88 } | |
89 | |
90 // LoremTextGenerator is an io.Reader which produces repeated Lorem Ipsum text | |
91 // data (very compressible). | |
92 func LoremTextGenerator() io.Reader { | |
93 repeater := repeatedByteGenerator{index: 0, data: []byte(lorem)} | |
94 return &repeater | |
95 } | |
OLD | NEW |