github.com/philpearl/plenc@v0.0.15/plenccodec/time_test.go (about)

     1  package plenccodec_test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/google/go-cmp/cmp"
     9  	fuzz "github.com/google/gofuzz"
    10  	"github.com/philpearl/plenc"
    11  	"github.com/philpearl/plenc/plenccodec"
    12  )
    13  
    14  func TestTime(t *testing.T) {
    15  	var p1, p2, p3 plenc.Plenc
    16  	p1.ProtoCompatibleTime = true
    17  	p1.RegisterDefaultCodecs()
    18  	p2.RegisterDefaultCodecs()
    19  	p3.RegisterDefaultCodecs()
    20  	p3.RegisterCodec(reflect.TypeOf(time.Time{}), plenccodec.BQTimestampCodec{})
    21  
    22  	f := fuzz.New()
    23  
    24  	type twrap struct {
    25  		T time.Time `plenc:"1"`
    26  		U int       `plenc:"2"`
    27  	}
    28  
    29  	for j, p := range []*plenc.Plenc{&p1, &p2, &p3} {
    30  		for i := 0; i < 100_000; i++ {
    31  			var t0 twrap
    32  			f.Fuzz(&t0)
    33  
    34  			if j == 2 {
    35  				t0.T = t0.T.Truncate(time.Microsecond)
    36  			}
    37  
    38  			data, err := p.Marshal(nil, &t0)
    39  			if err != nil {
    40  				t.Fatal(err)
    41  			}
    42  
    43  			var t1 twrap
    44  			if err := p.Unmarshal(data, &t1); err != nil {
    45  				t.Fatal(err)
    46  			}
    47  
    48  			if diff := cmp.Diff(t0, t1); diff != "" {
    49  				t.Logf("unix %d %d", t0.T.Unix(), t0.T.Nanosecond())
    50  				t.Fatalf("differs. %s", diff)
    51  			}
    52  		}
    53  	}
    54  }
    55  
    56  func TestTimeMarshal(t *testing.T) {
    57  	tests := []time.Time{
    58  		{},
    59  		time.Date(1970, 3, 15, 0, 0, 0, 0, time.UTC),
    60  		time.Now(),
    61  	}
    62  
    63  	for _, test := range tests {
    64  		t.Run(test.String(), func(t *testing.T) {
    65  			data, err := plenc.Marshal(nil, &test)
    66  			if err != nil {
    67  				t.Fatal(err)
    68  			}
    69  			var out time.Time
    70  			if err := plenc.Unmarshal(data, &out); err != nil {
    71  				t.Fatal(err)
    72  			}
    73  			if !out.Equal(test) {
    74  				t.Fatalf("times %s and %s differ", out, test)
    75  			}
    76  		})
    77  	}
    78  }
    79  
    80  func TestTimeInStructMarshal(t *testing.T) {
    81  	type twrap struct {
    82  		T time.Time `plenc:"1"`
    83  		U int       `plenc:"2"`
    84  	}
    85  
    86  	tests := []twrap{
    87  		{},
    88  		{U: 1},
    89  		{T: time.Date(1970, 3, 15, 0, 0, 0, 0, time.UTC)},
    90  		{T: time.Now()},
    91  	}
    92  
    93  	for _, test := range tests {
    94  		t.Run("", func(t *testing.T) {
    95  			data, err := plenc.Marshal(nil, &test)
    96  			if err != nil {
    97  				t.Fatal(err)
    98  			}
    99  			var out twrap
   100  			if err := plenc.Unmarshal(data, &out); err != nil {
   101  				t.Fatal(err)
   102  			}
   103  			if diff := cmp.Diff(test, out); diff != "" {
   104  				t.Fatal(diff)
   105  			}
   106  		})
   107  	}
   108  }
   109  
   110  func BenchmarkTime(b *testing.B) {
   111  	b.ReportAllocs()
   112  	in := time.Now()
   113  	b.RunParallel(func(pb *testing.PB) {
   114  		var data []byte
   115  		var out time.Time
   116  		for pb.Next() {
   117  			var err error
   118  			data, err = plenc.Marshal(data[:0], &in)
   119  			if err != nil {
   120  				b.Fatal(err)
   121  			}
   122  			if err := plenc.Unmarshal(data, &out); err != nil {
   123  				b.Fatal(err)
   124  			}
   125  		}
   126  	})
   127  }