11 ** Global variables used within this module.
13 static struct TestutilGlobal
{
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 */
24 /*************************************************************************
25 ** The following block is a copy of the implementation of SQLite function
26 ** sqlite3_randomness. This version has two important differences:
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.
31 ** 2. It is not threadsafe.
33 static struct sqlite3PrngType
{
34 unsigned char i
, j
; /* State variables */
35 unsigned char s
[256]; /* State variables */
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
74 /* Generate and return single random byte */
75 static unsigned char randomByte(void){
78 t
= sqlite3Prng
.s
[sqlite3Prng
.i
];
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
];
87 ** Return N random bytes.
89 static void randomBlob(int nBuf
, unsigned char *zBuf
){
91 for(i
=0; i
<nBuf
; i
++){
92 zBuf
[i
] = randomByte();
96 ** End of code copied from SQLite.
97 *************************************************************************/
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
);
107 unsigned int testPrngValue(unsigned int iVal
){
109 r
.aRand1
[iVal
& 0x000007FF] ^
110 r
.aRand2
[(iVal
>>11) & 0x000007FF] ^
111 r
.aRand3
[(iVal
>>22) & 0x000003FF]
115 void testPrngArray(unsigned int iVal
, unsigned int *aOut
, int nOut
){
117 for(i
=0; i
<nOut
; i
++){
118 aOut
[i
] = testPrngValue(iVal
+i
);
122 void testPrngString(unsigned int iVal
, char *aOut
, int nOut
){
124 for(i
=0; i
<(nOut
-1); i
++){
125 aOut
[i
] = 'a' + (testPrngValue(iVal
+i
) % 26);
130 void testErrorInit(int argc
, char **argv
){
135 void testPrintError(const char *zFormat
, ...){
137 va_start(ap
, zFormat
);
138 vfprintf(stderr
, zFormat
, ap
);
142 void testPrintFUsage(const char *zFormat
, ...){
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");
151 void testPrintUsage(const char *zArgs
){
152 testPrintError("Usage: %s %s %s\n", g
.argv
[0], g
.argv
[1], zArgs
);
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;
161 testPrintError("unrecognized %s \"%s\": must be ", zType
, zArg
);
162 for(pEntry
=(struct Entry
*)aData
;
164 pEntry
=(struct Entry
*)&((unsigned char *)pEntry
)[sz
]
166 if( zPrev
){ testPrintError("%s, ", zPrev
); }
167 zPrev
= pEntry
->zName
;
169 testPrintError("or %s\n", zPrev
);
179 struct Entry
{ const char *zName
; };
180 struct Entry
*pEntry
;
181 int nArg
= strlen(zArg
);
187 for(pEntry
=(struct Entry
*)aData
;
189 pEntry
=(struct Entry
*)&((unsigned char *)pEntry
)[sz
]
191 int nName
= strlen(pEntry
->zName
);
192 if( nArg
<=nName
&& memcmp(pEntry
->zName
, zArg
, nArg
)==0 ){
204 argError(aData
, zType
, sz
, zArg
);
211 struct timeval zero_time
;
213 void testTimeInit(void){
214 gettimeofday(&zero_time
, 0);
217 int testTimeGet(void){
219 gettimeofday(&now
, 0);
221 (((int)now
.tv_sec
- (int)zero_time
.tv_sec
)*1000) +
222 (((int)now
.tv_usec
- (int)zero_time
.tv_usec
)/1000);