RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
soa/types/testing/id_test.cc
00001 /* id_test.cc
00002    Jeremy Barnes, 17 February 2012
00003    Copyright (c) 2012 Datacratic.  All rights reserved.
00004 
00005 */
00006 
00007 #define BOOST_TEST_MAIN
00008 #define BOOST_TEST_DYN_LINK
00009 
00010 #include <boost/test/unit_test.hpp>
00011 #include <iostream>
00012 #include "soa/types/id.h"
00013 #include "jml/db/persistent.h"
00014 #include "soa/types/date.h"
00015 
00016 using namespace ML;
00017 using namespace std;
00018 using namespace Datacratic;
00019 
00020 void checkSerializeReconstitute(Id id)
00021 {
00022     ostringstream oStream;
00023     {
00024         DB::Store_Writer oStore(oStream);
00025         id.serialize(oStore);
00026     }
00027 
00028     //cerr << "wrote " << oStream.str().length() << " characters" << endl;
00029     
00030     istringstream iStream(oStream.str());
00031     Id id2;
00032     {
00033         DB::Store_Reader iStore(iStream);
00034         id2.reconstitute(iStore);
00035         BOOST_CHECK_EQUAL(iStore.try_to_have(1), 0);
00036     }
00037 
00038     BOOST_CHECK_EQUAL(id.toString(), id2.toString());
00039     BOOST_CHECK_EQUAL(id, id2);
00040     BOOST_CHECK_EQUAL(id.type, id2.type);
00041 }
00042 
00043 BOOST_AUTO_TEST_CASE( test_basic_id )
00044 {
00045     Id id;
00046     id.parse("");
00047     BOOST_CHECK_EQUAL(id.type, Id::NONE);
00048     BOOST_CHECK_EQUAL(id.toString(), "");
00049     checkSerializeReconstitute(id);
00050 }
00051 
00052 BOOST_AUTO_TEST_CASE( test_uuid_id )
00053 {
00054     string uuid = "0828398c-5965-11e0-84c8-0026b937c8e1";
00055     Id id(uuid);
00056     BOOST_CHECK_EQUAL(id.type, Id::UUID);
00057     BOOST_CHECK_EQUAL(id.toString(), uuid);
00058     checkSerializeReconstitute(id);
00059 }
00060 
00061 BOOST_AUTO_TEST_CASE( test_goog64_id )
00062 {
00063     string s = "CAESEAYra3NIxLT9C8twKrzqaA";
00064     Id id(s);
00065     BOOST_CHECK_EQUAL(id.type, Id::GOOG128);
00066     BOOST_CHECK_EQUAL(id.toString(), s);
00067     checkSerializeReconstitute(id);
00068 }
00069 
00070 BOOST_AUTO_TEST_CASE( test_bigdec_id )
00071 {
00072     string s = "7394206091425759590";
00073     Id id(s);
00074     BOOST_CHECK_EQUAL(id.type, Id::BIGDEC);
00075     BOOST_CHECK_EQUAL(id.toString(), s);
00076     checkSerializeReconstitute(id);
00077 }
00078 
00079 BOOST_AUTO_TEST_CASE( test_bigdec_id2 )
00080 {
00081     string s = "394206091425759590";
00082     Id id(s);
00083     BOOST_CHECK_EQUAL(id.type, Id::BIGDEC);
00084     BOOST_CHECK_EQUAL(id.toString(), s);
00085     checkSerializeReconstitute(id);
00086 }
00087 
00088 BOOST_AUTO_TEST_CASE( test_bigdec_false_positive1 )
00089 {
00090     string s = "0394206091425759590";
00091     Id id(s);
00092     BOOST_CHECK_EQUAL(id.type, Id::STR);
00093     BOOST_CHECK_EQUAL(id.toString(), s);
00094     checkSerializeReconstitute(id);
00095 }
00096 
00097 BOOST_AUTO_TEST_CASE( test_bigdec_false_positive2 )
00098 {
00099     string s = "2321323942060989898676554598877575564564435434534354345734371425759590";
00100     Id id(s);
00101     BOOST_CHECK_EQUAL(id.type, Id::STR);
00102     BOOST_CHECK_EQUAL(id.toString(), s);
00103     checkSerializeReconstitute(id);
00104 }
00105 
00106 BOOST_AUTO_TEST_CASE( test_string_id )
00107 {
00108     string s = "hello";
00109     Id id(s);
00110     BOOST_CHECK_EQUAL(id.type, Id::STR);
00111     BOOST_CHECK_EQUAL(id.toString(), s);
00112     checkSerializeReconstitute(id);
00113 }
00114 
00115 BOOST_AUTO_TEST_CASE( test_string_id_copying )
00116 {
00117     string s = "hello";
00118     Id id(s);
00119     id = Id(s);
00120     BOOST_CHECK_EQUAL(id.type, Id::STR);
00121     BOOST_CHECK_EQUAL(id.toString(), s);
00122     checkSerializeReconstitute(id);
00123 }
00124 
00125 BOOST_AUTO_TEST_CASE( test_null_id )
00126 {
00127     string s = "null";
00128     Id id(s);
00129     id = Id(s);
00130     BOOST_CHECK_EQUAL(id.type, Id::NULLID);
00131     BOOST_CHECK_EQUAL(id.toString(), s);
00132     checkSerializeReconstitute(id);
00133 }
00134 
00135 BOOST_AUTO_TEST_CASE( test_empty_id )
00136 {
00137     string s = "";
00138     Id id(s);
00139     id = Id(s);
00140     BOOST_CHECK_EQUAL(id.type, Id::NONE);
00141     BOOST_CHECK_EQUAL(id.toString(), s);
00142     checkSerializeReconstitute(id);
00143 }
00144 
00145 BOOST_AUTO_TEST_CASE( test_id_basics )
00146 {
00147     Id id1("++++++++++++++++");
00148     BOOST_CHECK_EQUAL(id1.type, Id::BASE64_96);
00149     BOOST_CHECK(id1.val == 0);
00150 
00151     Id id2("+++++++++++++++/");
00152     BOOST_CHECK_EQUAL(id2.type, Id::BASE64_96);
00153     BOOST_CHECK(id2.val == 1);
00154 
00155     Id id3("+++++++++++++++0");
00156     BOOST_CHECK_EQUAL(id3.type, Id::BASE64_96);
00157     BOOST_CHECK(id3.val == 2);
00158 
00159     Id id4("++++/+++++++++++");
00160     BOOST_CHECK_EQUAL(id4.type, Id::BASE64_96);
00161     BOOST_CHECK(id4.val == __uint128_t(1) << (11 * 6));
00162     BOOST_CHECK_LT(id3, id4);
00163 }
00164 
00165 BOOST_AUTO_TEST_CASE( test_id )
00166 {
00167     string s1 = "++++VpWW999gvYaw";
00168     string s2 = "+++/uRXa99O0T0+w";
00169     string s3 = "+++0Rk1K99Oe/3aw";
00170     string s4 = "jDhUJMWW9997leCw";
00171     
00172     Id id1(s1);
00173     Id id2(s2);
00174     Id id3(s3);
00175     Id id4(s4);
00176 
00177     BOOST_CHECK_EQUAL(id1.type, Id::BASE64_96);
00178     BOOST_CHECK_EQUAL(id2.type, Id::BASE64_96);
00179     BOOST_CHECK_EQUAL(id3.type, Id::BASE64_96);
00180     BOOST_CHECK_EQUAL(id4.type, Id::BASE64_96);
00181 
00182     BOOST_CHECK_LT(id1, id2);
00183     BOOST_CHECK_LT(id2, id3);
00184     BOOST_CHECK_LT(id3, id4);
00185 
00186     BOOST_CHECK_EQUAL(id1.toString(), s1);
00187     BOOST_CHECK_EQUAL(id2.toString(), s2);
00188     BOOST_CHECK_EQUAL(id3.toString(), s3);
00189     BOOST_CHECK_EQUAL(id4.toString(), s4);
00190 
00191     checkSerializeReconstitute(id1);
00192     checkSerializeReconstitute(id2);
00193     checkSerializeReconstitute(id3);
00194     checkSerializeReconstitute(id4);
00195 }
00196 
00197 const char * testBkIds[] = {
00198     "++++VpWW999gvYaw",
00199     "+++/uRXa99O0T0+w",
00200     "+++0Rk1K99Oe/3aw",
00201     "+++19DxK99YV5GBw",
00202     "+++19WxK999BtX5w",
00203     "+++1qAxK99YIIKPm",
00204     "+++2EAxK99Yu23Nw",
00205     "+++2VhLR99On4X5w",
00206     "+++2crRq99OVf1jw",
00207     "+++5WeWW99ecqwam",
00208     "+++6cDWc99O02v2w",
00209     "+++6ulL499YhPo2w",
00210     "+++7cWxK999Mu1Jw",
00211     "+++8j/Aa99eKbLjw",
00212     "+++9/rz599eAuC5w",
00213     "+++B1vDa99YS4SHw",
00214     "+++BY06P99evCCOw",
00215     "+++CjdWj99YfgfHw",
00216     "+++EBY6K99O2IRJw",
00217     "+++FYKXj99OuNKjm",
00218     "+++FaEXq99YFhGkw",
00219     "+++GYAxK99YkyNjw",
00220     "+++H9eWW999bv15w",
00221     "+++HqWxK99YW3z5w",
00222     "+++IDa1K99Yyta2w",
00223     "+++IJ06K99YZs1Ow",
00224     "+++JHEXq99Yn1Hjw",
00225     "+++JYk1K99eKr15w",
00226     "+++PnB6D99YIgaHw",
00227     "+++RPeWW99OJWRBw",
00228     "+++SDZL499Yg/ajw",
00229     "+++TWAxK99YHZ22m",
00230     "+++TwCyE99YcPbOw",
00231     "+++V5WxK99eQWEPw",
00232     "+++VUcAU99YCJ98w",
00233     "+++WDUL499Y48tkw",
00234     "+++X1GTa999SqChw",
00235     "+++Xca1s99Ydndam",
00236     "+++a4CDS9999gJPw",
00237     "+++bx6Ga99enfDow",
00238     "+++cK+yK999sjMHw",
00239     "+++fS/6j99YjJ6Jw",
00240     "+++hqaXW99OmnE2w",
00241     "+++iwa9K99919h8w",
00242     "+++jOtL4999pU3Ow",
00243     "+++jnDxK99OTXlOw",
00244     "+++l/a1K999VVz5w",
00245     "+++mVXAK99OYEz5w",
00246     "+++mnaXW999YKa2w",
00247     "+++oPByK99YWgSjw",
00248     "+++ovRyW99eU2YNw",
00249     "+++pYk9K999tGtkw",
00250     "+++pqP1K99eeS6jw",
00251     "+++pwLRc99YtjEjw",
00252     "+++t/6xK99Ynh15w",
00253     "+++tJ3RR999e+Saw",
00254     "+++vwrRq99ORsX5w",
00255     "+++xUG6j99e/Xz5w",
00256     "++/+BJTs99emwkCw",
00257     "++/+s6xK99ebRZBw",
00258     "++//VGGP99OXrz5w",
00259     "++/0DGAa99YQu3kw",
00260     "++/0au6K99Ort5hm",
00261     "++/1O8Tq999wO05w",
00262     "++/1YTTO99Y/nLCw",
00263     "++/3jN1K99Yq015w",
00264     "++/4KdWj99Or57Jw",
00265     "++/4sIRq99O4AU+w",
00266     "++/7B7L49995IvHw",
00267     "++/7P56D999o10Ow",
00268     "++/7cNAa99Y8kz5w",
00269     "++/8D/Ta99Y25X5w",
00270     "++/9xTyW99YfSEBw",
00271     "++/ADUya99eTA3Cw",
00272     "++/C1EXq99eZyVBw",
00273     "++/Cq9Tj99YCMX5w",
00274     "++/Ds06P99OStX5w",
00275     "++/FPXxs99YHBq8w",
00276     "++/GD2yK99OF6z5w",
00277     "++/H9vQO9991pLjw",
00278     "++/HVR6j99OcczCw",
00279     "++/JRP9K99O/cXBw",
00280     "++/JRW/c99eaUj8w",
00281     "++/JRbQO99YGKzPw",
00282     "++/Oek9K99e86Maw"
00283 };
00284 
00285 int nBkIds = sizeof(testBkIds) / sizeof(testBkIds[0]);
00286 
00287 BOOST_AUTO_TEST_CASE( test_id_sorting )
00288 {
00289     Id prev;
00290     Id curr;
00291 
00292     for (unsigned i = 0;  i < nBkIds;  ++i) {
00293         string s = testBkIds[i];
00294         curr.parse(s);
00295         BOOST_CHECK_EQUAL(curr.type, Id::BASE64_96);
00296 
00297         BOOST_CHECK_LT(prev, curr);
00298         
00299         BOOST_CHECK_EQUAL(curr.toString(), s);
00300         checkSerializeReconstitute(curr);
00301 
00302         prev = curr;
00303     }
00304 }
00305 
00306 BOOST_AUTO_TEST_CASE( test_compound_id )
00307 {
00308     Id id(Id("hello"), Id("world"));
00309 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator