void test_string_basic() { std::random_device rd; std::mt19937 gen(rd()); for(size_t i=0; i<100; ++i) { std::basic_string<char> o_string = random_basic_string<char>(gen); std::basic_string<char> o_string2 = ""; std::ostringstream os; { OArchive oar(os); oar(o_string); oar(o_string2); } std::basic_string<char> i_string; std::basic_string<char> i_string2; std::istringstream is(os.str()); { IArchive iar(is); iar(i_string); iar(i_string2); } BOOST_CHECK_EQUAL(i_string, o_string); BOOST_CHECK_EQUAL(i_string2, o_string2); } }
void test_polymorphic() { std::random_device rd; std::mt19937 gen(rd()); auto rngB = [&](){ return random_value<int>( gen ) % 2 == 0; }; auto rngI = [&](){ return random_value<int>( gen ); }; auto rngF = [&](){ return random_value<float>( gen ); }; auto rngD = [&](){ return random_value<double>( gen ); }; for(int ii=0; ii<100; ++ii) { std::shared_ptr<PolyBase> o_shared = std::make_shared<PolyDerived>( rngI(), rngF(), rngB(), rngD() ); std::weak_ptr<PolyBase> o_weak = o_shared; std::unique_ptr<PolyBase> o_unique( new PolyDerived( rngI(), rngF(), rngB(), rngD() ) ); auto pda = std::make_shared<PolyDerivedLA>( rngI() ); pda->vec.emplace_back( std::make_shared<PolyDerivedLA>( rngI() ) ); std::shared_ptr<PolyLA> o_sharedLA = pda; std::ostringstream os; { OArchive oar(os); oar( o_shared, o_weak, o_unique ); oar( o_sharedLA ); } decltype(o_shared) i_shared; decltype(o_weak) i_weak; decltype(o_unique) i_unique; decltype(o_sharedLA) i_sharedLA; std::istringstream is(os.str()); { IArchive iar(is); iar( i_shared, i_weak, i_unique ); iar( i_sharedLA ); } auto i_locked = i_weak.lock(); auto o_locked = o_weak.lock(); auto i_sharedLA2 = i_sharedLA->shared_from_this(); BOOST_CHECK_EQUAL(i_shared.get(), i_locked.get()); BOOST_CHECK_EQUAL(*((PolyDerived*)i_shared.get()), *((PolyDerived*)o_shared.get())); BOOST_CHECK_EQUAL(*((PolyDerived*)i_shared.get()), *((PolyDerived*)i_locked.get())); BOOST_CHECK_EQUAL(*((PolyDerived*)i_locked.get()), *((PolyDerived*)o_locked.get())); BOOST_CHECK_EQUAL(*((PolyDerived*)i_unique.get()), *((PolyDerived*)o_unique.get())); BOOST_CHECK_EQUAL(*((PolyDerivedLA*)i_sharedLA.get()), *((PolyDerivedLA*)o_sharedLA.get())); BOOST_CHECK_EQUAL(*((PolyDerivedLA*)i_sharedLA2.get()), *((PolyDerivedLA*)o_sharedLA.get())); } }
template <class IArchive, class OArchive> inline void test_map_memory() { std::random_device rd; std::mt19937 gen(rd()); for(int ii=0; ii<100; ++ii) { std::map<int, std::unique_ptr<int>> o_uniqueptrMap; std::map<int, std::shared_ptr<int>> o_sharedptrMap; for(int j=0; j<100; ++j) { #ifdef CEREAL_OLDER_GCC o_uniqueptrMap.insert( std::make_pair(random_value<int>(gen), std::unique_ptr<int>( new int( random_value<int>(gen) ) )) ); o_sharedptrMap.insert( std::make_pair(random_value<int>(gen), std::make_shared<int>( random_value<int>(gen) )) ); #else // NOT CEREAL_OLDER_GCC o_uniqueptrMap.emplace( random_value<int>(gen), std::unique_ptr<int>( new int( random_value<int>(gen) ) ) ); o_sharedptrMap.emplace( random_value<int>(gen), std::make_shared<int>( random_value<int>(gen) ) ); #endif // NOT CEREAL_OLDER_GCC } std::ostringstream os; { OArchive oar(os); oar( o_uniqueptrMap ); oar( o_sharedptrMap ); } decltype( o_uniqueptrMap ) i_uniqueptrMap; decltype( o_sharedptrMap ) i_sharedptrMap; std::istringstream is(os.str()); { IArchive iar(is); iar( i_uniqueptrMap ); iar( i_sharedptrMap ); } CHECK_EQ(o_sharedptrMap.size(), i_sharedptrMap.size()); CHECK_EQ(o_uniqueptrMap.size(), i_uniqueptrMap.size()); auto o_v_it = o_uniqueptrMap.begin(); auto i_v_it = i_uniqueptrMap.begin(); for(;o_v_it != o_uniqueptrMap.end(); ++o_v_it, ++i_v_it) { CHECK_EQ(i_v_it->first, o_v_it->first); CHECK_EQ(*i_v_it->second, *o_v_it->second); } } }
void test_structs_minimal() { std::random_device rd; std::mt19937 gen(rd()); for(int ii=0; ii<100; ++ii) { TestStruct o_struct = { random_basic_string<char>(gen), random_value<double>(gen), random_value<std::uint32_t>(gen), random_value<uint8_t>(gen) % 2 ? true : false }; Issue79Struct o_struct2 = { random_value<std::int32_t>(gen) }; Issue79StructInternal o_struct3 = { random_value<std::int32_t>(gen) }; std::ostringstream os; { OArchive oar(os); oar( o_struct ); oar( o_struct2 ); oar( o_struct3 ); } decltype(o_struct) i_struct; decltype(o_struct2) i_struct2; decltype(o_struct3) i_struct3; std::istringstream is(os.str()); { IArchive iar(is); iar( i_struct ); iar( i_struct2 ); iar( i_struct3 ); } BOOST_CHECK(o_struct.mm.x == i_struct.mm.x); BOOST_CHECK_CLOSE(o_struct.mmv.x, i_struct.mmv.x, 1e-5); BOOST_CHECK(o_struct.nmm.x == i_struct.nmm.x); BOOST_CHECK(o_struct.nmmv.x == i_struct.nmmv.x); BOOST_CHECK(o_struct2.x == i_struct2.x); BOOST_CHECK(o_struct3.x == i_struct3.x); } }
/* Write catalog to binary file. */ void CatalogManager::WriteArchiveFile() { string file_name = path_ + "catalog"; ofstream ofs; ofs.open(file_name.c_str(), ios::binary); boost::archive::binary_oarchive oar( ofs ); oar << (*this); ofs.close(); }
void test_string_all() { std::random_device rd; std::mt19937 gen(rd()); for(size_t i=0; i<100; ++i) { std::basic_string<char> o_string = random_basic_string<char>(gen); std::basic_string<wchar_t> o_wstring = random_basic_string<wchar_t>(gen); std::basic_string<char16_t> o_u16string = random_basic_string<char16_t>(gen); std::basic_string<char32_t> o_u32string = random_basic_string<char32_t>(gen); std::ostringstream os; { OArchive oar(os); oar(o_string); oar(o_wstring); oar(o_u16string); oar(o_u32string); } std::basic_string<char> i_string; std::basic_string<wchar_t> i_wstring; std::basic_string<char16_t> i_u16string; std::basic_string<char32_t> i_u32string; std::istringstream is(os.str()); { IArchive iar(is); iar(i_string); iar(i_wstring); iar(i_u16string); iar(i_u32string); } BOOST_CHECK_EQUAL(i_string, o_string); BOOST_CHECK_EQUAL_COLLECTIONS(i_wstring.begin(), i_wstring.end(), o_wstring.begin(), o_wstring.end()); BOOST_CHECK_EQUAL_COLLECTIONS(i_u16string.begin(), i_u16string.end(), o_u16string.begin(), o_u16string.end()); BOOST_CHECK_EQUAL_COLLECTIONS(i_u32string.begin(), i_u32string.end(), o_u32string.begin(), o_u32string.end()); } }
TEST_F(TwoIndexGFTest,saveload) { namespace g=alps::gf; { alps::hdf5::archive oar("gf.h5","w"); gf(g::matsubara_index(4), g::index(1))=std::complex<double>(7., 3.); gf.save(oar,"/gf"); } { alps::hdf5::archive iar("gf.h5"); gf2.load(iar,"/gf"); } EXPECT_EQ(7, gf2(g::matsubara_index(4), g::index(1)).real()); EXPECT_EQ(3, gf2(g::matsubara_index(4), g::index(1)).imag()); { alps::hdf5::archive oar("gf.h5","rw"); oar["/gf/version/major"]<<7; EXPECT_THROW(gf2.load(oar,"/gf"),std::runtime_error); } EXPECT_EQ(7, gf2(g::matsubara_index(4), g::index(1)).real()); EXPECT_EQ(3, gf2(g::matsubara_index(4), g::index(1)).imag()); }
void test_pair() { std::random_device rd; std::mt19937 gen(rd()); auto rng = [&](){ return random_value<int>(gen); }; for(int ii=0; ii<100; ++ii) { std::pair<int, int> o_podpair = {rng(), rng()}; std::pair<StructInternalSerialize, StructInternalSerialize> o_iserpair = {{rng(), rng()}, {rng(), rng()}}; std::pair<StructInternalSplit, StructInternalSplit> o_isplpair = {{rng(), rng()}, {rng(), rng()}}; std::pair<StructExternalSerialize, StructExternalSerialize> o_eserpair = {{rng(), rng()}, {rng(), rng()}}; std::pair<StructExternalSplit, StructExternalSplit> o_esplpair = {{rng(), rng()}, {rng(), rng()}}; std::ostringstream os; { OArchive oar(os); oar(o_podpair); oar(o_iserpair); oar(o_isplpair); oar(o_eserpair); oar(o_esplpair); } std::pair<int, int> i_podpair; std::pair<StructInternalSerialize, StructInternalSerialize> i_iserpair; std::pair<StructInternalSplit, StructInternalSplit> i_isplpair; std::pair<StructExternalSerialize, StructExternalSerialize> i_eserpair; std::pair<StructExternalSplit, StructExternalSplit> i_esplpair; std::istringstream is(os.str()); { IArchive iar(is); iar(i_podpair); iar(i_iserpair); iar(i_isplpair); iar(i_eserpair); iar(i_esplpair); } BOOST_CHECK_EQUAL( i_podpair.first, o_podpair.first ); BOOST_CHECK_EQUAL( i_podpair.second, o_podpair.second ); BOOST_CHECK_EQUAL( i_iserpair.first, o_iserpair.first ); BOOST_CHECK_EQUAL( i_iserpair.second, o_iserpair.second ); BOOST_CHECK_EQUAL( i_isplpair.first, o_isplpair.first ); BOOST_CHECK_EQUAL( i_isplpair.second, o_isplpair.second ); BOOST_CHECK_EQUAL( i_eserpair.first, o_eserpair.first ); BOOST_CHECK_EQUAL( i_eserpair.second, o_eserpair.second ); BOOST_CHECK_EQUAL( i_esplpair.first, o_esplpair.first ); BOOST_CHECK_EQUAL( i_esplpair.second, o_esplpair.second ); } }
void test_memory_cycles() { std::random_device rd; std::mt19937 gen(rd()); for(int ii=0; ii<100; ++ii) { auto o_ptr1 = std::make_shared<MemoryCycle>( random_value<int>(gen) ); o_ptr1->ptr = o_ptr1; auto o_ptr2 = std::make_shared<MemoryCycleLoadAndConstruct>( random_value<int>(gen) ); o_ptr2->ptr = o_ptr2; std::ostringstream os; { OArchive oar(os); oar( o_ptr1 ); oar( o_ptr2 ); } decltype(o_ptr1) i_ptr1; decltype(o_ptr2) i_ptr2; std::istringstream is(os.str()); { IArchive iar(is); iar( i_ptr1 ); iar( i_ptr2 ); } BOOST_CHECK_EQUAL( o_ptr1->value, i_ptr1->value ); BOOST_CHECK_EQUAL( i_ptr1.get(), i_ptr1->ptr.lock().get() ); BOOST_CHECK_EQUAL( o_ptr2->value, i_ptr2->value ); BOOST_CHECK_EQUAL( i_ptr2.get(), i_ptr2->ptr.lock().get() ); } }
void MainWindow::fileParsed() { Parser2::ParseFile* parser(qobject_cast<Parser2::ParseFile*>(sender())); QStringList errors(parser->errors()); if (!errors.isEmpty()) { QMsgBox::warning(0, "IQmol", errors.join("\n")); } Data::Bank* bank(parser->takeData()); qDebug() << "-------- Dumping read data ------------"; bank->dump(); qDebug() << "-------- Serialization Test ------------"; { qDebug() << "Saving data to test.iqmol"; Parser2::IQmol iqmol; iqmol.saveData("test.iqmol", *bank); qDebug() << "Finished with .iqmol archive write"; } // Regular text archive { std::ofstream ofs("SerializationTest"); IQmol::Data::OutputArchive oar(ofs); bank->serialize(oar, 0); qDebug() << "Finished with archive write"; qDebug(); qDebug(); } delete bank; qDebug() << "Finished deleting Bank"; { IQmol::Data::Factory& factory = IQmol::Data::Factory::instance(); IQmol::Data::Base* base = factory.create(IQmol::Data::Type::Bank); std::ifstream ifs("SerializationTest"); IQmol::Data::InputArchive iar(ifs); qDebug() << "Bank archive read from file:"; base->serialize(iar); base->dump(); delete base; } parser->deleteLater(); }
template <class IArchive, class OArchive> inline void test_structs() { std::random_device rd; std::mt19937 gen(rd()); for(int ii=0; ii<100; ++ii) { StructInternalSerialize o_iser = { random_value<int>(gen), random_value<int>(gen) }; StructInternalSplit o_ispl = { random_value<int>(gen), random_value<int>(gen) }; StructExternalSerialize o_eser = { random_value<int>(gen), random_value<int>(gen) }; StructExternalSplit o_espl = { random_value<int>(gen), random_value<int>(gen) }; std::ostringstream os; { OArchive oar(os); oar( o_iser, o_ispl, o_eser, o_espl); } StructInternalSerialize i_iser; StructInternalSplit i_ispl; StructExternalSerialize i_eser; StructExternalSplit i_espl; std::istringstream is(os.str()); { IArchive iar(is); iar( i_iser, i_ispl, i_eser, i_espl); } CHECK_EQ(i_iser, o_iser); CHECK_EQ(i_ispl, o_ispl); CHECK_EQ(i_eser, o_eser); CHECK_EQ(i_espl, o_espl); } }
int main(int argc, char* argv[]) { google::InitGoogleLogging(argv[0]); std::string corpus_filepath("/data/Corpus/wikipedia_dump/de_wiki_sen_tokenized_lower_per_line.txt"); int wordvec_dim = 100; int window_size = 11; float learning_rate = 0.025; int skipgram = 1; int hs = 1; int negative = 10; float sample = 1e-5; int num_iters = 10; int num_threads = 4; int verbose = 1; Corpus &corpus = Corpus::getCorpus(corpus_filepath); try{ /* corpus.build_vocab(); corpus.save_vocab(std::string("cbow_vocab.txt")); */ corpus.load_vocab(std::string("cbow_vocab.txt")); corpus.create_huffman_tree(); } catch (std::exception& e) { LOG(FATAL) << e.what(); } Word2Vec model(corpus, wordvec_dim, window_size, learning_rate, skipgram, hs, negative, sample, num_iters, num_threads, verbose); model.start_train(); LOG(INFO) << "Training done!"; { const char* name = "word2vec_sg_trained_model.bin"; std::ofstream out_stream(name); boost::archive::binary_oarchive oar(out_stream); oar << model; out_stream.close(); } model.export_vectors(std::string("cbow_word_vectors_save.bin")); return 0; }
TEST_F(ThreeIndexTestGF, TailSaveLoad) { namespace g=alps::gf; typedef g::two_index_gf<double, g::momentum_index_mesh, g::index_mesh> density_matrix_type; density_matrix_type denmat=density_matrix_type(g::momentum_index_mesh(get_data_for_momentum_mesh()), g::index_mesh(nspins)); // prepare diagonal matrix double U=3.0; denmat.initialize(); for (g::momentum_index i=g::momentum_index(0); i<denmat.mesh1().extent(); ++i) { denmat(i,g::index(0))=0.5*U; denmat(i,g::index(1))=0.5*U; } // Attach a tail to the GF int order=0; // FIXME: TODO: gf.set_tail(min_order, max_order, denmat, ...); g::omega_k_sigma_gf_with_tail gft(gf); g::omega_k_sigma_gf_with_tail gft2(gft); EXPECT_EQ(g::TAIL_NOT_SET,gft.min_tail_order()); EXPECT_EQ(g::TAIL_NOT_SET,gft.max_tail_order()); gft.set_tail(order, denmat); EXPECT_EQ(0,gft.min_tail_order()); EXPECT_EQ(0,gft.max_tail_order()); EXPECT_EQ(0,(denmat-gft.tail(0)).norm()); { alps::hdf5::archive oar("gft.h5","w"); gft(g::matsubara_index(4),g::momentum_index(3), g::index(1))=std::complex<double>(7., 3.); gft.save(oar,"/gft"); } { alps::hdf5::archive iar("gft.h5"); gft2.load(iar,"/gft"); } EXPECT_EQ(gft2.tail().size(), gft.tail().size()) << "Tail size mismatch"; EXPECT_NEAR(0, (gft.tail(0)-gft2.tail(0)).norm(), 1E-8)<<"Tail loaded differs from tail stored"; EXPECT_EQ(7, gft2(g::matsubara_index(4),g::momentum_index(3), g::index(1)).real()) << "GF real part mismatch"; EXPECT_EQ(3, gft2(g::matsubara_index(4),g::momentum_index(3), g::index(1)).imag()) << "GF imag part mismatch"; }
TEST_F(GreensFunctionTailTest, TailSaveLoad) { typedef gfns::greenf<std::complex<double>, gfns::matsubara_mesh<gfns::mesh::POSITIVE_ONLY>, gfns::index_mesh> omega_sigma_gf; typedef gfns::gf_tail<omega_sigma_gf, gfns::greenf<double, gfns::index_mesh> > omega_sigma_gf_with_tail; typedef gfns::greenf<double, gfns::index_mesh> density_matrix_type; density_matrix_type denmat = density_matrix_type(gfns::index_mesh(nspins)); omega_sigma_gf gf(gfns::matsubara_positive_mesh(beta,nfreq), alps::gf::index_mesh(nspins)); // prepare diagonal matrix double U=3.0; denmat.initialize(); denmat(gfns::index(0))=0.5*U; denmat(gfns::index(1))=0.5*U; // Attach a tail to the GF int order=0; omega_sigma_gf_with_tail gft(gf); omega_sigma_gf_with_tail gft2(gft); EXPECT_EQ(gfns::TAIL_NOT_SET,gft.min_tail_order()); EXPECT_EQ(gfns::TAIL_NOT_SET,gft.max_tail_order()); gft.set_tail(order, denmat); EXPECT_EQ(0,gft.min_tail_order()); EXPECT_EQ(0,gft.max_tail_order()); EXPECT_EQ(0,(denmat - gft.tail(0)).norm()); { alps::hdf5::archive oar("gf_2i_tailsaveload.h5","w"); gft(gfns::matsubara_index(4),gfns::index(1))=std::complex<double>(7., 3.); oar["/gft"] << gft; } { alps::hdf5::archive iar("gf_2i_tailsaveload.h5"); iar["/gft"] >> gft2; } EXPECT_EQ(gft2.tail().size(), gft.tail().size()) << "Tail size mismatch"; EXPECT_NEAR(0, (gft.tail(0)-gft2.tail(0)).norm(), 1E-8)<<"Tail loaded differs from tail stored"; EXPECT_EQ(7, gft2(gfns::matsubara_index(4), gfns::index(1)).real()) << "GF real part mismatch"; EXPECT_EQ(3, gft2(gfns::matsubara_index(4), gfns::index(1)).imag()) << "GF imag part mismatch"; }
TEST_F(TwoIndexGFTest, TailSaveLoad) { namespace g=alps::gf; typedef g::one_index_gf<double, g::index_mesh> density_matrix_type; density_matrix_type denmat=density_matrix_type(g::index_mesh(nspins)); // prepare diagonal matrix double U=3.0; denmat.initialize(); denmat(g::index(0))=0.5*U; denmat(g::index(1))=0.5*U; // Attach a tail to the GF int order=0; g::omega_sigma_gf_with_tail gft(gf); g::omega_sigma_gf_with_tail gft2(gft); EXPECT_EQ(g::TAIL_NOT_SET,gft.min_tail_order()); EXPECT_EQ(g::TAIL_NOT_SET,gft.max_tail_order()); gft.set_tail(order, denmat); EXPECT_EQ(0,gft.min_tail_order()); EXPECT_EQ(0,gft.max_tail_order()); EXPECT_EQ(0,(denmat-gft.tail(0)).norm()); { alps::hdf5::archive oar("gft.h5","w"); gft(g::matsubara_index(4),g::index(1))=std::complex<double>(7., 3.); gft.save(oar,"/gft"); } { alps::hdf5::archive iar("gft.h5"); gft2.load(iar,"/gft"); } EXPECT_EQ(gft2.tail().size(), gft.tail().size()) << "Tail size mismatch"; EXPECT_NEAR(0, (gft.tail(0)-gft2.tail(0)).norm(), 1E-8)<<"Tail loaded differs from tail stored"; EXPECT_EQ(7, gft2(g::matsubara_index(4), g::index(1)).real()) << "GF real part mismatch"; EXPECT_EQ(3, gft2(g::matsubara_index(4), g::index(1)).imag()) << "GF imag part mismatch"; }
void test_structs_specialized() { std::random_device rd; std::mt19937 gen(rd()); for(int ii=0; ii<100; ++ii) { SpecializedMSerialize o_iser = { random_value<int>(gen) }; SpecializedMSerializeVersioned o_iserv = { random_value<int>(gen) }; SpecializedMSplit o_ispl = { random_value<int>(gen) }; SpecializedMSplitVersioned o_isplv = { random_value<int>(gen) }; // added re: issue #180 std::shared_ptr<BogusBasePolymorphic> o_shared_ispl = std::make_shared<SpecializedMSplitPolymorphic>( random_value<int>(gen) ); SpecializedMSplitMinimal o_isplm = { random_value<int>(gen) }; SpecializedMSplitVersionedMinimal o_isplvm = { random_value<int>(gen) }; SpecializedNMSerialize o_eser = { random_value<int>(gen) }; SpecializedNMSerializeVersioned o_eserv = { random_value<int>(gen) }; SpecializedNMSplit o_espl = { random_value<int>(gen) }; SpecializedNMSplitVersioned o_esplv = { random_value<int>(gen) }; SpecializedNMSplitMinimal o_esplm = { random_value<int>(gen) }; SpecializedNMSplitVersionedMinimal o_esplvm = { random_value<int>(gen) }; std::ostringstream os; { OArchive oar(os); oar( o_iser, o_iserv, o_ispl, o_isplv, o_shared_ispl, o_isplm, o_isplvm, o_eser, o_eserv, o_espl, o_esplv, o_esplm, o_esplvm ); } decltype(o_iser) i_iser; decltype(o_iserv) i_iserv; decltype(o_ispl) i_ispl; decltype(o_isplv) i_isplv; decltype(o_shared_ispl) i_shared_ispl; decltype(o_isplm) i_isplm; decltype(o_isplvm) i_isplvm; decltype(o_eser) i_eser; decltype(o_eserv) i_eserv; decltype(o_espl) i_espl; decltype(o_esplv) i_esplv; decltype(o_esplm) i_esplm; decltype(o_esplvm) i_esplvm; std::istringstream is(os.str()); { IArchive iar(is); iar( i_iser, i_iserv, i_ispl, i_isplv, i_shared_ispl, i_isplm, i_isplvm, i_eser, i_eserv, i_espl, i_esplv, i_esplm, i_esplvm ); } BOOST_CHECK(i_iser.x == o_iser.x); BOOST_CHECK(i_iserv.x == o_iserv.x); BOOST_CHECK(i_ispl.x == o_ispl.x); BOOST_CHECK(i_isplv.x == o_isplv.x); BOOST_CHECK_EQUAL(((SpecializedMSplitPolymorphic*)i_shared_ispl.get())->x, ((SpecializedMSplitPolymorphic*)o_shared_ispl.get())->x); BOOST_CHECK(i_isplm.x == o_isplm.x); BOOST_CHECK(i_isplvm.x == o_isplvm.x); BOOST_CHECK(i_eser.x == o_eser.x); BOOST_CHECK(i_eserv.x == o_eserv.x); BOOST_CHECK(i_espl.x == o_espl.x); BOOST_CHECK(i_esplv.x == o_esplv.x); BOOST_CHECK(i_esplm.x == o_esplm.x); BOOST_CHECK(i_esplvm.x == o_esplvm.x); } }
void test_unordered_loads() { std::random_device rd; std::mt19937 gen(rd()); auto rngB = [&](){ return random_value<int>( gen ) % 2 == 0; }; auto rngI = [&](){ return random_value<int>( gen ); }; auto rngF = [&](){ return random_value<float>( gen ); }; auto rngD = [&](){ return random_value<double>( gen ); }; auto rngS = [&](){ return random_basic_string<char>( gen ); }; for(int ii=0; ii<100; ++ii) { auto const name1 = rngS(); auto const name2 = rngS(); auto const name3 = rngS(); auto const name4 = rngS(); auto const name5 = rngS(); auto const name6 = rngS(); auto const name7 = rngS(); int o_int1 = rngI(); double o_double2 = rngD(); std::vector<bool> o_vecbool3 = { rngB(), rngB(), rngB(), rngB(), rngB() }; int o_int4 = rngI(); int o_int5 = rngI(); int o_int6 = rngI(); std::pair<float, unordered_naming> o_un7; o_un7.first = rngF(); o_un7.second.x = rngI(); o_un7.second.y = rngI(); o_un7.second.z = rngI(); std::ostringstream os; { OArchive oar(os); oar( cereal::make_nvp( name1, o_int1 ), cereal::make_nvp( name2, o_double2 ), cereal::make_nvp( name3, o_vecbool3 ), cereal::make_nvp( name4, o_int4 ), cereal::make_nvp( name5, o_int5 ), cereal::make_nvp( name6, o_int6 ), cereal::make_nvp( name7, o_un7 ) ); } decltype(o_int1) i_int1; decltype(o_double2) i_double2; decltype(o_vecbool3) i_vecbool3; decltype(o_int4) i_int4; decltype(o_int5) i_int5; decltype(o_int6) i_int6; decltype(o_un7) i_un7; std::istringstream is(os.str()); { IArchive iar(is); iar( cereal::make_nvp( name7, i_un7 ), cereal::make_nvp( name2, i_double2 ), cereal::make_nvp( name4, i_int4 ), cereal::make_nvp( name3, i_vecbool3 ), cereal::make_nvp( name1, i_int1 ), cereal::make_nvp( name5, i_int5 ), i_int6 ); } BOOST_CHECK_EQUAL(o_int1, i_int1); BOOST_CHECK_CLOSE(o_double2 , o_double2, 1e-5); BOOST_CHECK_EQUAL(o_vecbool3.size(), i_vecbool3.size()); BOOST_CHECK_EQUAL_COLLECTIONS(i_vecbool3.begin(), i_vecbool3.end(), o_vecbool3.begin(), o_vecbool3.end()); BOOST_CHECK_EQUAL(o_int4, i_int4); BOOST_CHECK_EQUAL(o_int5, i_int5); BOOST_CHECK_EQUAL(o_int6, i_int6); BOOST_CHECK_EQUAL(o_un7.first, i_un7.first); BOOST_CHECK_EQUAL(o_un7.second, i_un7.second); } }
void test_valarray() { std::random_device rd; std::mt19937 gen(rd()); for (int ii = 0; ii<100; ++ii) { std::valarray<int> o_podvalarray(100); for (auto & elem : o_podvalarray) elem = random_value<int>(gen); std::valarray<StructInternalSerialize> o_iservalarray(100); for (auto & elem : o_iservalarray) elem = StructInternalSerialize(random_value<int>(gen), random_value<int>(gen)); std::valarray<StructInternalSplit> o_isplvalarray(100); for (auto & elem : o_isplvalarray) elem = StructInternalSplit(random_value<int>(gen), random_value<int>(gen)); std::valarray<StructExternalSerialize> o_eservalarray(100); for (auto & elem : o_eservalarray) elem = StructExternalSerialize(random_value<int>(gen), random_value<int>(gen)); std::valarray<StructExternalSplit> o_esplvalarray(100); for (auto & elem : o_esplvalarray) elem = StructExternalSplit(random_value<int>(gen), random_value<int>(gen)); std::ostringstream os; { OArchive oar(os); oar(o_podvalarray); oar(o_iservalarray); oar(o_isplvalarray); oar(o_eservalarray); oar(o_esplvalarray); } std::valarray<int> i_podvalarray; std::valarray<StructInternalSerialize> i_iservalarray; std::valarray<StructInternalSplit> i_isplvalarray; std::valarray<StructExternalSerialize> i_eservalarray; std::valarray<StructExternalSplit> i_esplvalarray; std::istringstream is(os.str()); { IArchive iar(is); iar(i_podvalarray); iar(i_iservalarray); iar(i_isplvalarray); iar(i_eservalarray); iar(i_esplvalarray); } BOOST_CHECK_EQUAL(i_podvalarray.size(), o_podvalarray.size()); BOOST_CHECK_EQUAL(i_iservalarray.size(), o_iservalarray.size()); BOOST_CHECK_EQUAL(i_isplvalarray.size(), o_isplvalarray.size()); BOOST_CHECK_EQUAL(i_eservalarray.size(), o_eservalarray.size()); BOOST_CHECK_EQUAL(i_esplvalarray.size(), o_esplvalarray.size()); BOOST_CHECK_EQUAL_COLLECTIONS(std::begin(i_podvalarray), std::end(i_podvalarray), std::begin(o_podvalarray), std::end(o_podvalarray)); BOOST_CHECK_EQUAL_COLLECTIONS(std::begin(i_iservalarray), std::end(i_iservalarray), std::begin(o_iservalarray), std::end(o_iservalarray)); BOOST_CHECK_EQUAL_COLLECTIONS(std::begin(i_isplvalarray), std::end(i_isplvalarray), std::begin(o_isplvalarray), std::end(o_isplvalarray)); BOOST_CHECK_EQUAL_COLLECTIONS(std::begin(i_eservalarray), std::end(i_eservalarray), std::begin(o_eservalarray), std::end(o_eservalarray)); BOOST_CHECK_EQUAL_COLLECTIONS(std::begin(i_esplvalarray), std::end(i_esplvalarray), std::begin(o_esplvalarray), std::end(o_esplvalarray)); } }
// Last parameter exists to keep everything hidden in options template <class IArchive, class OArchive> inline void test_endian_serialization( typename IArchive::Options const & iOptions, typename OArchive::Options const & oOptions, const std::uint8_t inputLittleEndian ) { std::random_device rd; std::mt19937 gen(rd()); for(size_t i=0; i<100; ++i) { bool o_bool = random_value<uint8_t>(gen) % 2 ? true : false; uint8_t o_uint8 = random_value<uint8_t>(gen); int8_t o_int8 = random_value<int8_t>(gen); uint16_t o_uint16 = random_value<uint16_t>(gen); int16_t o_int16 = random_value<int16_t>(gen); uint32_t o_uint32 = random_value<uint32_t>(gen); int32_t o_int32 = random_value<int32_t>(gen); uint64_t o_uint64 = random_value<uint64_t>(gen); int64_t o_int64 = random_value<int64_t>(gen); float o_float = random_value<float>(gen); double o_double = random_value<double>(gen); std::vector<int32_t> o_vector(100); for(auto & elem : o_vector) elem = random_value<uint32_t>(gen); std::ostringstream os; { OArchive oar(os, oOptions); oar(o_bool); oar(o_uint8); oar(o_int8); oar(o_uint16); oar(o_int16); oar(o_uint32); oar(o_int32); oar(o_uint64); oar(o_int64); oar(o_float); oar(o_double); // We can't test vector directly here since we are artificially interfering with the endianness, // which can result in the size being incorrect oar(cereal::binary_data( o_vector.data(), static_cast<std::size_t>( o_vector.size() * sizeof(int32_t) ) )); } bool i_bool = false; uint8_t i_uint8 = 0; int8_t i_int8 = 0; uint16_t i_uint16 = 0; int16_t i_int16 = 0; uint32_t i_uint32 = 0; int32_t i_int32 = 0; uint64_t i_uint64 = 0; int64_t i_int64 = 0; float i_float = 0; double i_double = 0; std::vector<int32_t> i_vector(100); std::istringstream is(os.str()); { IArchive iar(is, iOptions); iar(i_bool); iar(i_uint8); iar(i_int8); iar(i_uint16); iar(i_int16); iar(i_uint32); iar(i_int32); iar(i_uint64); iar(i_int64); iar(i_float); iar(i_double); iar(cereal::binary_data( i_vector.data(), static_cast<std::size_t>( i_vector.size() * sizeof(int32_t) ) )); } // Convert to big endian if we expect to read big and didn't start big if( cereal::portable_binary_detail::is_little_endian() ^ inputLittleEndian ) // Convert to little endian if { CEREAL_TEST_SWAP_OUTPUT for( auto & val : o_vector ) swapBytes(val); } CEREAL_TEST_CHECK_EQUAL check_collection(i_vector, o_vector); }
template <class IArchive, class OArchive> inline void test_valarray() { std::random_device rd; std::mt19937 gen(rd()); for (int ii = 0; ii<100; ++ii) { std::valarray<int> o_podvalarray(100); for (auto & elem : o_podvalarray) elem = random_value<int>(gen); std::valarray<StructInternalSerialize> o_iservalarray(100); for (auto & elem : o_iservalarray) elem = StructInternalSerialize(random_value<int>(gen), random_value<int>(gen)); std::valarray<StructInternalSplit> o_isplvalarray(100); for (auto & elem : o_isplvalarray) elem = StructInternalSplit(random_value<int>(gen), random_value<int>(gen)); std::valarray<StructExternalSerialize> o_eservalarray(100); for (auto & elem : o_eservalarray) elem = StructExternalSerialize(random_value<int>(gen), random_value<int>(gen)); std::valarray<StructExternalSplit> o_esplvalarray(100); for (auto & elem : o_esplvalarray) elem = StructExternalSplit(random_value<int>(gen), random_value<int>(gen)); std::ostringstream os; { OArchive oar(os); oar(o_podvalarray); oar(o_iservalarray); oar(o_isplvalarray); oar(o_eservalarray); oar(o_esplvalarray); } std::valarray<int> i_podvalarray; std::valarray<StructInternalSerialize> i_iservalarray; std::valarray<StructInternalSplit> i_isplvalarray; std::valarray<StructExternalSerialize> i_eservalarray; std::valarray<StructExternalSplit> i_esplvalarray; std::istringstream is(os.str()); { IArchive iar(is); iar(i_podvalarray); iar(i_iservalarray); iar(i_isplvalarray); iar(i_eservalarray); iar(i_esplvalarray); } CHECK_EQ(i_podvalarray.size(), o_podvalarray.size()); CHECK_EQ(i_iservalarray.size(), o_iservalarray.size()); CHECK_EQ(i_isplvalarray.size(), o_isplvalarray.size()); CHECK_EQ(i_eservalarray.size(), o_eservalarray.size()); CHECK_EQ(i_esplvalarray.size(), o_esplvalarray.size()); check_collection(i_podvalarray , o_podvalarray ); check_collection(i_iservalarray, o_iservalarray); check_collection(i_isplvalarray, o_isplvalarray); check_collection(i_eservalarray, o_eservalarray); check_collection(i_esplvalarray, o_esplvalarray); } }
void test_stack() { std::random_device rd; std::mt19937 gen(rd()); for(int ii=0; ii<100; ++ii) { std::stack<int> o_podstack; for(int j=0; j<100; ++j) o_podstack.push(random_value<int>(gen)); std::stack<StructInternalSerialize> o_iserstack; for(int j=0; j<100; ++j) o_iserstack.push({ random_value<int>(gen), random_value<int>(gen) }); std::stack<StructInternalSplit> o_isplstack; for(int j=0; j<100; ++j) o_isplstack.push({ random_value<int>(gen), random_value<int>(gen) }); std::stack<StructExternalSerialize> o_eserstack; for(int j=0; j<100; ++j) o_eserstack.push({ random_value<int>(gen), random_value<int>(gen) }); std::stack<StructExternalSplit> o_esplstack; for(int j=0; j<100; ++j) o_esplstack.push({ random_value<int>(gen), random_value<int>(gen) }); std::ostringstream os; { OArchive oar(os); oar(o_podstack); oar(o_iserstack); oar(o_isplstack); oar(o_eserstack); oar(o_esplstack); } std::stack<int> i_podstack; std::stack<StructInternalSerialize> i_iserstack; std::stack<StructInternalSplit> i_isplstack; std::stack<StructExternalSerialize> i_eserstack; std::stack<StructExternalSplit> i_esplstack; std::istringstream is(os.str()); { IArchive iar(is); iar(i_podstack); iar(i_iserstack); iar(i_isplstack); iar(i_eserstack); iar(i_esplstack); } auto & i_podstack_c = cereal::stack_detail::container(i_podstack); auto & i_iserstack_c = cereal::stack_detail::container(i_iserstack); auto & i_isplstack_c = cereal::stack_detail::container(i_isplstack); auto & i_eserstack_c = cereal::stack_detail::container(i_eserstack); auto & i_esplstack_c = cereal::stack_detail::container(i_esplstack); auto & o_podstack_c = cereal::stack_detail::container(o_podstack); auto & o_iserstack_c = cereal::stack_detail::container(o_iserstack); auto & o_isplstack_c = cereal::stack_detail::container(o_isplstack); auto & o_eserstack_c = cereal::stack_detail::container(o_eserstack); auto & o_esplstack_c = cereal::stack_detail::container(o_esplstack); BOOST_CHECK_EQUAL_COLLECTIONS(i_podstack_c.begin(), i_podstack_c.end(), o_podstack_c.begin(), o_podstack_c.end()); BOOST_CHECK_EQUAL_COLLECTIONS(i_iserstack_c.begin(), i_iserstack_c.end(), o_iserstack_c.begin(), o_iserstack_c.end()); BOOST_CHECK_EQUAL_COLLECTIONS(i_isplstack_c.begin(), i_isplstack_c.end(), o_isplstack_c.begin(), o_isplstack_c.end()); BOOST_CHECK_EQUAL_COLLECTIONS(i_eserstack_c.begin(), i_eserstack_c.end(), o_eserstack_c.begin(), o_eserstack_c.end()); BOOST_CHECK_EQUAL_COLLECTIONS(i_esplstack_c.begin(), i_esplstack_c.end(), o_esplstack_c.begin(), o_esplstack_c.end()); } }
template <class IArchive, class OArchive> inline void test_map() { std::random_device rd; std::mt19937 gen(rd()); for(int ii=0; ii<100; ++ii) { std::map<size_t, std::vector<StructInternalSerialize>> o_vectormap; for(int j=0; j<10; ++j) { size_t id = random_value<size_t>(gen); for(int k=0; k<100; ++k) o_vectormap[id].emplace_back(random_value<int>(gen), random_value<int>(gen)); } std::map<std::string, int> o_podmap; for(int j=0; j<100; ++j) o_podmap.insert({random_value<std::string>(gen), random_value<int>(gen)}); std::map<int, StructInternalSerialize> o_isermap; for(int j=0; j<100; ++j) o_isermap.insert({random_value<int>(gen), { random_value<int>(gen), random_value<int>(gen) }}); std::map<int, StructInternalSplit> o_isplmap; for(int j=0; j<100; ++j) o_isplmap.insert({random_value<int>(gen), { random_value<int>(gen), random_value<int>(gen) }}); std::map<uint32_t, StructExternalSerialize> o_esermap; for(int j=0; j<100; ++j) o_esermap.insert({random_value<uint32_t>(gen), { random_value<int>(gen), random_value<int>(gen) }}); std::map<int8_t, StructExternalSplit> o_esplmap; for(int j=0; j<100; ++j) o_esplmap.insert({random_value<char>(gen), { random_value<int>(gen), random_value<int>(gen) }}); std::ostringstream os; { OArchive oar(os); oar(o_vectormap); oar(o_podmap); oar(o_isermap); oar(o_isplmap); oar(o_esermap); oar(o_esplmap); } std::map<size_t, std::vector<StructInternalSerialize>> i_vectormap; std::map<std::string, int> i_podmap; std::map<int, StructInternalSerialize> i_isermap; std::map<int, StructInternalSplit> i_isplmap; std::map<uint32_t, StructExternalSerialize> i_esermap; std::map<int8_t, StructExternalSplit> i_esplmap; std::istringstream is(os.str()); { IArchive iar(is); iar(i_vectormap); iar(i_podmap); iar(i_isermap); iar(i_isplmap); iar(i_esermap); iar(i_esplmap); } CHECK_EQ(i_vectormap.size(), o_vectormap.size()); auto o_v_it = o_vectormap.begin(); auto i_v_it = i_vectormap.begin(); for(;o_v_it != o_vectormap.end(); ++o_v_it, ++i_v_it) { CHECK_EQ(i_v_it->second.size(), o_v_it->second.size()); check_collection(i_v_it->second, o_v_it->second); } check_collection(i_podmap, o_podmap); check_collection(i_isermap, o_isermap); check_collection(i_isplmap, o_isplmap); check_collection(i_esermap, o_esermap); check_collection(i_esplmap, o_esplmap); } }
void test_polymorphic() { std::random_device rd; std::mt19937 gen(rd()); auto rngB = [&](){ return random_value<int>( gen ) % 2 == 0; }; auto rngI = [&](){ return random_value<int>( gen ); }; auto rngL = [&](){ return random_value<long>( gen ); }; auto rngF = [&](){ return random_value<float>( gen ); }; auto rngD = [&](){ return random_value<double>( gen ); }; for(int ii=0; ii<100; ++ii) { std::shared_ptr<PolyBase> o_shared = std::make_shared<PolyDerived>( rngI(), rngF(), rngB(), rngD() ); std::weak_ptr<PolyBase> o_weak = o_shared; std::unique_ptr<PolyBase> o_unique( new PolyDerived( rngI(), rngF(), rngB(), rngD() ) ); std::shared_ptr<PolyBaseA> o_sharedA = std::make_shared<PolyDerivedD>( random_basic_string<char>(gen), rngD(), rngI(), rngL() ); std::weak_ptr<PolyBaseA> o_weakA = o_sharedA; std::unique_ptr<PolyBaseA> o_uniqueA( new PolyDerivedD( random_basic_string<char>(gen), rngD(), rngI(), rngL() ) ); auto pda = std::make_shared<PolyDerivedLA>( rngI() ); pda->vec.emplace_back( std::make_shared<PolyDerivedLA>( rngI() ) ); std::shared_ptr<PolyLA> o_sharedLA = pda; std::ostringstream os; { OArchive oar(os); oar( o_shared, o_weak, o_unique ); oar( o_sharedLA ); oar( o_sharedA, o_weakA, o_uniqueA ); } decltype(o_shared) i_shared; decltype(o_weak) i_weak; decltype(o_unique) i_unique; decltype(o_sharedLA) i_sharedLA; decltype(o_sharedA) i_sharedA; decltype(o_weakA) i_weakA; decltype(o_uniqueA) i_uniqueA; std::istringstream is(os.str()); { IArchive iar(is); iar( i_shared, i_weak, i_unique ); iar( i_sharedLA ); iar( i_sharedA, i_weakA, i_uniqueA ); } auto i_locked = i_weak.lock(); auto o_locked = o_weak.lock(); auto i_sharedLA2 = i_sharedLA->shared_from_this(); auto i_lockedA = i_weakA.lock(); auto o_lockedA = o_weakA.lock(); #if CEREAL_THREAD_SAFE std::lock_guard<std::mutex> lock( boostTestMutex ); #endif BOOST_CHECK_EQUAL(i_shared.get(), i_locked.get()); BOOST_CHECK_EQUAL(*((PolyDerived*)i_shared.get()), *((PolyDerived*)o_shared.get())); BOOST_CHECK_EQUAL(*((PolyDerived*)i_shared.get()), *((PolyDerived*)i_locked.get())); BOOST_CHECK_EQUAL(*((PolyDerived*)i_locked.get()), *((PolyDerived*)o_locked.get())); BOOST_CHECK_EQUAL(*((PolyDerived*)i_unique.get()), *((PolyDerived*)o_unique.get())); BOOST_CHECK_EQUAL(*((PolyDerivedLA*)i_sharedLA.get()), *((PolyDerivedLA*)o_sharedLA.get())); BOOST_CHECK_EQUAL(*((PolyDerivedLA*)i_sharedLA2.get()), *((PolyDerivedLA*)o_sharedLA.get())); BOOST_CHECK_EQUAL(i_sharedA.get(), i_lockedA.get()); BOOST_CHECK_EQUAL(*dynamic_cast<PolyDerivedD*>(i_sharedA.get()), *dynamic_cast<PolyDerivedD*>(o_sharedA.get())); BOOST_CHECK_EQUAL(*dynamic_cast<PolyDerivedD*>(i_sharedA.get()), *dynamic_cast<PolyDerivedD*>(i_lockedA.get())); BOOST_CHECK_EQUAL(*dynamic_cast<PolyDerivedD*>(i_lockedA.get()), *dynamic_cast<PolyDerivedD*>(o_lockedA.get())); BOOST_CHECK_EQUAL(*dynamic_cast<PolyDerivedD*>(i_uniqueA.get()), *dynamic_cast<PolyDerivedD*>(o_uniqueA.get())); } }
void test_deque() { std::random_device rd; std::mt19937 gen(rd()); for(int ii=0; ii<100; ++ii) { std::deque<int> o_poddeque(100); for(auto & elem : o_poddeque) elem = random_value<int>(gen); std::deque<StructInternalSerialize> o_iserdeque(100); for(auto & elem : o_iserdeque) elem = StructInternalSerialize( random_value<int>(gen), random_value<int>(gen) ); std::deque<StructInternalSplit> o_ispldeque(100); for(auto & elem : o_ispldeque) elem = StructInternalSplit( random_value<int>(gen), random_value<int>(gen) ); std::deque<StructExternalSerialize> o_eserdeque(100); for(auto & elem : o_eserdeque) elem = StructExternalSerialize( random_value<int>(gen), random_value<int>(gen) ); std::deque<StructExternalSplit> o_espldeque(100); for(auto & elem : o_espldeque) elem = StructExternalSplit( random_value<int>(gen), random_value<int>(gen) ); std::ostringstream os; { OArchive oar(os); oar(o_poddeque); oar(o_iserdeque); oar(o_ispldeque); oar(o_eserdeque); oar(o_espldeque); } std::deque<int> i_poddeque; std::deque<StructInternalSerialize> i_iserdeque; std::deque<StructInternalSplit> i_ispldeque; std::deque<StructExternalSerialize> i_eserdeque; std::deque<StructExternalSplit> i_espldeque; std::istringstream is(os.str()); { IArchive iar(is); iar(i_poddeque); iar(i_iserdeque); iar(i_ispldeque); iar(i_eserdeque); iar(i_espldeque); } CHECK_EQ(i_poddeque.size(), o_poddeque.size()); CHECK_EQ(i_iserdeque.size(), o_iserdeque.size()); CHECK_EQ(i_ispldeque.size(), o_ispldeque.size()); CHECK_EQ(i_eserdeque.size(), o_eserdeque.size()); CHECK_EQ(i_espldeque.size(), o_espldeque.size()); check_collection(i_poddeque, o_poddeque ); check_collection(i_iserdeque, o_iserdeque); check_collection(i_ispldeque, o_ispldeque); check_collection(i_eserdeque, o_eserdeque); check_collection(i_espldeque, o_espldeque); } }
void test_pod() { std::random_device rd; std::mt19937 gen(rd()); for(size_t i=0; i<100; ++i) { bool const o_bool = random_value<uint8_t>(gen) % 2 ? true : false; char const o_char = random_value<char>(gen); unsigned char const o_uchar = random_value<unsigned char>(gen); uint8_t const o_uint8 = random_value<uint8_t>(gen); int8_t const o_int8 = random_value<int8_t>(gen); uint16_t const o_uint16 = random_value<uint16_t>(gen); int16_t const o_int16 = random_value<int16_t>(gen); uint32_t const o_uint32 = random_value<uint32_t>(gen); int32_t const o_int32 = random_value<int32_t>(gen); uint64_t const o_uint64 = random_value<uint64_t>(gen); int64_t const o_int64 = random_value<int64_t>(gen); float const o_float = random_value<float>(gen); double const o_double = random_value<double>(gen); long double const o_long_double = random_value<long double>(gen); long const o_long = random_value<long>(gen); unsigned long const o_ulong = random_value<unsigned long>(gen); long long const o_long_long = random_value<long long>(gen); unsigned long long const o_ulong_long = random_value<unsigned long long>(gen); std::ostringstream os; { OArchive oar(os); oar(o_bool); oar(o_char); oar(o_uchar); oar(o_uint8); oar(o_int8); oar(o_uint16); oar(o_int16); oar(o_uint32); oar(o_int32); oar(o_uint64); oar(o_int64); oar(o_float); oar(o_double); oar(o_long_double); oar(o_long); oar(o_ulong); oar(o_long_long); oar(o_ulong_long); } bool i_bool = false; char i_char = 0; unsigned char i_uchar = 0; uint8_t i_uint8 = 0; int8_t i_int8 = 0; uint16_t i_uint16 = 0; int16_t i_int16 = 0; uint32_t i_uint32 = 0; int32_t i_int32 = 0; uint64_t i_uint64 = 0; int64_t i_int64 = 0; float i_float = 0; double i_double = 0; long double i_long_double = 0; long i_long = 0; unsigned long i_ulong = 0; long long i_long_long = 0; unsigned long long i_ulong_long = 0; std::istringstream is(os.str()); { IArchive iar(is); iar(i_bool); iar(i_char); iar(i_uchar); iar(i_uint8); iar(i_int8); iar(i_uint16); iar(i_int16); iar(i_uint32); iar(i_int32); iar(i_uint64); iar(i_int64); iar(i_float); iar(i_double); iar(i_long_double); iar(i_long); iar(i_ulong); iar(i_long_long); iar(i_ulong_long); } BOOST_CHECK_EQUAL(i_bool , o_bool); BOOST_CHECK_EQUAL(i_char , o_char); BOOST_CHECK_EQUAL(i_uchar , o_uchar); BOOST_CHECK_EQUAL(i_uint8 , o_uint8); BOOST_CHECK_EQUAL(i_int8 , o_int8); BOOST_CHECK_EQUAL(i_uint16 , o_uint16); BOOST_CHECK_EQUAL(i_int16 , o_int16); BOOST_CHECK_EQUAL(i_uint32 , o_uint32); BOOST_CHECK_EQUAL(i_int32 , o_int32); BOOST_CHECK_EQUAL(i_uint64 , o_uint64); BOOST_CHECK_EQUAL(i_int64 , o_int64); BOOST_CHECK_CLOSE(i_float , o_float, (float)1e-5); BOOST_CHECK_CLOSE(i_double , o_double, 1e-5); BOOST_CHECK_CLOSE(i_long_double, o_long_double, 1e-5); BOOST_CHECK_EQUAL(i_long, o_long); BOOST_CHECK_EQUAL(i_ulong, o_ulong); BOOST_CHECK_EQUAL(i_long_long, o_long_long); BOOST_CHECK_EQUAL(i_ulong_long, o_ulong_long); } }
int main( int argc, char **argv ) { foo = new Foo; TestThread threads[NTHREADS]; lunchbox::Clock clock; for( size_t i=0; i<NTHREADS; ++i ) TEST( threads[i].start( )); for( size_t i=0; i<NTHREADS; ++i ) TEST( threads[i].join( )); const float time = clock.getTimef(); std::cout << time << " ms for " << 3*NREFS << " lunchbox::RefPtr operations" << " in " << NTHREADS << " threads (" << time/(3*NREFS*NTHREADS)*1000000 << "ns/op)" << std::endl; TEST( foo->getRefCount() == 1 ); #ifdef LUNCHBOX_USE_BOOST bFoo = new Foo; BThread bThreads[NTHREADS]; clock.reset(); for( size_t i=0; i<NTHREADS; ++i ) TEST( bThreads[i].start( )); for( size_t i=0; i<NTHREADS; ++i ) TEST( bThreads[i].join( )); const float bTime = clock.getTimef(); std::cout << bTime << " ms for " << 3*NREFS << " boost::intrusive_ptr ops " << "in " << NTHREADS << " threads (" << bTime/(3*NREFS*NTHREADS)*1000000 << "ns/op)" << std::endl; TEST( bFoo->getRefCount() == 1 ); boost::intrusive_ptr< Foo > boostFoo( foo.get( )); TEST( foo->getRefCount() == 2 ); boostFoo = 0; TEST( foo->getRefCount() == 1 ); bBar = BarPtr( new Bar ); BarThread barThreads[NTHREADS]; clock.reset(); for( size_t i=0; i<NTHREADS; ++i ) TEST( barThreads[i].start( )); for( size_t i=0; i<NTHREADS; ++i ) TEST( barThreads[i].join( )); const float barTime = clock.getTimef(); std::cout << barTime << " ms for " << 3*NREFS <<" boost::shared_ptr ops in " << NTHREADS << " threads (" << barTime/(3*NREFS*NTHREADS)*1000000 << "ns/op)" << std::endl; bBar = boost::make_shared< Bar >(); clock.reset(); for( size_t i=0; i<NTHREADS; ++i ) TEST( barThreads[i].start( )); for( size_t i=0; i<NTHREADS; ++i ) TEST( barThreads[i].join( )); const float barTime2 = clock.getTimef(); std::cout << barTime2 << " ms for " << 3*NREFS<<" boost::shared_ptr ops in " << NTHREADS << " threads (" << barTime2/(3*NREFS*NTHREADS)*1000000 << "ns/op) using make_shared" << std::endl; foo = 0; FooPtr inFoo1 = new Foo; TEST( inFoo1->getRefCount() == 1 ); FooPtr inFoo2 = inFoo1; TEST( inFoo2->getRefCount() == 2 ); FooPtr outFoo1; std::stringstream stream; boost::archive::text_oarchive oar( stream ); oar & inFoo1; boost::archive::text_iarchive iar( stream ); iar & outFoo1; TEST( outFoo1->getRefCount() == 1 ); FooPtr outFoo2 = outFoo1; TEST( outFoo2->getRefCount() == 2 ); #endif return EXIT_SUCCESS; }
// ###################################################################### int main() { std::cout << std::boolalpha << std::endl; { std::ofstream os("file.json"); cereal::JSONOutputArchive oar( os ); //auto f = std::make_shared<Fixture>(); //auto f2 = f; //oar( f ); //oar( f2 ); Stuff s; s.fillData(); oar( cereal::make_nvp("best data ever", s) ); } { std::ifstream is("file.json"); std::string str((std::istreambuf_iterator<char>(is)), std::istreambuf_iterator<char>()); std::cout << "---------------------" << std::endl << str << std::endl << "---------------------" << std::endl; } // playground { cereal::JSONOutputArchive archive( std::cout ); bool arr[] = {true, false}; std::vector<int> vec = {1, 2, 3, 4, 5}; archive( CEREAL_NVP(vec), arr ); auto f = std::make_shared<Fixture>(); auto f2 = f; archive( f ); archive( f2 ); } // test out of order std::stringstream oos; { cereal::JSONOutputArchive ar(oos); cereal::JSONOutputArchive ar2(std::cout, cereal::JSONOutputArchive::Options(2, cereal::JSONOutputArchive::Options::IndentChar::space, 2) ); ar( cereal::make_nvp( "1", 1 ), cereal::make_nvp( "2", 2 ), 3, 0, // unused cereal::make_nvp( "4", 4 ), cereal::make_nvp( "5", 5 ) ); int x = 33; ar.saveBinaryValue( &x, sizeof(int), "bla" ); ar2( cereal::make_nvp( "1", 1 ), cereal::make_nvp( "2", 2 ), 3, 0, // unused cereal::make_nvp( "4", 4 ), cereal::make_nvp( "5", 5 ) ); ar2.saveBinaryValue( &x, sizeof(int), "bla" ); OOJson oo( 1, 2, true, 4.2 ); ar( CEREAL_NVP(oo) ); ar2( CEREAL_NVP(oo) ); // boost stuff ar & cereal::make_nvp("usingop&", oo ) & 6; ar << 5 << 4 << 3; ar2 & cereal::make_nvp("usingop&", oo ) & 6; ar2 << 5 << 4 << 3; long double ld = std::numeric_limits<long double>::max(); long long ll = std::numeric_limits<long long>::max(); unsigned long long ull = std::numeric_limits<unsigned long long>::max(); ar( CEREAL_NVP(ld), CEREAL_NVP(ll), CEREAL_NVP(ull) ); ar2( CEREAL_NVP(ld), CEREAL_NVP(ll), CEREAL_NVP(ull) ); } { cereal::JSONInputArchive ar(oos); int i1, i2, i3, i4, i5, x; ar( i1 ); ar( cereal::make_nvp( "2", i2 ), i3 ); ar( cereal::make_nvp( "4", i4 ), i5 ); ar.loadBinaryValue( &x, sizeof(int) ); OOJson ii; ar( cereal::make_nvp("oo", ii) ); ar( cereal::make_nvp( "2", i2 ) ); std::cout << i1 << " " << i2 << " " << i3 << " " << i4 << " " << i5 << std::endl; std::cout << x << std::endl; std::cout << ii.a << " " << ii.b << " " << ii.c.first << " " << ii.c.second << " "; for( auto z : ii.d ) std::cout << z << " "; std::cout << std::endl; OOJson oo; ar >> cereal::make_nvp("usingop&", oo ); std::cout << oo.a << " " << oo.b << " " << oo.c.first << " " << oo.c.second << " "; for( auto z : oo.d ) std::cout << z << " "; int aa, a, b, c; ar & aa & a & b & c; std::cout << aa << " " << a << " " << b << " " << c << std::endl; long double ld; long long ll; unsigned long long ull; ar( CEREAL_NVP(ld), CEREAL_NVP(ll), CEREAL_NVP(ull) ); std::cout << (ld == std::numeric_limits<long double>::max()) << std::endl; std::cout << (ll == std::numeric_limits<long long>::max()) << std::endl; std::cout << (ull == std::numeric_limits<unsigned long long>::max()) << std::endl; } return 0; }
void test_vector() { std::random_device rd; std::mt19937 gen(rd()); for(int ii=0; ii<100; ++ii) { std::vector<int> o_podvector(100); for(auto & elem : o_podvector) elem = random_value<int>(gen); std::vector<bool> o_boolvector; o_boolvector.resize(100); for( size_t i = 0; i < 100; ++i ) o_boolvector[i] = (random_value<int>(gen) % 2) == 0; std::vector<StructInternalSerialize> o_iservector(100); for(auto & elem : o_iservector) elem = StructInternalSerialize( random_value<int>(gen), random_value<int>(gen) ); std::vector<StructInternalSplit> o_isplvector(100); for(auto & elem : o_isplvector) elem = StructInternalSplit( random_value<int>(gen), random_value<int>(gen) ); std::vector<StructExternalSerialize> o_eservector(100); for(auto & elem : o_eservector) elem = StructExternalSerialize( random_value<int>(gen), random_value<int>(gen) ); std::vector<StructExternalSplit> o_esplvector(100); for(auto & elem : o_esplvector) elem = StructExternalSplit( random_value<int>(gen), random_value<int>(gen) ); std::ostringstream os; { OArchive oar(os); oar(o_podvector); oar(o_boolvector); oar(o_iservector); oar(o_isplvector); oar(o_eservector); oar(o_esplvector); } std::vector<int> i_podvector; std::vector<bool> i_boolvector; std::vector<StructInternalSerialize> i_iservector; std::vector<StructInternalSplit> i_isplvector; std::vector<StructExternalSerialize> i_eservector; std::vector<StructExternalSplit> i_esplvector; std::istringstream is(os.str()); { IArchive iar(is); iar(i_podvector); iar(i_boolvector); iar(i_iservector); iar(i_isplvector); iar(i_eservector); iar(i_esplvector); } BOOST_CHECK_EQUAL(i_podvector.size(), o_podvector.size()); BOOST_CHECK_EQUAL(i_boolvector.size(), o_boolvector.size()); BOOST_CHECK_EQUAL(i_iservector.size(), o_iservector.size()); BOOST_CHECK_EQUAL(i_isplvector.size(), o_isplvector.size()); BOOST_CHECK_EQUAL(i_eservector.size(), o_eservector.size()); BOOST_CHECK_EQUAL(i_esplvector.size(), o_esplvector.size()); BOOST_CHECK_EQUAL_COLLECTIONS(i_podvector.begin(), i_podvector.end(), o_podvector.begin(), o_podvector.end()); BOOST_CHECK_EQUAL_COLLECTIONS(i_boolvector.begin(), i_boolvector.end(), o_boolvector.begin(), o_boolvector.end()); BOOST_CHECK_EQUAL_COLLECTIONS(i_iservector.begin(), i_iservector.end(), o_iservector.begin(), o_iservector.end()); BOOST_CHECK_EQUAL_COLLECTIONS(i_isplvector.begin(), i_isplvector.end(), o_isplvector.begin(), o_isplvector.end()); BOOST_CHECK_EQUAL_COLLECTIONS(i_eservector.begin(), i_eservector.end(), o_eservector.begin(), o_eservector.end()); BOOST_CHECK_EQUAL_COLLECTIONS(i_esplvector.begin(), i_esplvector.end(), o_esplvector.begin(), o_esplvector.end()); } }
void test_list() { std::random_device rd; std::mt19937 gen(rd()); for(int ii=0; ii<100; ++ii) { std::list<int> o_podlist(100); for(auto & elem : o_podlist) elem = random_value<int>(gen); std::list<StructInternalSerialize> o_iserlist(100); for(auto & elem : o_iserlist) elem = StructInternalSerialize( random_value<int>(gen), random_value<int>(gen) ); std::list<StructInternalSplit> o_ispllist(100); for(auto & elem : o_ispllist) elem = StructInternalSplit( random_value<int>(gen), random_value<int>(gen) ); std::list<StructExternalSerialize> o_eserlist(100); for(auto & elem : o_eserlist) elem = StructExternalSerialize( random_value<int>(gen), random_value<int>(gen) ); std::list<StructExternalSplit> o_espllist(100); for(auto & elem : o_espllist) elem = StructExternalSplit( random_value<int>(gen), random_value<int>(gen) ); std::ostringstream os; { OArchive oar(os); oar(o_podlist); oar(o_iserlist); oar(o_ispllist); oar(o_eserlist); oar(o_espllist); } std::list<int> i_podlist; std::list<StructInternalSerialize> i_iserlist; std::list<StructInternalSplit> i_ispllist; std::list<StructExternalSerialize> i_eserlist; std::list<StructExternalSplit> i_espllist; std::istringstream is(os.str()); { IArchive iar(is); iar(i_podlist); iar(i_iserlist); iar(i_ispllist); iar(i_eserlist); iar(i_espllist); } BOOST_CHECK_EQUAL_COLLECTIONS(i_podlist.begin(), i_podlist.end(), o_podlist.begin(), o_podlist.end()); BOOST_CHECK_EQUAL_COLLECTIONS(i_iserlist.begin(), i_iserlist.end(), o_iserlist.begin(), o_iserlist.end()); BOOST_CHECK_EQUAL_COLLECTIONS(i_ispllist.begin(), i_ispllist.end(), o_ispllist.begin(), o_ispllist.end()); BOOST_CHECK_EQUAL_COLLECTIONS(i_eserlist.begin(), i_eserlist.end(), o_eserlist.begin(), o_eserlist.end()); BOOST_CHECK_EQUAL_COLLECTIONS(i_espllist.begin(), i_espllist.end(), o_espllist.begin(), o_espllist.end()); } }
template <class IArchive, class OArchive> inline void test_tuple() { std::random_device rd; std::mt19937 gen(rd()); auto rng = [&](){ return random_value<int>(gen); }; for(int ii=0; ii<100; ++ii) { auto o_podtuple = std::make_tuple( rng(), rng(), rng(), rng() ); auto o_podtuple11 = std::make_tuple( rng(), rng(), rng(), rng(), rng(), rng(), rng(), rng(), rng(), rng(), rng() ); auto o_isertuple = std::make_tuple( StructInternalSerialize( rng(), rng() ), StructInternalSerialize( rng(), rng() ), StructInternalSerialize( rng(), rng() ), StructInternalSerialize( rng(), rng() ) ); auto o_ispltuple = std::make_tuple( StructInternalSplit( rng(), rng() ), StructInternalSplit( rng(), rng() ), StructInternalSplit( rng(), rng() ), StructInternalSplit( rng(), rng() ) ); auto o_esertuple = std::make_tuple( StructExternalSerialize( rng(), rng() ), StructExternalSerialize( rng(), rng() ), StructExternalSerialize( rng(), rng() ), StructExternalSerialize( rng(), rng() ) ); auto o_espltuple = std::make_tuple( StructExternalSerialize( rng(), rng() ), StructExternalSerialize( rng(), rng() ), StructExternalSerialize( rng(), rng() ), StructExternalSerialize( rng(), rng() ) ); std::ostringstream os; { OArchive oar(os); oar(o_podtuple); oar(o_podtuple11); oar(o_isertuple); oar(o_ispltuple); oar(o_esertuple); oar(o_espltuple); } decltype( o_podtuple ) i_podtuple; decltype( o_podtuple11 ) i_podtuple11; decltype( o_isertuple ) i_isertuple; decltype( o_ispltuple ) i_ispltuple; decltype( o_esertuple ) i_esertuple; decltype( o_espltuple ) i_espltuple; std::istringstream is(os.str()); { IArchive iar(is); iar(i_podtuple); iar(i_podtuple11); iar(i_isertuple); iar(i_ispltuple); iar(i_esertuple); iar(i_espltuple); } CHECK_EQ( i_podtuple, o_podtuple); CHECK_EQ( i_podtuple11, o_podtuple11); CHECK_EQ( i_isertuple, o_isertuple); CHECK_EQ( i_ispltuple, o_ispltuple); CHECK_EQ( i_esertuple, o_esertuple); CHECK_EQ( i_espltuple, o_espltuple); } }