• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2 // test_simple_class.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 <cstdlib> // for rand(), NULL, size_t
12 
13 #include <fstream>
14 #include <boost/config.hpp>
15 
16 #include <cstdio> // remove
17 #if defined(BOOST_NO_STDC_NAMESPACE)
18 namespace std{
19     using ::rand;
20     using ::remove;
21 }
22 #endif
23 
24 #include "test_tools.hpp"
25 
26 #include <boost/serialization/nvp.hpp>
27 #include <boost/serialization/binary_object.hpp>
28 
29 class A {
30     friend class boost::serialization::access;
31     char data[150];
32     // note: from an aesthetic perspective, I would much prefer to have this
33     // defined out of line.  Unfortunately, this trips a bug in the VC 6.0
34     // compiler. So hold our nose and put it her to permit running of tests.
35     template<class Archive>
serialize(Archive & ar,const unsigned int)36     void serialize(Archive & ar, const unsigned int /* file_version */){
37         ar & boost::serialization::make_nvp(
38             "data",
39             boost::serialization::make_binary_object(data, sizeof(data))
40         );
41     }
42 
43 public:
44     A();
45     bool operator==(const A & rhs) const;
46 };
47 
A()48 A::A(){
49     int i = sizeof(data);
50     while(i-- > 0)
51         data[i] = static_cast<char>(0xff & std::rand());
52 }
53 
operator ==(const A & rhs) const54 bool A::operator==(const A & rhs) const {
55     int i = sizeof(data);
56     while(i-- > 0)
57         if(data[i] != rhs.data[i])
58             return false;
59     return true;
60 }
61 
test_main(int,char * [])62 int test_main( int /* argc */, char* /* argv */[] )
63 {
64     const char * testfile = boost::archive::tmpnam(NULL);
65     BOOST_REQUIRE(NULL != testfile);
66 
67     const A a;
68     char s1[] = "a";
69     char s2[] = "ab";
70     char s3[] = "abc";
71     char s4[] = "abcd";
72     const int i = 12345;
73 
74     A a1;
75     char s1_1[10];
76     char s1_2[10];
77     char s1_3[10];
78     char s1_4[10];
79     int i1 = 34790;
80 
81     std::memset(s1_1, '\0', sizeof(s1_1));
82     std::memset(s1_2, '\0', sizeof(s1_2));
83     std::memset(s1_3, '\0', sizeof(s1_3));
84     std::memset(s1_4, '\0', sizeof(s1_4));
85     {
86         test_ostream os(testfile, TEST_STREAM_FLAGS);
87         test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
88         oa << boost::serialization::make_nvp(
89             "s1",
90             boost::serialization::make_binary_object(
91                 s1,
92                 sizeof(s1)
93             )
94         );
95         oa << boost::serialization::make_nvp(
96             "s2",
97             boost::serialization::make_binary_object(
98                 s2,
99                 sizeof(s2)
100             )
101         );
102         oa << boost::serialization::make_nvp(
103             "s3",
104             boost::serialization::make_binary_object(
105                 s3,
106                 sizeof(s3)
107             )
108         );
109         oa << boost::serialization::make_nvp(
110             "s4",
111             boost::serialization::make_binary_object(
112                 s4,
113                 sizeof(s4)
114             )
115         );
116         oa << BOOST_SERIALIZATION_NVP(a);
117         // note: add a little bit on the end of the archive to detect
118         // failure of text mode binary.
119         oa << BOOST_SERIALIZATION_NVP(i);
120     }
121     {
122         test_istream is(testfile, TEST_STREAM_FLAGS);
123         test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
124         ia >> boost::serialization::make_nvp(
125             "s1",
126             boost::serialization::make_binary_object(
127                 s1_1,
128                 sizeof(s1)
129             )
130         );
131         ia >> boost::serialization::make_nvp(
132             "s2",
133             boost::serialization::make_binary_object(
134                 s1_2,
135                 sizeof(s2)
136             )
137         );
138         ia >> boost::serialization::make_nvp(
139             "s3",
140             boost::serialization::make_binary_object(
141                 s1_3,
142                 sizeof(s3)
143             )
144         );
145         ia >> boost::serialization::make_nvp(
146             "s4",
147             boost::serialization::make_binary_object(
148                 s1_4,
149                 sizeof(s4)
150             )
151         );
152         ia >> BOOST_SERIALIZATION_NVP(a1);
153         // note: add a little bit on the end of the archive to detect
154         // failure of text mode binary.
155         ia >> BOOST_SERIALIZATION_NVP(i1);
156     }
157     BOOST_CHECK(0 == std::strcmp(s1, s1_1));
158     BOOST_CHECK(0 == std::strcmp(s2, s1_2));
159     BOOST_CHECK(0 == std::strcmp(s3, s1_3));
160     BOOST_CHECK(0 == std::strcmp(s4, s1_4));
161     BOOST_CHECK(a == a1);
162     BOOST_CHECK(i == i1);
163     std::remove(testfile);
164     return EXIT_SUCCESS;
165 }
166 
167 // EOF
168