[CMake] Create a real library instead of an object library for stress tests
[libcds.git] / test / unit / misc / bit_reversal.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 #include <cds/algo/bit_reversal.h>
33
34 namespace {
35
36     template <typename UInt>
37     class bit_reversal: public ::testing::Test
38     {
39         typedef UInt uint_type;
40         static std::vector<uint_type> arr_;
41         static size_t const c_size = 100'000'000;
42
43     public:
44         static void SetUpTestCase()
45         {
46             arr_.resize( c_size );
47             for ( size_t i = 0; i < c_size; ++i )
48                 arr_[i] = static_cast<uint_type>((i << 32) + (~i));
49         }
50
51         static void TearDownTestCase()
52         {
53             arr_.resize( 0 );
54         }
55
56         template <typename Algo>
57         void test()
58         {
59             Algo f;
60             for ( auto i : arr_ ) {
61                 EXPECT_EQ( i, f( f( i )));
62             }
63         }
64
65         template <typename Algo>
66         void test_eq()
67         {
68             Algo f;
69             for ( auto i : arr_ ) {
70                 EXPECT_EQ( cds::algo::bit_reversal::swar()( i ), f( i ) ) << "i=" << i;
71             }
72         }
73     };
74
75     template <typename UInt> std::vector<UInt> bit_reversal<UInt>::arr_;
76
77     typedef bit_reversal<uint32_t> bit_reversal32;
78     typedef bit_reversal<uint64_t> bit_reversal64;
79
80 #define TEST_32BIT( x ) \
81     TEST_F( bit_reversal32, x )      { test<cds::algo::bit_reversal::x>(); } \
82     TEST_F( bit_reversal32, x##_eq ) { test_eq<cds::algo::bit_reversal::x>(); }
83
84 #define TEST_64BIT( x ) \
85     TEST_F( bit_reversal64, x )      { test<cds::algo::bit_reversal::x>(); } \
86     TEST_F( bit_reversal64, x##_eq ) { test_eq<cds::algo::bit_reversal::x>(); }
87
88     TEST_32BIT( swar )
89     TEST_32BIT( lookup )
90     TEST_32BIT( muldiv )
91
92     TEST_64BIT( swar )
93     TEST_64BIT( lookup )
94     TEST_64BIT( muldiv )
95
96 } // namespace