1 /*
2   Copyright (c) 2014, Randolph Voorhies, Shane Grant
3   All rights reserved.
4 
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions are met:
7       * Redistributions of source code must retain the above copyright
8         notice, this list of conditions and the following disclaimer.
9       * Redistributions in binary form must reproduce the above copyright
10         notice, this list of conditions and the following disclaimer in the
11         documentation and/or other materials provided with the distribution.
12       * Neither the name of cereal nor the
13         names of its contributors may be used to endorse or promote products
14         derived from this software without specific prior written permission.
15 
16   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19   DISCLAIMED. IN NO EVENT SHALL RANDOLPH VOORHIES AND SHANE GRANT BE LIABLE FOR ANY
20   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
28 #include "basic_string.hpp"
29 
30 TEST_SUITE("basic_string");
31 
32 TEST_CASE("binary_string")
33 {
34   test_string_all<cereal::BinaryInputArchive, cereal::BinaryOutputArchive>();
35 }
36 
37 TEST_CASE("portable_binary_string")
38 {
39   test_string_all<cereal::PortableBinaryInputArchive, cereal::PortableBinaryOutputArchive>();
40 }
41 
42 TEST_CASE("xml_string_basic")
43 {
44   test_string_basic<cereal::XMLInputArchive, cereal::XMLOutputArchive>();
45 }
46 
47 TEST_CASE("json_string_basic")
48 {
49   test_string_basic<cereal::JSONInputArchive, cereal::JSONOutputArchive>();
50 }
51 
52 template <class IArchive, class OArchive, class Out, class In = Out>
test_ws_in_out(Out const & o_value_with_ws)53 void test_ws_in_out(Out const & o_value_with_ws)
54 {
55   std::ostringstream os;
56   {
57     OArchive oar(os);
58     oar(o_value_with_ws);
59   }
60 
61   In i_value_with_ws;
62 
63   std::istringstream is(os.str());
64   {
65     IArchive iar(is);
66     iar(i_value_with_ws);
67   }
68 
69   CHECK(i_value_with_ws == o_value_with_ws);
70 }
71 
72 TEST_CASE("xml_string_issue109")
73 {
74   char strings[][20] = {
75     "some text",
76     "some text ",
77     " some text",
78     " some text ",
79     "  ",
80     "    text    ",
81     " ]]> ",
82     " &gt; > ]]> ",
83     " < <]>] &lt; ",
84     " &amp; &   "
85   };
86 
87   for( size_t i=0; i<( sizeof( strings ) / sizeof( strings[0] ) ); ++i )
88   {
89     std::basic_string<char> o_string = strings[i];
90 
91     test_ws_in_out<cereal::XMLInputArchive, cereal::XMLOutputArchive>( o_string );
92   }
93 }
94 
95 TEST_CASE("xml_char_issue109")
96 {
97   uint8_t chars[] = {
98     ' ',
99     '\t',
100     '\n',
101     '\r',
102     '&',
103     '>',
104     '<',
105     '\'',
106     '"',
107     '!',
108     '|'
109   };
110 
111   for( size_t i=0; i<( sizeof( chars ) / sizeof( chars[0] ) ); ++i )
112   {
113     test_ws_in_out<cereal::XMLInputArchive, cereal::XMLOutputArchive>( chars[i] );
114   }
115 
116   for( size_t i=0; i<( sizeof( chars ) / sizeof( chars[0] ) ); ++i )
117   {
118     test_ws_in_out<cereal::XMLInputArchive, cereal::XMLOutputArchive>( int8_t( chars[i] ) );
119   }
120 
121   for( size_t i=0; i<( sizeof( chars ) / sizeof( chars[0] ) ); ++i )
122   {
123     test_ws_in_out<cereal::XMLInputArchive, cereal::XMLOutputArchive>( char( chars[i] ) );
124   }
125 }
126 
127 template <class IArchive, class OArchive, class Out, size_t Nb, class In = Out>
test_ws_in_out_array(Out const (& o_a_value_with_ws)[Nb])128 void test_ws_in_out_array(Out const (&o_a_value_with_ws)[Nb])
129 {
130     std::ostringstream os;
131     {
132         OArchive oar(os);
133         for (const auto& o_value_with_ws : o_a_value_with_ws)
134         {
135             oar(o_value_with_ws);
136         }
137     }
138 
139     In i_a_value_with_ws[Nb];
140 
141     std::istringstream is(os.str());
142     {
143         IArchive iar(is);
144         for (In& i_value_with_ws : i_a_value_with_ws)
145         {
146             iar(i_value_with_ws);
147         }
148     }
149 
150     for (size_t uiIndex = 0; uiIndex < Nb; ++uiIndex)
151     {
152         CHECK(i_a_value_with_ws[uiIndex] == o_a_value_with_ws[uiIndex]);
153     }
154 }
155 
156 TEST_CASE("xml_string_issue_consecutive_calls")
157 {
158     std::string strings[] = {
159         "some text",
160         " some text",
161         " some text ",
162         "Long text without ws at the end",
163         "some text ",
164         " some text",
165         " some text ",
166     };
167 
168     test_ws_in_out_array<cereal::XMLInputArchive, cereal::XMLOutputArchive>(strings);
169 }
170 
171 TEST_SUITE_END();
172