Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r64680 - in sandbox/SOC/2010/bit_masks: boost/integer/detail/bitfield_vector lib/integer/test/bitfield_vector_testing
From: bbartmanboost_at_[hidden]
Date: 2010-08-08 09:53:53


Author: bbartman
Date: 2010-08-08 09:53:52 EDT (Sun, 08 Aug 2010)
New Revision: 64680
URL: http://svn.boost.org/trac/boost/changeset/64680

Log:
working on testing the implicit conversion operator for the proxy reference type for bitfield_vector
Text files modified:
   sandbox/SOC/2010/bit_masks/boost/integer/detail/bitfield_vector/bitfield_vector_member_impl.hpp | 176 +++++++++------------------------------
   sandbox/SOC/2010/bit_masks/lib/integer/test/bitfield_vector_testing/proxy_reference_test.cpp | 144 +++++++++----------------------
   sandbox/SOC/2010/bit_masks/lib/integer/test/bitfield_vector_testing/test_utility.hpp | 19 ++++
   3 files changed, 105 insertions(+), 234 deletions(-)

Modified: sandbox/SOC/2010/bit_masks/boost/integer/detail/bitfield_vector/bitfield_vector_member_impl.hpp
==============================================================================
--- sandbox/SOC/2010/bit_masks/boost/integer/detail/bitfield_vector/bitfield_vector_member_impl.hpp (original)
+++ sandbox/SOC/2010/bit_masks/boost/integer/detail/bitfield_vector/bitfield_vector_member_impl.hpp 2010-08-08 09:53:52 EDT (Sun, 08 Aug 2010)
@@ -13,10 +13,9 @@
 #include <boost/mpl/equal.hpp>
 #include <boost/type_traits/is_signed.hpp>
 #include <boost/assert.hpp>
-#include <iostream>
-#include <iomanip>
 #include <cstring>
 
+
 namespace boost { namespace detail {
 
 template <std::size_t Width, bool = bool((Width%8) > 0)>
@@ -101,19 +100,19 @@
 
     /** Copy Constructor. */
     proxy_reference_type(_self const& x)
- :_ptr(x._ptr), _offset(x._offset)
+ :_ptr(x._ptr), _mask(x._mask )
     { }
 
     /** pointer, offset constructor. */
     proxy_reference_type(storage_type* ptr, offset_type offset)
- :_ptr(ptr), _offset(offset)
+ :_ptr(ptr), _mask(get_mask_detail<Width>(offset) )
     { }
     //@}
 
     /** Copy assignment. */
     _self& operator=(_self const& x) {
         _ptr = x._ptr;
- _offset = x._offset;
+ _mask = x._mask;
         return *this;
     }
 
@@ -129,7 +128,7 @@
     // private:
     /** Member variables. */
     storage_type* _ptr;
- offset_type _offset;
+ mask_detail _mask;
 };
 
 
@@ -142,104 +141,7 @@
     std::size_t last_left_shift;
 };
 
-
-/** Calculates the size of an array needed to store a particular mask inside of
- * of char array.
- */
-template <std::size_t Width>
-inline std::size_t get_mask_array_size(std::size_t offset) {
- BOOST_ASSERT(( offset < 8));
- std::size_t total_bits = Width + offset;
- std::size_t ret = 0;
- ret = total_bits / 8;
- if(total_bits % 8) {
- ret += 1;
- }
- return ret;
-}
-
-
-void print_mask_array(storage_ptr_t start,storage_ptr_t end) {
- std::cout << "Value Of Mask Array: " ;
- for(;start != end;++start) {
- std::cout << std::hex << std::size_t(*start) << "|";
- }
- std::cout << std::endl;
-}
-
-
-/** Creates a mask the which is used to extract the information from within
- * a the storage unsigned char array.
- */
-template <std::size_t Width>
-inline mask_array_info
-make_field_mask(std::size_t offset) {
-
- // I should consider using malloc for something like this shouldn't I.
- // either way all I need to remember to do is make sure that it gets
- // deleted.
- std::size_t mask_size = get_mask_array_size<Width>(offset);
- mask_array_info ret;
- ret.mask_size = mask_size;
- ret.mask = new storage_t[mask_size];
- ret.last_left_shift = 0;
- std::memset(ret.mask, 0, mask_size);
- storage_ptr_t mask_ptr = ret.mask;
-
- // calculate bit_count for for statement
- std::size_t bit_count = Width + offset;
- storage_t mask = 0x80;
- mask >>= offset;
-
- // creating begining mask.
- for(std::size_t index = offset;
- index < bit_count && index <= 8;
- ++index)
- {
- *mask_ptr |= mask;
- mask >>= 1;
- }
-
- // This is a basic condition where if the mask has a size of 1 byte then
- // it should simply be returned.
- // this also denotes that the mask is not going to cross char boundries.
- if(mask_size == 1) {
- return ret;
- }
-
- // fill all but the last block with 0xff
- storage_ptr_t end_mask_ptr = mask_ptr + mask_size - 1;
- ++mask_ptr;
- for(;mask_ptr < end_mask_ptr; ++mask_ptr) {
- *mask_ptr = 0xff;
- }
-
- // if both the offset + width is divisible by 8 then that means that
- // the mask ends on a char boundry and the last byte needs to be set to 0
- // and the array returned.
- if(!((offset + Width)%8)) {
- mask_ptr = ret.mask + mask_size - 1;
- *mask_ptr = 0xFF;
- ret.last_left_shift = 8;
- return ret;
- }
-
- // filling out the rest of the trailing bits.
- mask = 0x80;
-
- // calculate the number of trailing bits which need to be filled out.
- std::size_t remaining_bits = (Width + offset) % 8;
- ret.last_left_shift = remaining_bits;
- // set the mask pointer to the last valid index inside of the array.
- mask_ptr = ret.mask + mask_size - 1;
- for(std::size_t bit_index = 0; bit_index < remaining_bits; ++bit_index) {
- *mask_ptr |= mask;
- mask >>= 1;
- }
- return ret;
-}
-
-
+/** Proxy reference type for unsigned types. */
 template <typename RetType, std::size_t Width>
 class proxy_reference_type<RetType,Width,false> {
     typedef proxy_reference_type<RetType,Width,false> _self;
@@ -258,53 +160,61 @@
 
     /** Copy Constructor. */
     proxy_reference_type(_self const& x)
- :_ptr(x._ptr),
- _offset(x._offset),
- _mask(make_field_mask<Width>(_offset))
+ :_ptr(x._ptr), _mask(x._mask)
     { }
 
     /** pointer, offset constructor. */
     proxy_reference_type(storage_type* ptr, offset_type offset)
- :_ptr(ptr),
- _offset(offset),
- _mask(make_field_mask<Width>(_offset))
+ :_ptr(ptr), _mask(get_mask_detail<Width>(offset))
+
     { }
 
- ~proxy_reference_type() {
- delete _mask.mask;
- }
     //@}
 
     /** Copy assignment. */
     _self& operator=(_self const& x) {
- storage_ptr_t mask_ptr = _mask.mask;
         _ptr = x._ptr;
- _offset = x._offset;
- _mask = make_field_mask<Width>(_offset);
- delete mask_ptr;
+ _mask = x._mask;
         return *this;
     }
 
     /** Implicit Conversion Operator*/
     operator value_type() const {
- value_type ret = 0;
+ if(_mask._size == 1) {
+ return (value_type( _mask._first_byte & *_ptr ) >>
+ (8 - (_mask._offset + width)));
+ }
 
- storage_ptr_t mask_ptr = _mask.mask;
- storage_ptr_t end_mask_ptr = _mask.mask + _mask.mask_size - 1 ;
- storage_ptr_t data_ptr = _ptr;
+ value_type ret = 0;
+ storage_ptr_t byte_ptr = _ptr;
 
- if(_mask.mask_size == 1) {
- return value_type(*data_ptr & *mask_ptr) >> (8-(_offset + width));
+ if(_mask._size == 2) {
+ ret = value_type(_mask._first_byte & *byte_ptr) <<
+ _mask._last_shift;
+
+ ++byte_ptr;
+ ret += value_type( _mask._last_byte & *byte_ptr) >> (8 - _mask._last_shift);
+ if( _mask._last_byte != 0xFF) {
+ ret >>= _mask._last_shift - 1;
+ }
+ return ret;
         }
- for(;mask_ptr < end_mask_ptr;++mask_ptr) {
+
+ const storage_t all_bits = 0xFF;
+ // gettting first byte.
+ ret = value_type(_mask._first_byte & *byte_ptr) << 8;
+ ++byte_ptr;
+ // getting middle bytes
+ for(std::size_t index = 0; index < _mask._size - 2; ++index) {
             ret <<= 8;
- ret += *data_ptr & *mask_ptr;
- ++data_ptr;
+ ret += *byte_ptr & all_bits;
+ ++byte_ptr;
         }
- ret <<= _mask.last_left_shift;
- ++data_ptr;
- ++mask_ptr;
- return ret += *data_ptr & *mask_ptr;
+ // shifting bits
+ ++byte_ptr;
+ ret <<= _mask._last_shift;
+ ret += value_type( *byte_ptr & _mask._last_byte ) >> (8 - _mask._last_shift);
+ return ret;
     }
 
     /** value_type storage assignement operator.*/
@@ -319,9 +229,9 @@
 
 // private:
     /** Member variables. */
- storage_type* _ptr;
- offset_type _offset;
- mask_array_info _mask;
+ storage_type* _ptr;
+ mask_detail _mask;
+ // mask_array_info _mask;
 };
 
 

Modified: sandbox/SOC/2010/bit_masks/lib/integer/test/bitfield_vector_testing/proxy_reference_test.cpp
==============================================================================
--- sandbox/SOC/2010/bit_masks/lib/integer/test/bitfield_vector_testing/proxy_reference_test.cpp (original)
+++ sandbox/SOC/2010/bit_masks/lib/integer/test/bitfield_vector_testing/proxy_reference_test.cpp 2010-08-08 09:53:52 EDT (Sun, 08 Aug 2010)
@@ -47,7 +47,7 @@
         unsigned char temp[20];
 #define TEST_MACRO_PTR_OFFSET_CTOR(NUMBER) \
         test_type_##NUMBER t##NUMBER(temp,2); \
- BOOST_TEST(t##NUMBER._offset == 2); \
+ BOOST_TEST(t##NUMBER._mask._offset == 2); \
         BOOST_TEST(t##NUMBER._ptr == temp);
 
         TEST_MACRO_PTR_OFFSET_CTOR(1);
@@ -69,7 +69,7 @@
 #define TEST_MACRO_COPY_CTOR(NUMBER) \
         test_type_##NUMBER t##NUMBER(temp,2); \
         test_type_##NUMBER u##NUMBER(t##NUMBER); \
- BOOST_TEST(u##NUMBER._offset == 2); \
+ BOOST_TEST(u##NUMBER._mask._offset == 2); \
         BOOST_TEST(u##NUMBER._ptr == temp);
 
         TEST_MACRO_COPY_CTOR(1);
@@ -93,10 +93,10 @@
 #define TEST_MACRO_COPY_ASSIGNMENT_OPERATOR(NUMBER) \
         test_type_##NUMBER t##NUMBER(temp,2); \
         test_type_##NUMBER u##NUMBER(temp2,4); \
- BOOST_TEST(u##NUMBER._offset == 4); \
+ BOOST_TEST(u##NUMBER._mask._offset == 4); \
         BOOST_TEST(u##NUMBER._ptr == temp2); \
         u##NUMBER = t##NUMBER; \
- BOOST_TEST(u##NUMBER._offset == 2); \
+ BOOST_TEST(u##NUMBER._mask._offset == 2); \
         BOOST_TEST(u##NUMBER._ptr == temp);
 
         TEST_MACRO_COPY_ASSIGNMENT_OPERATOR(1);
@@ -112,117 +112,30 @@
 
 #undef TEST_MACRO_COPY_ASSIGNMENT_OPERATOR
     }
-
- // functions used for constructing the char_array mask.
- // Testing: get_mask_array_size
- {
- std::size_t ret_value = 0;
-
- ret_value = get_mask_array_size<3>(6);
- BOOST_TEST(ret_value == 2);
-
- ret_value = get_mask_array_size<4>(0);
- BOOST_TEST(ret_value == 1);
-
- ret_value = get_mask_array_size<50>(0);
- BOOST_TEST(ret_value == 7);
- }
- // make_field_mask creates mask used to retrieve or set data.
- {
- mask_array_info manager_ptr;
- storage_ptr_t ptr;
-
- manager_ptr = make_field_mask<3>(6);
- ptr = manager_ptr.mask;
- BOOST_TEST(*ptr == 0x3);
- ++ptr;
- BOOST_TEST(*ptr == 0x80);
- delete manager_ptr.mask;
-
- manager_ptr = make_field_mask<3>(1);
- ptr = manager_ptr.mask;
- BOOST_TEST( *ptr == 0x70 );
- delete manager_ptr.mask;
-
- manager_ptr = make_field_mask<4>(0);
- ptr = (manager_ptr.mask);
- BOOST_TEST( *ptr == 0xf0 );
- delete manager_ptr.mask;
-
- manager_ptr = make_field_mask<50>(0);
- ptr = (manager_ptr.mask);
- // 1
- BOOST_TEST( *ptr == 0xFF );
- // 2
- ++ptr;
- BOOST_TEST( *ptr == 0xFF );
- // 3
- ++ptr;
- BOOST_TEST( *ptr == 0xFF );
- // 4
- ++ptr;
- BOOST_TEST( *ptr == 0xFF );
- // 5
- ++ptr;
- BOOST_TEST( *ptr == 0xFF );
- // 6
- ++ptr;
- BOOST_TEST( *ptr == 0xFF );
- // 7
- ++ptr;
- BOOST_TEST( *ptr == 0xC0 );
-
- delete manager_ptr.mask;
-
-
- manager_ptr = make_field_mask<50>(2);
- ptr = (manager_ptr.mask);
- // 1
- BOOST_TEST( *ptr == 0x3F );
- // 2
- ++ptr;
- BOOST_TEST( *ptr == 0xFF );
- // 3
- ++ptr;
- BOOST_TEST( *ptr == 0xFF );
- // 4
- ++ptr;
- BOOST_TEST( *ptr == 0xFF );
- // 5
- ++ptr;
- BOOST_TEST( *ptr == 0xFF );
- // 6
- ++ptr;
- BOOST_TEST( *ptr == 0xFF );
- // 7
- ++ptr;
- BOOST_TEST( *ptr == 0xF0 );
-
- delete manager_ptr.mask;
-
- }
-
     // encoding and decoding tests.
     {
+
+ // single byte mask testing
         typedef unsigned char storage_type;
         typedef storage_type* storage_ptr;
+ std::cout << "-----------------------------------------" << std::endl;
+ std::cout << "single byte mask" << std::endl;
+ std::cout << "-----------------------------------------" << std::endl;
         storage_type storage[20];
         storage_ptr ptr = storage;
         *ptr = storage_type(0x7) << 5;
         std::cout << "Test type 1. First byte value in hex: " << std::endl;
         std::cout << std::hex << std::size_t(*ptr) << std::endl;
-
         // first within the first index.
         test_type_1 t1(ptr,0);
         std::cout << "fist use of t1" << std::endl;
         test_type_1::value_type x = t1;
- // std::cout << "Value returned by t1: "<< std::hex << x << std::endl;
-
         BOOST_TEST(x == 0x7);
-
-
-
- // now trying second index.
+
+ // now trying second index. Still single byte mask testing
+ std::cout << "-----------------------------------------" << std::endl;
+ std::cout << "middle of byte mask single bit" << std::endl;
+ std::cout << "-----------------------------------------" << std::endl;
         test_type_1 t2(ptr,3);
         std::cout << "fist use of t2" << std::endl;
         BOOST_TEST(t2 == 0);
@@ -233,10 +146,39 @@
         std::cout << "Value returned by t2: "<< std::hex << t2 << std::endl;
         std::cout << "third use of t2" << std::endl;
         BOOST_TEST(t2 == 7);
+
+ // 2 byte mask testing.
+ std::cout << "-----------------------------------------" << std::endl;
+ std::cout << "2 byte mask" << std::endl;
+ std::cout << "-----------------------------------------" << std::endl;
+ test_type_1 t3(ptr,6);
+ *ptr = 0x03;
+ ++ptr;
+ *ptr = 0x80;
+ --ptr;
+ print_type_and_value(t3);
+ print_mask_details(t3);
+ BOOST_TEST( t3 == 7 );
+
+ // two byte with second byte = to 8.
+ std::cout << "-----------------------------------------" << std::endl;
+ std::cout << "2 byte mask. 0x1 and 0xFF" << std::endl;
+ std::cout << "-----------------------------------------" << std::endl;
+ proxy_reference_type<unsigned int, 9> t4(ptr,7);
+ std::memset(ptr,0,3);
+ *ptr = 0x01;
+ ++ptr;
+ *ptr = 0xFF;
+ --ptr;
+ print_type_and_value(t4);
+ print_mask_details(t4);
+ BOOST_TEST(t4 == 0x1FF);
 
+ // testing multi byte > 2
         
     }
     return boost::report_errors();
 }
 
 
+

Modified: sandbox/SOC/2010/bit_masks/lib/integer/test/bitfield_vector_testing/test_utility.hpp
==============================================================================
--- sandbox/SOC/2010/bit_masks/lib/integer/test/bitfield_vector_testing/test_utility.hpp (original)
+++ sandbox/SOC/2010/bit_masks/lib/integer/test/bitfield_vector_testing/test_utility.hpp 2010-08-08 09:53:52 EDT (Sun, 08 Aug 2010)
@@ -53,4 +53,23 @@
     }
 };
 
+template<typename T>
+void print_type_and_value( T const& x) {
+ std::cout << "Type: " << typestr<T>() << " Value:"
+ << std::hex << std::size_t(x) << std::endl;
+}
+
+template <typename T>
+void print_mask_details( T const& x) {
+ std::cout << "Mask detail for the following class" << std::endl;
+ std::cout << "Type: " << typestr<T>() << std::endl;
+ std::cout << "Mask Values" << std::endl;
+ std::cout << "size:" << x._mask._size << std::endl;
+ std::cout << "offset:" << x._mask._offset << std::endl;
+ std::cout << "first_byte:" << std::hex << std::size_t(x._mask._first_byte) << std::endl;
+ std::cout << "last_byte:" << std::hex << std::size_t(x._mask._last_byte) << std::endl;
+ std::cout << "last_shift:" << x._mask._last_shift << std::endl << std::endl;
+
+}
+
 #endif


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk