util

package
v0.0.0-...-8a317fa Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Digits = "0123456789"
	Uppers = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	Lowers = "abcdefghijklmnopqrstuvwxyz"
)

Variables

View Source
var (
	// true: test only one case in AssertEqualRunResultsInf / CheckRunResultsInf
	Once bool

	// true: only print test case number when assertion failed
	DisableLogInput bool

	// when DebugTLE > 0, a running case would cause a fatal error when timeout
	DebugTLE = 2 * time.Second
)

Functions

func AssertEqual

func AssertEqual(t *testing.T, rawText string, runFunc ioFunc)

func AssertEqualCase

func AssertEqualCase(t *testing.T, rawText string, targetCaseNum int, runFunc ioFunc)

func AssertEqualFileCase

func AssertEqualFileCase(t *testing.T, dir string, targetCaseNum int, runFunc ioFunc)

func AssertEqualFileCaseWithName

func AssertEqualFileCaseWithName(t *testing.T, dir, inName, ansName string, targetCaseNum int, runFunc ioFunc)

func AssertEqualResultsInf

func AssertEqualResultsInf(t *testing.T, inputGenerator func() string, runFuncAC, runFunc ioFunc)

无尽对拍测试 inputGenerator 生成随机数据,runFuncAC 为暴力逻辑或已 AC 逻辑,runFunc 为当前测试的逻辑

func AssertEqualRunResult

func AssertEqualRunResult(t *testing.T, inputs []string, targetCaseNum int, runFuncAC, runFunc ioFunc)

对拍 runFuncAC 为暴力逻辑或已 AC 逻辑,runFunc 为当前测试的逻辑

func AssertEqualStringCase

func AssertEqualStringCase(t *testing.T, testCases [][2]string, targetCaseNum int, runFunc ioFunc)

func AssertEqualStringCaseWithPrefix

func AssertEqualStringCaseWithPrefix(t *testing.T, testCases [][2]string, targetCaseNum int, runFunc ioFunc, prefix string)

func CheckRunResultsInf

func CheckRunResultsInf(t *testing.T, inputGenerator func() (string, OutputChecker), runFunc ioFunc)

func CheckRunResultsInfWithTarget

func CheckRunResultsInfWithTarget(t *testing.T, inputGenerator func() (string, OutputChecker), targetCaseNum int, runFunc ioFunc)

无尽验证测试 inputGenerator 除了返回随机输入数据外,还需要返回一个闭包,这个闭包接受 runFunc 的输出结果,根据输出数据验证输出结果是否正确

func IsDebugging

func IsDebugging() bool

IsDebugging 检查当前进程是否正在被 Delve 调试器调试。 该函数通过追溯进程的父进程,判断是否为 Delve 调试器。

func TransEdge

func TransEdge(edges [][2]int) [][]int

Types

type OutputChecker

type OutputChecker func(string) bool

type RG

type RG struct {
	// contains filtered or unexported fields
}

func NewRandGenerator

func NewRandGenerator() *RG

func NewRandGeneratorWithSeed

func NewRandGeneratorWithSeed(seed int64) *RG

func (*RG) BinaryTree

func (r *RG) BinaryTree(n, st int) (children [][2]int)

BinaryTree returns a random binary tree with a fixed size and its values in [st, st+n]

func (*RG) Byte

func (r *RG) Byte(b byte)

func (*RG) Bytes

func (r *RG) Bytes(s string)

func (*RG) Clear

func (r *RG) Clear()

func (*RG) Float

func (r *RG) Float(min, max float64, precision int) float64

Float returns a random float in [min, max] with a fixed precision

func (*RG) FloatSlice

func (r *RG) FloatSlice(size int, min, max float64, precision int) []float64

FloatSlice returns a random slice of floats in [min, max] with a fixed precision

func (*RG) GraphEdges

func (r *RG) GraphEdges(n, m, st int, directed bool) (edges [][2]int)

GraphEdges returns a random graph with n nodes, m edges, st-index, without self-loops and multiple edges; TIPS: pass directed=false to generate a DAG

func (*RG) GraphHackSPFA

func (r *RG) GraphHackSPFA(n, row, st, minWeight, maxWeight int) (edges [][3]int)

Graph Hack SPFA GraphHackSPFA generates a undirected grid graph with n nodes, st-index, without self-loops and multiple edges, edge weights in range [minWeight, maxWeight]

For example, a 10 nodes 2 row grid graph looks like this: 1-2-3-4-5 | | | | | 6-7-8-9-10

And an 11 nodes 2 row grid graph looks like this: 1-2-3-4-5 | | | | | 6-7-8-9-10-11

All weights of vertical edges are 1, the others are random.

In practice, set row to 6 will make SPFA to run in worst case (about n^2/10 relaxations). (Random weights are in range [1,1e5])

  n    avg relax (100 runs)  Dijkstra's algorithm (for comparing)
5e3    2'418'393                            5'493
1e4    9'674'877                           18'323
2e4   38'586'596                           36'658
3e4   87'033'045                           54'992
1e5  966'319'883 (10 runs)                183'320

Reference: https://blog.csdn.net/qq_45721135/article/details/102472101 https://www.zhihu.com/question/292283275 https://www.zhihu.com/question/268382638

func (*RG) GraphMatrix

func (r *RG) GraphMatrix(n int, directed bool) (g [][]byte)

func (*RG) GraphWeightedEdges

func (r *RG) GraphWeightedEdges(n, m, st, minWeight, maxWeight int, directed bool) (edges [][3]int)

GraphWeightedEdges returns a random graph with n nodes, m edges, st-index, edge weights in range [minWeight, maxWeight]; TIPS: pass directed=false to generate a DAG

func (*RG) Int

func (r *RG) Int(min, max int) int

Int returns a random integer in [min, max]

func (*RG) IntMatrix

func (r *RG) IntMatrix(row, col int, min, max int) [][]int

IntMatrix returns a random matrix of integers in [min, max]

func (*RG) IntMatrixInSet

func (r *RG) IntMatrixInSet(row, col int, set []int) [][]int

IntMatrixInSet returns a random matrix of integers in set

func (*RG) IntOnly

func (r *RG) IntOnly(min, max int) int

func (*RG) IntSlice

func (r *RG) IntSlice(size int, min, max int) []int

IntSlice returns a random slice of integers in [min, max]

func (*RG) IntSliceDiffNeighbor

func (r *RG) IntSliceDiffNeighbor(size int, min, max int) []int

IntSliceDiffNeighbor returns a random slice of integers in [min, max] with no same neighbor; It's guaranteed that a[i] != a[i+1] for all in [0, size-2]

func (*RG) IntSliceInSet

func (r *RG) IntSliceInSet(size int, set []int) []int

IntSliceInSet returns a random slice of integers in set

func (*RG) IntSliceOrdered

func (r *RG) IntSliceOrdered(size int, min, max int, unique, inc bool) []int

IntSliceOrdered returns a random slice of integers in [min, max] ordered by increasing/decreasing

func (*RG) IntSliceUnique

func (r *RG) IntSliceUnique(size int, min, max int) []int

func (*RG) NewLine

func (r *RG) NewLine()

func (*RG) One

func (r *RG) One()

func (*RG) Permutation

func (r *RG) Permutation(min, max int) []int

Permutation returns a random permutation with a fixed size and its values in [min, max]

func (*RG) Space

func (r *RG) Space()

func (*RG) Str

func (r *RG) Str(minLen, maxLen int, min, max byte) string

Str returns a random string with a length in [minLen, maxLen] and its chars in [min, max]

func (*RG) StrInSet

func (r *RG) StrInSet(minLen, maxLen int, chars string) string

StrInSet returns a random string with a length in [minLen, maxLen] and its chars in chars

func (*RG) String

func (r *RG) String() string

func (*RG) TreeEdges

func (r *RG) TreeEdges(n, st int) (edges [][2]int)

TreeEdges returns a random tree with n nodes, st-index and v<w for each edge v-w; TODO: 支持设置最大深度限制

func (*RG) TreeWeightedEdges

func (r *RG) TreeWeightedEdges(n, st, minWeight, maxWeight int) (edges [][3]int)

TreeWeightedEdges returns a random tree with n nodes, st-index, edge weights in range [minWeight, maxWeight]

func (*RG) UniquePoints

func (r *RG) UniquePoints(n, minX, maxX, minY, maxY int) (points [][2]int)

UniquePoints returns a random set of unique points with a fixed size and its values in [minX, maxX] and [minY, maxY]

func (*RG) UniqueSlice

func (r *RG) UniqueSlice(size int, min, max int) []int

UniqueSlice returns a random slice of unique integers in [min, max]

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL