1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "bit_array.h"
#define SIZE (8*sizeof(int)*3)
static bit_array array;
static int simple_array[SIZE];
static int bits;
int are_equal()
{
int i;
for (i=0 ; i < SIZE ; i++)
if (SampleBit(&array,i) != simple_array[i]){
printf("failed bit %d (packed=%d, simple=%d)\n", i,
SampleBit(&array,i), simple_array[i]);
return 0;
}
return 1;
}
int is_same_vacant()
{
int vacant;
for (vacant =0 ; simple_array[vacant]!=0 ; vacant++)
if ( vacant >= SIZE) {
vacant=-1;
break;
}
if ( VacantBit(&array) == vacant )
return 1;
else
return 0;
}
void assign_both(int bit_nr, int bit_val)
{
int old_bit= simple_array[bit_nr];
simple_array[bit_nr]= bit_val;
bits+=bit_val - old_bit;
assert(AssignBit(&array, bit_nr, bit_val) == old_bit);
assert(are_equal());
assert(is_same_vacant());
}
int main()
{
unsigned int integers[SIZE >> 5];
int i,j;
assert( AssembleArray(&array, integers, SIZE)
== &array);
ResetArray(&array);
for (i=0 ; i<SIZE ; i++)
simple_array[i]=0;
for (j=5 ; j ; j--) {
printf("\rleft %d\r",j);
for (i=0 ; VacantBit(&array) != -1 ; i++ ) {
if (i % 256 == 0) {
printf("left %d ",j);
printf("%3d up \r", bits);
fflush(stdout);
}
assign_both(rand() % SIZE,
(rand()% SIZE > bits ) ? 0 : 1 );
}
assign_both(rand() % SIZE, 1);
for (i=0 ; bits ; i++ ) {
if (i % 256 == 0) {
printf("left %d ",j);
printf("%3d down\r", bits);
fflush(stdout);
}
assign_both(rand() % SIZE,
(rand()% SIZE <= (SIZE-bits) ) ? 0 : 1 );
}
assign_both(rand() % SIZE, 0);
}
putchar('\n');
return 0;
}
|