Line data Source code
1 : // Copyright (c) 2021 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/
4 : // contact@m2osw.com
5 : //
6 : // This program is free software; you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation; either version 2 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License along
17 : // with this program; if not, write to the Free Software Foundation, Inc.,
18 : // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 :
20 : /** \file
21 : * \brief Verify the various __int128 output support.
22 : *
23 : * This file implements tests to verify that the support to print out
24 : * large numbers works as expected.
25 : */
26 :
27 : // self
28 : //
29 : #include "catch_main.h"
30 :
31 :
32 :
33 : // snapdev lib
34 : //
35 : #include <snapdev/ostream_int128.h>
36 :
37 :
38 : // last include
39 : //
40 : #include <snapdev/poison.h>
41 :
42 :
43 : #pragma GCC diagnostic ignored "-Wpedantic"
44 :
45 :
46 7 : CATCH_TEST_CASE("ostream_int128", "[ostream][int128]")
47 : {
48 10 : CATCH_START_SECTION("ostream_int128: small numbers (-10 to +10)")
49 : {
50 22 : for(int i(-10); i <= 10; ++i)
51 : {
52 21 : __int128 l(i);
53 42 : std::stringstream ss;
54 21 : ss << l;
55 21 : CATCH_REQUIRE(ss.str() == std::to_string(i));
56 : }
57 : }
58 : CATCH_END_SECTION()
59 :
60 10 : CATCH_START_SECTION("ostream_int128: positive numbers")
61 : {
62 1001 : for(int i(0); i < 1000; ++i)
63 : {
64 1000 : std::int32_t v((rand() & 0x7FFFFFFF));
65 1000 : __int128 l(v);
66 2000 : std::stringstream ss;
67 1000 : ss << l;
68 1000 : CATCH_REQUIRE(ss.str() == std::to_string(v));
69 : }
70 : }
71 : CATCH_END_SECTION()
72 :
73 10 : CATCH_START_SECTION("ostream_int128: negative numbers")
74 : {
75 1001 : for(int i(0); i < 1000; ++i)
76 : {
77 1000 : std::int32_t v(-(rand() & 0x7FFFFFFF));
78 1000 : __int128 l(v);
79 2000 : std::stringstream ss;
80 1000 : ss << l;
81 1000 : CATCH_REQUIRE(ss.str() == std::to_string(v));
82 : }
83 : }
84 : CATCH_END_SECTION()
85 :
86 10 : CATCH_START_SECTION("ostream_int128: most positive number")
87 : {
88 1 : unsigned __int128 l(0);
89 1 : l = ~l;
90 2 : std::stringstream ss;
91 1 : ss << l;
92 1 : CATCH_REQUIRE(ss.str() == "340282366920938463463374607431768211455");
93 : }
94 : CATCH_END_SECTION()
95 :
96 10 : CATCH_START_SECTION("ostream_int128: most negative number")
97 : {
98 1 : __int128 l(1);
99 1 : l <<= 127;
100 2 : std::stringstream ss;
101 1 : ss << l;
102 1 : CATCH_REQUIRE(ss.str() == "-170141183460469231731687303715884105728");
103 : }
104 : CATCH_END_SECTION()
105 11 : }
106 :
107 :
108 : // vim: ts=4 sw=4 et
|