2007-01-24 01:03:52 +00:00
|
|
|
#include <check.h>
|
|
|
|
#include <assert.h>
|
2007-01-25 22:38:22 +00:00
|
|
|
#include <lladd/transactional.h>
|
2007-01-24 01:03:52 +00:00
|
|
|
#define LOG_NAME "check_rangeTracker.log"
|
2007-01-25 22:38:22 +00:00
|
|
|
|
2007-01-24 01:03:52 +00:00
|
|
|
#include <lladd/io/rangeTracker.h>
|
|
|
|
|
2007-01-25 22:38:22 +00:00
|
|
|
#include "../check_includes.h"
|
|
|
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
long myrandom(long x) {
|
|
|
|
double xx = x;
|
|
|
|
double r = random();
|
|
|
|
double max = ((uint64_t)RAND_MAX)+1;
|
|
|
|
max /= xx;
|
|
|
|
return (long)((r/max));
|
|
|
|
}
|
|
|
|
|
2007-01-24 01:03:52 +00:00
|
|
|
void printRT(rangeTracker * rt) {
|
2007-01-25 22:38:22 +00:00
|
|
|
const transition ** ts = rangeTrackerEnumerate(rt);
|
2007-01-24 01:03:52 +00:00
|
|
|
int i = 0;
|
|
|
|
printf("Range tracker:\n");
|
|
|
|
while(ts[i]) {
|
|
|
|
printf("%s\n", transitionToString(ts[i]));
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
free (ts);
|
|
|
|
}
|
|
|
|
|
|
|
|
START_TEST(rangeTracker_smokeTest) {
|
|
|
|
rangeTracker * rt = rangeTrackerInit(512);
|
|
|
|
|
2007-01-25 22:38:22 +00:00
|
|
|
const transition ** ts = rangeTrackerEnumerate(rt);
|
2007-01-24 01:03:52 +00:00
|
|
|
|
|
|
|
assert(ts[0] == 0);
|
|
|
|
|
|
|
|
free(ts);
|
|
|
|
|
|
|
|
// printRT(rt);
|
|
|
|
|
|
|
|
|
|
|
|
range r;
|
|
|
|
r.start = 10;
|
2007-01-25 22:38:22 +00:00
|
|
|
r.stop = 100;
|
2007-01-24 01:03:52 +00:00
|
|
|
|
|
|
|
rangeTrackerAdd(rt, &r);
|
|
|
|
|
|
|
|
// printRT(rt);
|
|
|
|
|
2007-01-25 22:38:22 +00:00
|
|
|
ts = rangeTrackerEnumerate(rt);
|
2007-01-24 01:03:52 +00:00
|
|
|
|
|
|
|
// 10 20 80 90 100
|
|
|
|
// | | | | |
|
|
|
|
// |1111|11111111111111|1111|111|
|
|
|
|
// | | | | |
|
|
|
|
|
|
|
|
assert(ts[0]->pos == 10 && ts[0]->delta == 1 && ts[0]->pins == 0 &&
|
|
|
|
ts[1]->pos == 100&& ts[1]->delta == -1 && ts[1]->pins == 1 && !ts[2]);
|
|
|
|
|
|
|
|
|
|
|
|
r.start = 20;
|
2007-01-25 22:38:22 +00:00
|
|
|
r.stop = 80;
|
2007-01-24 01:03:52 +00:00
|
|
|
|
|
|
|
rangeTrackerRemove(rt, &r);
|
|
|
|
|
|
|
|
// printRT(rt);
|
|
|
|
|
2007-01-25 22:38:22 +00:00
|
|
|
ts = rangeTrackerEnumerate(rt);
|
2007-01-24 01:03:52 +00:00
|
|
|
|
|
|
|
// 10 20 80 90 100
|
|
|
|
// | | | | |
|
|
|
|
// |1111| |1111|111|
|
|
|
|
// | | | | |
|
|
|
|
|
|
|
|
assert(ts[0]->pos == 10 && ts[0]->delta == 1 && ts[0]->pins == 0 &&
|
|
|
|
ts[1]->pos == 20 && ts[1]->delta == -1 && ts[1]->pins == 1 &&
|
|
|
|
ts[2]->pos == 80 && ts[2]->delta == 1 && ts[2]->pins == 0 &&
|
|
|
|
ts[3]->pos == 100&& ts[3]->delta == -1 && ts[3]->pins == 1 && !ts[4]);
|
|
|
|
|
|
|
|
|
|
|
|
rangeTrackerAdd(rt, &r);
|
|
|
|
|
|
|
|
// printRT(rt);
|
|
|
|
|
2007-01-25 22:38:22 +00:00
|
|
|
ts = rangeTrackerEnumerate(rt);
|
2007-01-24 01:03:52 +00:00
|
|
|
|
|
|
|
// 10 20 80 90 100
|
|
|
|
// | | | | |
|
|
|
|
// |1111|11111111111111|1111|111|
|
|
|
|
// | | | | |
|
|
|
|
|
|
|
|
|
|
|
|
assert(ts[0]->pos == 10 && ts[0]->delta == 1 && ts[0]->pins == 0 &&
|
|
|
|
ts[1]->pos == 100&& ts[1]->delta == -1 && ts[1]->pins == 1 && !ts[2]);
|
|
|
|
|
|
|
|
rangeTrackerAdd(rt, &r);
|
|
|
|
|
|
|
|
// printRT(rt);
|
|
|
|
|
2007-01-25 22:38:22 +00:00
|
|
|
ts = rangeTrackerEnumerate(rt);
|
2007-01-24 01:03:52 +00:00
|
|
|
|
|
|
|
// 10 20 80 90 100
|
|
|
|
// | | | | |
|
|
|
|
// |1111|22222222222222|1111|111|
|
|
|
|
// | | | | |
|
|
|
|
|
|
|
|
assert(ts[0]->pos == 10 && ts[0]->delta == 1 && ts[0]->pins == 0 &&
|
|
|
|
ts[1]->pos == 20 && ts[1]->delta == 1 && ts[1]->pins == 1 &&
|
|
|
|
ts[2]->pos == 80 && ts[2]->delta == -1 && ts[2]->pins == 2 &&
|
|
|
|
ts[3]->pos == 100&& ts[3]->delta == -1 && ts[3]->pins == 1 && !ts[4]);
|
|
|
|
|
|
|
|
|
|
|
|
rangeTrackerRemove(rt, &r);
|
2007-01-25 22:38:22 +00:00
|
|
|
ts = rangeTrackerEnumerate(rt);
|
2007-01-24 01:03:52 +00:00
|
|
|
|
|
|
|
// 10 20 80 90 100
|
|
|
|
// | | | | |
|
|
|
|
// |1111|11111111111111|1111|111|
|
|
|
|
// | | | | |
|
|
|
|
|
|
|
|
|
|
|
|
assert(ts[0]->pos == 10 && ts[0]->delta == 1 && ts[0]->pins == 0 &&
|
|
|
|
ts[1]->pos == 100&& ts[1]->delta == -1 && ts[1]->pins == 1 && !ts[2]);
|
|
|
|
rangeTrackerRemove(rt, &r);
|
|
|
|
|
|
|
|
// printRT(rt);
|
|
|
|
|
2007-01-25 22:38:22 +00:00
|
|
|
ts = rangeTrackerEnumerate(rt);
|
2007-01-24 01:03:52 +00:00
|
|
|
|
|
|
|
// 10 20 80 90 100
|
|
|
|
// | | | | |
|
|
|
|
// |1111| |1111|111|
|
|
|
|
// | | | | |
|
|
|
|
|
|
|
|
assert(ts[0]->pos == 10 && ts[0]->delta == 1 && ts[0]->pins == 0 &&
|
|
|
|
ts[1]->pos == 20 && ts[1]->delta == -1 && ts[1]->pins == 1 &&
|
|
|
|
ts[2]->pos == 80 && ts[2]->delta == 1 && ts[2]->pins == 0 &&
|
|
|
|
ts[3]->pos == 100&& ts[3]->delta == -1 && ts[3]->pins == 1 && !ts[4]);
|
|
|
|
|
|
|
|
r.start = 80;
|
2007-01-25 22:38:22 +00:00
|
|
|
r.stop = 90;
|
2007-01-24 01:03:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rangeTrackerAdd(rt, &r);
|
|
|
|
|
|
|
|
// printRT(rt);
|
|
|
|
|
2007-01-25 22:38:22 +00:00
|
|
|
ts = rangeTrackerEnumerate(rt);
|
2007-01-24 01:03:52 +00:00
|
|
|
|
|
|
|
// 10 20 80 90 100
|
|
|
|
// | | | | |
|
|
|
|
// |1111| |2222|111|
|
|
|
|
// | | | | |
|
|
|
|
|
|
|
|
|
|
|
|
assert(ts[0]->pos == 10 && ts[0]->delta == 1 && ts[0]->pins == 0 &&
|
|
|
|
ts[1]->pos == 20 && ts[1]->delta == -1 && ts[1]->pins == 1 &&
|
|
|
|
ts[2]->pos == 80 && ts[2]->delta == 2 && ts[2]->pins == 0 &&
|
|
|
|
ts[3]->pos == 90 && ts[3]->delta == -1 && ts[3]->pins == 2 &&
|
|
|
|
ts[4]->pos == 100&& ts[4]->delta == -1 && ts[4]->pins == 1 && !ts[5]);
|
|
|
|
|
|
|
|
r.start = 80;
|
2007-01-25 22:38:22 +00:00
|
|
|
r.stop = 100;
|
2007-01-24 01:03:52 +00:00
|
|
|
rangeTrackerRemove(rt, &r);
|
|
|
|
|
|
|
|
// printRT(rt);
|
2007-01-25 22:38:22 +00:00
|
|
|
ts = rangeTrackerEnumerate(rt);
|
2007-01-24 01:03:52 +00:00
|
|
|
|
|
|
|
// 10 20 80 90 100
|
|
|
|
// | | | | |
|
|
|
|
// |1111| |1111| |
|
|
|
|
// | | | | |
|
|
|
|
|
|
|
|
|
|
|
|
assert(ts[0]->pos == 10 && ts[0]->delta == 1 && ts[0]->pins == 0 &&
|
|
|
|
ts[1]->pos == 20 && ts[1]->delta == -1 && ts[1]->pins == 1 &&
|
|
|
|
ts[2]->pos == 80 && ts[2]->delta == 1 && ts[2]->pins == 0 &&
|
|
|
|
ts[3]->pos == 90 && ts[3]->delta == -1 && ts[3]->pins == 1 && !ts[4]);
|
|
|
|
|
|
|
|
|
|
|
|
r.start = 10;
|
2007-01-25 22:38:22 +00:00
|
|
|
r.stop = 20;
|
2007-01-24 01:03:52 +00:00
|
|
|
rangeTrackerRemove(rt, &r);
|
|
|
|
|
|
|
|
// printRT(rt);
|
2007-01-25 22:38:22 +00:00
|
|
|
ts = rangeTrackerEnumerate(rt);
|
2007-01-24 01:03:52 +00:00
|
|
|
|
|
|
|
// 10 20 80 90 100
|
|
|
|
// | | | | |
|
|
|
|
// | | |1111| |
|
|
|
|
// | | | | |
|
|
|
|
|
|
|
|
|
|
|
|
assert(ts[0]->pos == 80 && ts[0]->delta == 1 && ts[0]->pins == 0 &&
|
|
|
|
ts[1]->pos == 90 && ts[1]->delta == -1 && ts[1]->pins == 1 && !ts[2]);
|
|
|
|
|
|
|
|
|
|
|
|
r.start = 80;
|
2007-01-25 22:38:22 +00:00
|
|
|
r.stop = 90;
|
2007-01-24 01:03:52 +00:00
|
|
|
|
|
|
|
rangeTrackerRemove(rt, &r);
|
|
|
|
|
|
|
|
// printRT(rt);
|
2007-01-25 22:38:22 +00:00
|
|
|
ts = rangeTrackerEnumerate(rt);
|
2007-01-24 01:03:52 +00:00
|
|
|
|
|
|
|
// 10 20 80 90 100
|
|
|
|
// | | | | |
|
|
|
|
// | | | | |
|
|
|
|
// | | | | |
|
|
|
|
|
|
|
|
|
2007-01-25 22:38:22 +00:00
|
|
|
assert(!ts[0]);
|
2007-01-24 01:03:52 +00:00
|
|
|
rangeTrackerDeinit(rt);
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
2007-01-25 22:38:22 +00:00
|
|
|
#define RANGE_SIZE 1000
|
|
|
|
#define ITERATIONS 1000
|
|
|
|
#define RANGE_COUNT 100
|
|
|
|
void randomRange(range * r) {
|
|
|
|
long start = myrandom(RANGE_SIZE-1);
|
|
|
|
long len = 1+myrandom(RANGE_SIZE - start - 1);
|
|
|
|
|
|
|
|
r->start = start;
|
|
|
|
r->stop = start + len;
|
|
|
|
assert(r->start < RANGE_SIZE);
|
|
|
|
assert(r->stop < RANGE_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkRangeTrackerConsistency(rangeTracker * rt) {
|
|
|
|
const transition ** t = rangeTrackerEnumerate(rt);
|
|
|
|
|
|
|
|
int pins = 0;
|
|
|
|
long pos = -1;
|
|
|
|
int i = 0;
|
|
|
|
while(t[i]) {
|
|
|
|
|
|
|
|
assert(pos < t[i]->pos);
|
|
|
|
assert(pins == t[i]->pins);
|
|
|
|
assert(pins >= 0);
|
|
|
|
assert(t[i]->delta);
|
|
|
|
|
|
|
|
pos = t[i]->pos;
|
|
|
|
pins += t[i]->delta;
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(pins == 0);
|
|
|
|
|
|
|
|
free(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
START_TEST (rangeTracker_randomTest) {
|
|
|
|
|
|
|
|
struct timeval time;
|
|
|
|
|
|
|
|
gettimeofday(&time,0);
|
|
|
|
|
|
|
|
long seed = time.tv_usec + time.tv_sec * 1000000;
|
|
|
|
printf("\nSeed = %ld\n", seed);
|
|
|
|
srandom(seed);
|
|
|
|
|
|
|
|
range * ranges = malloc(sizeof(range) * RANGE_COUNT);
|
|
|
|
int * pins = calloc(RANGE_COUNT, sizeof(int));
|
|
|
|
rangeTracker * rt = rangeTrackerInit(512);
|
|
|
|
for(long i = 0; i < RANGE_COUNT; i++) {
|
|
|
|
randomRange(&(ranges[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
char * bitmask = calloc(RANGE_SIZE, sizeof(char));
|
|
|
|
|
|
|
|
for(long i = 0; i < ITERATIONS; i++) {
|
|
|
|
|
|
|
|
int range = myrandom(RANGE_COUNT);
|
|
|
|
|
|
|
|
switch(myrandom(3)) {
|
|
|
|
case 0: { // add range
|
|
|
|
rangeTrackerAdd(rt, &ranges[range]);
|
|
|
|
pins[range]++;
|
|
|
|
checkRangeTrackerConsistency(rt);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 1: { // del range
|
|
|
|
if(pins[range]) {
|
|
|
|
rangeTrackerRemove(rt, &ranges[range]);
|
|
|
|
pins[range]--;
|
|
|
|
}
|
|
|
|
checkRangeTrackerConsistency(rt);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 2: { // change range
|
|
|
|
for(long i = 0; i < RANGE_COUNT; i++) {
|
|
|
|
if(!pins[i]) {
|
|
|
|
randomRange(&ranges[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(long i = 0; i < RANGE_COUNT; i++) {
|
|
|
|
while(pins[i]) {
|
|
|
|
rangeTrackerRemove(rt, &ranges[i]);
|
|
|
|
pins[i]--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free (bitmask);
|
|
|
|
|
|
|
|
rangeTrackerDeinit(rt);
|
|
|
|
|
|
|
|
} END_TEST
|
|
|
|
|
2007-01-24 01:03:52 +00:00
|
|
|
/**
|
|
|
|
Add suite declarations here
|
|
|
|
*/
|
|
|
|
Suite * check_suite(void) {
|
|
|
|
Suite *s = suite_create("rangeTracker");
|
|
|
|
/* Begin a new test */
|
|
|
|
TCase *tc = tcase_create("rangeTracker");
|
|
|
|
tcase_set_timeout(tc, 600); // ten minute timeout
|
|
|
|
|
|
|
|
/* Sub tests are added, one per line, here */
|
|
|
|
tcase_add_test(tc, rangeTracker_smokeTest);
|
2007-01-25 22:38:22 +00:00
|
|
|
tcase_add_test(tc, rangeTracker_randomTest);
|
2007-01-24 01:03:52 +00:00
|
|
|
|
|
|
|
/* --------------------------------------------- */
|
|
|
|
tcase_add_checked_fixture(tc, setup, teardown);
|
|
|
|
suite_add_tcase(s, tc);
|
|
|
|
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "../check_setup.h"
|