]> git.ozlabs.org Git - ccan/blob - ccan/ilog/test/run.c
Rename _info.c to _info: this means we can simple compile *.c.
[ccan] / ccan / ilog / test / run.c
1 #include "ilog/ilog.h"
2 #include "ilog/ilog.c"
3 #include <stdio.h>
4 #include "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 nmatches;
24   int i;
25   int j;
26   /*This is how many tests you plan to run.*/
27   plan_tests(2);
28   nmatches=0;
29   for(i=0;i<=32;i++){
30     uint32_t v;
31     /*Test each bit in turn (and 0).*/
32     v=i?(uint32_t)1U<<(i-1):0;
33     for(j=0;j<NTRIALS;j++){
34       int l;
35       l=test_ilog32(v);
36       if(ILOG_32(v)!=l){
37         fprintf(stderr,"ILOG_32(0x%08lX): %i != %i\n",(long)v,ILOG_32(v),l);
38       }
39       else nmatches++;
40       if(ilog32(v)!=l){
41         fprintf(stderr,"ilog32(0x%08lX): %i != %i\n",(long)v,ilog32(v),l);
42       }
43       else nmatches++;
44       if(STATIC_ILOG_32(v)!=l){
45         fprintf(stderr,"STATIC_ILOG_32(0x%08lX): %i != %i\n",
46          (long)v,STATIC_ILOG_32(v),l);
47       }
48       else nmatches++;
49       /*Also try a few more pseudo-random values with at most the same number
50          of bits.*/
51       v=(1103515245U*v+12345U)&0xFFFFFFFFU>>((33-i)>>1)>>((32-i)>>1);
52     }
53   }
54   ok1(nmatches==3*(32+1)*NTRIALS);
55   nmatches=0;
56   for(i=0;i<=64;i++){
57     uint64_t v;
58     /*Test each bit in turn (and 0).*/
59     v=i?(uint64_t)1U<<(i-1):0;
60     for(j=0;j<NTRIALS;j++){
61       int l;
62       l=test_ilog64(v);
63       if(ILOG_64(v)!=l){
64         fprintf(stderr,"ILOG_64(0x%016llX): %i != %i\n",
65          (long long)v,ILOG_64(v),l);
66       }
67       else nmatches++;
68       if(ilog64(v)!=l){
69         fprintf(stderr,"ilog64(0x%016llX): %i != %i\n",
70          (long long)v,ilog64(v),l);
71       }
72       else nmatches++;
73       if(STATIC_ILOG_64(v)!=l){
74         fprintf(stderr,"STATIC_ILOG_64(0x%016llX): %i != %i\n",
75          (long long)v,STATIC_ILOG_64(v),l);
76       }
77       else nmatches++;
78       /*Also try a few more pseudo-random values with at most the same number
79          of bits.*/
80       v=(uint64_t)((2862933555777941757ULL*v+3037000493ULL)
81         &0xFFFFFFFFFFFFFFFFULL>>((65-i)>>1)>>((64-i)>>1));
82     }
83   }
84   ok1(nmatches==3*(64+1)*NTRIALS);
85   return exit_status();
86 }