From 9f138260dafeb5a1b541fff8dd577422439feb0b Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 23 Apr 2013 10:25:26 -0600 Subject: Fixed random zeros bug. They weren't that random, the resize routine in PackedIntArray was written poorly. It was growing too much and computing the size of the original array incorrectly, so not all the data was being copied every time. --- src/options.cpp | 2 +- src/packedintarray.cpp | 7 ++++--- src/unitnumber.cpp | 14 ++++++++++++-- src/unitnumber.h | 1 + 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/options.cpp b/src/options.cpp index 89397f4..4fcf1e2 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -17,7 +17,7 @@ Options::~Options() { } -int Options::selfTest( Bu::StringArray aArgs ) +int Options::selfTest( Bu::StringArray ) { UnitNumber().run(); diff --git a/src/packedintarray.cpp b/src/packedintarray.cpp index b91358e..824f589 100644 --- a/src/packedintarray.cpp +++ b/src/packedintarray.cpp @@ -4,7 +4,7 @@ #define bitsizeof( x ) ((sizeof(x))*8) #define StoreBits ((bitsizeof(PackedIntArray::Store))) -#define StoreCount( x ) (((x*iBitWidth)/StoreBits)+(((x*iBitWidth)%StoreBits)?1:0)) +#define StoreCount( x ) ((((x)*iBitWidth)/StoreBits)+((((x)*iBitWidth)%StoreBits)?1:0)) PackedIntArray::PackedIntArray( PackedIntArray::Unit iBitWidth ) : iBitWidth( iBitWidth ), @@ -207,10 +207,11 @@ void PackedIntArray::checkCapacity() { // Bu::println("!!! Resizing !!!"); Store *aOldData = aData; + int iSize = StoreCount(iCapacity); int iNewSize = (iCapacity==0)?(bitsizeof(Store)/iBitWidth):(iCapacity=StoreCount(iCapacity*2)); - while( iNewSize < iCount ) + int iCountSize = StoreCount(iCount); + while( iNewSize < iCountSize ) iNewSize *= 2; - int iSize = StoreCount(iCapacity); // Bu::println(" %1 => %2 (%3 bit words)").arg( iSize ).arg( iNewSize ) // .arg( StoreBits ); aData = new Store[iNewSize]; diff --git a/src/unitnumber.cpp b/src/unitnumber.cpp index d4bc7a8..79797c9 100644 --- a/src/unitnumber.cpp +++ b/src/unitnumber.cpp @@ -5,6 +5,8 @@ UnitNumber::UnitNumber() { setName("Number"); + add( static_cast(&UnitNumber::parse1), + "parse1", Bu::UnitSuite::expectPass ); add( static_cast(&UnitNumber::multiply1), "multiply1", Bu::UnitSuite::expectPass ); } @@ -13,10 +15,18 @@ UnitNumber::~UnitNumber() { } +void UnitNumber::parse1() +{ + unitTest( Number("121932631356500531347203169112635269").toString() == + "121932631356500531347203169112635269" ); +} + void UnitNumber::multiply1() { + unitTest(Number("123456789") * Number("987654321") == "121932631112635269"); unitTest( - (Number("123456789123456789") * Number("987654321987654321")).toString() - == "121932631356500531347203169112635269" ); + Number("123456789123456789") * Number("987654321987654321") == + "121932631356500531347203169112635269" + ); } diff --git a/src/unitnumber.h b/src/unitnumber.h index 89b1c0f..76496b9 100644 --- a/src/unitnumber.h +++ b/src/unitnumber.h @@ -9,6 +9,7 @@ public: UnitNumber(); virtual ~UnitNumber(); + void parse1(); void multiply1(); }; -- cgit v1.2.3