Line data Source code
1 : /*
2 : * Copyright (c) 2018-2019 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 that the log2() function.
24 : *
25 : * This file implements test for the log2() function.
26 : */
27 :
28 : // self
29 : //
30 : #include "main.h"
31 :
32 :
33 :
34 : // snapdev lib
35 : //
36 : #include "snapdev/log2.h"
37 :
38 :
39 :
40 :
41 :
42 4 : CATCH_TEST_CASE("log2", "[math]")
43 : {
44 4 : CATCH_START_SECTION("powers of 2")
45 : {
46 65 : for(int i = 0; i < 64; ++i)
47 : {
48 64 : std::uint64_t v = 1ULL << i;
49 64 : CATCH_REQUIRE(snap::log2(v) == i);
50 : }
51 : }
52 : CATCH_END_SECTION()
53 :
54 4 : CATCH_START_SECTION("random numbers")
55 : {
56 65 : for(int i = 0; i < 64; ++i)
57 : {
58 64 : std::uint64_t r = (static_cast<std::uint64_t>(rand()) << 32) ^ static_cast<std::uint64_t>(rand());
59 64 : r &= (1ULL << i) - 1ULL;
60 64 : std::uint64_t v = (1ULL << i) | r;
61 :
62 : // same difference, all the lower bits are ignored in the
63 : // computation
64 : //
65 64 : CATCH_REQUIRE(snap::log2(v) == i);
66 : }
67 : }
68 : CATCH_END_SECTION()
69 8 : }
70 :
71 :
72 :
73 : // vim: ts=4 sw=4 et
|