OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package cmpbin |
| 6 |
| 7 import ( |
| 8 "bytes" |
| 9 "io" |
| 10 "math/rand" |
| 11 "sort" |
| 12 "testing" |
| 13 |
| 14 . "github.com/smartystreets/goconvey/convey" |
| 15 ) |
| 16 |
| 17 func TestFloats(t *testing.T) { |
| 18 t.Parallel() |
| 19 |
| 20 Convey("floats", t, func() { |
| 21 b := &bytes.Buffer{} |
| 22 |
| 23 Convey("good", func() { |
| 24 f1 := float64(1.234) |
| 25 n, err := WriteFloat64(b, f1) |
| 26 So(err, ShouldBeNil) |
| 27 So(n, ShouldEqual, 8) |
| 28 f, n, err := ReadFloat64(b) |
| 29 So(err, ShouldBeNil) |
| 30 So(n, ShouldEqual, 8) |
| 31 So(f, ShouldEqual, f1) |
| 32 }) |
| 33 |
| 34 Convey("bad", func() { |
| 35 _, n, err := ReadFloat64(b) |
| 36 So(err, ShouldEqual, io.EOF) |
| 37 So(n, ShouldEqual, 0) |
| 38 }) |
| 39 }) |
| 40 } |
| 41 |
| 42 func TestFloatSortability(t *testing.T) { |
| 43 t.Parallel() |
| 44 |
| 45 Convey("floats maintain sort order", t, func() { |
| 46 vec := make(sort.Float64Slice, randomTestSize) |
| 47 r := rand.New(rand.NewSource(*seed)) |
| 48 for i := range vec { |
| 49 vec[i] = r.Float64() |
| 50 } |
| 51 |
| 52 bin := make(sort.StringSlice, len(vec)) |
| 53 b := &bytes.Buffer{} |
| 54 for i := range bin { |
| 55 b.Reset() |
| 56 n, err := WriteFloat64(b, vec[i]) |
| 57 So(err, ShouldBeNil) |
| 58 So(n, ShouldEqual, 8) |
| 59 bin[i] = b.String() |
| 60 } |
| 61 |
| 62 vec.Sort() |
| 63 bin.Sort() |
| 64 |
| 65 for i := range vec { |
| 66 r, _, err := ReadFloat64(bytes.NewBufferString(bin[i])) |
| 67 So(err, ShouldBeNil) |
| 68 So(vec[i], ShouldEqual, r) |
| 69 } |
| 70 }) |
| 71 } |
OLD | NEW |