comparison cdbtest.c @ 0:2cb46628feec

from cdb-0.75 per http://cr.yp.to/cdb.html, retrieved 2025-01-03, contents appear to have been create on 2000-02-19
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Wed, 29 Jan 2025 10:50:34 +0000
parents
children 3bfef1f22a97
comparison
equal deleted inserted replaced
-1:000000000000 0:2cb46628feec
1 #include "uint32.h"
2 #include "fmt.h"
3 #include "buffer.h"
4 #include "strerr.h"
5 #include "seek.h"
6 #include "cdb.h"
7
8 #define FATAL "cdbtest: fatal: "
9
10 void die_read(void)
11 {
12 strerr_die2sys(111,FATAL,"unable to read input: ");
13 }
14 void die_write(void)
15 {
16 strerr_die2sys(111,FATAL,"unable to write output: ");
17 }
18 void put(char *buf,unsigned int len)
19 {
20 if (buffer_put(buffer_1small,buf,len) == -1) die_write();
21 }
22 void putflush(void)
23 {
24 if (buffer_flush(buffer_1small) == -1) die_write();
25 }
26
27 uint32 pos = 0;
28
29 void get(char *buf,unsigned int len)
30 {
31 int r;
32 while (len > 0) {
33 r = buffer_get(buffer_0,buf,len);
34 if (r == -1) die_read();
35 if (r == 0)
36 strerr_die2x(111,FATAL,"unable to read input: truncated file");
37 pos += r;
38 buf += r;
39 len -= r;
40 }
41 }
42
43 void getnum(uint32 *num)
44 {
45 char buf[4];
46 get(buf,4);
47 uint32_unpack(buf,num);
48 }
49
50 char strnum[FMT_ULONG];
51
52 void putnum(char *label,unsigned long count)
53 {
54 put(label,str_len(label));
55 put(strnum,fmt_ulong(strnum,count));
56 put("\n",1);
57 }
58
59 char key[1024];
60
61 unsigned long numuntested = 0;
62 unsigned long numnotfound = 0;
63 unsigned long numotherpos = 0;
64 unsigned long numbadlen = 0;
65 unsigned long numfound = 0;
66
67 static struct cdb c;
68
69 main()
70 {
71 uint32 eod;
72 uint32 klen;
73 uint32 dlen;
74 seek_pos rest;
75 int r;
76
77 cdb_init(&c,0);
78
79 getnum(&eod);
80 while (pos < 2048) getnum(&dlen);
81
82 while (pos < eod) {
83 getnum(&klen);
84 getnum(&dlen);
85 if (klen > sizeof key) {
86 ++numuntested;
87 while (klen) { get(key,1); --klen; }
88 }
89 else {
90 get(key,klen);
91 rest = seek_cur(0);
92 switch(cdb_find(&c,key,klen)) {
93 case -1:
94 die_read();
95 case 0:
96 ++numnotfound;
97 break;
98 default:
99 if (cdb_datapos(&c) != pos)
100 ++numotherpos;
101 else
102 if (cdb_datalen(&c) != dlen)
103 ++numbadlen;
104 else
105 ++numfound;
106 }
107 if (seek_set(0,rest) == -1) die_read();
108 }
109 while (dlen) { get(key,1); --dlen; }
110 }
111
112 putnum("found: ",numfound);
113 putnum("different record: ",numotherpos);
114 putnum("bad length: ",numbadlen);
115 putnum("not found: ",numnotfound);
116 putnum("untested: ",numuntested);
117 putflush();
118 _exit(0);
119 }