int test2 (void) { struct tagbstring t = bsStatic ("Hello world"); bstring b; int ret = 0, reto; printf ("TEST: bSetChar function.\n"); ret += 0 <= bSetChar (&t, 4, ','); ret += 0 > bSetChar (b = bstrcpy (&t), 4, ','); ret += 0 >= biseqcstr (b, "Hell, world"); ret += 0 <= bSetChar (b, -1, 'x'); b->slen = 2; ret += 0 > bSetChar (b, 1, 'i'); ret += 0 >= biseqcstr (b, "Hi"); ret += 0 > bSetChar (b, 2, 's'); ret += 0 >= biseqcstr (b, "His"); ret += 0 > bSetChar (b, 1, '\0'); ret += blength (b) != 3; ret += bchare (b, 0, '?') != 'H'; ret += bchare (b, 1, '?') != '\0'; ret += bchare (b, 2, '?') != 's'; bdestroy (b); printf ("\t# failures: %d\n", ret); reto = ret; ret = 0; printf ("TEST: bSetCstrChar function.\n"); ret += 0 <= bSetCstrChar (&t, 4, ','); ret += 0 > bSetCstrChar (b = bstrcpy (&t), 4, ','); ret += 0 >= biseqcstr (b, "Hell, world"); ret += 0 <= bSetCstrChar (b, -1, 'x'); b->slen = 2; ret += 0 > bSetCstrChar (b, 1, 'i'); ret += 0 >= biseqcstr (b, "Hi"); ret += 0 > bSetCstrChar (b, 2, 's'); ret += 0 >= biseqcstr (b, "His"); ret += 0 > bSetCstrChar (b, 1, '\0'); ret += blength (b) != 1; ret += bchare (b, 0, '?') != 'H'; bdestroy (b); printf ("\t# failures: %d\n", ret); return reto + ret; }
static int test13_fgetc (void * ctx) { struct vfgetc * vctx = (struct vfgetc *) ctx; int c; if (NULL == vctx || NULL == vctx->base) return EOF; if (vctx->ofs >= blength (vctx->base)) return EOF; c = bchare (vctx->base, vctx->ofs, EOF); vctx->ofs++; return c; }
uint32_t Hashmap_djb_hash(void* data) { bstring s = (bstring)data; uint32_t hash = 5381; int i = 0; for(i = 0; i < blength(s); i++) { hash = ((hash << 5) + hash) + bchare(s, i, 0); // hash * 33 + c } return hash; }
uint32_t Hashmap_fnvla_hash(void* data) { bstring s = (bstring)data; uint32_t hash = FNV_OFFSET_BIAS; int i = 0; for(i = 0; i < blength(s); i++) { hash ^= bchare(s, i, 0); hash *= FNV_PRIME; } return hash; }
uint32_t Hashmap_adler32_hash(void* data) { bstring s = (bstring)data; uint32_t a = 1, b = 0; int i = 0; for(i = 0; i < blength(s); i++) { a = (a + bchare(s, i, 0)) % MOD_ADLER; b = (b + a) % MOD_ADLER; } return (b << 16) | a; }
uint32_t Hashmap_my_hash( void *data) { bstring s = (bstring)data; uint32_t hash = 0; int i = 0; for( i = 0 ; i < blength(s); i++) { hash += bchare(s , i , 0 ); } return hash; }
END_TEST START_TEST(core_002) { struct tagbstring t = bsStatic("Hello world"); int ret = 0; bstring b; ret = bSetChar(&t, 4, ','); ck_assert_int_eq(ret, BSTR_ERR); ret = bSetChar(b = bstrcpy(&t), 4, ','); ck_assert_int_eq(ret, 0); ret = biseqcstr(b, "Hell, world"); ck_assert_int_eq(ret, 1); ret = bSetChar(b, -1, 'x'); ck_assert_int_eq(ret, BSTR_ERR); b->slen = 2; ret = bSetChar(b, 1, 'i'); ck_assert_int_eq(ret, 0); ret = biseqcstr(b, "Hi"); ck_assert_int_eq(ret, 1); ret = bSetChar(b, 2, 's'); ck_assert_int_eq(ret, 0); ret = biseqcstr(b, "His"); ck_assert_int_eq(ret, 1); ret = bSetChar(b, 1, '\0'); ck_assert_int_eq(ret, 0); ret = blength(b); ck_assert_int_eq(ret, 3); ret = bchare(b, 0, '?'); ck_assert_int_eq(ret, 'H'); ret = bchare(b, 1, '?'); ck_assert_int_eq(ret, '\0'); ret = bchare(b, 2, '?'); ck_assert_int_eq(ret, 's'); ret = bdestroy(b); ck_assert_int_eq(ret, BSTR_OK); ret = 0; ret = bSetCstrChar(&t, 4, ','); ck_assert_int_eq(ret, BSTR_ERR); b = bstrcpy(&t); ck_assert(b != NULL); ret = bSetCstrChar(b, 4, ','); ck_assert_int_eq(ret, 0); ret = biseqcstr(b, "Hell, world"); ck_assert_int_eq(ret, 1); ret = bSetCstrChar(b, -1, 'x'); ck_assert_int_eq(ret, BSTR_ERR); b->slen = 2; ret = bSetCstrChar(b, 1, 'i'); ck_assert_int_eq(ret, 0); ret = biseqcstr(b, "Hi"); ck_assert_int_eq(ret, 1); ret = bSetCstrChar(b, 2, 's'); ck_assert_int_eq(ret, 0); ret = biseqcstr(b, "His"); ck_assert_int_eq(ret, 1); ret = bSetCstrChar(b, 1, '\0'); ck_assert_int_eq(ret, 0); ret = blength(b); ck_assert_int_eq(ret, 1); ret = bchare(b, 0, '?'); ck_assert_int_eq(ret, 'H'); ret = bdestroy(b); ck_assert_int_eq(ret, BSTR_OK); }