Ticket #5265: test_unordered.patch

File test_unordered.patch, 7.0 KB (added by Jim Bell <jim@…>, 12 years ago)

Patch for libs/serialization/test (test_map.cpp and test_set.cpp)

  • libs/serialization/test/test_map.cpp

     
    147147    template<>
    148148    struct hash<random_key>{
    149149        std::size_t operator()(const random_key& r) const {
    150             return (std::size_t)r;
     150            return static_cast<std::size_t>(r);
    151151        }
    152152    };
    153153} // namespace BOOST_STD_EXTENSION_NAMESPACE
     
    216216}
    217217#endif
    218218
     219#include <boost/serialization/unordered_map.hpp>
     220#include <boost/tr1/functional.hpp> // requires changeset [69520]; Ticket #5254
     221
     222namespace std {
     223namespace tr1 {
     224
     225    template<>
     226    struct hash<random_key>{
     227        std::size_t operator()(const random_key& r) const {
     228            return static_cast<std::size_t>(r);
     229        }
     230    };
     231} // namespace tr1
     232} // namespace std
     233
     234void
     235test_unordered_map(){
     236    const char * testfile = boost::archive::tmpnam(NULL);
     237    BOOST_REQUIRE(NULL != testfile);
     238
     239    BOOST_CHECKPOINT("unordered_map");
     240    // test unordered_map of objects
     241    std::tr1::unordered_map<random_key, A> anunordered_map;
     242    anunordered_map.insert(std::make_pair(random_key(), A()));
     243    anunordered_map.insert(std::make_pair(random_key(), A()));
     244    {   
     245        test_ostream os(testfile, TEST_STREAM_FLAGS);
     246        test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
     247        oa << boost::serialization::make_nvp("anunorderedmap",anunordered_map);
     248    }
     249    std::tr1::unordered_map<random_key, A> anunordered_map1;
     250    {
     251        test_istream is(testfile, TEST_STREAM_FLAGS);
     252        test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
     253        ia >> boost::serialization::make_nvp("anunorderedmap",anunordered_map1);
     254    }
     255
     256    std::vector< std::pair<random_key, A> > tvec, tvec1;
     257    std::copy(anunordered_map.begin(), anunordered_map.end(), std::back_inserter(tvec));
     258    std::sort(tvec.begin(), tvec.end());
     259    std::copy(anunordered_map1.begin(), anunordered_map1.end(), std::back_inserter(tvec1));
     260    std::sort(tvec1.begin(), tvec1.end());
     261    BOOST_CHECK(tvec == tvec1);
     262
     263    std::remove(testfile);
     264}
     265
     266void
     267test_unordered_multimap(){
     268    const char * testfile = boost::archive::tmpnam(NULL);
     269    BOOST_REQUIRE(NULL != testfile);
     270
     271    BOOST_CHECKPOINT("unordered_multimap");
     272    std::tr1::unordered_multimap<random_key, A> anunordered_multimap;
     273    anunordered_multimap.insert(std::make_pair(random_key(), A()));
     274    anunordered_multimap.insert(std::make_pair(random_key(), A()));
     275    {   
     276        test_ostream os(testfile, TEST_STREAM_FLAGS);
     277        test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
     278        oa << boost::serialization::make_nvp("anunordered_multimap", anunordered_multimap);
     279    }
     280    std::tr1::unordered_multimap<random_key, A> anunordered_multimap1;
     281    {
     282        test_istream is(testfile, TEST_STREAM_FLAGS);
     283        test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
     284        ia >> boost::serialization::make_nvp("anunordered_multimap", anunordered_multimap1);
     285    }
     286    std::vector< std::pair<random_key, A> > tvec, tvec1;
     287    tvec.clear();
     288    tvec1.clear();
     289    std::copy(anunordered_multimap.begin(), anunordered_multimap.end(), std::back_inserter(tvec));
     290    std::sort(tvec.begin(), tvec.end());
     291    std::copy(anunordered_multimap1.begin(), anunordered_multimap1.end(), std::back_inserter(tvec1));
     292    std::sort(tvec1.begin(), tvec1.end());
     293    BOOST_CHECK(tvec == tvec1);
     294    std::remove(testfile);
     295}
     296
     297
    219298int test_main( int /* argc */, char* /* argv */[] )
    220299{
    221300    test_map();
     
    225304    test_hash_map();
    226305    test_hash_multimap();
    227306    #endif
     307    test_unordered_map();
     308    test_unordered_multimap();
    228309    return EXIT_SUCCESS;
    229310}
  • libs/serialization/test/test_set.cpp

     
    9494    template<>
    9595    struct hash<A> {
    9696        std::size_t operator()(const A& a) const {
    97             return (std::size_t)a;
     97            return static_cast<std::size_t>(a);
    9898        }
    9999    };
    100100}
     
    164164}
    165165#endif
    166166
     167#include <boost/serialization/unordered_set.hpp>
     168#include <boost/tr1/functional.hpp> // requires changeset [69520]; Ticket #5254
     169
     170
     171namespace std {
     172namespace tr1 {
     173    template<>
     174    struct hash<A> {
     175        std::size_t operator()(const A& a) const {
     176            return static_cast<std::size_t>(a);
     177        }
     178    };
     179} // namespace tr1
     180} // namespace std
     181
     182void
     183test_unordered_set(){
     184    const char * testfile = boost::archive::tmpnam(NULL);
     185    BOOST_REQUIRE(NULL != testfile);
     186
     187    // test array of objects
     188    std::tr1::unordered_set<A> anunordered_set;
     189    A a, a1;
     190    anunordered_set.insert(a);
     191    anunordered_set.insert(a1);
     192    {   
     193        test_ostream os(testfile, TEST_STREAM_FLAGS);
     194        test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
     195        oa << boost::serialization::make_nvp("anunordered_set", anunordered_set);
     196    }
     197    std::tr1::unordered_set<A> anunordered_set1;
     198    {
     199        test_istream is(testfile, TEST_STREAM_FLAGS);
     200        test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
     201        ia >> boost::serialization::make_nvp("anunordered_set", anunordered_set1);
     202    }
     203    std::vector<A> tvec, tvec1;
     204    tvec.clear();
     205    tvec1.clear();
     206    std::copy(anunordered_set.begin(), anunordered_set.end(), std::back_inserter(tvec));
     207    std::sort(tvec.begin(), tvec.end());
     208    std::copy(anunordered_set1.begin(), anunordered_set1.end(), std::back_inserter(tvec1));
     209    std::sort(tvec1.begin(), tvec1.end());
     210    BOOST_CHECK(tvec == tvec1);
     211    std::remove(testfile);
     212}
     213
     214void
     215test_unordered_multiset(){
     216    const char * testfile = boost::archive::tmpnam(NULL);
     217    BOOST_REQUIRE(NULL != testfile);
     218
     219    std::tr1::unordered_multiset<A> anunordered_multiset;
     220    anunordered_multiset.insert(A());
     221    anunordered_multiset.insert(A());
     222    {   
     223        test_ostream os(testfile, TEST_STREAM_FLAGS);
     224        test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
     225        oa << boost::serialization::make_nvp("anunordered_multiset", anunordered_multiset);
     226    }
     227    std::tr1::unordered_multiset<A> anunordered_multiset1;
     228    {
     229        test_istream is(testfile, TEST_STREAM_FLAGS);
     230        test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
     231        ia >> boost::serialization::make_nvp("anunordered_multiset", anunordered_multiset1);
     232    }
     233
     234    std::vector<A> tvec, tvec1;
     235    tvec.clear();
     236    tvec1.clear();
     237    std::copy(anunordered_multiset.begin(), anunordered_multiset.end(), std::back_inserter(tvec));
     238    std::sort(tvec.begin(), tvec.end());
     239    std::copy(anunordered_multiset1.begin(), anunordered_multiset1.end(), std::back_inserter(tvec1));
     240    std::sort(tvec1.begin(), tvec1.end());
     241    BOOST_CHECK(tvec == tvec1);
     242
     243    std::remove(testfile);
     244}
     245
     246
    167247int test_main( int /* argc */, char* /* argv */[] )
    168248{
    169249    test_set();
     
    172252    test_hash_set();
    173253    test_hash_multiset();
    174254    #endif
     255        test_unordered_set();
     256        test_unordered_multiset();
    175257    return EXIT_SUCCESS;
    176258}