66 lines
1.6 KiB
C
66 lines
1.6 KiB
C
#ifdef UNITTEST
|
|
// if we want to test the mremap-version of mstorage under Linux, we
|
|
// need to have _GNU_SOURCE defined otherwise MREMAP_MAYMOVE is missing
|
|
#define _GNU_SOURCE
|
|
#endif
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <libowfat/str.h>
|
|
#include "bstr.h"
|
|
#include "mstorage.h"
|
|
#include "mduptab.h"
|
|
#include <libowfat/uint32.h>
|
|
|
|
#ifdef UNITTEST
|
|
static int failafter;
|
|
ssize_t fail_mstorage_add(mstorage_t* p,const char* s,size_t n) {
|
|
if (--failafter == 0)
|
|
return -1;
|
|
return mstorage_add(p,s,n);
|
|
}
|
|
#define mstorage_add fail_mstorage_add
|
|
#endif
|
|
|
|
ssize_t mduptab_add(mduptab_t* t,const char* s,size_t len) {
|
|
unsigned int i;
|
|
unsigned long* l=(unsigned long*)t->table.root;
|
|
ssize_t x,bak;
|
|
for (i=0; i<t->table.used/sizeof(unsigned long); ++i)
|
|
if (bstr_equal2(t->Strings->root+l[i],s,len))
|
|
return l[i];
|
|
bak=t->Strings->used;
|
|
if ((x=mstorage_add_bin(t->Strings,s,len))<0)
|
|
return -1;
|
|
if (mstorage_add(&t->table,(const char*)&x,sizeof(x))<0) {
|
|
t->Strings->used=bak;
|
|
return -1;
|
|
}
|
|
return x;
|
|
}
|
|
|
|
#ifdef UNITTEST
|
|
#undef mstorage_add
|
|
#include <assert.h>
|
|
#undef UNITTEST
|
|
#include "mduptab_init.c"
|
|
#include "mstorage_init.c"
|
|
#include "mstorage_add.c"
|
|
#include "mstorage_add_bin.c"
|
|
#include "mduptab_reset.c"
|
|
#include "mstorage_unmap.c"
|
|
#include "bstr_diff2.c"
|
|
|
|
int main() {
|
|
mduptab_t t;
|
|
mduptab_init(&t);
|
|
assert(mduptab_add(&t,"foo",3) == 0);
|
|
assert(mduptab_add(&t,"bar",3) == 4);
|
|
assert(mduptab_add(&t,"foo",3) == 0);
|
|
assert(mduptab_add(&t,"bar",3) == 4);
|
|
assert(mduptab_add(&t, "foo", (size_t)-1) == -1);
|
|
failafter=1;
|
|
assert(mduptab_add(&t, "baz", 3) == -1);
|
|
mduptab_reset(&t);
|
|
}
|
|
#endif
|