[Avida-SVN] r3347 - in development/source: targets/unit-tests tools

brysonda at myxo.css.msu.edu brysonda at myxo.css.msu.edu
Tue Jun 30 14:10:00 PDT 2009


Author: brysonda
Date: 2009-06-30 17:10:00 -0400 (Tue, 30 Jun 2009)
New Revision: 3347

Modified:
   development/source/targets/unit-tests/main.cc
   development/source/tools/cBitArray.cc
   development/source/tools/cBitArray.h
Log:
Add unit tests to the unit testing framework for the recently implemented increment operators in cBitArray.  Also fix LeftShift operation on PowerPC.  Apparently GCC on i386 systems truncates variable shifts to 5 bits before performing the shift.  On PowerPC, the full value of the variable shift is honored, thus it will allow shifts to flush away all bits.  The intent of the masking statement is to have no effect when num_bits % 32 is 0, thus I have simply made the masking effect of the i386 version explicit so that it will work on all platforms.

Modified: development/source/targets/unit-tests/main.cc
===================================================================
--- development/source/targets/unit-tests/main.cc	2009-06-29 18:02:06 UTC (rev 3346)
+++ development/source/targets/unit-tests/main.cc	2009-06-30 21:10:00 UTC (rev 3347)
@@ -313,6 +313,22 @@
     
     bit_array9.SHIFT(66, -65);
     ReportTestResult("Shift Right (multiple bit fields)", (bit_array9.GetBit(0) && bit_array9.CountBits(66) == 1));
+    
+    // INCREMENT
+    
+    cRawBitArray bit_array10(1);
+    
+    bit_array10.INCREMENT(1);
+    ReportTestResult("Increment", (bit_array10.GetBit(0) && bit_array10.CountBits(1) == 1));
+    
+    bit_array10.INCREMENT(1);
+    ReportTestResult("Increment Overflow", (bit_array10.GetBit(0) == false && bit_array10.CountBits(1) == 0));
+    
+    cRawBitArray bit_array11(33);
+    for (int i = 0; i < 32; i++) { bit_array11.SetBit(i, true); }
+    bit_array11.INCREMENT(33);
+    ReportTestResult("Increment (multiple bit fields)", (bit_array11.GetBit(32) == 1 && bit_array11.CountBits(33) == 1));
+    
   }
 };
 
@@ -338,6 +354,8 @@
     ReportTestResult("operator<<", ((ba << 65).CountBits() == 2));
     ReportTestResult("operator>>", ((ba >> 65).CountBits() == 2));
     ReportTestResult("Chained Bitwise Operations", ((~ba & ~ba2).CountBits() == 31));
+    ReportTestResult("++operator", ((++(~ba & ~ba2)).CountBits() == 30));
+    ReportTestResult("operator++", (((~ba & ~ba2)++).CountBits() == 31));
   }
 };
 

Modified: development/source/tools/cBitArray.cc
===================================================================
--- development/source/tools/cBitArray.cc	2009-06-29 18:02:06 UTC (rev 3346)
+++ development/source/tools/cBitArray.cc	2009-06-30 21:10:00 UTC (rev 3347)
@@ -143,9 +143,10 @@
   int num_fields = GetNumFields(num_bits);
   int field_shift = shift_size / 32;
   int bit_shift = shift_size % 32;
-      
+  
+  
   // acount for field_shift
-  if(field_shift) {
+  if (field_shift) {
     for (int i = num_fields - 1; i >= field_shift; i--) {
       bit_fields[i] = bit_fields[i - field_shift];
     }
@@ -154,18 +155,19 @@
     }
   }
   
+  
   // account for bit_shift
   int temp = 0;
   for (int i = 0; i < num_fields; i++) {
     temp = bit_fields[i] >> (32 - bit_shift);
     bit_fields[i] <<= bit_shift;
-    bit_fields[i - 1] |= temp;  // same as += in this case since lower bit_shift bits of bit_fields[i - 1] are all 0 at this point -- any advantage?  
+    if (i > 0) bit_fields[i - 1] |= temp;  // same as += in this case since lower bit_shift bits of bit_fields[i - 1] are all 0 at this point -- any advantage?  
     //could also check for temp != 0 here before assignment -- would that save any time for sparse arrays, or is it always going to be useless?
   }
   
   // mask out any bits that have left-shifted away, allowing CountBits and CountBits2 to work
   // blw: if CountBits/CountBits2 are fixed, this code should be removed as it will be redundant
-  unsigned int shift_mask = 0xffffffff >> 32 - (num_bits % 32);
+  unsigned int shift_mask = 0xFFFFFFFF >> ((32 - (num_bits % 32)) & 0x1F);
   bit_fields[num_fields - 1] &= shift_mask;
 }
 

Modified: development/source/tools/cBitArray.h
===================================================================
--- development/source/tools/cBitArray.h	2009-06-29 18:02:06 UTC (rev 3346)
+++ development/source/tools/cBitArray.h	2009-06-30 21:10:00 UTC (rev 3347)
@@ -99,8 +99,7 @@
   const cRawBitArray & operator=(const cRawBitArray & in_array)
     { assert(false); return *this; }
 
-  inline int GetNumFields(const int num_bits) const
-    { return 1 + ((num_bits-1) >> 5); }
+  inline int GetNumFields(const int num_bits) const { return 1 + ((num_bits - 1) >> 5); }
   inline int GetField(const int index) const { return index >> 5; }
   inline int GetFieldPos(const int index) const { return index & 31; }
 public:




More information about the Avida-cvs mailing list