libtenum
|
00001 00008 #define TENUM_USE_SHORTCUTS 1 00009 #include "tenum.hpp" 00010 00011 #define BOOST_TEST_MODULE tenum_bit_field_tests 00012 #include <boost/test/unit_test.hpp> 00013 00014 lte_bit_field(bit_field, 00015 lte_ev(bit1,0b0001) 00016 lte_ev(bit2,0b0010) 00017 lte_ev(bit3,0b0100) 00018 lte_evn(bit4,0b1000,"last_bit"), 00019 ::boost::uint16_t 00020 ) 00021 00022 00025 BOOST_AUTO_TEST_SUITE(bit_field_tests) 00026 00032 BOOST_AUTO_TEST_CASE(test_bit_field_from_or_serialization) { 00033 bit_field_t enum_value = static_cast< bit_field_t > (bit_field::bit1 | bit_field::bit2); 00034 ::std::string string_value; 00035 ::std::stringstream stream; 00036 stream << enum_value; 00037 stream >> string_value; 00038 BOOST_CHECK_EQUAL(string_value,"bit1+bit2"); 00039 } 00040 00046 BOOST_AUTO_TEST_CASE(test_bit_field_serialization_explicit) { 00047 bit_field_t enum_value = static_cast< bit_field_t > (bit_field::bit2 | bit_field::bit4); 00048 ::std::string string_value; 00049 ::std::stringstream stream; 00050 stream << enum_value; 00051 stream >> string_value; 00052 BOOST_CHECK_EQUAL(string_value,"bit2+last_bit"); 00053 } 00054 00060 BOOST_AUTO_TEST_CASE(test_bit_field_from_int_serialization) { 00061 bit_field_t enum_value = static_cast< bit_field_t > (0b110); 00062 ::std::string string_value; 00063 ::std::stringstream stream; 00064 stream << enum_value; 00065 stream >> string_value; 00066 BOOST_CHECK_EQUAL(string_value,"bit2+bit3"); 00067 } 00068 00074 BOOST_AUTO_TEST_CASE(test_bit_field_deserialization) { 00075 bit_field_t enum_value; 00076 ::std::string string_value = "bit1+bit3"; 00077 ::std::stringstream stream; 00078 stream << string_value; 00079 stream >> enum_value; 00080 BOOST_CHECK_EQUAL(enum_value,static_cast< bit_field_t > (bit_field::bit1 | bit_field::bit3)); 00081 BOOST_CHECK_EQUAL(enum_value,static_cast< bit_field_t > (0b101)); 00082 } 00083 00089 BOOST_AUTO_TEST_CASE(test_bit_field_or_variable) { 00090 bit_field_t enum_value = static_cast< bit_field_t > (bit_field::bit1 | bit_field::bit2); 00091 enum_value = static_cast< bit_field_t > (enum_value | bit_field::bit3); 00092 ::std::string string_value; 00093 ::std::stringstream stream; 00094 stream << enum_value; 00095 stream >> string_value; 00096 BOOST_CHECK_EQUAL(string_value,"bit1+bit2+bit3"); 00097 } 00098 00104 BOOST_AUTO_TEST_CASE(test_bit_field_xor_variable) { 00105 bit_field_t enum_value = static_cast< bit_field_t > (bit_field::bit1 | bit_field::bit2); 00106 enum_value = static_cast< bit_field_t > (enum_value ^ bit_field::bit2); 00107 ::std::string string_value; 00108 ::std::stringstream stream; 00109 stream << enum_value; 00110 stream >> string_value; 00111 BOOST_CHECK_EQUAL(string_value,"bit1"); 00112 } 00113 00119 BOOST_AUTO_TEST_CASE(test_bit_field_empty_serialization) { 00120 bit_field_t enum_value = static_cast< bit_field_t > (0); 00121 ::std::string string_value; 00122 ::std::stringstream stream; 00123 stream << enum_value; 00124 stream >> string_value; 00125 BOOST_CHECK_EQUAL(string_value,""); 00126 } 00127 00133 BOOST_AUTO_TEST_CASE(test_bit_field_empty_deserialization) { 00134 bit_field_t enum_value; 00135 ::std::string string_value = ""; 00136 ::std::stringstream stream; 00137 stream << string_value; 00138 stream >> enum_value; 00139 BOOST_CHECK_EQUAL(enum_value,static_cast< bit_field_t > (0)); 00140 } 00141 00147 BOOST_AUTO_TEST_CASE(test_bit_field_integers_eq) { 00148 bit_field_t enum_value = bit_field::bit2; 00149 ::boost::uint64_t integer_value = 2; 00150 BOOST_CHECK(integer_value == enum_value); 00151 BOOST_CHECK(enum_value == integer_value); 00152 } 00153 00159 BOOST_AUTO_TEST_CASE(test_bit_field_integers_neq) { 00160 bit_field_t enum_value = bit_field::bit2; 00161 ::boost::uint64_t integer_value = 0; 00162 BOOST_CHECK(integer_value != enum_value); 00163 BOOST_CHECK(enum_value != integer_value); 00164 } 00165 00171 BOOST_AUTO_TEST_CASE(test_bit_field_integers_bitwise_and) { 00172 bit_field_t enum_value = bit_field::bit2; 00173 ::boost::uint64_t integer_value1 = 0b011 & enum_value; 00174 BOOST_CHECK(integer_value1 == 0b010); 00175 ::boost::uint64_t integer_value2 = enum_value & 0b011; 00176 BOOST_CHECK(integer_value2 == 0b010); 00177 } 00178 00184 BOOST_AUTO_TEST_CASE(test_bit_field_integers_bitwise_or) { 00185 bit_field_t enum_value = bit_field::bit2; 00186 ::boost::uint64_t integer_value1 = 0b001 | enum_value; 00187 BOOST_CHECK(integer_value1 == 0b011); 00188 ::boost::uint64_t integer_value2 = enum_value | 0b001; 00189 BOOST_CHECK(integer_value2 == 0b011); 00190 } 00191 00197 BOOST_AUTO_TEST_CASE(test_bit_field_integers_bitwise_xor) { 00198 bit_field_t enum_value = bit_field::bit2; 00199 ::boost::uint64_t integer_value1 = 0b011 ^ enum_value; 00200 BOOST_CHECK(integer_value1 == 0b001); 00201 ::boost::uint64_t integer_value2 = enum_value ^ 0b011; 00202 BOOST_CHECK(integer_value2 == 0b001); 00203 } 00204 00210 BOOST_AUTO_TEST_CASE(test_bit_field_integers_bitwise_and_assign) { 00211 bit_field_t enum_value = bit_field::bit2; 00212 ::boost::uint64_t integer_value = 0b011; 00213 integer_value &= enum_value; 00214 BOOST_CHECK(integer_value == 0b010); 00215 } 00216 00222 BOOST_AUTO_TEST_CASE(test_bit_field_integers_bitwise_or_assign) { 00223 bit_field_t enum_value = bit_field::bit2; 00224 ::boost::uint64_t integer_value = 0b001; 00225 integer_value |= enum_value; 00226 BOOST_CHECK(integer_value == 0b011); 00227 } 00228 00234 BOOST_AUTO_TEST_CASE(test_bit_field_integers_bitwise_xor_assign) { 00235 bit_field_t enum_value = bit_field::bit2; 00236 ::boost::uint64_t integer_value = 0b011; 00237 integer_value ^= enum_value; 00238 BOOST_CHECK(integer_value == 0b001); 00239 } 00240 00241 BOOST_AUTO_TEST_SUITE_END()