Browse Source

* bitmap.[ch]

master
Alex 'AdUser' Z 11 years ago
parent
commit
2e6d7f562c
  1. 10
      src/bitmap.c
  2. 9
      src/bitmap.h
  3. 3
      tests/Makefile
  4. 38
      tests/bitmap.c

10
src/bitmap.c

@ -17,14 +17,14 @@
#include "main.h"
#include "bitmap.h"
int bitmap_compare(bitmap_t *a, bitmap_t *b)
int bitmap_compare(const unsigned char *a, const unsigned char *b)
{
uint16_t row = 0;
uint16_t diff = 0;
unsigned char diff = 0;
size_t i = 0;
size_t cnt = 0;
for (row = 0; row < 16; row++) {
diff = (*a)[row] ^ (*b)[row];
for (i = 0; i < BITMAP_SIZE; i++, a++, b++) {
diff = *a ^ *b;
while (diff) {
cnt += (diff & 1);
diff >>= 1;

9
src/bitmap.h

@ -1,6 +1,9 @@
#ifndef HAS_BITMAP_H
typedef uint16_t bitmap_t[16];
int bitmap_compare(bitmap_t *a, bitmap_t *b);
#define BITMAP_SIZE 32
typedef unsigned char bitmap_t[BITMAP_SIZE];
int bitmap_compare(const unsigned char *a, const unsigned char *b);
#endif
#define HAS_BITMAP_H
#define HAS_BITMAP_H 1

3
tests/Makefile

@ -8,3 +8,6 @@ CFLAGS=-Wall -Wextra -O0 -g -ggdb -pedantic
SRC=../src/bitmap.c bitmap.c
test-bitmap:
gcc $(CFLAGS) $(SRC) -o $@
clean:
rm -f test-bitmap

38
tests/bitmap.c

@ -4,31 +4,31 @@
int
main(int argc, char **argv)
{
bitmap a, b;
bitmap_t a, b;
memset (&a, 0x00, 16 * sizeof(uint16_t));
memset (&b, 0x00, 16 * sizeof(uint16_t));
assert(bitmap_compare(&a, &b) == 0);
memset (a, 0x00, sizeof(bitmap_t));
memset (b, 0x00, sizeof(bitmap_t));
assert(bitmap_compare(a, b) == 0);
memset (&a, 0xFE, 16 * sizeof(uint16_t));
memset (&b, 0xFF, 16 * sizeof(uint16_t));
assert(bitmap_compare(&a, &b) == 32);
memset (a, 0xFE, sizeof(bitmap_t));
memset (b, 0xFF, sizeof(bitmap_t));
assert(bitmap_compare(a, b) == 32);
memset (&a, 0x00, 16 * sizeof(uint16_t));
memset (&b, 0x55, 16 * sizeof(uint16_t));
assert(bitmap_compare(&a, &b) == 128);
memset (a, 0x00, sizeof(bitmap_t));
memset (b, 0x55, sizeof(bitmap_t));
assert(bitmap_compare(a, b) == 128);
memset (&a, 0x00, 16 * sizeof(uint16_t));
memset (&b, 0xAA, 16 * sizeof(uint16_t));
assert(bitmap_compare(&a, &b) == 128);
memset (a, 0x00, sizeof(bitmap_t));
memset (b, 0xAA, sizeof(bitmap_t));
assert(bitmap_compare(a, b) == 128);
memset (&a, 0xAA, 16 * sizeof(uint16_t));
memset (&b, 0x55, 16 * sizeof(uint16_t));
assert(bitmap_compare(&a, &b) == 256);
memset (a, 0xAA, sizeof(bitmap_t));
memset (b, 0x55, sizeof(bitmap_t));
assert(bitmap_compare(a, b) == 256);
memset (&a, 0x00, 16 * sizeof(uint16_t));
memset (&b, 0xFF, 16 * sizeof(uint16_t));
assert(bitmap_compare(&a, &b) == 256);
memset (a, 0x00, sizeof(bitmap_t));
memset (b, 0xFF, sizeof(bitmap_t));
assert(bitmap_compare(a, b) == 256);
return 0;
}

Loading…
Cancel
Save