<commit_before>// Copyright 2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.

package test

import (
	"bytes"
	"errors"
	"fmt"
	"path"
	"regexp"
	"strings"
	"testing"
	"time"

	"github.com/keybase/client/go/libkb"
	"github.com/keybase/client/go/protocol"
	"github.com/keybase/kbfs/libkbfs"
)

type m map[string]string

const (
	alice = username("alice")
	bob   = username("bob")
	eve   = username("eve")
)

type opt struct {
	writerNames     []libkb.NormalizedUsername
	readerNames     []libkb.NormalizedUsername
	users           map[libkb.NormalizedUsername]User
	t               *testing.T
	initDone        bool
	engine          Engine
	writers         []keybase1.UID
	readers         []keybase1.UID
	blockSize       int64
	blockChangeSize int64
	clock           *libkbfs.TestClock
}

func test(t *testing.T, actions ...optionOp) {
	o := &opt{}
	o.engine = createEngine()
	o.engine.Init()
	o.t = t
	defer o.close()
	for _, omod := range actions {
		omod(o)
	}
}

func (o *opt) close() {
	for _, user := range o.users {
		o.expectSuccess("Shutdown", o.engine.Shutdown(user))
	}
}

func (o *opt) runInitOnce() {
	if o.initDone {
		return
	}
	o.clock = &libkbfs.TestClock{}
	o.clock.Set(time.Unix(0, 0))
	o.users = o.engine.InitTest(o.t, o.blockSize, o.blockChangeSize,
		append(o.writerNames, o.readerNames...), o.clock)

	for _, uname := range o.writerNames {
		uid := o.engine.GetUID(o.users[uname])
		o.writers = append(o.writers, uid)
	}
	for _, uname := range o.readerNames {
		uid := o.engine.GetUID(o.users[uname])
		o.readers = append(o.readers, uid)
	}

	o.initDone = true
}

func ntimesString(n int, s string) string {
	var bs bytes.Buffer
	for i := 0; i < n; i++ {
		bs.WriteString(s)
	}
	return bs.String()
}

func setBlockSizes(t *testing.T, config libkbfs.Config, blockSize, blockChangeSize int64) {
	// Set the block sizes, if any
	if blockSize > 0 || blockChangeSize > 0 {
		if blockSize == 0 {
			blockSize = 512 * 1024
		}
		if blockChangeSize < 0 {
			t.Fatal("Can't handle negative blockChangeSize")
		}
		if blockChangeSize == 0 {
			blockChangeSize = 8 * 1024
		}
		bsplit, err := libkbfs.NewBlockSplitterSimple(blockSize,
			uint64(blockChangeSize), config.Codec())
		if err != nil {
			t.Fatalf("Couldn't make block splitter for block size %d,"+
				" blockChangeSize %d: %v", blockSize, blockChangeSize, err)<commit_msg>Rem unused<commit_after>// Copyright 2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.

package test

import (
	"bytes"
	"errors"
	"fmt"
	"path"
	"regexp"
	"strings"
	"testing"
	"time"

	"github.com/keybase/client/go/libkb"
	"github.com/keybase/kbfs/libkbfs"
)

type m map[string]string

const (
	alice = username("alice")
	bob   = username("bob")
	eve   = username("eve")
)

type opt struct {
	writerNames     []libkb.NormalizedUsername
	readerNames     []libkb.NormalizedUsername
	users           map[libkb.NormalizedUsername]User
	t               *testing.T
	initDone        bool
	engine          Engine
	blockSize       int64
	blockChangeSize int64
	clock           *libkbfs.TestClock
}

func test(t *testing.T, actions ...optionOp) {
	o := &opt{}
	o.engine = createEngine()
	o.engine.Init()
	o.t = t
	defer o.close()
	for _, omod := range actions {
		omod(o)
	}
}

func (o *opt) close() {
	for _, user := range o.users {
		o.expectSuccess("Shutdown", o.engine.Shutdown(user))
	}
}

func (o *opt) runInitOnce() {
	if o.initDone {
		return
	}
	o.clock = &libkbfs.TestClock{}
	o.clock.Set(time.Unix(0, 0))
	o.users = o.engine.InitTest(o.t, o.blockSize, o.blockChangeSize,
		append(o.writerNames, o.readerNames...), o.clock)
	o.initDone = true
}

func ntimesString(n int, s string) string {
	var bs bytes.Buffer
	for i := 0; i < n; i++ {
		bs.WriteString(s)
	}
	return bs.String()
}

func setBlockSizes(t *testing.T, config libkbfs.Config, blockSize, blockChangeSize int64) {
	// Set the block sizes, if any
	if blockSize > 0 || blockChangeSize > 0 {
		if blockSize == 0 {
			blockSize = 512 * 1024
		}
		if blockChangeSize < 0 {
			t.Fatal("Can't handle negative blockChangeSize")
		}
		if blockChangeSize == 0 {
			blockChangeSize = 8 * 1024
		}
		bsplit, err := libkbfs.NewBlockSplitterSimple(blockSize,
			uint64(blockChangeSize), config.Codec())
		if err != nil {
			t.Fatalf("Couldn't make block splitter for block size %d,"+
				" blockChangeSize %d: %v", blockSize, blockChangeSize, err)
{'fraction_non_alphanumeric': 0.13396811337466785, 'fraction_numerical': 0.014614703277236492, 'mean_word_length': 3.2096924510717613, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6767397', 'n_tokens_mistral': 1794, 'n_tokens_neox': 1665, 'n_words': 419}