Uses different pass count for different parallel queue test cases
[libcds.git] / test / unit / misc / bitop.cpp
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
15     * Redistributions in binary form must reproduce the above copyright notice,
16       this list of conditions and the following disclaimer in the documentation
17       and/or other materials provided with the distribution.
18
19     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <cds_test/ext_gtest.h>
32
33 #include <cds/algo/int_algo.h>
34 //#include <cds/details/bit_reverse_counter.h>
35
36 namespace {
37     class bitop : public ::testing::Test
38     {};
39
40     TEST_F( bitop, bitop32 )
41     {
42         uint32_t n = 0;
43
44         EXPECT_EQ( cds::bitop::MSB(n), 0 ) << "n=" << n;
45         EXPECT_EQ( cds::bitop::LSB( n ), 0 ) << "n=" << n;
46         EXPECT_EQ( cds::bitop::SBC( n ), 0 ) << "n=" << n;
47         EXPECT_EQ( cds::bitop::ZBC( n ), static_cast<int>( sizeof( n ) * 8 )) << "n=" << n;
48
49         int nBit = 1;
50         for ( n = 1; n != 0; n *= 2 ) {
51             EXPECT_EQ( cds::bitop::MSB( n ), nBit ) << "n=" << n;
52             EXPECT_EQ( cds::bitop::LSB( n ), nBit ) << "n=" << n;
53             EXPECT_EQ( cds::bitop::MSBnz( n ), nBit - 1 ) << "n=" << n;
54             EXPECT_EQ( cds::bitop::LSBnz( n ), nBit - 1 ) << "n=" << n;
55             EXPECT_EQ( cds::bitop::SBC( n ), 1 ) << "n=" << n;
56             EXPECT_EQ( cds::bitop::ZBC( n ), static_cast<int>( sizeof( n ) * 8 - 1 )) << "n=" << n;
57
58             ++nBit;
59         }
60     }
61
62     TEST_F( bitop, bitop64 )
63     {
64         uint64_t n = 0;
65
66         EXPECT_EQ( cds::bitop::MSB( n ), 0 ) << "n=" << n;
67         EXPECT_EQ( cds::bitop::LSB( n ), 0 ) << "n=" << n;
68         EXPECT_EQ( cds::bitop::SBC( n ), 0 ) << "n=" << n;
69         EXPECT_EQ( cds::bitop::ZBC( n ), static_cast<int>( sizeof( n ) * 8 )) << "n=" << n;
70
71         int nBit = 1;
72         for ( n = 1; n != 0; n *= 2 ) {
73             EXPECT_EQ( cds::bitop::MSB( n ), nBit ) << "n=" << n;
74             EXPECT_EQ( cds::bitop::LSB( n ), nBit ) << "n=" << n;
75             EXPECT_EQ( cds::bitop::MSBnz( n ), nBit - 1 ) << "n=" << n;
76             EXPECT_EQ( cds::bitop::LSBnz( n ), nBit - 1 ) << "n=" << n;
77             EXPECT_EQ( cds::bitop::SBC( n ), 1 ) << "n=" << n;
78             EXPECT_EQ( cds::bitop::ZBC( n ), static_cast<int>( sizeof( n ) * 8 - 1 )) << "n=" << n;
79
80             ++nBit;
81         }
82     }
83
84     TEST_F( bitop, floor_pow2 )
85     {
86         EXPECT_EQ( cds::beans::floor2( 0u ), 1u );
87         EXPECT_EQ( cds::beans::floor2( 1u ), 1u );
88         EXPECT_EQ( cds::beans::floor2( 2u ), 2u );
89         EXPECT_EQ( cds::beans::floor2( 3u ), 2u );
90         EXPECT_EQ( cds::beans::floor2( 4u ), 4u );
91         EXPECT_EQ( cds::beans::floor2( 5u ), 4u );
92         EXPECT_EQ( cds::beans::floor2( 7u ), 4u );
93         EXPECT_EQ( cds::beans::floor2( 8u ), 8u );
94         EXPECT_EQ( cds::beans::floor2( 9u ), 8u );
95
96         for ( uint32_t n = 2; n; n <<= 1 )
97         {
98             EXPECT_EQ( cds::beans::floor2( n - 1 ), n / 2 );
99             EXPECT_EQ( cds::beans::floor2( n + 1 ), n );
100         }
101
102         for ( uint64_t n = 2; n; n <<= 1 )
103         {
104             EXPECT_EQ( cds::beans::floor2( n - 1 ), n / 2 );
105             EXPECT_EQ( cds::beans::floor2( n ), n );
106             EXPECT_EQ( cds::beans::floor2( n + 1 ), n );
107         }
108     }
109
110     TEST_F( bitop, ceil_pow2 )
111     {
112         EXPECT_EQ( cds::beans::ceil2( 0u ), 1u );
113         EXPECT_EQ( cds::beans::ceil2( 1u ), 1u );
114         EXPECT_EQ( cds::beans::ceil2( 2u ), 2u );
115         EXPECT_EQ( cds::beans::ceil2( 3u ), 4u );
116         EXPECT_EQ( cds::beans::ceil2( 4u ), 4u );
117         EXPECT_EQ( cds::beans::ceil2( 5u ), 8u );
118         EXPECT_EQ( cds::beans::ceil2( 7u ), 8u );
119         EXPECT_EQ( cds::beans::ceil2( 8u ), 8u );
120         EXPECT_EQ( cds::beans::ceil2( 9u ), 16u );
121
122         for ( uint32_t n = 4; n < (uint32_t(1) << 31); n <<= 1 )
123         {
124             EXPECT_EQ( cds::beans::ceil2( n - 1 ), n );
125             EXPECT_EQ( cds::beans::ceil2( n ), n );
126             EXPECT_EQ( cds::beans::ceil2( n + 1 ), n * 2 );
127         }
128
129         for ( uint64_t n = 4; n < (uint64_t(1) << 63); n <<= 1 )
130         {
131             EXPECT_EQ( cds::beans::ceil2( n - 1 ), n );
132             EXPECT_EQ( cds::beans::ceil2( n ), n );
133             EXPECT_EQ( cds::beans::ceil2( n + 1 ), n * 2 );
134         }
135     }
136
137     /*
138     TEST_F( bitop, bit_reverse_counter )
139     {
140         cds::bitop::bit_reverse_counter<> c;
141
142         while ( c.value() < 8 ) {
143             size_t res = c.inc();
144             std::cout << "inc result: " << res
145                       << " value: " << c.value()
146                       << " reversed: " << c.reversed_value()
147                       << " high_bit: " << c.high_bit() << "\n";
148         }
149
150         while ( c.value() > 0 ) {
151             size_t res = c.dec();
152             std::cout << "dec result: " << res
153                 << " value: " << c.value()
154                 << " reversed: " << c.reversed_value()
155                 << " high_bit: " << c.high_bit() << "\n";
156         }
157     }
158     */
159
160 } // namespace