1
0
mirror of https://github.com/samba-team/samba.git synced 2025-06-22 07:17:05 +03:00
samba-mirror/lib/ccan/tally/test/run-median.c
Rusty Russell 894b0273fc lib/ccan/tally.h: update for FreeBSD compile.
Based on commit 0284423676209380a2e07086b9b356096a2f93e6 from CCAN:
Author: Rusty Russell <rusty@rustcorp.com.au>
Date:   Tue Jun 21 10:43:31 2011 +0930

    tally: fix FreeBSD compile, memleak in tests.

    Posix says ssize_t is in sys/types.h; on Linux stdlib.h is enough.

Autobuild-User: Rusty Russell <rusty@rustcorp.com.au>
Autobuild-Date: Tue Jun 21 05:52:12 CEST 2011 on sn-devel-104
2011-06-21 05:52:12 +02:00

47 lines
1.1 KiB
C

#include <ccan/tally/tally.c>
#include <ccan/tap/tap.h>
int main(void)
{
int i;
struct tally *tally = tally_new(100);
ssize_t min, max, median;
size_t err;
max = (ssize_t)~(1ULL << (sizeof(max)*CHAR_BIT - 1));
min = (ssize_t)(1ULL << (sizeof(max)*CHAR_BIT - 1));
plan_tests(100*2 + 100*2 + 100*2);
/* Simple median test: should always be around 0. */
for (i = 0; i < 100; i++) {
tally_add(tally, i);
tally_add(tally, -i);
median = tally_approx_median(tally, &err);
ok1(err <= 4);
ok1(median - (ssize_t)err <= 0 && median + (ssize_t)err >= 0);
}
/* Works for big values too... */
for (i = 0; i < 100; i++) {
tally_add(tally, max - i);
tally_add(tally, min + 1 + i);
median = tally_approx_median(tally, &err);
/* Error should be < 100th of max - min. */
ok1(err <= max / 100 * 2);
ok1(median - (ssize_t)err <= 0 && median + (ssize_t)err >= 0);
}
free(tally);
tally = tally_new(10);
for (i = 0; i < 100; i++) {
tally_add(tally, i);
median = tally_approx_median(tally, &err);
ok1(err <= i / 10 + 1);
ok1(median - (ssize_t)err <= i/2
&& median + (ssize_t)err >= i/2);
}
free(tally);
return exit_status();
}