-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathfield_comparator_test.cpp
More file actions
283 lines (212 loc) · 7.78 KB
/
field_comparator_test.cpp
File metadata and controls
283 lines (212 loc) · 7.78 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright (c) 2013, 2014, Huang-Ming Huang, Object Computing, Inc.
// All rights reserved.
//
// This file is part of mFAST.
//
// mFAST is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// mFAST is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with mFast. If not, see <http://www.gnu.org/licenses/>.
//
#include <catch2/catch_test_macros.hpp>
#include <mfast.h>
#include <mfast/field_comparator.h>
#include <mfast/xml_parser/dynamic_templates_description.h>
#include <cstring>
#include "debug_allocator.h"
#include <stdexcept>
using namespace mfast;
TEST_CASE("test the == operator of a simple message", "[simple_template_test]")
{
debug_allocator alloc;
const char* xml_content =
"<?xml version=\" 1.0 \"?>\n"
"<templates xmlns=\"http://www.fixprotocol.org/ns/template-definition\" "
"templateNs=\"http://www.fixprotocol.org/ns/templates/sample\" ns=\"http://www.fixprotocol.org/ns/fix\">\n"
"<template name=\"Test\">\n"
"<uInt32 name=\"field1\" id=\"11\"><copy/></uInt32>\n"
"<uInt32 name=\"field2\" id=\"12\"><copy/></uInt32>\n"
"<decimal name=\"field3\" id=\"13\"><copy/></decimal>\n"
"<string name=\"field4\" id=\"14\"><copy/></string>\n"
"</template>\n"
"</templates>\n";
dynamic_templates_description description(xml_content);
message_type m1(&alloc, description[0]);
message_type m2(&alloc, description[0]);
message_mref m1ref = m1.ref();
message_mref m2ref = m2.ref();
m1ref[0].as(0);
m1ref[1].as(1);
m1ref[2].as(2);
m1ref[3].as("abcd");
m2ref[0].as(0);
m2ref[1].as(1);
m2ref[2].as(2);
m2ref[3].as("abcd");
REQUIRE(m1ref[0] == m2ref[0]);
REQUIRE(m1ref == m2ref);
m2ref[2].as(3);
REQUIRE_FALSE(m1ref == m2ref);
debug_allocator alloc2;
message_type m3(m1.cref(), &alloc2);
REQUIRE(m3.cref() == m1ref);
}
TEST_CASE("test the == operator of a group","[group_compare_test]")
{
debug_allocator alloc;
const char* xml_content =
"<?xml version=\" 1.0 \"?>\n"
"<templates xmlns=\"http://www.fixprotocol.org/ns/template-definition\" "
"templateNs=\"http://www.fixprotocol.org/ns/templates/sample\" ns=\"http://www.fixprotocol.org/ns/fix\">\n"
"<template name=\"Test\">\n"
"<uInt32 name=\"field1\" id=\"11\"><copy/></uInt32>\n"
"<group name=\"group1\" presence=\"optional\">"
"<uInt32 name=\"field2\" id=\"12\"><copy/></uInt32>\n"
"<uInt32 name=\"field3\" id=\"13\"><copy/></uInt32>\n"
"</group>"
"</template>\n"
"</templates>\n";
dynamic_templates_description description(xml_content);
message_type m1(&alloc, description[0]);
message_type m2(&alloc, description[0]);
message_mref m1ref = m1.ref();
message_mref m2ref = m2.ref();
m1ref[0].as(0);
group_mref m1group(m1ref[1]);
m1group[0].as(1);
m1group[1].as(2);
m2ref[0].as(0);
group_mref m2group(m2ref[1]);
m2group[0].as(1);
m2group[1].as(2);
REQUIRE(m1ref == m2ref);
m2group[1].as(3);
REQUIRE_FALSE(m1ref == m2ref);
debug_allocator alloc2;
message_type m3(m1.cref(), &alloc2);
REQUIRE(m3.cref() == m1ref);
}
TEST_CASE("test the == operator of sequence","[sequence_compare_test]")
{
debug_allocator alloc;
const char* xml_content =
"<?xml version=\" 1.0 \"?>\n"
"<templates xmlns=\"http://www.fixprotocol.org/ns/template-definition\" "
"templateNs=\"http://www.fixprotocol.org/ns/templates/sample\" ns=\"http://www.fixprotocol.org/ns/fix\">\n"
"<template name=\"Test\">\n"
"<uInt32 name=\"field1\" id=\"11\"><copy/></uInt32>\n"
"<sequence name=\"sequence1\" presence=\"optional\">"
"<uInt32 name=\"field2\" id=\"12\"><copy/></uInt32>\n"
"<uInt32 name=\"field3\" id=\"13\"><copy/></uInt32>\n"
"</sequence>"
"</template>\n"
"</templates>\n";
dynamic_templates_description description(xml_content);
message_type m1(&alloc, description[0]);
message_type m2(&alloc, description[0]);
message_mref m1ref = m1.ref();
message_mref m2ref = m2.ref();
m1ref[0].as(0);
sequence_mref m1seq(m1ref[1]);
m1seq.resize(2);
m1seq[0][0].as(1);
m1seq[0][1].as(2);
m1seq[1][0].as(3);
m1seq[1][1].as(4);
m2ref[0].as(0);
sequence_mref m2seq(m2ref[1]);
m2seq.resize(2);
m2seq[0][0].as(1);
m2seq[0][1].as(2);
m2seq[1][0].as(3);
m2seq[1][1].as(4);
REQUIRE(m1ref == m2ref);
m2seq[1][1].as(10);
REQUIRE_FALSE(m1ref == m2ref);
debug_allocator alloc2;
message_type m3(m1.cref(), &alloc2);
REQUIRE(m3.cref() == m1ref);
}
TEST_CASE("test the == operator of static templateref","[static_templateref_compare_test]")
{
debug_allocator alloc;
const char* xml_content =
"<?xml version=\" 1.0 \"?>\n"
"<templates xmlns=\"http://www.fixprotocol.org/ns/template-definition\" "
"templateNs=\"http://www.fixprotocol.org/ns/templates/sample\" ns=\"http://www.fixprotocol.org/ns/fix\">\n"
"<template name=\"Nested\" id=\"01\">\n"
"<uInt32 name=\"field2\" id=\"12\"><copy/></uInt32>\n"
"<uInt32 name=\"field3\" id=\"13\"><copy/></uInt32>\n"
"</template>"
"<template name=\"Test\" id=\"02\">\n"
"<uInt32 name=\"field1\" id=\"11\"><copy/></uInt32>\n"
"<templateRef name=\"Nested\" />"
"</template>\n"
"</templates>\n";
dynamic_templates_description description(xml_content);
message_type m1(&alloc, description[1]);
message_type m2(&alloc, description[1]);
message_mref m1ref = m1.ref();
message_mref m2ref = m2.ref();
m1ref[0].as(1);
m1ref[1].as(2);
m1ref[2].as(3);
m2ref[0].as(1);
m2ref[1].as(2);
m2ref[2].as(3);
REQUIRE(m1ref == m2ref);
debug_allocator alloc2;
message_type m3(m1.cref(), &alloc2);
REQUIRE(m3.cref() == m1ref);
}
TEST_CASE("test the == operator of dynamic templateref","[dynamic_templateref_compare_test]")
{
debug_allocator alloc;
const char* xml_content =
"<?xml version=\" 1.0 \"?>\n"
"<templates xmlns=\"http://www.fixprotocol.org/ns/template-definition\" "
"templateNs=\"http://www.fixprotocol.org/ns/templates/sample\" ns=\"http://www.fixprotocol.org/ns/fix\">\n"
"<template name=\"Nested\" id=\"01\">\n"
"<uInt32 name=\"field2\" id=\"12\"><copy/></uInt32>\n"
"<uInt32 name=\"field3\" id=\"13\"><copy/></uInt32>\n"
"</template>"
"<template name=\"Test\" id=\"02\">\n"
"<uInt32 name=\"field1\" id=\"11\"><copy/></uInt32>\n"
"<templateRef/>"
"</template>\n"
"</templates>\n";
dynamic_templates_description description(xml_content);
message_type m1(&alloc, description[1]);
message_type m2(&alloc, description[1]);
message_mref m1ref = m1.ref();
message_mref m2ref = m2.ref();
m1ref[0].as(1);
nested_message_mref nested1(m1ref[1]);
message_mref target1 = nested1.rebind(description[0]);
target1[0].as(2);
target1[1].as(3);
nested_message_cref nested1_cref(m1.cref()[1]);
REQUIRE(nested1_cref.target_instruction() == description[0]);
const field_instruction* inst = nested1_cref.target().instruction();
REQUIRE( dynamic_cast<const template_instruction*>(inst));
message_cref the_nested1(nested1_cref.target());
REQUIRE( static_cast<uint32_cref>(the_nested1[0]).value() == 2U);
REQUIRE( static_cast<uint32_cref>(the_nested1[1]).value() == 3U);
m2ref[0].as(1);
nested_message_mref nested2(m2ref[1]);
message_mref target2 = nested2.rebind(description[0]);
target2[0].as(2);
target2[1].as(3);
REQUIRE(m1ref == m2ref);
debug_allocator alloc2;
message_type m3(m1.cref(), &alloc2);
REQUIRE(m3.cref() == m1ref);
}