]> git.ozlabs.org Git - ccan/blob - ccan/ilog/test/run-out-of-line.c
ilog: rework to reduce interface.
[ccan] / ccan / ilog / test / run-out-of-line.c
1 #include <ccan/ilog/ilog.h>
2 #include <ccan/ilog/ilog.c>
3 #include <stdio.h>
4 #include <ccan/tap/tap.h>
5
6 /*Dead simple (but slow) versions to compare against.*/
7
8 static int test_ilog32(uint32_t _v){
9   int ret;
10   for(ret=0;_v;ret++)_v>>=1;
11   return ret;
12 }
13
14 static int test_ilog64(uint64_t _v){
15   int ret;
16   for(ret=0;_v;ret++)_v>>=1;
17   return ret;
18 }
19
20 #define NTRIALS (64)
21
22 int main(int _argc,const char *_argv[]){
23   int i;
24   int j;
25   int (*il32)(uint32_t) = ilog32;
26   int (*il64)(uint64_t) = ilog64;
27   int (*il32_nz)(uint32_t) = ilog32_nz;
28   int (*il64_nz)(uint64_t) = ilog64_nz;
29
30   /*This is how many tests you plan to run.*/
31   plan_tests(33 * NTRIALS * 3 + 65 * NTRIALS * 3);
32   for(i=0;i<=32;i++){
33     uint32_t v;
34     /*Test each bit in turn (and 0).*/
35     v=i?(uint32_t)1U<<(i-1):0;
36     for(j=0;j<NTRIALS;j++){
37       int l;
38       l=test_ilog32(v);
39       ok1(STATIC_ILOG_32(v)==l);
40       ok1(il32(v)==l);
41       ok1(il32_nz(v) == l || v == 0);
42       /*Also try a few more pseudo-random values with at most the same number
43          of bits.*/
44       v=(1103515245U*v+12345U)&0xFFFFFFFFU>>((33-i)>>1)>>((32-i)>>1);
45     }
46   }
47
48   for(i=0;i<=64;i++){
49     uint64_t v;
50     /*Test each bit in turn (and 0).*/
51     v=i?(uint64_t)1U<<(i-1):0;
52     for(j=0;j<NTRIALS;j++){
53       int l;
54       l=test_ilog64(v);
55       ok1(STATIC_ILOG_64(v)==l);
56       ok1(il64(v)==l);
57       ok1(il64_nz(v) == l || v == 0);
58       /*Also try a few more pseudo-random values with at most the same number
59          of bits.*/
60       v=(uint64_t)((2862933555777941757ULL*v+3037000493ULL)
61         &0xFFFFFFFFFFFFFFFFULL>>((65-i)>>1)>>((64-i)>>1));
62     }
63   }
64   return exit_status();
65 }