Commit 20b7f47e authored by Nicolas Denoyelle's avatar Nicolas Denoyelle Committed by Swann Perarnau
Browse files

[feature/ci] add bitmap methods, fasten tests

parent 509cbef2
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
#define AML_BITMAP_BYTES (AML_BITMAP_MAX/8) #define AML_BITMAP_BYTES (AML_BITMAP_MAX/8)
#define AML_BITMAP_TYPE unsigned long #define AML_BITMAP_TYPE unsigned long
#define AML_BITMAP_SIZE (AML_BITMAP_BYTES/sizeof(AML_BITMAP_TYPE)) #define AML_BITMAP_SIZE (AML_BITMAP_BYTES/sizeof(AML_BITMAP_TYPE))
#define AML_BITMAP_NBITS (8 * sizeof(AML_BITMAP_TYPE))
struct aml_bitmap { struct aml_bitmap {
unsigned long mask[AML_BITMAP_SIZE]; unsigned long mask[AML_BITMAP_SIZE];
......
#include "aml.h" #include "aml.h"
#include <string.h>
#define AML_BITMAP_EMPTY (0UL) #define AML_BITMAP_EMPTY (0UL)
#define AML_BITMAP_FULL (~0UL) #define AML_BITMAP_FULL (~0UL)
#define AML_BITMAP_NBITS (8 * sizeof(AML_BITMAP_TYPE))
#define AML_BITMAP_NTH(i) ((i) / AML_BITMAP_NBITS) #define AML_BITMAP_NTH(i) ((i) / AML_BITMAP_NBITS)
#define AML_BITMAP_ITH(i) (((i) % AML_BITMAP_NBITS)) #define AML_BITMAP_ITH(i) (((i) % AML_BITMAP_NBITS))
struct aml_bitmap *aml_bitmap_alloc(void) struct aml_bitmap * aml_bitmap_create(void)
{ {
struct aml_bitmap *b = malloc(sizeof(struct aml_bitmap)); struct aml_bitmap *b = malloc(sizeof(struct aml_bitmap));
if(b == NULL) if(b == NULL)
...@@ -37,14 +37,14 @@ void aml_bitmap_copy_ulong(struct aml_bitmap *dst, unsigned long *src, ...@@ -37,14 +37,14 @@ void aml_bitmap_copy_ulong(struct aml_bitmap *dst, unsigned long *src,
struct aml_bitmap *aml_bitmap_dup(const struct aml_bitmap *a) struct aml_bitmap *aml_bitmap_dup(const struct aml_bitmap *a)
{ {
struct aml_bitmap *b = aml_bitmap_alloc(); struct aml_bitmap *b = aml_bitmap_create();
if(b == NULL) if(b == NULL)
return NULL; return NULL;
aml_bitmap_copy(b, a); aml_bitmap_copy(b, a);
return b; return b;
} }
void aml_bitmap_free(struct aml_bitmap *bitmap) void aml_bitmap_destroy(struct aml_bitmap *bitmap)
{ {
free(bitmap); free(bitmap);
} }
...@@ -141,7 +141,7 @@ int aml_bitmap_clear_range(struct aml_bitmap *bitmap, ...@@ -141,7 +141,7 @@ int aml_bitmap_clear_range(struct aml_bitmap *bitmap,
if(i >= AML_BITMAP_MAX || ii >= AML_BITMAP_MAX || i > ii) if(i >= AML_BITMAP_MAX || ii >= AML_BITMAP_MAX || i > ii)
return -1; return -1;
if(i == ii) if(i == ii)
return aml_bitmap_set(bitmap, i); return aml_bitmap_clear(bitmap, i);
unsigned long k = AML_BITMAP_ITH(ii+1); unsigned long k = AML_BITMAP_ITH(ii+1);
unsigned long low = ~(AML_BITMAP_FULL << AML_BITMAP_ITH(i)); unsigned long low = ~(AML_BITMAP_FULL << AML_BITMAP_ITH(i));
...@@ -177,3 +177,44 @@ unsigned long aml_bitmap_nset(const struct aml_bitmap *bitmap) ...@@ -177,3 +177,44 @@ unsigned long aml_bitmap_nset(const struct aml_bitmap *bitmap)
} }
return nset; return nset;
} }
int aml_bitmap_last(const struct aml_bitmap *bitmap)
{
if(bitmap == NULL)
return -1;
int n , i = 0;
for(n = AML_BITMAP_SIZE-1; n>=0 && bitmap->mask[n]==0; n--);
if(n < 0)
return -1;
AML_BITMAP_TYPE mask = bitmap->mask[n];
for(i=0; i<AML_BITMAP_NBITS && mask; i++)
mask = mask >> 1;
return (AML_BITMAP_NBITS * n) + i - 1;
}
int aml_bitmap_first(const struct aml_bitmap *bitmap)
{
if(bitmap == NULL)
return -1;
int n , i = 0;
for(n = 0; n<AML_BITMAP_SIZE && bitmap->mask[n]==0; n++);
if(n == AML_BITMAP_SIZE)
return -1;
AML_BITMAP_TYPE mask = bitmap->mask[n];
for(i=0; i<AML_BITMAP_NBITS && mask; i++)
mask = mask << 1;
int res = (AML_BITMAP_NBITS * n) + AML_BITMAP_NBITS - i;
return res;
}
#include "aml.h" #include "aml.h"
#include <assert.h> #include <assert.h>
static const unsigned long is[8] = {
0,
AML_BITMAP_NBITS / 4,
3 * AML_BITMAP_NBITS / 4,
AML_BITMAP_NBITS - 1,
AML_BITMAP_NBITS,
AML_BITMAP_NBITS + AML_BITMAP_NBITS / 4,
AML_BITMAP_NBITS + 3 * AML_BITMAP_NBITS / 4,
AML_BITMAP_NBITS + AML_BITMAP_NBITS - 1
};
static const int nis = sizeof(is) / sizeof(*is);
void test_bitmap_fill(){ void test_bitmap_fill(){
unsigned long i; unsigned long i;
struct aml_bitmap b; struct aml_bitmap b;
aml_bitmap_fill(&b); aml_bitmap_fill(&b);
for(i = 0; i < AML_BITMAP_MAX; i++) for(i = 0; i < nis; i++)
assert(aml_bitmap_isset(&b, i)); assert(aml_bitmap_isset(&b, is[i]));
assert(aml_bitmap_nset(&b) == AML_BITMAP_MAX); assert(aml_bitmap_nset(&b) == AML_BITMAP_MAX);
} }
...@@ -14,8 +27,8 @@ void test_bitmap_zero(){ ...@@ -14,8 +27,8 @@ void test_bitmap_zero(){
unsigned long i; unsigned long i;
struct aml_bitmap b; struct aml_bitmap b;
aml_bitmap_zero(&b); aml_bitmap_zero(&b);
for(i = 0; i < AML_BITMAP_MAX; i++) for(i = 0; i < nis; i++)
assert(!aml_bitmap_isset(&b, i)); assert(!aml_bitmap_isset(&b, is[i]));
assert(aml_bitmap_nset(&b) == 0); assert(aml_bitmap_nset(&b) == 0);
} }
...@@ -24,37 +37,38 @@ void test_bitmap_set(){ ...@@ -24,37 +37,38 @@ void test_bitmap_set(){
struct aml_bitmap b; struct aml_bitmap b;
aml_bitmap_zero(&b); aml_bitmap_zero(&b);
for(i = 0; i < AML_BITMAP_MAX; i++){ for(i = 0; i < nis; i++){
aml_bitmap_set(&b, i); aml_bitmap_set(&b, is[i]);
assert(aml_bitmap_isset(&b, i)); assert(aml_bitmap_isset(&b, is[i]));
for(j = 0; j < i; j++) for(j = 0; j < is[i]; j++)
assert(!aml_bitmap_isset(&b, j)); assert(!aml_bitmap_isset(&b, j));
for(j = i+1; j < AML_BITMAP_MAX; j++) for(j = is[i]+1; j < AML_BITMAP_MAX; j++)
assert(!aml_bitmap_isset(&b, j)); assert(!aml_bitmap_isset(&b, j));
assert(aml_bitmap_nset(&b) == 1); assert(aml_bitmap_nset(&b) == 1);
aml_bitmap_clear(&b, i); aml_bitmap_clear(&b, is[i]);
assert(!aml_bitmap_isset(&b, i)); assert(!aml_bitmap_isset(&b, is[i]));
} }
} }
void test_bitmap_clear(){ void test_bitmap_clear(){
unsigned long i,j; unsigned long i,j;
struct aml_bitmap b; struct aml_bitmap b;
aml_bitmap_fill(&b); aml_bitmap_fill(&b);
for(i = 0; i < AML_BITMAP_MAX; i++){ for(i = 0; i < nis; i++){
aml_bitmap_clear(&b, i); aml_bitmap_clear(&b, is[i]);
assert(!aml_bitmap_isset(&b, i)); assert(!aml_bitmap_isset(&b, is[i]));
for(j = 0; j < i; j++) for(j = 0; j < is[i]; j++)
assert(aml_bitmap_isset(&b, j)); assert(aml_bitmap_isset(&b, j));
for(j = i+1; j < AML_BITMAP_MAX; j++) for(j = is[i]+1; j < AML_BITMAP_MAX; j++)
assert(aml_bitmap_isset(&b, j)); assert(aml_bitmap_isset(&b, j));
assert(aml_bitmap_nset(&b) == (AML_BITMAP_MAX-1)); assert(aml_bitmap_nset(&b) == (AML_BITMAP_MAX-1));
aml_bitmap_set(&b, i); aml_bitmap_set(&b, is[i]);
assert(aml_bitmap_isset(&b, i)); assert(aml_bitmap_isset(&b, is[i]));
} }
} }
...@@ -62,15 +76,16 @@ void test_bitmap_set_range(){ ...@@ -62,15 +76,16 @@ void test_bitmap_set_range(){
unsigned long i, ii, j; unsigned long i, ii, j;
struct aml_bitmap b; struct aml_bitmap b;
aml_bitmap_zero(&b); aml_bitmap_zero(&b);
for(i = 0; i < AML_BITMAP_MAX; i++){
for(ii = i+1; ii < AML_BITMAP_MAX; ii++){ for(i = 0; i < nis; i++){
assert(aml_bitmap_set_range(&b, i, ii) == 0); for(ii = i; ii < nis; ii++){
assert(aml_bitmap_nset(&b) == (1 + ii - i)); assert(aml_bitmap_set_range(&b, is[i], is[ii]) == 0);
for(j = 0; j < i; j++) assert(aml_bitmap_nset(&b) == (1 + is[ii] - is[i]));
for(j = 0; j < is[i]; j++)
assert(!aml_bitmap_isset(&b, j)); assert(!aml_bitmap_isset(&b, j));
for(j = i; j <= ii; j++) for(j = is[i]; j <= is[ii]; j++)
assert(aml_bitmap_isset(&b, j)); assert(aml_bitmap_isset(&b, j));
for(j = ii+1; j < AML_BITMAP_MAX; j++) for(j = is[ii]+1; j < AML_BITMAP_MAX; j++)
assert(!aml_bitmap_isset(&b, j)); assert(!aml_bitmap_isset(&b, j));
aml_bitmap_zero(&b); aml_bitmap_zero(&b);
} }
...@@ -81,15 +96,16 @@ void test_bitmap_clear_range(){ ...@@ -81,15 +96,16 @@ void test_bitmap_clear_range(){
unsigned long i, ii, j; unsigned long i, ii, j;
struct aml_bitmap b; struct aml_bitmap b;
aml_bitmap_fill(&b); aml_bitmap_fill(&b);
for(i = 0; i < AML_BITMAP_MAX; i++){
for(ii = i+1; ii < AML_BITMAP_MAX; ii++){ for(i = 0; i < nis; i++){
assert(aml_bitmap_clear_range(&b, i, ii) == 0); for(ii = i; ii < nis; ii++){
assert(aml_bitmap_nset(&b) == (AML_BITMAP_MAX-ii+i-1)); assert(aml_bitmap_clear_range(&b, is[i], is[ii]) == 0);
for(j = 0; j < i; j++) assert(aml_bitmap_nset(&b) == (AML_BITMAP_MAX - is[ii] + is[i] - 1));
for(j = 0; j < is[i]; j++)
assert(aml_bitmap_isset(&b, j)); assert(aml_bitmap_isset(&b, j));
for(j = i; j <= ii; j++) for(j = is[i]; j <= is[ii]; j++)
assert(!aml_bitmap_isset(&b, j)); assert(!aml_bitmap_isset(&b, j));
for(j = ii+1; j < AML_BITMAP_MAX; j++) for(j = is[ii]+1; j < AML_BITMAP_MAX; j++)
assert(aml_bitmap_isset(&b, j)); assert(aml_bitmap_isset(&b, j));
aml_bitmap_fill(&b); aml_bitmap_fill(&b);
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment