Skip to content

Commit

Permalink
lib: test-bits - add fraclog test for most usual case, constant fracbits
Browse files Browse the repository at this point in the history
Were there to be any difference between the constant and non-constant tests,
then it would be a compiler error, but it's better to be defensive and know
about that case.

Signed-off-by: Phil Carmody <phil@dovecot.fi>
  • Loading branch information
Phil Carmody authored and GitLab committed Apr 19, 2016
1 parent 20be2a7 commit 943ba39
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/lib/test-bits.c
Expand Up @@ -107,10 +107,36 @@ static void test_bits_fraclog(void)
}
}

/* The compiler *should* generate different code when the fracbits parameter
is a compile-time constant, so we also need to check that's the case.
*/
static void test_bits_fraclog_const(void)
{
#define FRACBITS 2
#define STR2(s) #s
#define STR(s) STR2(s)
test_begin("fraclog constant " STR(FRACBITS) " bit");

unsigned int i;
unsigned int last_end = ~0u;
for (i = 0; i < BITS_FRACLOG_BUCKETS(FRACBITS); i++) {
unsigned int start = bits_fraclog_bucket_start(i, FRACBITS);
unsigned int end = bits_fraclog_bucket_end(i, FRACBITS);
test_assert_idx(start == last_end + 1, i);
last_end = end;
test_assert_idx(bits_fraclog(start, FRACBITS) == i, i);
test_assert_idx(bits_fraclog(end, FRACBITS) == i, i);
}
test_assert(last_end == ~0u);

test_end();
}

void test_bits(void)
{
test_nearest_power();
test_bits_requiredXX();
test_bits_fraclog();
test_bits_fraclog_const();
test_sum_overflows();
}

0 comments on commit 943ba39

Please sign in to comment.