• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2 // test_optional.cpp
3 
4 // (C) Copyright 2004 Pavel Vozenilek
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 <cstddef> // NULL
12 #include <cstdio> // remove
13 #include <fstream>
14 
15 #include <boost/config.hpp>
16 
17 #if defined(BOOST_NO_STDC_NAMESPACE)
18 namespace std{
19     using ::remove;
20 }
21 #endif
22 
23 #include <boost/archive/archive_exception.hpp>
24 #include "test_tools.hpp"
25 
26 #include <boost/serialization/optional.hpp>
27 #include <boost/serialization/string.hpp>
28 
29 struct A {
30     int m_x;
31     template<class Archive>
serializeA32     void serialize(Archive & ar, const unsigned int /* version */){
33         ar & boost::serialization::make_nvp("x", m_x);
34     };
operator ==A35     bool operator==(const A & rhs) const {
36         return m_x == rhs.m_x;
37     }
38     // note that default constructor is not trivial
AA39     A() :
40         m_x(0)
41     {}
AA42     A(int x) :
43         m_x(x)
44     {}
45 };
46 
test_main(int,char * [])47 int test_main( int /* argc */, char* /* argv */[] )
48 {
49     const char * testfile = boost::archive::tmpnam(NULL);
50     BOOST_REQUIRE(NULL != testfile);
51 
52     const boost::optional<int> aoptional1;
53     const boost::optional<int> aoptional2(123);
54     const boost::optional<A> aoptional3;
55     A a(1);
56     const boost::optional<A> aoptional4(a);
57     const boost::optional<A *> aoptional5;
58     const boost::optional<A *> aoptional6(& a);
59     {
60         test_ostream os(testfile, TEST_STREAM_FLAGS);
61         test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
62         oa << boost::serialization::make_nvp("aoptional1",aoptional1);
63         oa << boost::serialization::make_nvp("aoptional2",aoptional2);
64         oa << boost::serialization::make_nvp("aoptional3",aoptional3);
65         oa << boost::serialization::make_nvp("aoptional4",aoptional4);
66         oa << boost::serialization::make_nvp("aoptional5",aoptional5);
67         oa << boost::serialization::make_nvp("aoptional6",aoptional6);
68     }
69     boost::optional<int> aoptional1a(999);
70     boost::optional<int> aoptional2a;
71     boost::optional<A> aoptional3a;
72     boost::optional<A> aoptional4a;
73     boost::optional<A *> aoptional5a;
74     boost::optional<A *> aoptional6a;
75     {
76         test_istream is(testfile, TEST_STREAM_FLAGS);
77         test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
78         ia >> boost::serialization::make_nvp("aoptional1",aoptional1a);
79         ia >> boost::serialization::make_nvp("aoptional2",aoptional2a);
80         ia >> boost::serialization::make_nvp("aoptional3",aoptional3a);
81         ia >> boost::serialization::make_nvp("aoptional4",aoptional4a);
82         ia >> boost::serialization::make_nvp("aoptional5",aoptional5a);
83         ia >> boost::serialization::make_nvp("aoptional6",aoptional6a);
84     }
85     BOOST_CHECK(aoptional1 == aoptional1a);
86     BOOST_CHECK(aoptional2 == aoptional2a);
87     BOOST_CHECK(aoptional3 == aoptional3a);
88     BOOST_CHECK(aoptional4.get() == aoptional4a.get());
89     BOOST_CHECK(aoptional5 == aoptional5a);
90     BOOST_CHECK(*aoptional6.get() == *aoptional6a.get());
91 
92     std::remove(testfile);
93     return EXIT_SUCCESS;
94 }
95 
96 // EOF
97