// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
#include <stdlib.h>

#include <iostream>

using namespace std;

#include "bits.h"

void test()
{
   int i;
   class BitString X,Y,Z;
   // pack some integers of various length
   for(i=0;i<8;i++) {
      X.AddBits(1,1);
      X.AddBits(3,2);
      X.AddBits(7,3);
      X.AddBits(15,4);
      X.AddBits(1917,16);
   };
   // get the resulting string and copy it
   int l;
   char tmp[100];
   cout << X.SizeString() << endl;
   X.GetString(tmp,sizeof(tmp),&l);
   Y.SetString(tmp,l);
   // unpack the integers
   while(!Y.EndString(1+2+3+4+16)) {
      cout << Y.GetBits(1) << endl;
      cout << Y.GetBits(2) << endl;
      cout << Y.GetBits(3) << endl;
      cout << Y.GetBits(4) << endl;
      cout << Y.GetBits(16) << endl;
   }
   // pack a lot of bits
   for(i=0;i<100000;i++) Z.AddBits(1,1);
   cout << Z.SizeString() << endl;
   return;
}

void testnegative()
{
   int i;
   class BitString X,Y,Z;
   // pack some integers of various length
   for(i=0;i<8;i++) {
      X.AddBits(-1,2);
      X.AddBits(3,3);
      X.AddBits(-7,4);
      X.AddBits(15,5);
      X.AddBits(-1917,17);
   };
   // get the resulting string and copy it
   int l;
   char tmp[100];
   cout << X.SizeString() << endl;
   X.GetString(tmp,sizeof(tmp),&l);
   Y.SetString(tmp,l);
   // unpack the integers
   while(!Y.EndString(2+3+4+5+17)) {
      cout << Y.GetBitsSigned(2) << endl;
      cout << Y.GetBitsSigned(3) << endl;
      cout << Y.GetBitsSigned(4) << endl;
      cout << Y.GetBitsSigned(5) << endl;
      cout << Y.GetBitsSigned(17) << endl;
   }
   // pack a lot of bits
   for(i=0;i<100000;i++) Z.AddBits(-1,2);
   cout << Z.SizeString() << endl;
   return;
}

int main()
{
   test();
   testnegative();
   exit(EXIT_SUCCESS);
}
