func TestMarshalUnmarshalArbitrary(t *testing.T) { f := func(arb Arbitrary) interface{} { a, _ := standardize(arb.Value); return a } g := func(arb Arbitrary) interface{} { data, err := Marshal(arb.Value, XMLFormat) if err != nil { t.Error(err) return nil } var result interface{} format, err := Unmarshal(data, &result) if err != nil { t.Error(err) return nil } if format != XMLFormat { t.Error(err) return nil } a, _ := standardize(result) return a } if err := quick.CheckEqual(f, g, nil); err != nil { out1 := err.(*quick.CheckEqualError).Out1[0] out2 := err.(*quick.CheckEqualError).Out2[0] findDifferences(t, out1, out2) t.Error(err) } }
func TestXXD(t *testing.T) { if *xxdFile == "" { t.Skip("-xxdFile argument not given") } data, err := ioutil.ReadFile(*xxdFile) if err != nil { t.Fatal(err) } test := func(fn func(r io.Reader, w io.Writer, s string) error) func(n uint64) []string { return func(n uint64) []string { size := n % uint64(len(data)) fmt.Printf("%d\n", size) var out bytes.Buffer if err := fn(&pathologicalReader{data[0:size]}, &out, ""); err != nil { return []string{err.Error()} } return strings.Split(out.String(), "\n") } } if err := quick.CheckEqual(test(xxd), test(xxdNative), nil); err != nil { cErr := err.(*quick.CheckEqualError) size := cErr.In[0].(uint64) % uint64(len(data)) for i := range cErr.Out1[0].([]string) { got := cErr.Out1[0].([]string)[i] want := cErr.Out2[0].([]string)[i] if got != want { t.Errorf("size: %d\n\ngot : %s\nwant: %s\n", size, got, want) break } } } }
func TestQuickPythonEncodeBits(t *testing.T) { if !*python { t.Skip("Skipping, use -python to enable") } us := func(orig []byte, partial uint) (string, error) { bits := calcBits(orig, partial) encoded := zbase32.EncodeBitsToString(orig, bits) return encoded, nil } them := func(orig []byte, partial uint) (string, error) { // the python library raises IndexError on zero-length input if len(orig) == 0 { return "", nil } bits := calcBits(orig, partial) cmd := exec.Command("python", "-c", ` import sys, zbase32 orig = sys.stdin.read() bits = int(sys.argv[1]) sys.stdout.write(zbase32.b2a_l(orig, bits)) `, strconv.Itoa(bits), ) cmd.Stdin = bytes.NewReader(orig) cmd.Stderr = os.Stderr output, err := cmd.Output() if err != nil { return "", fmt.Errorf("cannot run python: %v", err) } return string(output), nil } if err := quick.CheckEqual(us, them, nil); err != nil { t.Fatal(err) } }
func TestCompareRead(t *testing.T) { r := NewRandReader(42) buf := make([]byte, 10*1024*1024) r.Read(buf) blob := emptyBlob(t, &mock.InMemory{}) blob.WriteAt(buf, 0) got := func(p []byte, off int64) (int, error) { if off < 0 { off = -off } return blob.ReadAt(p, off) } rat := bytes.NewReader(buf) exp := func(p []byte, off int64) (int, error) { if off < 0 { off = -off } return rat.ReadAt(p, off) } config := quick.Config{ MaxCountScale: 100.0, } if err := quick.CheckEqual(got, exp, &config); err != nil { t.Error(err) } }
func TestPackageMethods(t *T) { pkg := func(a, b, c, d, e, f string) string { buf := setupLogger() defer resetLogger() Println(a, b, c, d, e, f) Print(a, b, c, d, e, f) Printf(a, b, c, d, e, f) return buf.String() } dft := func(a, b, c, d, e, f string) string { buf := setupLogger() defer resetLogger() Default.Println(a, b, c, d, e, f) Default.Print(a, b, c, d, e, f) Default.Printf(a, b, c, d, e, f) return buf.String() } if err := quick.CheckEqual(pkg, dft, nil); err != nil { t.Error(err) } }
func TestQuickPythonDecodeBytes(t *testing.T) { if !*python { t.Skip("Skipping, use -python to enable") } us := func(orig []byte) ([]byte, error) { encoded := zbase32.EncodeToString(orig) return zbase32.DecodeString(encoded) } them := func(orig []byte) ([]byte, error) { // the python library raises IndexError on zero-length input if len(orig) == 0 { return []byte{}, nil } encoded := zbase32.EncodeToString(orig) cmd := exec.Command("python", "-c", ` import sys, zbase32 enc = sys.stdin.read() sys.stdout.write(zbase32.a2b(enc)) `) cmd.Stdin = strings.NewReader(encoded) cmd.Stderr = os.Stderr output, err := cmd.Output() if err != nil { return nil, fmt.Errorf("cannot run python: %v", err) } return output, nil } if err := quick.CheckEqual(us, them, nil); err != nil { t.Fatal(err) } }
func TestSumBytes(t *testing.T) { config := &quick.Config{MaxCount: 1 << 10} fast := func(x TestSlice) uint64 { return FastSumUint8(x) } slow := func(x TestSlice) uint64 { return SumUint8(x) } if err := quick.CheckEqual(fast, slow, config); err != nil { t.Error(err) } }
func TestNodeID_logdist(t *testing.T) { logdistBig := func(a, b common.Hash) int { abig, bbig := new(big.Int).SetBytes(a[:]), new(big.Int).SetBytes(b[:]) return new(big.Int).Xor(abig, bbig).BitLen() } if err := quick.CheckEqual(logdist, logdistBig, quickcfg()); err != nil { t.Error(err) } }
func TestForward(t *testing.T) { if err := quick.CheckEqual(func(a, b []byte) ([]byte, error) { diff := Diff(a, b, 0) return Forward(a, diff) }, func(a, b []byte) ([]byte, error) { return b, nil }, nil); err != nil { t.Error(err) } }
func TestReverse(t *testing.T) { if err := quick.CheckEqual(func(a, b []byte) ([]byte, error) { diff := Diff(a, b, 0) return Reverse(b, diff) }, func(a, b []byte) ([]byte, error) { return a, nil }, nil); err != nil { t.Error(err) } }
func TestClientSeek(t *testing.T) { sftp, cmd := testClient(t, READONLY, NO_DELAY) defer cmd.Wait() defer sftp.Close() fOS, err := ioutil.TempFile("", "seek-test") if err != nil { t.Fatal(err) } defer fOS.Close() fSFTP, err := sftp.Open(fOS.Name()) if err != nil { t.Fatal(err) } defer fSFTP.Close() writeN(t, fOS, seekBytes) if err := quick.CheckEqual( func(s seek) (string, int64) { s.set(t, fOS); return readHash(t, fOS) }, func(s seek) (string, int64) { s.set(t, fSFTP); return readHash(t, fSFTP) }, nil, ); err != nil { t.Errorf("Seek: expected equal absolute seeks: %v", err) } if err := quick.CheckEqual( func(s seek) (string, int64) { s.current(t, fOS); return readHash(t, fOS) }, func(s seek) (string, int64) { s.current(t, fSFTP); return readHash(t, fSFTP) }, nil, ); err != nil { t.Errorf("Seek: expected equal seeks from middle: %v", err) } if err := quick.CheckEqual( func(s seek) (string, int64) { s.end(t, fOS); return readHash(t, fOS) }, func(s seek) (string, int64) { s.end(t, fSFTP); return readHash(t, fSFTP) }, nil, ); err != nil { t.Errorf("Seek: expected equal seeks from end: %v", err) } }
func TestNodeID_distcmp(t *testing.T) { distcmpBig := func(target, a, b common.Hash) int { tbig := new(big.Int).SetBytes(target[:]) abig := new(big.Int).SetBytes(a[:]) bbig := new(big.Int).SetBytes(b[:]) return new(big.Int).Xor(tbig, abig).Cmp(new(big.Int).Xor(tbig, bbig)) } if err := quick.CheckEqual(distcmp, distcmpBig, quickcfg()); err != nil { t.Error(err) } }
func TestConstantTimeByteEq(t *testing.T) { for i, test := range testConstandTimeByteEqData { if r := ConstantTimeByteEq(test.a, test.b); r != test.out { t.Errorf("#%d bad result (got %x, want %x)", i, r, test.out) } } err := quick.CheckEqual(ConstantTimeByteEq, byteEq, nil) if err != nil { t.Error(err) } }
func TestNewEscaper(t *testing.T) { of := func(in chars) string { return oldunescape(string(in)) } nf := func(in chars) string { return unescape(string(in)) } if err := quick.CheckEqual(of, nf, nil); err != nil { t.Errorf("quickcheck failure: %v", err) } }
func TestCFNumber_Float64(t *testing.T) { f := func(f float64) float64 { return f } g := func(f float64) float64 { cfNum := convertFloat64ToCFNumber(f) if cfNum == nil { t.Fatal("CFNumberRef is NULL (%#v)", f) } defer cfRelease(cfTypeRef(cfNum)) return convertCFNumberToFloat64(cfNum) } if err := quick.CheckEqual(f, g, nil); err != nil { t.Error(err) } }
func TestCFData(t *testing.T) { f := func(data []byte) []byte { return data } g := func(data []byte) []byte { cfData := convertBytesToCFData(data) if cfData == nil { t.Fatal("CFDataRef is NULL (%#v)", data) } defer cfRelease(cfTypeRef(cfData)) return convertCFDataToBytes(cfData) } if err := quick.CheckEqual(f, g, nil); err != nil { t.Error(err) } }
func TestCFNumber_UInt32(t *testing.T) { f := func(i uint32) uint32 { return i } g := func(i uint32) uint32 { cfNum := convertUInt32ToCFNumber(i) if cfNum == nil { t.Fatal("CFNumberRef is NULL (%#v)", i) } defer cfRelease(cfTypeRef(cfNum)) return convertCFNumberToUInt32(cfNum) } if err := quick.CheckEqual(f, g, nil); err != nil { t.Error(err) } }
func TestCFBoolean(t *testing.T) { f := func(b bool) bool { return b } g := func(b bool) bool { cfBool := convertBoolToCFBoolean(b) if cfBool == nil { t.Fatal("CFBooleanRef is NULL (%#v)", b) } defer cfRelease(cfTypeRef(cfBool)) return convertCFBooleanToBool(cfBool) } if err := quick.CheckEqual(f, g, nil); err != nil { t.Error(err) } }
func main() { f := func(data sort.IntSlice) sort.IntSlice { d := make(sort.IntSlice, len(data)) // Defensive copy! copy(d, data) sort.Sort(d) return d } g := func(data sort.IntSlice) sort.IntSlice { d := make(sort.IntSlice, len(data)) // Defensive copy! copy(d, data) BubbleSort(d) return d } fmt.Println("Counter examples against sort:", quick.CheckEqual(f, g, nil)) }
func TestCFString(t *testing.T) { // because the generator for string produces invalid strings, // lets generate []runes instead and convert those to strings in the function f := func(runes []rune) string { return string(runes) } g := func(runes []rune) string { cfStr := convertStringToCFString(string(runes)) if cfStr == nil { t.Fatal("CFStringRef is NULL (%#v)", runes) } defer cfRelease(cfTypeRef(cfStr)) return convertCFStringToString(cfStr) } if err := quick.CheckEqual(f, g, nil); err != nil { t.Error(err) } }
func TestQuickPublishConsumeOnly(t *testing.T) { c1 := integrationConnection(t, "quick-pub") c2 := integrationConnection(t, "quick-sub") if c1 != nil && c2 != nil { defer c1.Close() defer c2.Close() pub, err := c1.Channel() sub, err := c2.Channel() queue := "TestPublishConsumeOnly" if _, err = pub.QueueDeclare(queue, false, true, false, false, nil); err != nil { t.Errorf("Failed to declare: %s", err) return } if _, err = sub.QueueDeclare(queue, false, true, false, false, nil); err != nil { t.Errorf("Failed to declare: %s", err) return } defer sub.QueueDelete(queue, false, false, false) ch, err := sub.Consume(queue, "", false, false, false, false, nil) if err != nil { t.Errorf("Could not sub: %s", err) } quick.CheckEqual( func(msg Publishing) []byte { empty := Publishing{Body: msg.Body} if pub.Publish("", queue, false, false, empty) != nil { return []byte{'X'} } return msg.Body }, func(msg Publishing) []byte { out := <-ch out.Ack(false) return out.Body }, nil) } }
func TestCFDate(t *testing.T) { // We know the CFDate conversion explicitly truncates to milliseconds // because CFDates use floating point for representation. round := func(nano int64) int64 { return int64(time.Duration(nano) / time.Millisecond * time.Millisecond) } f := func(nano int64) time.Time { return time.Unix(0, round(nano)) } g := func(nano int64) time.Time { ti := time.Unix(0, round(nano)) cfDate := convertTimeToCFDate(ti) if cfDate == nil { t.Fatal("CFDateRef is NULL (%#v)", ti) } defer cfRelease(cfTypeRef(cfDate)) return convertCFDateToTime(cfDate) } if err := quick.CheckEqual(f, g, nil); err != nil { t.Error(err) } }
func TestArbitrary(t *testing.T) { // test arbitrary values of any plistable type f := func(arb Arbitrary) interface{} { a, _ := standardize(arb.Value); return a } g := func(arb Arbitrary) interface{} { if cfObj, err := convertValueToCFType(reflect.ValueOf(arb.Value)); err != nil { t.Error(err) } else { defer cfRelease(cfTypeRef(cfObj)) if val, err := convertCFTypeToInterface(cfObj); err != nil { t.Error(err) } else { a, _ := standardize(val) return a } } return nil } if err := quick.CheckEqual(f, g, nil); err != nil { input := err.(*quick.CheckEqualError).In[0].(Arbitrary).Value t.Logf("Input value type: %T", input) t.Error(err) } }
func TestConstantTimeEq(t *testing.T) { err := quick.CheckEqual(ConstantTimeEq, eq, nil) if err != nil { t.Error(err) } }
func TestRotateQuick(t *testing.T) { if err := quick.CheckEqual(rotate.RotateByJuggle, rotate.RotateByReverse, nil); nil != err { t.Error(err) } }
func TestConstantTimeCopy(t *testing.T) { err := quick.CheckEqual(constantTimeCopyWrapper, makeCopy, nil) if err != nil { t.Error(err) } }
func TestCacheFilename(t *testing.T) { quick.CheckEqual(CacheFilename("blob"), "", nil) }
func testCompareBoth(t *testing.T, saveEvery int) { f, err := ioutil.TempFile("", "baziltest-") if err != nil { t.Fatalf("tempfile error: %v") } defer f.Close() blob, err := blobs.Open(&mock.InMemory{}, &blobs.Manifest{ Type: "footype", ChunkSize: blobs.MinChunkSize, Fanout: 2, }, ) if err != nil { t.Fatalf("cannot open blob: %v", err) } if seed == 0 { seed = uint64(entropy.Seed()) } t.Logf("Seed is %d", seed) qconf := quick.Config{ Rand: rand.New(rand.NewSource(int64(seed))), } count := 0 got := func(isWrite bool, off int64, size int, writeSeed int64) (num int, read []byte, err error) { if off < 0 { off = -off } off = off % (10 * 1024 * 1024) if size < 0 { size = -size } size = size % (10 * 1024) if isWrite { count++ if saveEvery > 0 && count%saveEvery == 0 { _, err := blob.Save() if err != nil { return 0, nil, err } } p := make([]byte, size) NewRandReader(writeSeed).Read(p) t.Logf("write %d@%d", len(p), off) n, err := blob.WriteAt(p, off) return n, nil, err } else { p := make([]byte, size) t.Logf("read %d@%d", len(p), off) n, err := blob.ReadAt(p, off) // http://golang.org/pkg/io/#ReaderAt says "If the n = len(p) // bytes returned by ReadAt are at the end of the input // source, ReadAt may return either err == EOF or err == // nil." Unify the result if n == len(p) && err == io.EOF { err = nil } return n, p, err } } exp := func(isWrite bool, off int64, size int, writeSeed int64) (num int, read []byte, err error) { if off < 0 { off = -off } off = off % (10 * 1024 * 1024) if size < 0 { size = -size } size = size % (10 * 1024) if isWrite { p := make([]byte, size) NewRandReader(writeSeed).Read(p) n, err := f.WriteAt(p, off) return n, nil, err } else { p := make([]byte, size) n, err := f.ReadAt(p, off) // http://golang.org/pkg/io/#ReaderAt says "If the n = len(p) // bytes returned by ReadAt are at the end of the input // source, ReadAt may return either err == EOF or err == // nil." Unify the result if n == len(p) && err == io.EOF { err = nil } return n, p, err } } if err := quick.CheckEqual(got, exp, &qconf); err != nil { t.Error(err) } }
func TestCounter(t *testing.T) { // Yes, this is really close to testing golang "sync/atomic" package. if err := quick.CheckEqual(count, reference, nil); err != nil { t.Error(err) } }
func TestDifferentBitCountInSha256(t *testing.T) { err := quick.CheckEqual(DifferentBitCountInSha256, naiveCount, nil) if err != nil { t.Error(err) } }