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