#include "catch/catch.hpp" #include "lib/jetfix/codec/FIXFragment.h" #include "lib/jetfix/codec/Fields.h" #include "lib/utils/datetime.h" namespace bts { namespace jetfix { TEST_CASE ("JetFIX Field2Tests", "[jetfix]") { SECTION("char_tests") { char out[] = {0,0}; typedef CharField<123> Field; REQUIRE (Field::max_value_size ('A')==1); auto result = Field::write (out,'A'); REQUIRE (Field::tag_s==std::string ("123")); REQUIRE (Field::tag_sz==3); REQUIRE (Field::tag_checksum=='1'+'2'+'3'); REQUIRE (result.first==1); REQUIRE (result.second=='A'); REQUIRE (out[0]=='A'); REQUIRE (out[1]==0); } SECTION("bool_tests") { char out[] = {0,0}; typedef BoolField<9> Field; REQUIRE (Field::max_value_size (true)==1); REQUIRE (Field::tag_s==std::string ("9")); REQUIRE (Field::tag_sz==1); REQUIRE (Field::tag_checksum=='9'); auto result = Field::write (out,true); REQUIRE (result.first==1); REQUIRE (result.second=='Y'); REQUIRE (out[0]=='Y'); REQUIRE (out[1]==0); result = Field::write (out,false); REQUIRE (result.first==1); REQUIRE (result.second=='N'); REQUIRE (out[0]=='N'); REQUIRE (out[1]==0); } SECTION("float_tests") { std::string out (20, ' '); typedef FloatField<123> Field; REQUIRE (Field::max_value_size (1.2,-1,6)==6); auto result = Field::write (&out[0],1.2,-1,6); REQUIRE (result.first==6); REQUIRE (result.second=='0'+'0'+'0'+'1'+'.'+'2'); REQUIRE (out.compare (0,6,"0001.2") == 0); REQUIRE (out[6]=='\0'); } SECTION("int_tests") { std::string out (20, ' '); typedef IntField<123> Field; REQUIRE(Field::max_value_size (12,3)==3); auto result = Field::write (&out[0],12,3); REQUIRE (result.first==3); REQUIRE (result.second=='0'+'1'+'2'); REQUIRE (out.compare (0,3,"012") == 0); REQUIRE (out[4]==' '); } SECTION("string_tests") { std::string out (20, ' '); typedef StringField<123> Field; REQUIRE(Field::max_value_size (std::string_view{"hello"})==5); auto result = Field::write (&out[0],std::string_view{"hello"}); REQUIRE (result.first==5); REQUIRE (result.second=='h'+'e'+'l'+'l'+'o'); REQUIRE (out.compare (0,5,"hello") == 0); REQUIRE (out[6]==' '); } SECTION("ptime_tests") { std::string out (50, ' '); typedef TimeField<123> Field; const char*t_s = "20150427-19:05:18.922"; auto result = Field::write (&out[0],datetime::parse_FIX_timestamp (t_s)); REQUIRE (result.first==strlen (t_s)); REQUIRE (out.compare (0,strlen (t_s),t_s) == 0); REQUIRE (out[strlen (t_s)]==' '); } } TEST_CASE ("JetFIX FIXFragmentTests" , "[jetfix]") { SECTION("encoding_works") { FIXFragment g (0); g.add> ('a'); g.add> (1.2,-1,6); g.add> (std::string_view{"hello"}); g.add> (123,3); const char*t_s = "20150427-19:05:18.922"; g.add> (datetime::parse_FIX_timestamp (t_s)); g.add> (true); std::string out = g.to_str (); REQUIRE (out == "1=a" "\001" "2=0001.2" "\001" "3=hello" "\001" "4=123" "\001" "5=20150427-19:05:18.922" "\001" "6=Y" "\001"); uint32_t checksum = 0; for (auto c : out) checksum +=c; REQUIRE (checksum==g.checksum ()); } SECTION("bookmarking_works") { const char*t_s1 = "20150427-19:05:18.922"; const char*t_s2 = "20150427-19:05:18.923"; FIXFragment g; auto c_bm = g.add> ('a'); auto f_bm = g.add> (1.2,-1,6); auto s_bm = g.add> (std::string_view{"hello"}); auto i_bm = g.add> (123,3); auto t_bm = g.add> (datetime::parse_FIX_timestamp (t_s1)); auto b_bm = g.add> (true); g.overwrite (c_bm,'b'); g.overwrite (f_bm,1.3,-1,6); g.overwrite (s_bm,std::string_view {"olleh"}); g.overwrite (i_bm,321,3); g.overwrite (t_bm,(datetime::parse_FIX_timestamp (t_s2))); g.overwrite (b_bm,false); std::string out = g.to_str (); REQUIRE (out== "1=b" "\001" "2=0001.3" "\001" "3=olleh" "\001" "4=321" "\001" "5=20150427-19:05:18.923" "\001" "6=N" "\001"); uint32_t checksum = 0; for (auto c : out) checksum +=c; REQUIRE (checksum==g.checksum ()); } SECTION("append_works") { FIXFragment g1;; g1.add> ('a'); g1.add> (1.2,-1,6); g1.add> (std::string_view{"hello"}); auto chk1 = g1.checksum (); FIXFragment g2; g2.add> (std::string_view{"goodbye"}); g2.add> (321,3); auto chk2 = g2.checksum (); g1.append (g2); std::string out = g1.to_str (); REQUIRE (out== "1=a" "\001" "2=0001.2" "\001" "3=hello" "\001" "4=goodbye" "\001" "5=321" "\001"); REQUIRE (chk1+chk2==g1.checksum ()); } } }}