-
Notifications
You must be signed in to change notification settings - Fork 294
/
field_map_test.go
204 lines (163 loc) · 4.55 KB
/
field_map_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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// 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
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFieldMap_Clear(t *testing.T) {
var fMap FieldMap
fMap.init()
fMap.SetField(1, FIXString("hello"))
fMap.SetField(2, FIXString("world"))
fMap.Clear()
if fMap.Has(1) || fMap.Has(2) {
t.Error("All fields should be cleared")
}
}
func TestFieldMap_SetAndGet(t *testing.T) {
var fMap FieldMap
fMap.init()
fMap.SetField(1, FIXString("hello"))
fMap.SetField(2, FIXString("world"))
var testCases = []struct {
tag Tag
expectErr bool
expectValue string
}{
{tag: 1, expectValue: "hello"},
{tag: 2, expectValue: "world"},
{tag: 44, expectErr: true},
}
for _, tc := range testCases {
var testField FIXString
err := fMap.GetField(tc.tag, &testField)
if tc.expectErr {
assert.NotNil(t, err, "Expected Error")
continue
}
assert.Nil(t, err, "Unexpected error")
assert.Equal(t, tc.expectValue, string(testField))
}
}
func TestFieldMap_Length(t *testing.T) {
var fMap FieldMap
fMap.init()
fMap.SetField(1, FIXString("hello"))
fMap.SetField(2, FIXString("world"))
fMap.SetField(8, FIXString("FIX.4.4"))
fMap.SetField(9, FIXInt(100))
fMap.SetField(10, FIXString("100"))
assert.Equal(t, 16, fMap.length(), "Length should include all fields but beginString, bodyLength, and checkSum")
}
func TestFieldMap_Total(t *testing.T) {
var fMap FieldMap
fMap.init()
fMap.SetField(1, FIXString("hello"))
fMap.SetField(2, FIXString("world"))
fMap.SetField(8, FIXString("FIX.4.4"))
fMap.SetField(Tag(9), FIXInt(100))
fMap.SetField(10, FIXString("100"))
assert.Equal(t, 2116, fMap.total(), "Total should includes all fields but checkSum")
}
func TestFieldMap_TypedSetAndGet(t *testing.T) {
var fMap FieldMap
fMap.init()
fMap.SetString(1, "hello")
fMap.SetInt(2, 256)
s, err := fMap.GetString(1)
assert.Nil(t, err)
assert.Equal(t, "hello", s)
i, err := fMap.GetInt(2)
assert.Nil(t, err)
assert.Equal(t, 256, i)
_, err = fMap.GetInt(1)
assert.NotNil(t, err, "Type mismatch should occur error")
s, err = fMap.GetString(2)
assert.Nil(t, err)
assert.Equal(t, "256", s)
b, err := fMap.GetBytes(1)
assert.Nil(t, err)
assert.True(t, bytes.Equal([]byte("hello"), b))
}
func TestFieldMap_BoolTypedSetAndGet(t *testing.T) {
var fMap FieldMap
fMap.init()
fMap.SetBool(1, true)
v, err := fMap.GetBool(1)
assert.Nil(t, err)
assert.True(t, v)
s, err := fMap.GetString(1)
assert.Nil(t, err)
assert.Equal(t, "Y", s)
fMap.SetBool(2, false)
v, err = fMap.GetBool(2)
assert.Nil(t, err)
assert.False(t, v)
s, err = fMap.GetString(2)
assert.Nil(t, err)
assert.Equal(t, "N", s)
}
func TestFieldMap_CopyInto(t *testing.T) {
var fMapA FieldMap
fMapA.initWithOrdering(headerFieldOrdering)
fMapA.SetString(9, "length")
fMapA.SetString(8, "begin")
fMapA.SetString(35, "msgtype")
fMapA.SetString(1, "a")
assert.Equal(t, []Tag{8, 9, 35, 1}, fMapA.sortedTags())
var fMapB FieldMap
fMapB.init()
fMapB.SetString(1, "A")
fMapB.SetString(3, "C")
fMapB.SetString(4, "D")
assert.Equal(t, fMapB.sortedTags(), []Tag{1, 3, 4})
fMapA.CopyInto(&fMapB)
assert.Equal(t, []Tag{8, 9, 35, 1}, fMapB.sortedTags())
// new fields
s, err := fMapB.GetString(35)
assert.Nil(t, err)
assert.Equal(t, "msgtype", s)
// existing fields overwritten
s, err = fMapB.GetString(1)
assert.Nil(t, err)
assert.Equal(t, "a", s)
// old fields cleared
_, err = fMapB.GetString(3)
assert.NotNil(t, err)
// check that ordering is overwritten
fMapB.SetString(2, "B")
assert.Equal(t, []Tag{8, 9, 35, 1, 2}, fMapB.sortedTags())
// updating the existing map doesn't affect the new
fMapA.init()
fMapA.SetString(1, "AA")
s, err = fMapB.GetString(1)
assert.Nil(t, err)
assert.Equal(t, "a", s)
fMapA.Clear()
s, err = fMapB.GetString(1)
assert.Nil(t, err)
assert.Equal(t, "a", s)
}
func TestFieldMap_Remove(t *testing.T) {
var fMap FieldMap
fMap.init()
fMap.SetField(1, FIXString("hello"))
fMap.SetField(2, FIXString("world"))
fMap.Remove(1)
assert.False(t, fMap.Has(1))
assert.True(t, fMap.Has(2))
}