00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <sys/types.h>
00021 #include <sys/stat.h>
00022 #include <fcntl.h>
00023 #include <limits.h>
00024
00025 #include <libxml/parser.h>
00026 #include <libxml/tree.h>
00027
00028 #include <iostream>
00029
00030 #include "binary_msgs.h"
00031 #include "test_defines.h"
00032 #include "secstwo_serialize.h"
00033 #include "secstwomsg.h"
00034
00035 using namespace freesecs::secstwo;
00036
00037 static pmsg_t s3f17, s14f9, s127f127;
00038
00039 void test_deserialize(void)
00040 {
00041 int fd = open("xsd/test.xml", O_RDONLY);
00042 int schema_fd = open("xsd/secstwo.xsd", O_RDONLY);
00043
00044 TEST_ASSERT(fd > 0 && schema_fd > 0, "xml file and scheme file open");
00045
00046 xml_serializer_t::msg_container_t msgs;
00047
00048 int err = xml_deserializer_t::from_file(fd, schema_fd, msgs);
00049
00050 TEST_ASSERT(!err,"deserialization from file");
00051
00052 int ix = 0;
00053 for(xml_serializer_t::msg_container_t::const_iterator it = msgs.begin(); it != msgs.end(); ++it, ++ix)
00054 {
00055 if(3 == (*it)->stream && 17 == (*it)->function)
00056 {
00057 s3f17 = *it;
00058 TEST_ASSERT(NULL != s3f17.get(), "check s3f17 is not null");
00059 }
00060 else if(14 == (*it)->stream && 9 == (*it)->function)
00061 {
00062 s14f9 = *it;
00063 TEST_ASSERT(NULL != s14f9.get(), "check s14f9 is not null");
00064 }
00065 else if(127 == (*it)->stream && 127 == (*it)->function)
00066 {
00067 s127f127 = *it;
00068 TEST_ASSERT(NULL != s127f127.get(), "check s127f127 is not null");
00069 }
00070 }
00071
00072 TEST_ASSERT_STRINGS_EQUAL("bind_001", s3f17->name.c_str(), "check s3f17 name");
00073 TEST_ASSERT(3 == s3f17->stream, "check s3f17 stream");
00074 TEST_ASSERT(17 == s3f17->function, "check s3f17 function");
00075 TEST_ASSERT_STRINGS_EQUAL("ctljob", s14f9->name.c_str(), "check s14f9 name");
00076 TEST_ASSERT(14 == s14f9->stream, "check s14f9 stream");
00077 TEST_ASSERT(9 == s14f9->function, "check s14f9 function");
00078
00079 close(fd);
00080 close(schema_fd);
00081 }
00082
00083 void test_access(void)
00084 {
00085 uint16_t dataid_ix = (*s3f17)[NUM(0)][NUM(0)];
00086 uint16_t dataid_name = (*s3f17)["DATAID"];
00087 TEST_ASSERT(1 == dataid_ix, "S3F17 DATAID access by index");
00088 TEST_ASSERT(1 == dataid_name, "S3F17 DATAID access by name");
00089
00090 std::string CARRIERACTION = (*s3f17)["CARRIERACTION"];
00091
00092 std::string CATTRID_ix = (*s3f17)[NUM(0)][3][3][NUM(0)];
00093 std::string CATTRID_name = (*s3f17)["CARRIERLIST"][3]["CATTRID"];
00094 TEST_ASSERT_STRINGS_EQUAL("SlotMap", CATTRID_ix.c_str(), "S3F17 CATTRID access by index");
00095 TEST_ASSERT_STRINGS_EQUAL("SlotMap", CATTRID_name.c_str(), "S3F17 CATTRID access by name");
00096
00097 std::string OBJTYPE_ix = (*s14f9)[NUM(0)][NUM(0)];
00098 TEST_ASSERT_STRINGS_EQUAL("Equipment", OBJTYPE_ix.c_str(),"S14F9 OBJTYPE access by index");
00099
00100 uint8_t expected[5] = {1,2,3,4,5};
00101 for(int i = 0; i < sizeof(expected)/sizeof(uint8_t); ++i)
00102 {
00103 TEST_ASSERT(expected[i] == (uint8_t)(*s14f9)["SRC"][1][NUM(0)][i], "Data array elements access");
00104 }
00105 }
00106
00107 static const double euler_const = 2.71828182846;
00108 static const float pi = 3.14159265359;
00109 void test_numeric(void)
00110 {
00111 uint8_t binary[10] = {255, 254, 253, 252, 251, 0, 1, 2, 3, 4};
00112 for(int i = 0; i < sizeof(binary)/sizeof(binary[0]); ++i)
00113 {
00114 TEST_ASSERT(binary[i] == (uint8_t)(*s127f127)["binary"][i], "binary array");
00115 }
00116
00117 TEST_ASSERT(1 == (bool) (*s127f127)["boolean"], "boolean");
00118 TEST_ASSERT(LLONG_MIN == (int64_t) (*s127f127)["int64"], "int64");
00119 TEST_ASSERT(SCHAR_MIN == (int8_t) (*s127f127)["int8"], "int8");
00120 TEST_ASSERT(SHRT_MIN == (int16_t) (*s127f127)["int16"], "int16");
00121 TEST_ASSERT(INT_MIN == (int32_t) (*s127f127)["int32"], "int32");
00122 TEST_ASSERT(euler_const == (double) (*s127f127)["double"], "double");
00123 TEST_ASSERT(float(-1*pi) == (float) (*s127f127)["float"], "float");
00124 TEST_ASSERT(ULLONG_MAX == (uint64_t) (*s127f127)["uint64"], "uint64");
00125 TEST_ASSERT(UCHAR_MAX == (uint8_t) (*s127f127)["uint8"], "uint8");
00126 TEST_ASSERT(USHRT_MAX == (uint16_t) (*s127f127)["uint16"], "uint16");
00127 TEST_ASSERT(UINT_MAX == (uint32_t) (*s127f127)["uint32"], "uint32");
00128 }
00129 void test_string(void)
00130 {
00131 std::string ascii = (*s127f127)["ascii"];
00132 std::string jis8 = (*s127f127)["jis8"];
00133 std::string unicode = (*s127f127)["unicode"];
00134
00135 TEST_ASSERT_STRINGS_EQUAL("秒の規格のためのオープンソースプロジェクトです", jis8.c_str(), "jis8");
00136 TEST_ASSERT_STRINGS_EQUAL("Это строка юникод", unicode.c_str(), "unicode");
00137 TEST_ASSERT_STRINGS_EQUAL("This is ascii string", ascii.c_str(), "ascii");
00138 }
00139 void test_comparison(void)
00140 {
00141 pmsg_t s3f17_clone = s3f17->clone();
00142 pmsg_t s14f9_clone = s14f9->clone();
00143 pmsg_t s127f127_clone = s127f127->clone();
00144
00145 TEST_ASSERT(*s3f17_clone == *s3f17, "s3f17 and it's clone are equal");
00146 TEST_ASSERT(*s14f9_clone == *s14f9, "s14f9 and it's clone are equal");
00147 TEST_ASSERT(*s127f127_clone == *s127f127, "s127f127 and it's clone are equal");
00148 TEST_ASSERT(*s3f17 != *s14f9, "s3f17 not equal to s14f9");
00149 TEST_ASSERT(*s14f9 != *s3f17, "s14f9 not equal to s3f17");
00150 TEST_ASSERT(*s127f127_clone != *s3f17, "s127f127 not equal to s3f17");
00151
00152 (*s3f17_clone)["SlotMap"][7] = uint8_t(2);
00153 TEST_ASSERT(2 == (uint8_t)(*s3f17_clone)["SlotMap"][7], "s2f17 clone modified");
00154 TEST_ASSERT(*s3f17 != *s3f17_clone, "s3f17 and it's modified clone are not equal");
00155
00156 }
00157 void test_assignment(void)
00158 {
00159 pmsg_t s3f17_clone = s3f17->clone();
00160 pmsg_t s127f127_clone = s127f127->clone();
00161
00162 (*s127f127_clone)["binary"] = uint8_t(1);
00163 TEST_ASSERT((*s127f127_clone)["binary"].size() == 1, "binary size (case 1)");
00164 TEST_ASSERT(1 == (uint8_t)(*s127f127_clone)["binary"][NUM(0)], "binary (case 1)");
00165 (*s127f127_clone)["binary"] = uint8_t(2);
00166 TEST_ASSERT((*s127f127_clone)["binary"].size() == uint8_t(1), "binary size (case 2)");
00167 TEST_ASSERT(2 == (uint8_t)(*s127f127_clone)["binary"][NUM(0)], "binary(case 2)");
00168
00169 (*s127f127_clone)["boolean"] = false;
00170 TEST_ASSERT(false == (bool)(*s127f127_clone)["boolean"], "boolean (case 1)");
00171 (*s127f127_clone)["boolean"] = true;
00172 TEST_ASSERT(true == (bool)(*s127f127_clone)["boolean"], "boolean (case 2)");
00173
00174 (*s127f127_clone)["ascii"] = std::string("ascii test");
00175 (*s127f127_clone)["ascii"] = "ascii test";
00176 std::string str = (*s127f127_clone)["ascii"];
00177 printf("ascii test: %s\n", str.c_str());
00178 TEST_ASSERT_STRINGS_EQUAL("ascii test", std::string((*s127f127_clone)["ascii"]).c_str(), "ascii case 1");
00179 (*s127f127_clone)["ascii"] = L"тест с-строки";
00180 TEST_ASSERT_STRINGS_EQUAL("ascii test", std::string((*s127f127_clone)["ascii"]).c_str(), "ascii case 2");
00181 TEST_ASSERT((*s127f127_clone)["ascii"] == std::string("ascii test"), "ascii case 3");
00182
00183 (*s127f127_clone)["jis8"] = std::string("プロジェクトです");
00184 TEST_ASSERT_STRINGS_EQUAL("プロジェクトです", std::string((*s127f127_clone)["jis8"]).c_str(), "jis8 case 1");
00185 TEST_ASSERT((*s127f127_clone)["jis8"] == std::string("プロジェクトです"), "jis8 case 2");
00186 (*s127f127_clone)["unicode"] = std::string("юникод-строка");
00187 TEST_ASSERT_STRINGS_EQUAL("юникод-строка", std::string((*s127f127_clone)["unicode"]).c_str(), "unicode");
00188 TEST_ASSERT((*s127f127_clone)["unicode"] == std::string("юникод-строка"), "unicode case 2");
00189
00190 (*s127f127_clone)["int64"] = LLONG_MIN;
00191 TEST_ASSERT(LLONG_MIN == (int64_t)(*s127f127_clone)["int64"], "int64");
00192
00193 (*s127f127_clone)["int8"] = SCHAR_MIN;
00194 TEST_ASSERT(SCHAR_MIN == (int8_t)(*s127f127_clone)["int8"], "int8");
00195
00196 (*s127f127_clone)["int16"] = SHRT_MIN;
00197 TEST_ASSERT(SHRT_MIN == (int16_t)(*s127f127_clone)["int16"], "int16");
00198
00199 (*s127f127_clone)["int32"] = INT_MIN;
00200 TEST_ASSERT(INT_MIN == (int32_t)(*s127f127_clone)["int32"], "int32");
00201
00202 (*s127f127_clone)["double"] = (double)-3.14f;
00203 TEST_ASSERT(-3.14f == (double)(*s127f127_clone)["double"], "double");
00204 (*s127f127_clone)["double"] = (float)-3.14f;
00205 TEST_ASSERT(-3.14f == (double)(*s127f127_clone)["double"], "double case 2");
00206 TEST_ASSERT(-3.14f == (float)(*s127f127_clone)["double"], "double case 3");
00207
00208 TEST_ASSERT((*s127f127_clone)["double"] == (double)-3.14f, "double case 4");
00209 TEST_ASSERT((*s127f127_clone)["double"] == (float)-3.14f, "double case 5");
00210
00211 (*s127f127_clone)["float"] = (float)(-1.0*float(euler_const));
00212 TEST_ASSERT((-1.0*float(euler_const)) == (float)(*s127f127_clone)["float"], "float case 1");
00213 TEST_ASSERT((*s127f127_clone)["float"] == (-1.0*float(euler_const)), "float case 2");
00214
00215 (*s127f127_clone)["float"] = -5;
00216 TEST_ASSERT((*s127f127_clone)["float"] == -5, "float case 3");
00217 TEST_ASSERT(-5 == (int)(*s127f127_clone)["float"], "float case 4");
00218
00219
00220 (*s127f127_clone)["uint64"] = ULLONG_MAX;
00221 TEST_ASSERT(ULLONG_MAX == (uint64_t)(*s127f127_clone)["uint64"], "uint64");
00222
00223 (*s127f127_clone)["uint8"] = UCHAR_MAX;
00224 TEST_ASSERT(UCHAR_MAX == (uint8_t)(*s127f127_clone)["uint8"], "uint8");
00225
00226 (*s127f127_clone)["uint16"] = USHRT_MAX;
00227 TEST_ASSERT(USHRT_MAX == (uint16_t)(*s127f127_clone)["uint16"], "uint16");
00228
00229 (*s127f127_clone)["uint32"] = UINT_MAX;
00230 TEST_ASSERT(UINT_MAX == (uint32_t)(*s127f127_clone)["uint32"], "uint32");
00231
00232
00233
00234
00235 s127f127_clone = s127f127->clone();
00236 (*s127f127_clone)["uint32"] = 0;
00237
00238 for(int i=1; i < 10; ++i)
00239 {
00240 (*s127f127_clone)["uint32"] += i;
00241 }
00242 TEST_ASSERT(10 == (*s127f127_clone)["uint32"].size(), "uint32 array size");
00243 for(int i=0;i<10;++i)
00244 {
00245 TEST_ASSERT(i == (int)(*s127f127_clone)["uint32"][i], "uint32 array");
00246 }
00247 for(int i=9;i>0;--i)
00248 {
00249 TEST_ASSERT(i == (int)(*s127f127_clone)["uint32"][i], "uint32 array: reverse");
00250 }
00251 TEST_ASSERT(7 == (int)(*s127f127_clone)["uint32"][7], "uint32 array: random access");
00252
00253
00254 s3f17_clone = s3f17->clone();
00255
00256 (*s3f17_clone)["SlotMap"].clear_data();
00257 TEST_ASSERT((*s3f17_clone)["SlotMap"].size() == 0, "var array size check");
00258
00259 pdata_item_t slotmap_element = (*s3f17_clone)["SlotMap"][VAR_ARRAY_ELEMENT].clone();
00260
00261 for(int i=0; i < 25; ++i)
00262 {
00263 *slotmap_element = i;
00264 (*s3f17_clone)["SlotMap"] += slotmap_element;
00265 }
00266 for(int i=0; i < 25; ++i)
00267 {
00268 TEST_ASSERT((*s3f17_clone)["SlotMap"][i] == i, "var array += result");
00269 }
00270 }
00271
00272 void test_copy_data()
00273 {
00274 pmsg_t s3f17_clone = s3f17->clone();
00275 pmsg_t s14f9_clone = s14f9->clone();
00276 pmsg_t s127f127_clone = s127f127->clone();
00277 TEST_ASSERT(s3f17_clone.get(), "s3f17 clone is not empty");
00278 TEST_ASSERT(s14f9_clone.get(), "s14f9 clone is not empty");
00279 TEST_ASSERT(s127f127_clone.get(), "s127f127 clone is not empty");
00280
00281 s3f17_clone->clear_data();
00282 s14f9_clone->clear_data();
00283 s127f127_clone->clear_data();
00284
00285 TEST_ASSERT(s3f17_clone->match(*s3f17, STRUCT), "s3f17 clone has same structure as s3f17");
00286 TEST_ASSERT(false == s3f17_clone->match(*s3f17, DATA), "s3f17 clone does not have same data as s3f17");
00287 TEST_ASSERT(false == s3f17_clone->match(*s3f17, ALL), "s3f17 clone does not match s3f17");
00288 TEST_ASSERT(s14f9_clone->match(*s14f9, STRUCT), "s14f9 clone has same structure as s14f9");
00289 TEST_ASSERT(false == s14f9_clone->match(*s14f9, DATA), "s14f9 clone does not have same data as s14f9");
00290 TEST_ASSERT(false == s14f9_clone->match(*s14f9, ALL), "s14f9 clone does not match s14f9");
00291 TEST_ASSERT(s127f127_clone->match(*s127f127, STRUCT), "s127f127 clone has same structure as s127f127");
00292 TEST_ASSERT(false == s127f127_clone->match(*s127f127, DATA), "s127f127 clone does not have same data as s127f127");
00293 TEST_ASSERT(false == s127f127_clone->match(*s127f127, ALL), "s127f127 clone does not match s127f127");
00294
00295 s3f17_clone->copy_data(*s3f17);
00296 s14f9_clone->copy_data(*s14f9);
00297 s127f127_clone->copy_data(*s127f127);
00298
00299
00300
00301
00302 TEST_ASSERT(s3f17_clone->match(*s3f17, STRUCT), "s3f17 clone has same structure as s3f17 after data copy");
00303 TEST_ASSERT(s3f17_clone->match(*s3f17, DATA), "s3f17 clone has same data as s3f17 after data copy");
00304 TEST_ASSERT(s3f17_clone->match(*s3f17, ALL), "s3f17 clone matches s3f17 after data copy");
00305 TEST_ASSERT(s14f9_clone->match(*s14f9, STRUCT), "s14f9 clone has same structure as s14f9 after data copy");
00306 TEST_ASSERT(s14f9_clone->match(*s14f9, DATA), "s14f9 clone has same data as s14f9 after data copy");
00307 TEST_ASSERT(s14f9_clone->match(*s14f9, ALL), "s14f9 clone matches s14f9 after data copy");
00308 TEST_ASSERT(s127f127_clone->match(*s127f127, STRUCT), "s127f127 clone has same structure as s127f127 after data copy");
00309 TEST_ASSERT(s127f127_clone->match(*s127f127, DATA), "s127f127 clone has same data as s127f127 after data copy");
00310 TEST_ASSERT(s127f127_clone->match(*s127f127, ALL), "s127f127 clone matches s127f127 after data copy");
00311 }
00312
00313 test_t data_tests[] = {
00314 test_t( "deserialization test", test_deserialize ),
00315 test_t( "data access test", test_access ),
00316 test_t( "numeric data test", test_numeric ),
00317 test_t( "string data test", test_string ),
00318 test_t( "test message comparison", test_comparison ),
00319 test_t( "data asignment test", test_assignment ),
00320 test_t( "msg clone and data copy test", test_copy_data ),
00321 test_t(NULL, 0)
00322 };
00323
00324 test_suite_t data_suite("Data handling tests", data_tests);