]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Unit test for boost::lexical_cast. |
2 | // | |
3 | // See http://www.boost.org for most recent version, including documentation. | |
4 | // | |
92f5a8d4 | 5 | // Copyright Antony Polukhin, 2014-2019. |
7c673cae FG |
6 | // |
7 | // Distributed under the Boost | |
8 | // Software License, Version 1.0. (See accompanying file | |
9 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt). | |
10 | ||
11 | #include <boost/config.hpp> | |
12 | ||
13 | #include <boost/lexical_cast/try_lexical_convert.hpp> | |
14 | #include <boost/test/unit_test.hpp> | |
15 | ||
16 | using namespace boost::conversion; | |
17 | ||
18 | void try_uncommon_cases() | |
19 | { | |
20 | std::string sres; | |
21 | const bool res1 = try_lexical_convert(std::string("Test string"), sres); | |
22 | BOOST_CHECK(res1); | |
23 | BOOST_CHECK_EQUAL(sres, "Test string"); | |
24 | ||
25 | volatile int vires; | |
26 | const bool res2 = try_lexical_convert(100, vires); | |
27 | BOOST_CHECK(res2); | |
28 | BOOST_CHECK_EQUAL(vires, 100); | |
29 | ||
30 | const bool res3 = try_lexical_convert("Test string", sres); | |
31 | BOOST_CHECK(res3); | |
32 | BOOST_CHECK_EQUAL(sres, "Test string"); | |
33 | ||
34 | const bool res4 = try_lexical_convert("Test string", sizeof("Test string") - 1, sres); | |
35 | BOOST_CHECK(res4); | |
36 | BOOST_CHECK_EQUAL(sres, "Test string"); | |
37 | ||
38 | int ires; | |
39 | BOOST_CHECK(!try_lexical_convert("Test string", ires)); | |
40 | BOOST_CHECK(!try_lexical_convert(1.1, ires)); | |
41 | BOOST_CHECK(!try_lexical_convert(-1.9, ires)); | |
42 | BOOST_CHECK(!try_lexical_convert("1.1", ires)); | |
43 | BOOST_CHECK(!try_lexical_convert("1000000000000000000000000000000000000000", ires)); | |
44 | } | |
45 | ||
46 | ||
47 | void try_common_cases() | |
48 | { | |
49 | int ires = 0; | |
50 | const bool res1 = try_lexical_convert(std::string("100"), ires); | |
51 | BOOST_CHECK(res1); | |
52 | BOOST_CHECK_EQUAL(ires, 100); | |
53 | ||
54 | ires = 0; | |
55 | const bool res2 = try_lexical_convert("-100", ires); | |
56 | BOOST_CHECK(res2); | |
57 | BOOST_CHECK_EQUAL(ires, -100); | |
58 | ||
59 | float fres = 1.0f; | |
60 | const bool res3 = try_lexical_convert("0.0", fres); | |
61 | BOOST_CHECK(res3); | |
62 | BOOST_CHECK_EQUAL(fres, 0.0f); | |
63 | ||
64 | fres = 1.0f; | |
65 | const bool res4 = try_lexical_convert("0.0", sizeof("0.0") - 1, fres); | |
66 | BOOST_CHECK(res4); | |
67 | BOOST_CHECK_EQUAL(fres, 0.0f); | |
68 | } | |
69 | ||
70 | boost::unit_test::test_suite *init_unit_test_suite(int, char *[]) | |
71 | { | |
72 | boost::unit_test::test_suite *suite = | |
73 | BOOST_TEST_SUITE("Tests for try_lexical_convert"); | |
74 | suite->add(BOOST_TEST_CASE(&try_uncommon_cases)); | |
75 | suite->add(BOOST_TEST_CASE(&try_common_cases)); | |
76 | ||
77 | return suite; | |
78 | } |