1
2 #include "lsmtest.h"
3 #include <stdarg.h>
4 #include <stdio.h>
5 #include <string.h>
6 #ifndef _WIN32
7 # include <sys/time.h>
8 #endif
9
10 /*
11 ** Global variables used within this module.
12 */
13 static struct TestutilGlobal {
14 char **argv;
15 int argc;
16 } g = {0, 0};
17
18 static struct TestutilRnd {
19 unsigned int aRand1[2048]; /* Bits 0..10 */
20 unsigned int aRand2[2048]; /* Bits 11..21 */
21 unsigned int aRand3[1024]; /* Bits 22..31 */
22 } r;
23
24 /*************************************************************************
25 ** The following block is a copy of the implementation of SQLite function
26 ** sqlite3_randomness. This version has two important differences:
27 **
28 ** 1. It always uses the same seed. So the sequence of random data output
29 ** is the same for every run of the program.
30 **
31 ** 2. It is not threadsafe.
32 */
33 static struct sqlite3PrngType {
34 unsigned char i, j; /* State variables */
35 unsigned char s[256]; /* State variables */
36 } sqlite3Prng = {
37 0xAF, 0x28,
38 {
39 0x71, 0xF5, 0xB4, 0x6E, 0x80, 0xAB, 0x1D, 0xB8,
40 0xFB, 0xB7, 0x49, 0xBF, 0xFF, 0x72, 0x2D, 0x14,
41 0x79, 0x09, 0xE3, 0x78, 0x76, 0xB0, 0x2C, 0x0A,
42 0x8E, 0x23, 0xEE, 0xDF, 0xE0, 0x9A, 0x2F, 0x67,
43 0xE1, 0xBE, 0x0E, 0xA7, 0x08, 0x97, 0xEB, 0x77,
44 0x78, 0xBA, 0x9D, 0xCA, 0x49, 0x4C, 0x60, 0x9A,
45 0xF6, 0xBD, 0xDA, 0x7F, 0xBC, 0x48, 0x58, 0x52,
46 0xE5, 0xCD, 0x83, 0x72, 0x23, 0x52, 0xFF, 0x6D,
47 0xEF, 0x0F, 0x82, 0x29, 0xA0, 0x83, 0x3F, 0x7D,
48 0xA4, 0x88, 0x31, 0xE7, 0x88, 0x92, 0x3B, 0x9B,
49 0x3B, 0x2C, 0xC2, 0x4C, 0x71, 0xA2, 0xB0, 0xEA,
50 0x36, 0xD0, 0x00, 0xF1, 0xD3, 0x39, 0x17, 0x5D,
51 0x2A, 0x7A, 0xE4, 0xAD, 0xE1, 0x64, 0xCE, 0x0F,
52 0x9C, 0xD9, 0xF5, 0xED, 0xB0, 0x22, 0x5E, 0x62,
53 0x97, 0x02, 0xA3, 0x8C, 0x67, 0x80, 0xFC, 0x88,
54 0x14, 0x0B, 0x15, 0x10, 0x0F, 0xC7, 0x40, 0xD4,
55 0xF1, 0xF9, 0x0E, 0x1A, 0xCE, 0xB9, 0x1E, 0xA1,
56 0x72, 0x8E, 0xD7, 0x78, 0x39, 0xCD, 0xF4, 0x5D,
57 0x2A, 0x59, 0x26, 0x34, 0xF2, 0x73, 0x0B, 0xA0,
58 0x02, 0x51, 0x2C, 0x03, 0xA3, 0xA7, 0x43, 0x13,
59 0xE8, 0x98, 0x2B, 0xD2, 0x53, 0xF8, 0xEE, 0x91,
60 0x7D, 0xE7, 0xE3, 0xDA, 0xD5, 0xBB, 0xC0, 0x92,
61 0x9D, 0x98, 0x01, 0x2C, 0xF9, 0xB9, 0xA0, 0xEB,
62 0xCF, 0x32, 0xFA, 0x01, 0x49, 0xA5, 0x1D, 0x9A,
63 0x76, 0x86, 0x3F, 0x40, 0xD4, 0x89, 0x8F, 0x9C,
64 0xE2, 0xE3, 0x11, 0x31, 0x37, 0xB2, 0x49, 0x28,
65 0x35, 0xC0, 0x99, 0xB6, 0xD0, 0xBC, 0x66, 0x35,
66 0xF7, 0x83, 0x5B, 0xD7, 0x37, 0x1A, 0x2B, 0x18,
67 0xA6, 0xFF, 0x8D, 0x7C, 0x81, 0xA8, 0xFC, 0x9E,
68 0xC4, 0xEC, 0x80, 0xD0, 0x98, 0xA7, 0x76, 0xCC,
69 0x9C, 0x2F, 0x7B, 0xFF, 0x8E, 0x0E, 0xBB, 0x90,
70 0xAE, 0x13, 0x06, 0xF5, 0x1C, 0x4E, 0x52, 0xF7
71 }
72 };
73
74 /* Generate and return single random byte */
randomByte(void)75 static unsigned char randomByte(void){
76 unsigned char t;
77 sqlite3Prng.i++;
78 t = sqlite3Prng.s[sqlite3Prng.i];
79 sqlite3Prng.j += t;
80 sqlite3Prng.s[sqlite3Prng.i] = sqlite3Prng.s[sqlite3Prng.j];
81 sqlite3Prng.s[sqlite3Prng.j] = t;
82 t += sqlite3Prng.s[sqlite3Prng.i];
83 return sqlite3Prng.s[t];
84 }
85
86 /*
87 ** Return N random bytes.
88 */
randomBlob(int nBuf,unsigned char * zBuf)89 static void randomBlob(int nBuf, unsigned char *zBuf){
90 int i;
91 for(i=0; i<nBuf; i++){
92 zBuf[i] = randomByte();
93 }
94 }
95 /*
96 ** End of code copied from SQLite.
97 *************************************************************************/
98
99
testPrngInit(void)100 int testPrngInit(void){
101 randomBlob(sizeof(r.aRand1), (unsigned char *)r.aRand1);
102 randomBlob(sizeof(r.aRand2), (unsigned char *)r.aRand2);
103 randomBlob(sizeof(r.aRand3), (unsigned char *)r.aRand3);
104 return 0;
105 }
106
testPrngValue(unsigned int iVal)107 unsigned int testPrngValue(unsigned int iVal){
108 return
109 r.aRand1[iVal & 0x000007FF] ^
110 r.aRand2[(iVal>>11) & 0x000007FF] ^
111 r.aRand3[(iVal>>22) & 0x000003FF]
112 ;
113 }
114
testPrngArray(unsigned int iVal,unsigned int * aOut,int nOut)115 void testPrngArray(unsigned int iVal, unsigned int *aOut, int nOut){
116 int i;
117 for(i=0; i<nOut; i++){
118 aOut[i] = testPrngValue(iVal+i);
119 }
120 }
121
testPrngString(unsigned int iVal,char * aOut,int nOut)122 void testPrngString(unsigned int iVal, char *aOut, int nOut){
123 int i;
124 for(i=0; i<(nOut-1); i++){
125 aOut[i] = 'a' + (testPrngValue(iVal+i) % 26);
126 }
127 aOut[i] = '\0';
128 }
129
testErrorInit(int argc,char ** argv)130 void testErrorInit(int argc, char **argv){
131 g.argc = argc;
132 g.argv = argv;
133 }
134
testPrintError(const char * zFormat,...)135 void testPrintError(const char *zFormat, ...){
136 va_list ap;
137 va_start(ap, zFormat);
138 vfprintf(stderr, zFormat, ap);
139 va_end(ap);
140 }
141
testPrintFUsage(const char * zFormat,...)142 void testPrintFUsage(const char *zFormat, ...){
143 va_list ap;
144 va_start(ap, zFormat);
145 fprintf(stderr, "Usage: %s %s ", g.argv[0], g.argv[1]);
146 vfprintf(stderr, zFormat, ap);
147 fprintf(stderr, "\n");
148 va_end(ap);
149 }
150
testPrintUsage(const char * zArgs)151 void testPrintUsage(const char *zArgs){
152 testPrintError("Usage: %s %s %s\n", g.argv[0], g.argv[1], zArgs);
153 }
154
155
argError(void * aData,const char * zType,int sz,const char * zArg)156 static void argError(void *aData, const char *zType, int sz, const char *zArg){
157 struct Entry { const char *zName; };
158 struct Entry *pEntry;
159 const char *zPrev = 0;
160
161 testPrintError("unrecognized %s \"%s\": must be ", zType, zArg);
162 for(pEntry=(struct Entry *)aData;
163 pEntry->zName;
164 pEntry=(struct Entry *)&((unsigned char *)pEntry)[sz]
165 ){
166 if( zPrev ){ testPrintError("%s, ", zPrev); }
167 zPrev = pEntry->zName;
168 }
169 testPrintError("or %s\n", zPrev);
170 }
171
testArgSelectX(void * aData,const char * zType,int sz,const char * zArg,int * piOut)172 int testArgSelectX(
173 void *aData,
174 const char *zType,
175 int sz,
176 const char *zArg,
177 int *piOut
178 ){
179 struct Entry { const char *zName; };
180 struct Entry *pEntry;
181 int nArg = strlen(zArg);
182
183 int i = 0;
184 int iOut = -1;
185 int nOut = 0;
186
187 for(pEntry=(struct Entry *)aData;
188 pEntry->zName;
189 pEntry=(struct Entry *)&((unsigned char *)pEntry)[sz]
190 ){
191 int nName = strlen(pEntry->zName);
192 if( nArg<=nName && memcmp(pEntry->zName, zArg, nArg)==0 ){
193 iOut = i;
194 if( nName==nArg ){
195 nOut = 1;
196 break;
197 }
198 nOut++;
199 }
200 i++;
201 }
202
203 if( nOut!=1 ){
204 argError(aData, zType, sz, zArg);
205 }else{
206 *piOut = iOut;
207 }
208 return (nOut!=1);
209 }
210
211 struct timeval zero_time;
212
testTimeInit(void)213 void testTimeInit(void){
214 gettimeofday(&zero_time, 0);
215 }
216
testTimeGet(void)217 int testTimeGet(void){
218 struct timeval now;
219 gettimeofday(&now, 0);
220 return
221 (((int)now.tv_sec - (int)zero_time.tv_sec)*1000) +
222 (((int)now.tv_usec - (int)zero_time.tv_usec)/1000);
223 }
224