libmoost
|
00001 /* vim:set ts=3 sw=3 sts=3 et: */ 00028 // Include boost test framework required headers 00029 #include <boost/test/unit_test.hpp> 00030 #include <boost/test/test_tools.hpp> 00031 #include <boost/cstdint.hpp> 00032 00033 // Include CRT/STL required header(s) 00034 #include <stdexcept> 00035 #include <set> 00036 00037 // Include thrift headers 00038 00039 // Include application required header(s) 00040 #include "../../include/moost/utils/bits.hpp" 00041 00042 // Imported required namespace(s) 00043 using namespace moost::utils; 00044 00045 // Name the test suite 00046 BOOST_AUTO_TEST_SUITE( MoostUtilsBitsTest ) 00047 00048 // Define the test fixture 00049 struct Fixture 00050 { 00051 // C_tor 00052 Fixture() 00053 { 00054 } 00055 00056 // D_tor 00057 ~Fixture() 00058 { 00059 } 00060 }; 00061 00062 BOOST_FIXTURE_TEST_CASE( test_utils_next_power_of_two, Fixture ) 00063 { 00064 // We make use of this utility is KvdsPageStore to let's just check it works! 00065 00066 // 0 bit 00067 BOOST_CHECK(0x00U == next_power_of_two(0x00U)); 00068 00069 // 8 bit 00070 BOOST_CHECK(0x10U == next_power_of_two(0x0FU)); 00071 BOOST_CHECK(0x20U == next_power_of_two(0x1FU)); 00072 BOOST_CHECK(0x40U == next_power_of_two(0x2FU)); 00073 BOOST_CHECK(0x80U == next_power_of_two(0x4FU)); 00074 00075 // 16 bit 00076 BOOST_CHECK(0x1000U == next_power_of_two(0x0F00U)); 00077 BOOST_CHECK(0x2000U == next_power_of_two(0x1F00U)); 00078 BOOST_CHECK(0x4000U == next_power_of_two(0x2F00U)); 00079 BOOST_CHECK(0x8000U == next_power_of_two(0x4F00U)); 00080 00081 // 32 bit 00082 BOOST_CHECK(0x10000000U == next_power_of_two(0x0F000000U)); 00083 BOOST_CHECK(0x20000000U == next_power_of_two(0x1F000000U)); 00084 BOOST_CHECK(0x40000000U == next_power_of_two(0x2F000000U)); 00085 BOOST_CHECK(0x80000000U == next_power_of_two(0x4F000000U)); 00086 00087 // 64 bit 00088 BOOST_CHECK(UINT64_C(0x1000000000000000) == next_power_of_two(UINT64_C(0x0F00000000000000))); 00089 BOOST_CHECK(UINT64_C(0x2000000000000000) == next_power_of_two(UINT64_C(0x1F00000000000000))); 00090 BOOST_CHECK(UINT64_C(0x4000000000000000) == next_power_of_two(UINT64_C(0x2F00000000000000))); 00091 BOOST_CHECK(UINT64_C(0x8000000000000000) == next_power_of_two(UINT64_C(0x4F00000000000000))); 00092 } 00093 00094 BOOST_FIXTURE_TEST_CASE( test_utils_is_power_of_two, Fixture ) 00095 { 00096 // We make use of this utility is KvdsPageStore to let's just check it works! 00097 00098 // 0 bit 00099 BOOST_CHECK(is_power_of_two(0x00U)); 00100 00101 // 8 bit 00102 BOOST_CHECK(!is_power_of_two(0x0FU)); 00103 BOOST_CHECK(!is_power_of_two(0x1FU)); 00104 BOOST_CHECK(!is_power_of_two(0x2FU)); 00105 BOOST_CHECK(!is_power_of_two(0x4FU)); 00106 00107 // 16 bit 00108 BOOST_CHECK(!is_power_of_two(0x0F00U)); 00109 BOOST_CHECK(!is_power_of_two(0x1F00U)); 00110 BOOST_CHECK(!is_power_of_two(0x2F00U)); 00111 BOOST_CHECK(!is_power_of_two(0x4F00U)); 00112 00113 // 32 bit 00114 BOOST_CHECK(!is_power_of_two(0x0F000000U)); 00115 BOOST_CHECK(!is_power_of_two(0x1F000000U)); 00116 BOOST_CHECK(!is_power_of_two(0x2F000000U)); 00117 BOOST_CHECK(!is_power_of_two(0x4F000000U)); 00118 00119 // 64 bit 00120 BOOST_CHECK(!is_power_of_two(UINT64_C(0x0F00000000000000))); 00121 BOOST_CHECK(!is_power_of_two(UINT64_C(0x1F00000000000000))); 00122 BOOST_CHECK(!is_power_of_two(UINT64_C(0x2F00000000000000))); 00123 BOOST_CHECK(!is_power_of_two(UINT64_C(0x4F00000000000000))); 00124 00125 00126 // 8 bit 00127 BOOST_CHECK(is_power_of_two(0x10U)); 00128 BOOST_CHECK(is_power_of_two(0x20U)); 00129 BOOST_CHECK(is_power_of_two(0x40U)); 00130 BOOST_CHECK(is_power_of_two(0x80U)); 00131 00132 // 16 bit 00133 BOOST_CHECK(is_power_of_two(0x1000U)); 00134 BOOST_CHECK(is_power_of_two(0x2000U)); 00135 BOOST_CHECK(is_power_of_two(0x4000U)); 00136 BOOST_CHECK(is_power_of_two(0x8000U)); 00137 00138 // 32 bit 00139 BOOST_CHECK(is_power_of_two(0x10000000U)); 00140 BOOST_CHECK(is_power_of_two(0x20000000U)); 00141 BOOST_CHECK(is_power_of_two(0x40000000U)); 00142 BOOST_CHECK(is_power_of_two(0x80000000U)); 00143 00144 // 64 bit 00145 BOOST_CHECK(is_power_of_two(UINT64_C(0x1000000000000000))); 00146 BOOST_CHECK(is_power_of_two(UINT64_C(0x2000000000000000))); 00147 BOOST_CHECK(is_power_of_two(UINT64_C(0x4000000000000000))); 00148 BOOST_CHECK(is_power_of_two(UINT64_C(0x8000000000000000))); 00149 } 00150 00151 BOOST_FIXTURE_TEST_CASE( test_utils_msb_set, Fixture ) 00152 { 00153 // We make use of this utility is KvdsPageStore to let's just check it works! 00154 00155 // 0 bit 00156 BOOST_CHECK(-1 == msb_set(0x00U)); 00157 00158 // 8 bit 00159 BOOST_CHECK(3 == msb_set(0x0FU)); 00160 BOOST_CHECK(4 == msb_set(0x1FU)); 00161 BOOST_CHECK(5 == msb_set(0x2FU)); 00162 BOOST_CHECK(6 == msb_set(0x4FU)); 00163 00164 // 16 bit 00165 BOOST_CHECK(11 == msb_set(0x0F00U)); 00166 BOOST_CHECK(12 == msb_set(0x1F00U)); 00167 BOOST_CHECK(13 == msb_set(0x2F00U)); 00168 BOOST_CHECK(14 == msb_set(0x4F00U)); 00169 00170 // 32 bit 00171 BOOST_CHECK(27 == msb_set(0x0F000000U)); 00172 BOOST_CHECK(28 == msb_set(0x1F000000U)); 00173 BOOST_CHECK(29 == msb_set(0x2F000000U)); 00174 BOOST_CHECK(30 == msb_set(0x4F000000U)); 00175 00176 // 64 bit 00177 BOOST_CHECK(59 == msb_set(UINT64_C(0x0F00000000000000))); 00178 BOOST_CHECK(60 == msb_set(UINT64_C(0x1F00000000000000))); 00179 BOOST_CHECK(61 == msb_set(UINT64_C(0x2F00000000000000))); 00180 BOOST_CHECK(62 == msb_set(UINT64_C(0x4F00000000000000))); 00181 00182 00183 // 8 bit 00184 BOOST_CHECK(4 == msb_set(0x10U)); 00185 BOOST_CHECK(5 == msb_set(0x20U)); 00186 BOOST_CHECK(6 == msb_set(0x40U)); 00187 BOOST_CHECK(7 == msb_set(0x80U)); 00188 00189 // 16 bit 00190 BOOST_CHECK(12 == msb_set(0x1000U)); 00191 BOOST_CHECK(13 == msb_set(0x2000U)); 00192 BOOST_CHECK(14 == msb_set(0x4000U)); 00193 BOOST_CHECK(15 == msb_set(0x8000U)); 00194 00195 // 32 bit 00196 BOOST_CHECK(28 == msb_set(0x10000000U)); 00197 BOOST_CHECK(29 == msb_set(0x20000000U)); 00198 BOOST_CHECK(30 == msb_set(0x40000000U)); 00199 BOOST_CHECK(31 == msb_set(0x80000000U)); 00200 00201 // 64 bit 00202 BOOST_CHECK(60 == msb_set(UINT64_C(0x1000000000000000))); 00203 BOOST_CHECK(61 == msb_set(UINT64_C(0x2000000000000000))); 00204 BOOST_CHECK(62 == msb_set(UINT64_C(0x4000000000000000))); 00205 BOOST_CHECK(63 == msb_set(UINT64_C(0x8000000000000000))); 00206 } 00207 00208 // Define end of test suite 00209 BOOST_AUTO_TEST_SUITE_END()