-
Notifications
You must be signed in to change notification settings - Fork 294
/
fix_utc_timestamp_test.go
78 lines (66 loc) · 2.32 KB
/
fix_utc_timestamp_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) quickfixengine.org All rights reserved.
//
// This file may be distributed under the terms of the quickfixengine.org
// license as defined by quickfixengine.org and appearing in the file
// LICENSE included in the packaging of this file.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
// THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE.
//
// See http://www.quickfixengine.org/LICENSE for licensing information.
//
// Contact [email protected] if any conditions of this licensing
// are not clear to you.
package quickfix_test
import (
"bytes"
"testing"
"time"
"github.com/quickfixgo/quickfix"
)
func TestFIXUTCTimestampWrite(t *testing.T) {
ts := time.Date(2016, time.February, 8, 22, 7, 16, 954123123, time.UTC)
var tests = []struct {
precision quickfix.TimestampPrecision
val []byte
}{
{quickfix.Millis, []byte("20160208-22:07:16.954")},
{quickfix.Seconds, []byte("20160208-22:07:16")},
{quickfix.Micros, []byte("20160208-22:07:16.954123")},
{quickfix.Nanos, []byte("20160208-22:07:16.954123123")},
}
for _, test := range tests {
var f quickfix.FIXUTCTimestamp
f.Time = ts
f.Precision = test.precision
b := f.Write()
if !bytes.Equal(b, test.val) {
t.Errorf("got %s; want %s", b, test.val)
}
}
}
func TestFIXUTCTimestampRead(t *testing.T) {
var tests = []struct {
timeStr string
expectedTime time.Time
expectedPrecision quickfix.TimestampPrecision
}{
{"20160208-22:07:16.310", time.Date(2016, time.February, 8, 22, 7, 16, 310000000, time.UTC), quickfix.Millis},
{"20160208-22:07:16", time.Date(2016, time.February, 8, 22, 7, 16, 0, time.UTC), quickfix.Seconds},
{"20160208-22:07:16.123455", time.Date(2016, time.February, 8, 22, 7, 16, 123455000, time.UTC), quickfix.Micros},
{"20160208-22:07:16.954123123", time.Date(2016, time.February, 8, 22, 7, 16, 954123123, time.UTC), quickfix.Nanos},
}
for _, test := range tests {
var f quickfix.FIXUTCTimestamp
if err := f.Read([]byte(test.timeStr)); err != nil {
t.Errorf("Unexpected error: %v", err)
}
if !f.Time.Equal(test.expectedTime) {
t.Errorf("For Time expected %v got %v", test.expectedTime, f.Time)
}
if f.Precision != test.expectedPrecision {
t.Errorf("For NoMillis expected %v got %v", test.expectedPrecision, f.Precision)
}
}
}