Imported Upstream version 1.64.0
[platform/upstream/boost.git] / libs / serialization / test / test_native_array.cpp
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 <stdlib.h>
12
13 #include <boost/config.hpp>
14 #include <cstddef>
15 #include <fstream>
16 #include <algorithm> // equal
17 #include <cstdio> // remove
18 #if defined(BOOST_NO_STDC_NAMESPACE)
19 namespace std{ 
20     using ::remove;
21 }
22 #endif
23 #include "test_tools.hpp"
24 #include <boost/core/no_exceptions_support.hpp>
25 #include <boost/archive/archive_exception.hpp>
26
27 #include "A.hpp"
28 #include "A.ipp"
29
30 template <class T>
31 int test_native_array(){
32     const char * testfile = boost::archive::tmpnam(NULL);
33     BOOST_REQUIRE(NULL != testfile);
34
35     // test array of objects
36     const T a_array[10]={T(),T(),T(),T(),T(),T(),T(),T(),T(),T()};
37     const T b_array[2][3]={{T(),T(),T()},{T(),T(),T()}};
38     {
39         test_ostream os(testfile, TEST_STREAM_FLAGS);
40         {
41             test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
42             oa << boost::serialization::make_nvp("a_array", a_array);
43             oa << boost::serialization::make_nvp("b_array", b_array);
44         }
45         os.close();
46     }
47     {
48         T a_array1[10];
49         T b_array1[2][3];
50         test_istream is(testfile, TEST_STREAM_FLAGS);
51         {
52             test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
53             ia >> boost::serialization::make_nvp("a_array", a_array1);
54             ia >> boost::serialization::make_nvp("b_array", b_array1);
55         }
56         is.close();
57         BOOST_CHECK(std::equal(& a_array[0], & a_array[10], & a_array1[0]));
58         BOOST_CHECK(b_array[0][0] == b_array1[0][0]);
59         BOOST_CHECK(b_array[1][0] == b_array1[1][0]);
60     }
61     {
62         T a_array1[9];
63         T b_array1[2][3];
64         test_istream is(testfile, TEST_STREAM_FLAGS);
65         {
66             test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
67             bool exception_invoked = false;
68             BOOST_TRY {
69                 ia >> boost::serialization::make_nvp("a_array", a_array1);
70                 ia >> boost::serialization::make_nvp("b_array", b_array1);
71             }
72             BOOST_CATCH (boost::archive::archive_exception ae){
73                 BOOST_CHECK(
74                     boost::archive::archive_exception::array_size_too_short
75                     == ae.code
76                 );
77                 exception_invoked = true;
78             }
79             BOOST_CATCH_END
80             BOOST_CHECK(exception_invoked);
81         }
82         is.close();
83     }
84     std::remove(testfile);
85     return EXIT_SUCCESS;
86 }
87
88 int test_main( int /* argc */, char* /* argv */[] ){
89     int res;
90
91     // native array
92     res = test_native_array<A>();
93     if (res != EXIT_SUCCESS)
94         return EXIT_FAILURE;
95     // test an int array for which optimized versions should be available
96     res = test_native_array<int>();
97     if (res != EXIT_SUCCESS)
98         return EXIT_FAILURE;
99
100     return EXIT_SUCCESS;
101 }
102
103 // EOF