summaryrefslogtreecommitdiff
path: root/util/db_sql_codegen/generate_test.c
blob: a63d0dd2780f4041524911461cf6f70c19f011f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/*
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1996, 2015 Oracle and/or its affiliates.  All rights reserved.
 *
 */

/*
 * This compilation unit contains functions related to the generation
 * of a simple smoke test for the generated storage layer.
 */
#include "generation.h"

extern int maxbinsz; /* defined in buildpt.c */

static FILE *test_file;   /* stream for generated test code */
static int test_indent_level = 0;

static void pr_test(char *, ...);
static void pr_test_comment(char *, ...);
static void callback_function_enter_entop(ENTITY *);
static void callback_function_exit_entop(ENTITY *);
static void callback_function_attrop(ENTITY *, ATTRIBUTE *, int, int);
static void declare_record_instances_enter_entop(ENTITY *);
static void initialize_database_enter_entop(ENTITY *);
static void initialize_index_enter_entop(DB_INDEX *);
static void insertion_test_enter_entop(ENTITY *);
static void insertion_test_exit_entop(ENTITY *);
static void insertion_test_attrop(ENTITY *, ATTRIBUTE *, int, int);
static void retrieval_test_enter_entop(ENTITY *);
static void retrieval_test_exit_entop(ENTITY *);
static void retrieval_test_attrop(ENTITY *, ATTRIBUTE *, int, int);
static void invoke_full_iteration_enter_entop(ENTITY *);
static void invoke_full_iteration_exit_entop(ENTITY *e);
static void invoke_query_iteration_idxop(DB_INDEX *);
static void deletion_test_enter_entop(ENTITY *);
static void deletion_test_exit_entop(ENTITY *e);
static void close_secondary_test_idxop(DB_INDEX *);
static void close_primary_test_enter_entop(ENTITY *);
static void remove_secondary_test_idxop(DB_INDEX *);
static void remove_primary_test_enter_entop(ENTITY *);

/*
 * Generate_test is the sole entry point in this module.  It
 * orchestrates the generation of the C code for the smoke test.
 */
void
generate_test(tfile, hfilename)
	FILE *tfile;
	char *hfilename;
{
	test_file = tfile;

	pr_test_comment(
"Simple test for a Berkeley DB implementation                               \n\
generated from SQL DDL by db_sql                                            \n\
");

	pr_test("\n#include \"%s\"\n\n", hfilename);

	if (maxbinsz != 0) {
		pr_test_comment("Test data for raw binary types");
		pr_test("#define MAXBINSZ %d\n", maxbinsz);
		pr_test("char binary_data[MAXBINSZ];\n\n");
		pr_test_comment("A very simple binary comparison function");
		pr_test(
"char * compare_binary(char *p, int len)                                    \n\
{                                                                           \n\
 if (memcmp(p, binary_data, len) == 0)                                      \n\
  return \"*binary values match*\";                                         \n\
 return \"*binary values don't match*\";                                    \n\
}                                                                           \n\
									    \n\
");
	}

	pr_test_comment(
"These are the iteration callback functions.  One is defined per            \n\
database(table).  They are used for both full iterations and for            \n\
secondary index queries.  When a retrieval returns multiple records,        \n\
as in full iteration over an entire database, one of these functions        \n\
is called for each record found");

	iterate_over_entities(&callback_function_enter_entop,
	    &callback_function_exit_entop,
	    &callback_function_attrop);

	pr_test(
"                                                                           \n\
main(int argc, char **argv)                                                 \n\
{                                                                           \n\
 int i;                                                                     \n\
 int ret;								    \n\
									    \n\
");
	test_indent_level++;

	iterate_over_entities(&declare_record_instances_enter_entop,
	    NULL,
	    NULL);

	iterate_over_entities(&initialize_database_enter_entop,
	    NULL,
	    NULL);

	iterate_over_indexes(&initialize_index_enter_entop);

	pr_test("\n");

	if (maxbinsz != 0) {
		pr_test_comment(
		    "Fill the binary test data with random values");
		pr_test(
"for (i = 0; i < MAXBINSZ; i++) binary_data[i] = rand();\n\n");
	}

	pr_test_comment(
"Use the convenience method to initialize the environment.                  \n\
The initializations for each entity and environment can be                  \n\
done discretely if you prefer, but this is the easy way.");
	pr_test(
"ret = initialize_%s_environment();                                         \n\
if (ret != 0) {                                                              \n\
 printf(\"Initialize error\");                                              \n\
 goto exit_error;                                                           \n\
}                                                                           \n\
									    \n\
",
		    the_schema.environment.name);

	pr_test_comment(
"Now that everything is initialized, insert a single                        \n\
record into each database, using the ...insert_fields                       \n\
functions.  These functions take each field of the                          \n\
record as a separate argument");

	iterate_over_entities(&insertion_test_enter_entop,
	    &insertion_test_exit_entop,
	    &insertion_test_attrop);

	pr_test("\n");
	pr_test_comment(
"Next, retrieve the records just inserted, looking them up                  \n\
by their key values");

	iterate_over_entities(&retrieval_test_enter_entop,
	    &retrieval_test_exit_entop,
	    &retrieval_test_attrop);

	pr_test("\n");
	pr_test_comment(
"Now try iterating over every record, using the ...full_iteration           \n\
functions for each database.  For each record found, the                    \n\
appropriate ...iteration_callback_test function will be invoked             \n\
(these are defined above).");

	iterate_over_entities(&invoke_full_iteration_enter_entop,
	    &invoke_full_iteration_exit_entop,
	    NULL);

	pr_test("\n");
	pr_test_comment(
"For the secondary indexes, query for the known keys.  This also            \n\
results in the ...iteration_callback_test function's being called           \n\
for each record found.");

	iterate_over_indexes(&invoke_query_iteration_idxop);

	pr_test("\n");
	pr_test_comment(
"Now delete a record from each database using its primary key.");

	iterate_over_entities(&deletion_test_enter_entop,
	    &deletion_test_exit_entop,
	    NULL);

	pr_test("\n");
	test_indent_level--;
	pr_test("exit_error:\n");
	test_indent_level++;

	pr_test_comment("Close the secondary index databases");
	iterate_over_indexes(&close_secondary_test_idxop);

	pr_test_comment("Close the primary databases");
	iterate_over_entities(&close_primary_test_enter_entop,
	    NULL,
	    NULL);

	pr_test("\n");
	pr_test_comment("Delete the secondary index databases");
	iterate_over_indexes(&remove_secondary_test_idxop);

	pr_test("\n");
	pr_test_comment("Delete the primary databases");
	iterate_over_entities(&remove_primary_test_enter_entop,
	    NULL,
	    NULL);

	if (the_schema.environment.transactional) {
		pr_test(
"                                                                           \n\
if (ret != 0)								    \n\
 %s_txnp->abort(%s_txnp);						    \n\
else                                                                        \n\
 %s_txnp->commit(%s_txnp, 0);                                               \n\
",
		    the_schema.environment.name,
		    the_schema.environment.name,
		    the_schema.environment.name,
		    the_schema.environment.name);
	}

	pr_test("\n");
	pr_test_comment("Finally, close the environment");
	pr_test("%s_envp->close(%s_envp, 0);\n",
	    the_schema.environment.name, the_schema.environment.name);

	pr_test("return ret;\n");
	test_indent_level--;
	pr_test("}\n");
}

/*
 * Emit a printf-formatted string into the test code file.
 */
static void
pr_test(char *fmt, ...)
{
	va_list ap;
	char *s;
	static int enable_indent = 1;

	s = prepare_string(fmt,
	    enable_indent ? test_indent_level : 0,
	    0);

	va_start(ap, fmt);
	vfprintf(test_file, s, ap);
	va_end(ap);

	/*
	 * If the last char emitted was a newline, enable indentation
	 * for the next time.
	 */
	if (s[strlen(s) - 1] == '\n')
		enable_indent = 1;
	else
		enable_indent = 0;
}

/*
 * Emit a formatted comment into the test file.
 */
static void
pr_test_comment(char *fmt, ...)
{
	va_list ap;
	char *s;

	s = prepare_string(fmt, test_indent_level, 1);

	va_start(ap, fmt);
	vfprintf(test_file, s, ap);
	va_end(ap);
}

/*
 * Return the appropriate printf format string for the given type.
 */
static char *
format_string_for_type(ATTR_TYPE *t)
{
	char *c_type = t->c_type;

	if (is_array(t)) {
		return ("%s");
	} else if (strcmp(c_type, "char") == 0 ||
	    strcmp(c_type, "short") == 0 ||
	    strcmp(c_type, "int") == 0) {
		return ("%d");
	} else  if (strcmp(c_type, "long") == 0) {
		return ("%ld");
	} else if (strcmp(c_type, "float") == 0) {
		return ("%f");
	} else if (strcmp(c_type, "double") == 0) {
		return ("%lf");
	} else {
		fprintf(stderr,
		    "Unexpected C type in schema: %s", c_type);
		assert(0);
	}
	return NULL; /*NOTREACHED*/
}

/*
 * Return a data literal appropriate for the given type, to use as test data.
 */
static char *
data_value_for_type(ATTR_TYPE *t)
{
	char *c_type = t->c_type;

	if (is_string(t)) {
		/* If the field is really short, use a tiny string */
		if (t->array_dim < 12)
			return ("\"n\"");
		return ("\"ninety-nine\"");
	} else if (is_array(t)) {
		return ("binary_data");
	} else if (strcmp(c_type, "char") == 0 ||
	    strcmp(c_type, "short") == 0 ||
	    strcmp(c_type, "int") == 0 ||
	    strcmp(c_type, "long") == 0) {
		return ("99");
	} else if (strcmp(c_type, "float") == 0 ||
	    strcmp(c_type, "double") == 0) {
		return ("99.5");
	} else {
		fprintf(stderr,
		    "Unexpected C type in schema: %s", c_type);
		assert(0);
	}
	return NULL; /*NOTREACHED*/
}

/*
 * This entity operation function is called by the attribute iterator
 * when producing test code that declares record instances.
 */
static void
declare_record_instances_enter_entop(ENTITY *e)
{
	pr_test("%s_data %s_record;\n", e->name, e->name);
}

/*
 * This entity operation function is called by the attribute iterator
 * when producing test code that initialized database handle.
 */
static void
initialize_database_enter_entop(ENTITY *e)
{
	pr_test("%s_dbp = NULL;\n", e->name);
}

/*
 * This entity operation function is called by the attribute iterator
 * when producing test code that initialized index handle.
 */
static void
initialize_index_enter_entop(DB_INDEX *idx)
{
	pr_test("%s_dbp = NULL;\n", idx->name);
}

static void
invoke_full_iteration_enter_entop(ENTITY *e)
{
	pr_test(
"ret = %s_full_iteration(%s_dbp, %s%s&%s_iteration_callback_test,           \n\
  \"retrieval of %s record through full iteration\");\n",
	    e->name, e->name,
	    e->transactional ? the_schema.environment.name : "",
	    e->transactional ? "_txnp, " : "",
	    e->name, e->name);
}
static void
invoke_full_iteration_exit_entop(ENTITY *e)
{
	COMPQUIET(e, NULL);
	pr_test(
"if (ret != 0) {                                                             \n\
 printf(\"Full Iteration Error\\n\");                                       \n\
 goto exit_error;                                                           \n\
}                                                                           \n\
									    \n\
");
}

/*
 * This index operation function is called by the attribute iterator
 * when producing test code that creates the secondary databases.
 */
static void
invoke_query_iteration_idxop(DB_INDEX *idx)
{
	ATTR_TYPE *key_type = idx->attribute->type;

	pr_test("%s_query_iteration(%s_dbp, %s%s",
	    idx->name, idx->name,
	    idx->primary->transactional ? the_schema.environment.name : "",
	    idx->primary->transactional ? "_txnp, " : "");

	pr_test("%s", data_value_for_type(key_type));

	pr_test(
",\n  &%s_iteration_callback_test,                                          \n\
  \"retrieval of %s record through %s query\");\n",
		    idx->primary->name, idx->primary->name, idx->name);
}

/*
 * This next group of entity and attribute operation functions are
 * called by the attribute iterator when generating insertion test code.
 */
static void
insertion_test_enter_entop(ENTITY *e)
{
	pr_test("ret = %s_insert_fields(%s_dbp, %s%s",
	    e->name,
	    e->name,
	    e->transactional ? the_schema.environment.name : "",
	    e->transactional ? "_txnp, " : "");
}
static void
insertion_test_exit_entop(ENTITY *e)
{
	COMPQUIET(e, NULL);
	pr_test(");\n");
	pr_test(
"if (ret != 0) {                                                            \n\
 printf(\"Insert error\\n\");                                              \n\
 goto exit_error;                                                          \n\
}                                                                          \n\
									   \n\
");
}
static void
insertion_test_attrop(ENTITY *e, ATTRIBUTE *a, int first, int last)
{
	COMPQUIET(e, NULL);
	COMPQUIET(first, 0);

	pr_test("%s", data_value_for_type(a->type));
	if (!last)
		pr_test(", ");
}

/*
 * This next group of index and attribute operation functions are
 * called by the attribute iterator when generating the iteration
 * callback function.
 */
static void
callback_function_attrop(ENTITY *e, ATTRIBUTE *a, int first, int last)
{
	COMPQUIET(first, 0);
	COMPQUIET(last, 0);

	pr_test("printf(\"%s->%s: ", e->name, a->name);

	pr_test("%s", format_string_for_type(a->type));

	if (is_array(a->type) && !is_string(a->type)) {
		pr_test("\\n\", compare_binary(%s_record->%s, %s));\n",
		    e->name, a->name, array_dim_name(e, a) );
	} else {
		pr_test("\\n\", %s_record->%s);\n", e->name, a->name);
	}
}
static void
callback_function_enter_entop(ENTITY *e)
{
	pr_test(
"                                                                           \n\
void %s_iteration_callback_test(void *msg, %s_data *%s_record)              \n\
{                                                                           \n\
 printf(\"In iteration callback, message is: %%s\\n\", (char *)msg);\n\n",
	    e->name, e->name, e->name);

	test_indent_level++;
}
static void
callback_function_exit_entop(ENTITY *e)
{
	COMPQUIET(e, NULL);

	test_indent_level--;
	pr_test("}\n\n");
}

/*
 * This next group of entity and attribute operation functions are
 * called by the attribute iterator when generating retrieval test code
 */
static void
retrieval_test_enter_entop(ENTITY *e)
{
	pr_test("\nprintf(\"Retrieval of %s record by key\\n\");\n", e->name);
	pr_test("ret = get_%s_data( %s_dbp, %s%s%s, &%s_record);\n\n",
	    e->name, e->name,
	    e->transactional ? the_schema.environment.name : "",
	    e->transactional ? "_txnp, " : "",
	    data_value_for_type(e->primary_key->type), e->name);
}
static void
retrieval_test_exit_entop(ENTITY *e)
{
	COMPQUIET(e, NULL);
	pr_test(
"if (ret != 0)                                                              \n\
{                                                                           \n\
 printf(\"Retrieve error\\n\");                                             \n\
 goto exit_error;                                                           \n\
}                                                                           \n\
									    \n\
");
}

static void
retrieval_test_attrop(ENTITY *e, ATTRIBUTE *a, int first, int last)
{
	COMPQUIET(first, 0);
	COMPQUIET(last, 0);

	pr_test("printf(\"%s.%s: ", e->name, a->name);

	pr_test("%s", format_string_for_type(a->type));

	if (is_array(a->type) && !is_string(a->type)) {
		pr_test("\\n\", compare_binary(%s_record.%s, %s));\n",
		    e->name, a->name, array_dim_name(e, a) );
	} else {
		pr_test("\\n\", %s_record.%s);\n", e->name, a->name);
	}
}

/*
 * This entity operation function is
 * called by the attribute iterator when generating deletion test code.
 */
static void
deletion_test_enter_entop(ENTITY *e)
{
	pr_test("ret = delete_%s_key( %s_dbp, %s%s%s);\n",
	    e->name, e->name,
	    e->transactional ? the_schema.environment.name : "",
	    e->transactional ? "_txnp, " : "",
	    data_value_for_type(e->primary_key->type));
}
static void
deletion_test_exit_entop(ENTITY *e)
{
	COMPQUIET(e, NULL);
	pr_test(
"if (ret != 0) {                                                            \n\
 printf(\"Delete error\\n\");                                               \n\
 goto exit_error;                                                           \n\
}                                                                           \n\
									    \n\
");
}

/* This entity operation function generates primary database closures. */
static void
close_primary_test_enter_entop(ENTITY *e)
{
	pr_test(
"if (%s_dbp != NULL)                                                        \n\
 %s_dbp->close(%s_dbp, 0);                                                  \n\
									    \n\
",
	    e->name, e->name, e->name);
}

/* This entity operation function generates secondary database closures. */
static void
close_secondary_test_idxop(DB_INDEX *idx)
{
	pr_test(
"if (%s_dbp != NULL)                                                       \n\
 %s_dbp->close(%s_dbp, 0);                                                 \n\
									   \n\
",
	    idx->name, idx->name, idx->name);
}

/* This entity operation function generates primary database closures. */
static void
remove_primary_test_enter_entop(ENTITY *e)
{
	pr_test("remove_%s_database(%s_envp%s%s%s);\n", e->name,
	    the_schema.environment.name,
	    e->transactional ? ", " : "",
	    e->transactional ? the_schema.environment.name : "",
	    e->transactional ? "_txnp" : "");
}

/* This entity operation function generates secondary database closures. */
static void
remove_secondary_test_idxop(DB_INDEX *idx)
{
	pr_test("remove_%s_index(%s_envp%s%s%s);\n", idx->name,
	    the_schema.environment.name,
	    idx->primary->transactional ? ", " : "",
	    idx->primary->transactional ? the_schema.environment.name : "",
	    idx->primary->transactional ? "_txnp" : "");
}