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 = Relation_next( r, &i, query );
31 fprintf( stderr, "check %s\n", tuple2string( r, t ) );
35 static void query_report_all(Relation *r,Tuple *query[]) {
37 for ( j = 0; query[j]; j++ ) {
38 fprintf( stderr, "query %s\n", tuple2string( r, query[j] ) );
39 query_report( r, query[j] );
43 // Report any knocked out tuples from Relation_add or Relation_delete
44 // This will also clear and free the result Vector.
45 static void knockout_report(Relation *r,Vector *v) {
48 for ( i = 0; i < v->size; i++ ) {
49 Tuple **t = (Tuple **) Vector_next_used( v, &i );
50 fprintf( stderr, "knock %s\n", tuple2string( r, t? *t : 0 ) );
52 Vector_resize( v, 0, Vector_clear_any, 0 );
57 // Test addition with several tuples, terminated by 0
58 static void test_Relation_add(Relation *r,Tuple *query[]) {
60 for ( j = 0; query[j]; j++ ) {
61 fprintf( stderr, "add %s\n", tuple2string( r, query[j] ) );
62 knockout_report( r, Relation_add( r, query[j] ) );
66 // Test deletion with several queries, terminated by 0
67 static void test_Relation_delete(Relation *r,Tuple *query[]) {
69 for ( j = 0; query[j]; j++ ) {
70 fprintf( stderr, "delete %s\n", tuple2string( r, query[j] ) );
71 knockout_report( r, Relation_delete( r, query[j] ) );
75 int main(int argc,char **argv) {
90 Relation rel2 = RELATION( &stringitem, &stringitem );
92 test_Relation_add( &rel2, data2 );
93 query_report( &rel2, 0 );
94 query_report_all( &rel2, query2 );
95 test_Relation_delete( &rel2, query2 );
99 TUPLE( "a", "b", "c" ), // <a,b,?> <a,?,c> ***
100 TUPLE( "a", "c", "d" ), // <a,c,?> <a,?,d> ***
101 TUPLE( "a", "b", "e" ), // <a,b,?> <a,?,e> => -<a,b,c>
102 TUPLE( "b", "d", "d" ), // <b,d,?> <b,?,d>
103 TUPLE( "a", "d", "d" ), // <a,d,?> <a,?,d> => -<a,c,d>
104 TUPLE( "e", "c", "a" ), // <e,c,?> <e,?,a>
105 TUPLE( "f", "b", "d" ), // <f,b,?> <f,?,c>
109 TUPLE( "a", 0, "d" ),
112 TUPLE( "f", "b", "d" ),
115 Relation rel3 = RELATION( &stringitem, &stringitem, &stringitem );
116 Relation_add_constraint( &rel3, 1, 1, 0 ); // AxB -> C
117 Relation_add_constraint( &rel3, 1, 0, 1 ); // AxC -> B
119 test_Relation_add( &rel3, data3 );
120 query_report( &rel3, 0 );
121 query_report_all( &rel3, query3 );
122 test_Relation_delete( &rel3, query3 );