printf("m_expr which is %lld.%09lu \nand\n"
"n_expr which is %lld.%09lu\nare not close; diff=%lld.%09lunsec\n",
(long long)m.tv_sec, m.tv_nsec,
(long long)n.tv_sec, n.tv_nsec,
(long long)diff.tv_sec, diff.tv_nsec);
return FALSE;
}
u_int32
my_tick_to_tsf(u_int32 ticks)
{
// convert nanoseconds to l_fp fractional units, using double
// precision float calculations or, if available, 64bit integer
// arithmetic. This should give the precise fraction, rounded to
// the nearest representation.
#ifdef HAVE_U_INT64
return (u_int32)((( ((u_int64)(ticks)) << 32) + 500000000) / 1000000000);
#else
return (u_int32)((double(ticks)) * 4.294967296 + 0.5);
#endif
// And before you ask: if ticks >= 1000000000, the result is
// truncated nonsense, so don't use it out-of-bounds.
}
u_int32
my_tsf_to_tick(u_int32 tsf)
{
// Inverse operation: converts fraction to microseconds.
#ifdef HAVE_U_INT64
return (u_int32)(( ((u_int64)(tsf)) * 1000000000 + 0x80000000) >> 32);
#else
return (u_int32)(double(tsf) / 4.294967296 + 0.5);
#endif
// Beware: The result might be 10^9 due to rounding!
}
// ---------------------------------------------------------------------
// test support stuff -- part 1
// ---------------------------------------------------------------------
//----------------------------------------------------------------------
// test normalisation
//----------------------------------------------------------------------
void
test_Normalise(void)
{
long ns;
for ( ns = -2000000000; ns <= 2000000000; ns += 10000000) {
struct timespec x = timespec_init(0, ns);
x = normalize_tspec(x);
TEST_ASSERT_TRUE(timespec_isValid(x));
}
return;
}
//----------------------------------------------------------------------
// test classification
//----------------------------------------------------------------------
void
test_SignNoFrac(void)
{
// sign test, no fraction
int i;
for (i = -4; i <= 4; ++i) {
struct timespec a = timespec_init(i, 0);
int E = (i > 0) - (i < 0);
int r = test_tspec(a);
TEST_ASSERT_EQUAL(E, r);
}
return;
}
void
test_SignWithFrac(void)
{
// sign test, with fraction
int i;
for (i = -4; i <= 4; ++i) {
struct timespec a = timespec_init(i, 10);
int E = (i >= 0) - (i < 0);
int r = test_tspec(a);
TEST_ASSERT_EQUAL(E, r);
}
return;
}
//----------------------------------------------------------------------
// test compare
//----------------------------------------------------------------------
void
test_CmpFracEQ(void)
{
// fractions are equal
int i, j;
for (i = -4; i <= 4; ++i)
for (j = -4; j <= 4; ++j) {
struct timespec a = timespec_init( i , 200);
struct timespec b = timespec_init( j , 200);
int E = (i > j) - (i < j);
int r = cmp_tspec_denorm(a, b);
TEST_ASSERT_EQUAL(E, r);
}
return;
}
void
test_CmpFracGT(void)
{
// fraction a bigger fraction b
int i, j;
for (i = -4; i <= 4; ++i)
for (j = -4; j <= 4; ++j) {
struct timespec a = timespec_init(i, 999999800);
struct timespec b = timespec_init(j, 200);
int E = (i >= j) - (i < j);
int r = cmp_tspec_denorm(a, b);
TEST_ASSERT_EQUAL(E, r);
}
return;
}
void
test_CmpFracLT(void)
{
// fraction a less fraction b
int i, j;
for (i = -4; i <= 4; ++i)
for (j = -4; j <= 4; ++j) {
struct timespec a = timespec_init(i, 200);
struct timespec b = timespec_init(j, 999999800);
int E = (i > j) - (i <= j);
int r = cmp_tspec_denorm(a, b);
TEST_ASSERT_EQUAL(E, r);
}
return;
}
//----------------------------------------------------------------------
// Test addition (sum)
//----------------------------------------------------------------------
void
test_AddFullNorm(void)
{
int i, j;
for (i = -4; i <= 4; ++i)
for (j = -4; j <= 4; ++j) {
struct timespec a = timespec_init(i, 200);
struct timespec b = timespec_init(j, 400);
struct timespec E = timespec_init(i + j, 200 + 400);
struct timespec c;
c = add_tspec(a, b);
TEST_ASSERT_EQUAL_timespec(E, c);
}
return;
}
void
test_AddFullOflow1(void)
{
int i, j;
for (i = -4; i <= 4; ++i)
for (j = -4; j <= 4; ++j) {
struct timespec a = timespec_init(i, 200);
struct timespec b = timespec_init(j, 999999900);
struct timespec E = timespec_init(i + j + 1, 100);
struct timespec c;
c = add_tspec(a, b);
TEST_ASSERT_EQUAL_timespec(E, c);
}
return;
}
void
test_AddNsecNorm(void) {
int i;
for (i = -4; i <= 4; ++i) {
struct timespec a = timespec_init(i, 200);
struct timespec E = timespec_init(i, 600);
struct timespec c;
c = add_tspec_ns(a, 600 - 200);
TEST_ASSERT_EQUAL_timespec(E, c);
}
return;
}
void
test_AddNsecOflow1(void)
{
int i;
for (i = -4; i <= 4; ++i) {
struct timespec a = timespec_init(i, 200);
struct timespec E = timespec_init(i + 1, 100);
struct timespec c;
c = add_tspec_ns(a, NANOSECONDS - 100);
TEST_ASSERT_EQUAL_timespec(E, c);
}
return;
}
//----------------------------------------------------------------------
// test subtraction (difference)
//----------------------------------------------------------------------
void
test_SubFullNorm(void)
{
int i, j;
for (i = -4; i <= 4; ++i)
for (j = -4; j <= 4; ++j) {
struct timespec a = timespec_init( i , 600);
struct timespec b = timespec_init( j , 400);
struct timespec E = timespec_init(i-j, 200);
struct timespec c;
c = sub_tspec(a, b);
TEST_ASSERT_EQUAL_timespec(E, c);
}
return;
}
void
test_SubFullOflow(void)
{
int i, j;
for (i = -4; i <= 4; ++i)
for (j = -4; j <= 4; ++j) {
struct timespec a = timespec_init(i, 100);
struct timespec b = timespec_init(j, 999999900);
struct timespec E = timespec_init(i - j - 1, 200);
struct timespec c;
c = sub_tspec(a, b);
TEST_ASSERT_EQUAL_timespec(E, c);
}
return;
}
void
test_SubNsecNorm(void)
{
int i;
for (i = -4; i <= 4; ++i) {
struct timespec a = timespec_init(i, 600);
struct timespec E = timespec_init(i, 200);
struct timespec c;
c = sub_tspec_ns(a, 600 - 200);
TEST_ASSERT_EQUAL_timespec(E, c);
}
return;
}
void
test_SubNsecOflow(void)
{
int i;
for (i = -4; i <= 4; ++i) {
struct timespec a = timespec_init( i , 100);
struct timespec E = timespec_init(i-1, 200);
struct timespec c;
c = sub_tspec_ns(a, NANOSECONDS - 100);
TEST_ASSERT_EQUAL_timespec(E, c);
}
return;
}
//----------------------------------------------------------------------
// test negation
//----------------------------------------------------------------------
void
test_Neg(void)
{
int i;
for (i = -4; i <= 4; ++i) {
struct timespec a = timespec_init(i, 100);
struct timespec b;
struct timespec c;
b = neg_tspec(a);
c = add_tspec(a, b);
TEST_ASSERT_EQUAL(0, test_tspec(c));
}
return;
}
//----------------------------------------------------------------------
// test abs value
//----------------------------------------------------------------------
void
test_AbsNoFrac(void)
{
int i;
for (i = -4; i <= 4; ++i) {
struct timespec a = timespec_init(i , 0);
struct timespec b;
b = abs_tspec(a);
TEST_ASSERT_EQUAL((i != 0), test_tspec(b));
}
return;
}
void
test_AbsWithFrac(void)
{
int i;
for (i = -4; i <= 4; ++i) {
struct timespec a = timespec_init(i, 100);
struct timespec b;
b = abs_tspec(a);
TEST_ASSERT_EQUAL(1, test_tspec(b));
}
return;
}
// ---------------------------------------------------------------------
// test support stuff -- part 2
// ---------------------------------------------------------------------
for (x.tv_sec = -2; x.tv_sec < 3; x.tv_sec++)
for (x.tv_nsec = 1;
x.tv_nsec < 1000000000;
x.tv_nsec += 499999999) {
for (i = -4; i < 5; ++i) {
y = x;
y.tv_nsec += i;
if (i >= -2 && i <= 2) {
TEST_ASSERT_TRUE(AssertTimespecClose(x, y, limit));
}
else
{
TEST_ASSERT_FALSE(AssertTimespecClose(x, y, limit));
}
}
}
return;
}
//----------------------------------------------------------------------
// conversion to l_fp
//----------------------------------------------------------------------
// Not *exactly* a bittest, because 2**32 tests would take a
// really long time even on very fast machines! So we do test
// every 1000 fractional units.
u_int32 tsf;
for (tsf = 0; tsf < ~((u_int32)(1000)); tsf += 1000) {
struct timespec E = timespec_init(1, my_tsf_to_tick(tsf));
l_fp a = l_fp_init(1, tsf);
struct timespec r;
r = lfp_intv_to_tspec(a);
// The conversion might be off by one nanosecond when
// comparing to calculated value.
TEST_ASSERT_TRUE(AssertTimespecClose(E, r, limit));
}
for (i = 0; i < COUNTOF(data); ++i) {
struct timespec a = timespec_init(data[i].sec, data[i].nsec);
const char * E = data[i].repr;
const char * r = tspectoa(a);
TEST_ASSERT_EQUAL_STRING(E, r);
}