Line data Source code
1 : // Copyright (c) 2018-2021 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/snapdev
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 that the log2() function.
22 : *
23 : * This file implements test for the log2() function.
24 : */
25 :
26 : // self
27 : //
28 : #include "catch_main.h"
29 :
30 :
31 :
32 : // snapdev lib
33 : //
34 : #include <snapdev/log2.h>
35 :
36 :
37 : // last include
38 : //
39 : #include <snapdev/poison.h>
40 :
41 :
42 :
43 :
44 :
45 :
46 5 : CATCH_TEST_CASE("log2", "[math]")
47 : {
48 6 : CATCH_START_SECTION("log2: zero")
49 : {
50 1 : CATCH_REQUIRE(snap::log2(0) == -1);
51 : }
52 : CATCH_END_SECTION()
53 :
54 6 : CATCH_START_SECTION("log2: powers of 2")
55 : {
56 65 : for(int i = 0; i < 64; ++i)
57 : {
58 64 : std::uint64_t const v = 1ULL << i;
59 64 : CATCH_REQUIRE(snap::log2(v) == i);
60 : }
61 : }
62 : CATCH_END_SECTION()
63 :
64 6 : CATCH_START_SECTION("log2: random numbers")
65 : {
66 65 : for(int i = 0; i < 64; ++i)
67 : {
68 64 : std::uint64_t r = (static_cast<std::uint64_t>(rand()) << 32) ^ static_cast<std::uint64_t>(rand());
69 64 : r &= (1ULL << i) - 1ULL;
70 64 : std::uint64_t const v = (1ULL << i) | r;
71 :
72 : // same difference, all the lower bits are ignored in the
73 : // computation
74 : //
75 64 : CATCH_REQUIRE(snap::log2(v) == i);
76 : }
77 : }
78 : CATCH_END_SECTION()
79 9 : }
80 :
81 :
82 :
83 : // vim: ts=4 sw=4 et
|