| 1 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
|---|
| 2 | // test_array.cpp |
|---|
| 3 | |
|---|
| 4 | // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . |
|---|
| 5 | // Use, modification and distribution is subject to the Boost Software |
|---|
| 6 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
|---|
| 7 | // http://www.boost.org/LICENSE_1_0.txt) |
|---|
| 8 | |
|---|
| 9 | // should pass compilation and execution |
|---|
| 10 | |
|---|
| 11 | #include <fstream> |
|---|
| 12 | #include <cstdio> // remove |
|---|
| 13 | #include <boost/config.hpp> |
|---|
| 14 | #if defined(BOOST_NO_STDC_NAMESPACE) |
|---|
| 15 | namespace std{ |
|---|
| 16 | using ::remove; |
|---|
| 17 | } |
|---|
| 18 | #endif |
|---|
| 19 | |
|---|
| 20 | #include "test_tools.hpp" |
|---|
| 21 | #include <boost/preprocessor/stringize.hpp> |
|---|
| 22 | #include BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST) |
|---|
| 23 | #include <boost/detail/no_exceptions_support.hpp> |
|---|
| 24 | #include <boost/archive/archive_exception.hpp> |
|---|
| 25 | |
|---|
| 26 | #include <boost/serialization/nvp.hpp> |
|---|
| 27 | #include "A.hpp" |
|---|
| 28 | |
|---|
| 29 | template<class T> |
|---|
| 30 | struct array_equal_to : public std::binary_function<T, T, bool> |
|---|
| 31 | { |
|---|
| 32 | bool operator()(const T & _Left, const T & _Right) const |
|---|
| 33 | { |
|---|
| 34 | // consider alignment |
|---|
| 35 | int count_left = sizeof(_Left) / ( |
|---|
| 36 | static_cast<const char *>(static_cast<const void *>(&_Left[1])) |
|---|
| 37 | - static_cast<const char *>(static_cast<const void *>(&_Left[0])) |
|---|
| 38 | ); |
|---|
| 39 | int count_right = sizeof(_Right) / ( |
|---|
| 40 | static_cast<const char *>(static_cast<const void *>(&_Right[1])) |
|---|
| 41 | - static_cast<const char *>(static_cast<const void *>(&_Right[0])) |
|---|
| 42 | ); |
|---|
| 43 | if(count_right != count_left) |
|---|
| 44 | return false; |
|---|
| 45 | while(count_left-- > 0){ |
|---|
| 46 | if(_Left[count_left] == _Right[count_left]) |
|---|
| 47 | continue; |
|---|
| 48 | return false; |
|---|
| 49 | } |
|---|
| 50 | return true; |
|---|
| 51 | } |
|---|
| 52 | }; |
|---|
| 53 | |
|---|
| 54 | int test_main( int /* argc */, char* /* argv */[] ) |
|---|
| 55 | { |
|---|
| 56 | const char * testfile = boost::archive::tmpnam(NULL); |
|---|
| 57 | BOOST_REQUIRE(NULL != testfile); |
|---|
| 58 | |
|---|
| 59 | // test array of objects |
|---|
| 60 | const A a_array[10]; |
|---|
| 61 | { |
|---|
| 62 | test_ostream os(testfile, TEST_STREAM_FLAGS); |
|---|
| 63 | test_oarchive oa(os); |
|---|
| 64 | oa << boost::serialization::make_nvp("a_array", a_array); |
|---|
| 65 | } |
|---|
| 66 | { |
|---|
| 67 | A a_array1[10]; |
|---|
| 68 | test_istream is(testfile, TEST_STREAM_FLAGS); |
|---|
| 69 | test_iarchive ia(is); |
|---|
| 70 | ia >> boost::serialization::make_nvp("a_array", a_array1); |
|---|
| 71 | |
|---|
| 72 | array_equal_to<A[10]> Compare; |
|---|
| 73 | BOOST_CHECK(Compare(a_array, a_array1)); |
|---|
| 74 | } |
|---|
| 75 | { |
|---|
| 76 | A a_array1[9]; |
|---|
| 77 | test_istream is(testfile, TEST_STREAM_FLAGS); |
|---|
| 78 | BOOST_TRY { |
|---|
| 79 | test_iarchive ia(is); |
|---|
| 80 | bool exception_invoked = false; |
|---|
| 81 | BOOST_TRY { |
|---|
| 82 | ia >> boost::serialization::make_nvp("a_array", a_array1); |
|---|
| 83 | } |
|---|
| 84 | BOOST_CATCH (boost::archive::archive_exception ae){ |
|---|
| 85 | BOOST_CHECK( |
|---|
| 86 | boost::archive::archive_exception::array_size_too_short |
|---|
| 87 | == ae.code |
|---|
| 88 | ); |
|---|
| 89 | exception_invoked = true; |
|---|
| 90 | } |
|---|
| 91 | BOOST_CATCH_END |
|---|
| 92 | BOOST_CHECK(exception_invoked); |
|---|
| 93 | } |
|---|
| 94 | BOOST_CATCH (boost::archive::archive_exception ae){} |
|---|
| 95 | BOOST_CATCH_END |
|---|
| 96 | } |
|---|
| 97 | std::remove(testfile); |
|---|
| 98 | return EXIT_SUCCESS; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | // EOF |
|---|