2 * This is a collection of tests of the relation functions.
5 #include <stringitem.h>
8 #define SIZE(x) (sizeof(x)/sizeof(void*))
10 static char *tuple2string(relation *r,tuple *t) {
12 static char tmp[10][ LIMIT ];
20 int n = r->content.type->tostring( r->content.type, t, tmp[i], LIMIT );
27 static void query_report(relation *r,tuple *query) {
29 for ( i = 0; i < r->content.table.size; i++ ) {
30 tuple *t = hashvector_next( &r->content, &i, query );
31 fprintf( stderr, "check %s\n", tuple2string( r, t ) );
35 // Report any knocked out tuples from relation_add or relation_delete
36 // This will also clear and free the result vector.
37 static void knockout_report(relation *r,vector *v) {
40 for ( i = 0; i < v->size; i++ ) {
41 tuple **t = (tuple **) vector_next_used( v, &i );
42 fprintf( stderr, "knock %s\n", tuple2string( r, t? *t : 0 ) );
44 vector_resize( v, 0, vector_clear_any, 0 );
49 // Test addition with several tuples, terminated by 0
50 static void test_relation_add(relation *r,tuple *query[]) {
52 for ( j = 0; query[j]; j++ ) {
53 fprintf( stderr, "add %s\n", tuple2string( r, query[j] ) );
54 knockout_report( r, relation_add( r, query[j] ) );
58 // Test deletion with several queries, terminated by 0
59 static void test_relation_delete(relation *r,tuple *query[]) {
61 for ( j = 0; query[j]; j++ ) {
62 fprintf( stderr, "delete %s\n", tuple2string( r, query[j] ) );
63 knockout_report( r, relation_delete( r, query[j] ) );
67 int main(int argc,char **argv) {
82 relation rel2 = RELATION( &stringitem, &stringitem );
83 test_relation_add( &rel2, data2 );
84 query_report( &rel2, 0 );
85 for ( j = 0; query2[j]; j++ ) {
86 fprintf( stderr, "query %s\n", tuple2string( &rel2, query2[j] ) );
87 query_report( &rel2, query2[j] );
89 test_relation_delete( &rel2, query2 );
92 relation rel3 = RELATION( &stringitem, &stringitem, &stringitem );
94 relation_add_constraint( &rel3, 1, 1, 0 );
96 relation_add_constraint( &rel3, 1, 0, 1 );