本文整理汇总了Golang中testing/quick.Check函数的典型用法代码示例。如果您正苦于以下问题:Golang Check函数的具体用法?Golang Check怎么用?Golang Check使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Check函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestFu
// Its a hack to use the testing/quick functions to load up some
// test data. And that is it.
//
// NOTE:
// TestFu sets up the test data and it is expected to be in
// 0<whatever>_test.go. So don't move it from 0_test.go!
func TestFu(t *testing.T) {
config := &quick.Config{}
config.MaxCount = 100
// collect random k/vs
testdata[_testdata_keys] = make([]string, 0)
testdata[_testdata_kv] = make(map[string][]byte)
setup_kv := func(k string, v []byte) bool {
dataset := testdata[_testdata_kv].(map[string][]byte)
dataset[k] = v
keyset := testdata[_testdata_keys].([]string)
testdata[_testdata_keys] = append(keyset, k)
return true // always
}
quick.Check(setup_kv, config)
// collect random counter
testdata[_testdata_counters] = make(map[string]int64)
setup_counters := func(k string, v int64) bool {
dataset := testdata[_testdata_counters].(map[string]int64)
dataset[k] = v
return true // always
}
quick.Check(setup_counters, config)
// collect random numbers
testdata[_testdata_nums] = make([]int64, 0)
setup_nums := func(v int64) bool {
dataset := testdata[_testdata_nums].([]int64)
dataset = append(dataset, v)
return true // always
}
quick.Check(setup_nums, config)
}
示例2: TestFormatAndParse
func TestFormatAndParse(t *testing.T) {
const fmt = "Mon MST " + RFC3339 // all fields
f := func(sec int64) bool {
t1 := SecondsToLocalTime(sec)
if t1.Year < 1000 || t1.Year > 9999 {
// not required to work
return true
}
t2, err := Parse(fmt, t1.Format(fmt))
if err != nil {
t.Errorf("error: %s", err)
return false
}
if !same(t1, t2) {
t.Errorf("different: %q %q", t1, t2)
return false
}
return true
}
f32 := func(sec int32) bool { return f(int64(sec)) }
cfg := &quick.Config{MaxCount: 10000}
// Try a reasonable date first, then the huge ones.
if err := quick.Check(f32, cfg); err != nil {
t.Fatal(err)
}
if err := quick.Check(f, cfg); err != nil {
t.Fatal(err)
}
}
示例3: TestFormatAndParse
func TestFormatAndParse(t *testing.T) {
const fmt = "Mon MST " + RFC3339 // all fields
f := func(sec int64) bool {
t1 := Unix(sec, 0)
if t1.Year() < 1000 || t1.Year() > 9999 {
// not required to work
return true
}
t2, err := Parse(fmt, t1.Format(fmt))
if err != nil {
t.Errorf("error: %s", err)
return false
}
if t1.Unix() != t2.Unix() || t1.Nanosecond() != t2.Nanosecond() {
t.Errorf("FormatAndParse %d: %q(%d) %q(%d)", sec, t1, t1.Unix(), t2, t2.Unix())
return false
}
return true
}
f32 := func(sec int32) bool { return f(int64(sec)) }
cfg := &quick.Config{MaxCount: 10000}
// Try a reasonable date first, then the huge ones.
if err := quick.Check(f32, cfg); err != nil {
t.Fatal(err)
}
if err := quick.Check(f, cfg); err != nil {
t.Fatal(err)
}
}
示例4: ReadEmptyTests
func ReadEmptyTests(p MetricPersistence, t test.Tester) {
hasLabelPair := func(x int) (success bool) {
name := model.LabelName(string(x))
value := model.LabelValue(string(x))
labelSet := model.LabelSet{
name: value,
}
fingerprints, err := p.GetFingerprintsForLabelSet(labelSet)
if err != nil {
t.Error(err)
return
}
success = len(fingerprints) == 0
if !success {
t.Errorf("unexpected fingerprint length %d, got %d", 0, len(fingerprints))
}
return
}
err := quick.Check(hasLabelPair, nil)
if err != nil {
t.Error(err)
return
}
hasLabelName := func(x int) (success bool) {
labelName := model.LabelName(string(x))
fingerprints, err := p.GetFingerprintsForLabelName(labelName)
if err != nil {
t.Error(err)
return
}
success = len(fingerprints) == 0
if !success {
t.Errorf("unexpected fingerprint length %d, got %d", 0, len(fingerprints))
}
return
}
err = quick.Check(hasLabelName, nil)
if err != nil {
t.Error(err)
return
}
}
示例5: TestSecondsToUTCAndBack
func TestSecondsToUTCAndBack(t *testing.T) {
f := func(sec int64) bool { return Unix(sec, 0).UTC().Unix() == sec }
f32 := func(sec int32) bool { return f(int64(sec)) }
cfg := &quick.Config{MaxCount: 10000}
// Try a reasonable date first, then the huge ones.
if err := quick.Check(f32, cfg); err != nil {
t.Fatal(err)
}
if err := quick.Check(f, cfg); err != nil {
t.Fatal(err)
}
}
示例6: TestNanosecondsToUTCAndBack
func TestNanosecondsToUTCAndBack(t *testing.T) {
f := func(nsec int64) bool { return NanosecondsToUTC(nsec).Nanoseconds() == nsec }
f32 := func(nsec int32) bool { return f(int64(nsec)) }
cfg := &quick.Config{MaxCount: 10000}
// Try a small date first, then the large ones. (The span is only a few hundred years
// for nanoseconds in an int64.)
if err := quick.Check(f32, cfg); err != nil {
t.Fatal(err)
}
if err := quick.Check(f, cfg); err != nil {
t.Fatal(err)
}
}
示例7: main
func main() {
intf := func(x int) bool {
fmt.Println(x)
return true
}
quick.Check(intf, nil)
personf := func(p *Person) bool {
fmt.Printf("%+v\n", p)
return true
}
quick.Check(personf, nil)
}
示例8: ReadEmptyTests
func ReadEmptyTests(p metric.Persistence, t test.Tester) {
hasLabelPair := func(x int) (success bool) {
fingerprints, err := p.GetFingerprintsForLabelMatchers(metric.LabelMatchers{{
Type: metric.Equal,
Name: clientmodel.LabelName(string(x)),
Value: clientmodel.LabelValue(string(x)),
}})
if err != nil {
t.Error(err)
return
}
success = len(fingerprints) == 0
if !success {
t.Errorf("unexpected fingerprint length %d, got %d", 0, len(fingerprints))
}
return
}
err := quick.Check(hasLabelPair, nil)
if err != nil {
t.Error(err)
return
}
hasLabelName := func(x int) (success bool) {
labelName := clientmodel.LabelName(string(x))
values, err := p.GetLabelValuesForLabelName(labelName)
if err != nil {
t.Error(err)
return
}
success = len(values) == 0
if !success {
t.Errorf("unexpected values length %d, got %d", 0, len(values))
}
return
}
err = quick.Check(hasLabelName, nil)
if err != nil {
t.Error(err)
return
}
}
示例9: TestQuickGranularStorage
func TestQuickGranularStorage(t *testing.T) {
gs := newGranularStorage(8, adapters.NewMemoryStorage(make([]byte, 0, 4096)), adapters.NewMemoryStorage(make([]byte, 0, 4096)))
//add
if err := quick.Check(func(b []byte) bool {
i, err := gs.add(b)
if err != nil || i != gs.len()-1 {
return false
}
if i > 2 {
err = gs.set(i-1, []byte{})
if err != nil {
return false
}
}
if i > 5 {
err = gs.del(i - 3)
if err != nil {
return false
}
}
return true
}, nil); err != nil {
t.Error(err)
}
}
示例10: TestDomainQuick
func TestDomainQuick(t *testing.T) {
r := rand.New(rand.NewSource(0))
f := func(l int) bool {
db := GenerateDomain(r, l)
ds, _, err := UnpackDomainName(db, 0)
if err != nil {
panic(err)
}
buf := make([]byte, 255)
off, err := PackDomainName(ds, buf, 0, nil, false)
if err != nil {
t.Logf("error packing domain: %s", err.Error())
t.Logf(" bytes: %v\n", db)
t.Logf("string: %v\n", ds)
return false
}
if !bytes.Equal(db, buf[:off]) {
t.Logf("repacked domain doesn't match original:")
t.Logf("src bytes: %v", db)
t.Logf(" string: %v", ds)
t.Logf("out bytes: %v", buf[:off])
return false
}
return true
}
if err := quick.Check(f, nil); err != nil {
t.Error(err)
}
}
示例11: TestFuzz
func TestFuzz(t *testing.T) {
rnd := rand.New(rand.NewSource(42))
check := func() bool {
messages := rndMessages(rnd)
var buf bytes.Buffer
var written int
for i, msg := range messages {
n, err := WriteDelimited(&buf, msg)
if err != nil {
t.Fatalf("WriteDelimited(buf, %v[%d]) = ?, %v; wanted ?, nil", messages, i, err)
}
written += n
}
var read int
for i, msg := range messages {
out := Clone(msg)
out.Reset()
n, _ := ReadDelimited(&buf, out)
read += n
if !Equal(out, msg) {
t.Fatalf("out = %v; want %v[%d] = %#v", out, messages, i, msg)
}
}
if read != written {
t.Fatalf("%v read = %d; want %d", messages, read, written)
}
return true
}
if err := quick.Check(check, nil); err != nil {
t.Fatal(err)
}
}
示例12: Test_SessionCookieStore
func Test_SessionCookieStore(t *testing.T) {
if err := quick.Check(func(k, v string) bool {
expected := make(kocha.Session)
expected[k] = v
store := kocha.NewTestSessionCookieStore()
r, err := store.Save(expected)
if err != nil {
t.Fatal(err)
}
actual, err := store.Load(r)
if err != nil {
t.Fatal(err)
}
return reflect.DeepEqual(actual, expected)
}, nil); err != nil {
t.Error(err)
}
func() {
store := kocha.NewTestSessionCookieStore()
key := "invalid"
_, err := store.Load(key)
actual := err
expect := fmt.Errorf("kocha: session cookie value too short")
if !reflect.DeepEqual(actual, expect) {
t.Errorf(`SessionCookieStore.Load(%#v) => _, %#v; want %#v`, key, actual, expect)
}
}()
}
示例13: TestCFString_Invalid
func TestCFString_Invalid(t *testing.T) {
// go ahead and generate random strings and see if we actually get objects back.
// This is testing the unicode replacement functionality.
// Just to be safe in case testing/quick ever fixes their string generation to
// only generate valid strings, lets generate []bytes instead and then convert that.
f := func(bytes []byte) bool {
s := string(bytes)
cfStr := convertStringToCFString(s)
defer cfRelease(cfTypeRef(cfStr))
return cfStr != nil
}
if err := quick.Check(f, nil); err != nil {
t.Error(err)
}
// Test some manually-crafted strings
g := func(input, expected string) {
cfStr := convertStringToCFString(input)
defer cfRelease(cfTypeRef(cfStr))
if cfStr == nil {
t.Errorf("failed on input %#v", input)
return
}
output := convertCFStringToString(cfStr)
if output != expected {
t.Errorf("failed on input: %#v. Output: %#v. Expected: %#v", input, output, expected)
}
}
g("hello world", "hello world")
g("hello\x00world", "hello\x00world")
g("hello\uFFFDworld", "hello\uFFFDworld")
g("hello\uFEFFworld\x00", "hello\uFEFFworld\x00")
g("hello\x80world", "hello\uFFFDworld")
g("hello\xFE\xFFworld", "hello\uFFFD\uFFFDworld")
}
示例14: Test_BooleanEncoder_Quick
func Test_BooleanEncoder_Quick(t *testing.T) {
if err := quick.Check(func(values []bool) bool {
expected := values
if values == nil {
expected = []bool{}
}
// Write values to encoder.
enc := tsm1.NewBooleanEncoder()
for _, v := range values {
enc.Write(v)
}
// Retrieve compressed bytes.
buf, err := enc.Bytes()
if err != nil {
t.Fatal(err)
}
// Read values out of decoder.
got := make([]bool, 0, len(values))
dec := tsm1.NewBooleanDecoder(buf)
for dec.Next() {
got = append(got, dec.Read())
}
// Verify that input and output values match.
if !reflect.DeepEqual(expected, got) {
t.Fatalf("mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", expected, got)
}
return true
}, nil); err != nil {
t.Fatal(err)
}
}
示例15: TestNewRank
func TestNewRank(t *testing.T) {
err := quick.Check(func(r1, r2 Rank) bool {
if len(r1) == 0 || len(r2) == 0 {
return true
}
if r1.String() == r2.String() {
return true
}
between, err := NewRank(r1, r2)
if err != nil {
t.Log(err)
return false
}
if r1.String() > r2.String() {
r1, r2 = r2, r1
}
return r1.String() < between.String() &&
between.String() < r2.String()
}, nil)
if err != nil {
t.Error(err)
}
}