libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
string_conversions.cpp
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright © 2017 by J.M.McGuiness, coder@hussar.me.uk
3 **
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Lesser General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2.1 of the License, or (at your option) any later version.
8 **
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public
15 ** License along with this library; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
19 #include "stdafx.h"
20 
21 #define BOOST_TEST_MODULE libjmmcg_tests
22 #include <boost/test/included/unit_test.hpp>
23 
24 #include <boost/mpl/list.hpp>
25 
26 #include "core/ttypes.hpp"
27 
28 using namespace libjmmcg;
29 
30 typedef boost::mpl::list<
31  char[3],
32  char[1024]
34 
35 BOOST_AUTO_TEST_SUITE(conversions_tests)
36 
37 BOOST_AUTO_TEST_SUITE(tostrings)
38 
39 BOOST_AUTO_TEST_SUITE(as_ptr)
40 
41 BOOST_AUTO_TEST_CASE_TEMPLATE(zero, buffer_t, buffer_types) {
42  buffer_t buffer{};
43  const auto len=tostring(0, buffer, sizeof(buffer));
44  BOOST_CHECK_EQUAL(tostring(0), buffer);
45  BOOST_CHECK_EQUAL(1, len);
46 }
47 
48 BOOST_AUTO_TEST_CASE_TEMPLATE(one, buffer_t, buffer_types) {
49  buffer_t buffer{};
50  const auto len=tostring(1, buffer, sizeof(buffer));
51  BOOST_CHECK_EQUAL(tostring(1), buffer);
52  BOOST_CHECK_EQUAL(1, len);
53 }
54 
55 BOOST_AUTO_TEST_CASE_TEMPLATE(two, buffer_t, buffer_types) {
56  buffer_t buffer{};
57  const auto len=tostring(2, buffer, sizeof(buffer));
58  BOOST_CHECK_EQUAL(tostring(2), buffer);
59  BOOST_CHECK_EQUAL(1, len);
60 }
61 
62 BOOST_AUTO_TEST_CASE_TEMPLATE(ten, buffer_t, buffer_types) {
63  buffer_t buffer{};
64  const auto len=tostring(10, buffer, sizeof(buffer));
65  BOOST_CHECK_EQUAL(tostring(10), buffer);
66  BOOST_CHECK_EQUAL(2, len);
67 }
68 
69 BOOST_AUTO_TEST_CASE_TEMPLATE(fourty_two, buffer_t, buffer_types) {
70  buffer_t buffer{};
71  const auto len=tostring(42, buffer, sizeof(buffer));
72  BOOST_CHECK_EQUAL(tostring(42), buffer);
73  BOOST_CHECK_EQUAL(2, len);
74 }
75 
76 BOOST_AUTO_TEST_SUITE_END()
77 
78 BOOST_AUTO_TEST_SUITE(as_array)
79 
80 BOOST_AUTO_TEST_CASE_TEMPLATE(zero, buffer_t, buffer_types) {
81  buffer_t buffer{};
82  const auto len=tostring(0UL, buffer);
83  BOOST_CHECK_EQUAL(tostring(0), buffer);
84  BOOST_CHECK_EQUAL(1, len);
85 }
86 
87 BOOST_AUTO_TEST_CASE_TEMPLATE(one, buffer_t, buffer_types) {
88  buffer_t buffer{};
89  const auto len=tostring(1UL, buffer);
90  BOOST_CHECK_EQUAL(tostring(1), buffer);
91  BOOST_CHECK_EQUAL(1, len);
92 }
93 
94 BOOST_AUTO_TEST_CASE_TEMPLATE(two, buffer_t, buffer_types) {
95  buffer_t buffer{};
96  const auto len=tostring(2UL, buffer);
97  BOOST_CHECK_EQUAL(tostring(2), buffer);
98  BOOST_CHECK_EQUAL(1, len);
99 }
100 
101 BOOST_AUTO_TEST_CASE_TEMPLATE(ten, buffer_t, buffer_types) {
102  buffer_t buffer{};
103  const auto len=tostring(10UL, buffer);
104  BOOST_CHECK_EQUAL(tostring(10), buffer);
105  BOOST_CHECK_EQUAL(2, len);
106 }
107 
108 BOOST_AUTO_TEST_CASE_TEMPLATE(fourty_two, buffer_t, buffer_types) {
109  buffer_t buffer{};
110  const auto len=tostring(42UL, buffer);
111  BOOST_CHECK_EQUAL(tostring(42), buffer);
112  BOOST_CHECK_EQUAL(2, len);
113 }
114 
115 BOOST_AUTO_TEST_SUITE_END()
116 
117 BOOST_AUTO_TEST_SUITE_END()
118 
119 BOOST_AUTO_TEST_SUITE(fromstrings)
120 
121 BOOST_AUTO_TEST_CASE(zero) {
122  const char buff[]="0";
123  const long res0=fromstring<int>(buff, sizeof(buff)-1);
124  BOOST_CHECK_EQUAL(0, res0);
125  const long res0u=fromstring<unsigned int>(buff, sizeof(buff)-1);
126  BOOST_CHECK_EQUAL(0, res0u);
127  const long res1=fromstring<long>(buff, sizeof(buff)-1);
128  BOOST_CHECK_EQUAL(0, res1);
129  const long res1u=fromstring<unsigned long>(buff, sizeof(buff)-1);
130  BOOST_CHECK_EQUAL(0, res1u);
131  const long res2s=fromstring<std::size_t>(buff, sizeof(buff)-1);
132  BOOST_CHECK_EQUAL(0, res2s);
133  const double res2=fromstring<double>(buff, sizeof(buff)-1);
134  BOOST_CHECK_EQUAL(0, res2);
135 }
136 
137 BOOST_AUTO_TEST_CASE(one) {
138  const char buff[]="1";
139  const long res0=fromstring<int>(buff, sizeof(buff)-1);
140  BOOST_CHECK_EQUAL(1, res0);
141  const long res0u=fromstring<unsigned int>(buff, sizeof(buff)-1);
142  BOOST_CHECK_EQUAL(1, res0u);
143  const long res1=fromstring<long>(buff, sizeof(buff)-1);
144  BOOST_CHECK_EQUAL(1, res1);
145  const long res1u=fromstring<unsigned long>(buff, sizeof(buff)-1);
146  BOOST_CHECK_EQUAL(1, res1u);
147  const long res2s=fromstring<std::size_t>(buff, sizeof(buff)-1);
148  BOOST_CHECK_EQUAL(1, res2s);
149  const double res2=fromstring<double>(buff, sizeof(buff)-1);
150  BOOST_CHECK_EQUAL(1, res2);
151 }
152 
153 BOOST_AUTO_TEST_CASE(two) {
154  const char buff[]="2";
155  const long res0=fromstring<int>(buff, sizeof(buff)-1);
156  BOOST_CHECK_EQUAL(2, res0);
157  const long res0u=fromstring<unsigned int>(buff, sizeof(buff)-1);
158  BOOST_CHECK_EQUAL(2, res0u);
159  const long res1=fromstring<long>(buff, sizeof(buff)-1);
160  BOOST_CHECK_EQUAL(2, res1);
161  const long res1u=fromstring<unsigned long>(buff, sizeof(buff)-1);
162  BOOST_CHECK_EQUAL(2, res1u);
163  const long res2s=fromstring<std::size_t>(buff, sizeof(buff)-1);
164  BOOST_CHECK_EQUAL(2, res2s);
165  const double res2=fromstring<double>(buff, sizeof(buff)-1);
166  BOOST_CHECK_EQUAL(2, res2);
167 }
168 
169 BOOST_AUTO_TEST_CASE(e) {
170  const char buff[]="2.71828182846";
171  const double res=fromstring<double>(buff, sizeof(buff)-1);
172  BOOST_CHECK_EQUAL(2.71828182846, res);
173 }
174 
175 BOOST_AUTO_TEST_CASE(pi) {
176  const char buff[]="3.14159265359";
177  const double res=fromstring<double>(buff, sizeof(buff)-1);
178  BOOST_CHECK_EQUAL(3.14159265359, res);
179 }
180 
181 BOOST_AUTO_TEST_CASE(ten) {
182  const char buff[]="10";
183  const long res1=fromstring<long>(buff, sizeof(buff)-1);
184  BOOST_CHECK_EQUAL(10, res1);
185  const double res2=fromstring<double>(buff, sizeof(buff)-1);
186  BOOST_CHECK_EQUAL(10, res2);
187 }
188 
189 BOOST_AUTO_TEST_CASE(fourtytwo) {
190  const char buff[]="42";
191  const long res0=fromstring<int>(buff, sizeof(buff)-1);
192  BOOST_CHECK_EQUAL(42, res0);
193  const long res0u=fromstring<unsigned int>(buff, sizeof(buff)-1);
194  BOOST_CHECK_EQUAL(42, res0u);
195  const long res1=fromstring<long>(buff, sizeof(buff)-1);
196  BOOST_CHECK_EQUAL(42, res1);
197  const long res1u=fromstring<unsigned long>(buff, sizeof(buff)-1);
198  BOOST_CHECK_EQUAL(42, res1u);
199  const long res2s=fromstring<std::size_t>(buff, sizeof(buff)-1);
200  BOOST_CHECK_EQUAL(42, res2s);
201  const double res2=fromstring<double>(buff, sizeof(buff)-1);
202  BOOST_CHECK_EQUAL(42, res2);
203 }
204 
205 BOOST_AUTO_TEST_SUITE_END()
206 
207 BOOST_AUTO_TEST_SUITE_END()