• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/test_tools.hpp"
21 
22 #include <boost/preprocessor/stringize.hpp>
23 //#include <boost/preprocessor/cat.hpp>
24 // the following fails with (only!) gcc 3.4
25 // #include BOOST_PP_STRINGIZE(BOOST_PP_CAT(../test/,BOOST_ARCHIVE_TEST))
26 // just copy over the files from the test directory
27 #include BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST)
28 
29 #include <boost/core/no_exceptions_support.hpp>
30 #include <boost/archive/archive_exception.hpp>
31 
32 #include <boost/serialization/nvp.hpp>
33 #include "../test/A.hpp"
34 #include "../test/A.ipp"
35 
36 struct array_equal_to //: public std::binary_function<T, T, bool>
37 {
38 template<class T, class U>
operator ()array_equal_to39     bool operator()(const T & _Left, const U & _Right) const
40     {
41         // consider alignment
42         int count_left = sizeof(_Left) /    (
43             static_cast<const char *>(static_cast<const void *>(&_Left[1]))
44             - static_cast<const char *>(static_cast<const void *>(&_Left[0]))
45         );
46         int count_right = sizeof(_Right) /  (
47             static_cast<const char *>(static_cast<const void *>(&_Right[1]))
48             - static_cast<const char *>(static_cast<const void *>(&_Right[0]))
49         );
50         if(count_right != count_left)
51             return false;
52         while(count_left-- > 0){
53             if(_Left[count_left] == _Right[count_left])
54                 continue;
55             return false;
56         }
57         return true;
58     }
59 };
60 
61 template <class T>
test_array(T)62 int test_array(T)
63 {
64     const char * testfile = boost::archive::tmpnam(NULL);
65     BOOST_REQUIRE(NULL != testfile);
66 
67     // test array of objects
68     const T a_array[10]={T(),T(),T(),T(),T(),T(),T(),T(),T(),T()};
69     {
70         test_ostream os(testfile, TEST_STREAM_FLAGS);
71         test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
72         oa << boost::serialization::make_nvp("a_array", a_array);
73     }
74     {
75         T a_array1[10];
76         test_istream is(testfile, TEST_STREAM_FLAGS);
77         test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
78         ia >> boost::serialization::make_nvp("a_array", a_array1);
79 
80         array_equal_to/*<A[10]>*/ Compare;
81         BOOST_CHECK(Compare(a_array, a_array1));
82     }
83     {
84         T a_array1[9];
85         test_istream is(testfile, TEST_STREAM_FLAGS);
86         BOOST_TRY {
87             test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
88             bool exception_invoked = false;
89             BOOST_TRY {
90                 ia >> boost::serialization::make_nvp("a_array", a_array1);
91             }
92             BOOST_CATCH (boost::archive::archive_exception ae){
93                 BOOST_CHECK(
94                     boost::archive::archive_exception::array_size_too_short
95                     == ae.code
96                 );
97                 exception_invoked = true;
98             }
99             BOOST_CATCH_END
100             BOOST_CHECK(exception_invoked);
101         }
102         BOOST_CATCH (boost::archive::archive_exception ae){}
103         BOOST_CATCH_END
104     }
105     std::remove(testfile);
106     return EXIT_SUCCESS;
107 }
108 
test_main(int,char * [])109 int test_main( int /* argc */, char* /* argv */[] )
110 {
111    int res = test_array(A());
112     // test an int array for which optimized versions should be available
113    if (res == EXIT_SUCCESS)
114      res = test_array(0);
115    return res;
116 }
117 
118 // EOF
119