#include <bitset>
#include <sstream>
#include <iostream>
int testBits(ostringstream &
result, ostringstream &reference) {
cout<<"result : "<<result.str()<<endl;
cout<<"ref : "<<reference.str()<<endl;
if (result.str().compare(reference.str())!=0) {
cout<<"ERROR : result and reference don't align"<<endl;
return -1;
}
return 0;
}
typedef unsigned int VTYPELOCAL;
class BitStreamTestClass :
public BitStream {
public:
if (sizeof(VTYPE)!=sizeof(VTYPELOCAL))
assert("BitStream size mismatch for testing");
}
unsigned int basicTypeSize() {
return sizeof(VTYPE);
}
};
#ifndef __MINGW32__
typedef unsigned long ulong;
#else
typedef unsigned long long ulong;
#endif
int main(
int argc,
char *argv[]) {
{
BitStreamTestClass bitStream;
if (bitStream.basicTypeSize()*CHAR_BIT!=32) {
cerr<<argv[0]<<" BitStream VTYPE size != 32 bits - this test is not setup for that bit count"<<endl;
abort();
}
}
{
const ulong l[]={(ulong)0xfedcba987654321, (ulong)0x123456789abcdef, (ulong)0xfefcfaf8f6f4f2f};
BitStreamTestClass bitStream;
bitStream<<l[0]<<l[1]<<l[2];
if (size != bitStream.size()){
cout<<"size mismatch please fix...."<<endl;
return -1;
}
int cnt=0;
for (
unsigned int i=1; cnt<
size; cnt+=i, i++){
cout<<"i "<<i<<" cnt "<<cnt<<endl;
ostringstream
result, reference;
BitStreamTestClass bitStream2;
bitStream2.push_back(bitStream.pop_back<ulong>(i), i);
result<<bitStream2;
bitset<size> bsl(l[0]);
for (int j=0; j<sizeof(ulong)*CHAR_BIT; j++)
bsl[j]=(l[0]>>j)&0x1;
bsl<<=(sizeof(ulong)*8);
for (int j=0; j<sizeof(ulong)*CHAR_BIT; j++)
bsl[j]=(l[1]>>j)&0x1;
bsl<<=(sizeof(ulong)*8);
for (int j=0; j<sizeof(ulong)*CHAR_BIT; j++)
bsl[j]=(l[2]>>j)&0x1;
ulong maskL=(ulong)pow(2.,(float)i)-1;
bitset<size> mask;
for (int j=0; j<sizeof(ulong)*CHAR_BIT; j++)
mask[j]=(maskL>>j)&0x1;
reference<<((bsl&(mask<<cnt))>>cnt);
reference.str(reference.str().substr(bsl.size()-i, i));
if (testBits(result, reference)<0) return -1;
cout<<endl;
}
}
{
const ulong l[]={(ulong)0xfedcba987654321, (ulong)0x123456789abcdef, (ulong)0xfefcfaf8f6f4f2f};
BitStreamTestClass bitStream;
bitStream<<l[0]<<l[1]<<l[2];
if (size != bitStream.size()){
cout<<"size mismatch please fix...."<<endl;
return -1;
}
int cnt=0;
for (int i=19; i>=0; cnt+=i, i--){
cout<<"i "<<i<<" cnt "<<cnt<<endl;
ostringstream
result, reference;
BitStreamTestClass bitStream2;
bitStream2.push_back(bitStream.pop_back<unsigned long>(i), i);
result<<bitStream2;
bitset<size> bsl(l[0]);
for (int j=0; j<sizeof(ulong)*CHAR_BIT; j++)
bsl[j]=(l[0]>>j)&0x1;
bsl<<=(sizeof(ulong)*8);
for (int j=0; j<sizeof(ulong)*CHAR_BIT; j++)
bsl[j]=(l[1]>>j)&0x1;
bsl<<=(sizeof(ulong)*8);
for (int j=0; j<sizeof(ulong)*CHAR_BIT; j++)
bsl[j]=(l[2]>>j)&0x1;
ulong maskL=(ulong)pow(2.,(float)i)-1;
bitset<size> mask;
for (int j=0; j<sizeof(ulong)*CHAR_BIT; j++)
mask[j]=(maskL>>j)&0x1;
reference<<((bsl&(mask<<cnt))>>cnt);
reference.str(reference.str().substr(bsl.size()-i, i));
if (testBits(result, reference)<0) return -1;
cout<<endl;
}
}
{
BitStreamTestClass bitStream;
for (int i=0; i<135; i++)
bitStream<<(unsigned int)i;
bitStream.clear();
if (bitStream.size()!=0){
cout<<"the bitstream didn't clear : error"<<endl;
return -1;
}
}
cout<<"all passed"<<endl;
return 0;
}