Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(784)

Unified Diff: common/dirwalk/tests/tools/gendir/content.go

Issue 2054763004: luci-go/common/dirwalk: Code for walking a directory tree efficiently Base URL: https://github.com/luci/luci-go@master
Patch Set: Fixes. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: common/dirwalk/tests/tools/gendir/content.go
diff --git a/common/dirwalk/tests/tools/gendir/content.go b/common/dirwalk/tests/tools/gendir/content.go
new file mode 100644
index 0000000000000000000000000000000000000000..f4248c78d3f4dfac9ab6fb778d038879d3351798
--- /dev/null
+++ b/common/dirwalk/tests/tools/gendir/content.go
@@ -0,0 +1,90 @@
+// Copyright 2016 The LUCI Authors. All rights reserved.
+// Use of this source code is governed under the Apache License, Version 2.0
+// that can be found in the LICENSE file.
+
+package main
+
+import (
+ "io"
+ "math/rand"
+)
+
+// io.Reader which produces truly random binary data (totally uncompressible).
+func RandomBinaryGenerator(r *rand.Rand) io.Reader {
+ // rand.Rand already produces random binary data via Read()
+ return r
+}
+
+// io.Reader which produces a random text.
+type textRandomGenerator struct {
+ r *rand.Rand
+}
+
+func (g *textRandomGenerator) Read(p []byte) (n int, err error) {
+ i := 0
+ for {
+ bytes := []byte(string(randChar(g.r, textChars)))
+ if i+len(bytes) > len(p) {
+ break
+ }
+
+ for j := range bytes {
+ p[i+j] = bytes[j]
+ }
+ i += len(bytes)
+ }
+ return i, nil
+}
+
+// io.Reader which produces truly random text data (mostly uncompressible).
+func RandomTextGenerator(r *rand.Rand) io.Reader {
+ reader := textRandomGenerator{r: r}
+ return &reader
+}
+
+// Repeated sequence size range
+const (
+ SEQUENCE_MINSIZE uint64 = 16
+ SEQUENCE_MAXSIZE uint64 = 4 * 1024
+)
+
+// io.Reader which produces the given byte array repetitively.
+type repeatedByteGenerator struct {
+ data []byte
+ index int
+}
+
+func (g *repeatedByteGenerator) Read(p []byte) (n int, err error) {
+ for i := range p {
+ p[i] = g.data[g.index]
+ g.index = (g.index + 1) % len(g.data)
+ }
+ return len(p), nil
+}
+
+// io.Reader which produces repeated binary data (some what compressible).
+func RepeatedBinaryGenerator(r *rand.Rand) io.Reader {
+ // Figure out how big the repeated sequence will be
+ sequence_size := randBetween(r, SEQUENCE_MINSIZE, SEQUENCE_MAXSIZE)
+
+ repeater := repeatedByteGenerator{index: 0, data: make([]byte, sequence_size)}
+ r.Read(repeater.data)
+
+ return &repeater
+}
+
+// io.Reader which produces repeated text data (very compressible).
+func RepeatedTextGenerator(r *rand.Rand) io.Reader {
+ // Figure out how big the repeated sequence will be
+ sequence_size := randBetween(r, SEQUENCE_MINSIZE, SEQUENCE_MAXSIZE)
+
+ repeater := repeatedByteGenerator{index: 0, data: []byte(randStr(r, sequence_size, textChars))}
+
+ return &repeater
+}
+
+// io.Reader which produces repeated Lorem Ipsum text data (very compressible).
+func LoremTextGenerator() io.Reader {
+ repeater := repeatedByteGenerator{index: 0, data: []byte(lorem)}
+ return &repeater
+}

Powered by Google App Engine
This is Rietveld 408576698