initial test
[rrq/rrqmisc.git] / tests / relationtests.c
1 /**
2  * This is a collection of tests of the relation functions.
3  */
4 #include <stdlib.h>
5 #include <stringitem.h>
6 #include <relation.h>
7
8 #define SIZE(x) (sizeof(x)/sizeof(void*))
9
10 static char *tuple2string(relation *r,tuple *t) {
11     #define LIMIT 10000
12     static char tmp[10][ LIMIT ];
13     static int i = 0;
14     if ( t == 0 ) {
15         return "(null)";
16     }
17     if ( i >= 10 ) {
18         i = 0;
19     }
20     int n = r->content.type->tostring(  r->content.type, t, tmp[i], LIMIT );
21     if ( n == 0 ) {
22         *(tmp[i]) = 0;
23     }
24     return tmp[i++];
25 }
26
27 static void query_report(relation *r,tuple *query) {
28     vector_index i;
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 ) );
32     }
33 }
34
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) {
38     vector_index i;
39     if ( 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 ) );
43         }
44         vector_resize( v, 0, vector_clear_any, 0 );
45         free( v );
46     }
47 }
48
49 // Test addition with several tuples, terminated by 0                          
50 static void test_relation_add(relation *r,tuple *query[]) {
51     int j;
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] ) );
55     }
56 }
57
58 // Test deletion with several queries, terminated by 0
59 static void test_relation_delete(relation *r,tuple *query[]) {
60     int j;
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] ) );
64     }
65 }
66
67 int main(int argc,char **argv) {
68     tuple *data2[] = {
69         TUPLE( "a", "b" ),
70         TUPLE( "a", "c" ),
71         TUPLE( "a", "d" ),
72         TUPLE( "b", "d" ),
73         0
74     };
75     tuple *query2[] = {
76         TUPLE( "a", 0 ),
77         TUPLE( 0, "d" ),
78         0
79     };
80     int j;
81     // AxB
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] );
88     }
89     test_relation_delete( &rel2, query2 );
90     
91     // AxBxC
92     relation rel3 = RELATION( &stringitem, &stringitem, &stringitem );
93     // AxB -> C
94     relation_add_constraint( &rel3, 1, 1, 0 );
95     // AxC -> B
96     relation_add_constraint( &rel3, 1, 0, 1 );
97
98     return 0;
99 }