]> git.ozlabs.org Git - ccan/blob - ccan/ilog/test/run.c
hash: use config.h settings for endian.
[ccan] / ccan / ilog / test / run.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   /*This is how many tests you plan to run.*/
26   plan_tests(33 * NTRIALS * 3 + 65 * NTRIALS * 3);
27   for(i=0;i<=32;i++){
28     uint32_t v;
29     /*Test each bit in turn (and 0).*/
30     v=i?(uint32_t)1U<<(i-1):0;
31     for(j=0;j<NTRIALS;j++){
32       int l;
33       l=test_ilog32(v);
34       ok1(STATIC_ILOG_32(v)==l);
35       ok1(ilog32(v)==l);
36       ok1(ilog32_nz(v) == l || v == 0);
37       /*Also try a few more pseudo-random values with at most the same number
38          of bits.*/
39       v=(1103515245U*v+12345U)&0xFFFFFFFFU>>((33-i)>>1)>>((32-i)>>1);
40     }
41   }
42
43   for(i=0;i<=64;i++){
44     uint64_t v;
45     /*Test each bit in turn (and 0).*/
46     v=i?(uint64_t)1U<<(i-1):0;
47     for(j=0;j<NTRIALS;j++){
48       int l;
49       l=test_ilog64(v);
50       ok1(STATIC_ILOG_64(v)==l);
51       ok1(ilog64(v)==l);
52       ok1(ilog64_nz(v) == l || v == 0);
53       /*Also try a few more pseudo-random values with at most the same number
54          of bits.*/
55       v=(uint64_t)((2862933555777941757ULL*v+3037000493ULL)
56         &0xFFFFFFFFFFFFFFFFULL>>((65-i)>>1)>>((64-i)>>1));
57     }
58   }
59   return exit_status();
60 }