#include "tm.h" #include "tmlist.h" #include "tmtopicmap.h" #include "tmsubject.h" /* struct TMValueType Topic; struct TMValueType TopicSet; struct TMValueType Text; struct TMValueType Integer; struct TMValueType TextSet; struct TMValueType ASIDP; struct TMTopicMapStorageDescriptor default_mem_storage_descriptor; */ #define FAIL 0 #define OK 1 #define EXPECT(expr) do { if( ! (expr) ) { sprintf(terror,"EXPECT(%s) in %s line %d",#expr,__FILE__,__LINE__); return FAIL;} } while(0) #define EXPECT_OK( f ) do { \ printf("%-30s",#f); \ if( f() == OK ) printf("ok\n"); \ else printf("failed, %s\n",terror); \ } while(0) #define EXPECT_FAIL( f ) do { \ printf("%-30s",#f); \ if( f() == FAIL ) printf("ok\n"); \ else printf("failed, %s\n",terror); \ } while(0) static char terror[1024]; int find_df(void) { TM tm; TMError e; e = tm_init(&tm,"www-df",NULL); if( e != TM_OK) { return(FAIL); } tm_cleanup(&tm); return(OK); } int not_find_df(void) { TM tm; TMError e; e = tm_init(&tm,"foo",NULL); if( e != TM_OK) { return(FAIL); } tm_cleanup(&tm); strcpy(terror,"df was found but should not"); return(OK); } int errstr_resize(void) { TM tm; char long_message[TM_ERRSTR_INITSIZE+20]; if(tm_init(&tm,"www-df",NULL) != TM_OK) return FAIL; tm_set_error(tm,"abcdefg"); EXPECT ( strcmp(tm_get_error(tm),"abcdefg") == 0); /* The following piece of code tests the auto-resize of the * tm-internal error buffer. It starts at TM_ERRSTR_INITSIZE * and should now be resized to +20bytes. */ memset(long_message,'X',sizeof(long_message)); long_message[sizeof(long_message)-1] = '\0'; tm_set_error(tm,long_message); /* naturally, we'd like the two sntrings to be equal: */ EXPECT ( strcmp(tm_get_error(tm),long_message) == 0); tm_cleanup(&tm); return(OK); } int pool_tree(void) { TM tm; TMPool p1,p2,p3; if(tm_init(&tm,"www-df",NULL) != TM_OK) return FAIL; p1 = tm_pool_new("pool-level1-child1",tm_get_pool(tm)); p2 = tm_pool_new("pool-level1-child2",tm_get_pool(tm)); p3 = tm_pool_new("pool-level2-child1",p2); TM_ALLOC(p3,1024); tm_cleanup(&tm); return(OK); } int build_list(void) { TM tm; TMList list = NULL; if(tm_init(&tm,"www-df",NULL) != TM_OK) return FAIL; list = tm_list_push(tm_get_pool(tm),list,(void*)1); list = tm_list_push(tm_get_pool(tm),list,(void*)2); list = tm_list_push(tm_get_pool(tm),list,(void*)3); list = tm_list_push(tm_get_pool(tm),list,(void*)4); EXPECT( tm_list_size(list) == 4); tm_list_delete(tm_get_pool(tm),&list); EXPECT( list == NULL ); tm_cleanup(&tm); return(OK); } int sort_list(void) { TM tm; TMList list = NULL; if(tm_init(&tm,"www-df",NULL) != TM_OK) return FAIL; list = tm_list_push(tm_get_pool(tm),list,(void*)1); list = tm_list_push(tm_get_pool(tm),list,(void*)2); list = tm_list_push(tm_get_pool(tm),list,(void*)3); list = tm_list_push(tm_get_pool(tm),list,(void*)4); EXPECT( tm_list_size(list) == 4); tm_list_delete(tm_get_pool(tm),&list); EXPECT( list == NULL ); tm_cleanup(&tm); return(OK); } int load_model(void) { TM tm; TMError e; TMModel model; if(tm_init(&tm,"www-df",NULL) != TM_OK) return FAIL; if( (e = tm_disclosureframework_lookup_model( tm_get_disclosureframework(tm), tm, "http://purl.org/tm/Example/", &model)) != TM_OK) { strcpy(terror,tm_get_error(tm)); return FAIL; } if( (e = tm_disclosureframework_lookup_model( tm_get_disclosureframework(tm), tm, "http://purl.org/tm/Example/", &model)) != TM_OK) { strcpy(terror,tm_get_error(tm)); return FAIL; } tm_cleanup(&tm); return(OK); } int create_topicmap(void) { TM tm; TMError e; TMTopicMap topicmap; if(tm_init(&tm,"www-df",NULL) != TM_OK) return FAIL; topicmap = tm_topicmap_new(tm_get_pool(tm),tm); tm_topicmap_delete(&topicmap); tm_cleanup(&tm); return(OK); } int setstore_topicmap(void) { TM tm; TMError e; TMTopicMap topicmap; if(tm_init(&tm,"www-df",NULL) != TM_OK) return FAIL; topicmap = tm_topicmap_new(tm_get_pool(tm),tm); tm_topicmap_set_storage(topicmap,"MemStore"); tm_topicmap_delete(&topicmap); tm_cleanup(&tm); return(OK); } int open_topicmap(void) { TM tm; TMError e; TMTopicMap topicmap; if(tm_init(&tm,"www-df",NULL) != TM_OK) return FAIL; topicmap = tm_topicmap_new(tm_get_pool(tm),tm); tm_topicmap_set_storage(topicmap,"MemStore"); tm_topicmap_open(topicmap,NULL); tm_topicmap_close(topicmap); tm_topicmap_delete(&topicmap); tm_cleanup(&tm); return(OK); } int require_model(void) { TM tm; TMError e; TMTopicMap topicmap; if(tm_init(&tm,"www-df",NULL) != TM_OK) return FAIL; topicmap = tm_topicmap_new(tm_get_pool(tm),tm); tm_topicmap_set_storage(topicmap,"MemStore"); tm_topicmap_open(topicmap,NULL); tm_topicmap_require_model(topicmap,"http://purl.org/tm/Example/"); tm_topicmap_dump(topicmap,stdout); tm_topicmap_close(topicmap); tm_topicmap_delete(&topicmap); tm_cleanup(&tm); return(OK); } int empty_transaction(void) { TM tm; TMError e; TMTopicMap topicmap; if(tm_init(&tm,"www-df",NULL) != TM_OK) return FAIL; topicmap = tm_topicmap_new(tm_get_pool(tm),tm); tm_topicmap_set_storage(topicmap,"MemStore"); tm_topicmap_open(topicmap,NULL); tm_topicmap_start_transaction(topicmap,"test-transaction"); tm_topicmap_end_transaction(topicmap); tm_topicmap_close(topicmap); tm_topicmap_delete(&topicmap); tm_cleanup(&tm); return(OK); } int add_topic(void) { TM tm; int i; TMError e; struct TMSubject sbj; TMTopicMap topicmap; if(tm_init(&tm,"www-df",NULL) != TM_OK) return FAIL; topicmap = tm_topicmap_new(tm_get_pool(tm),tm); tm_topicmap_set_storage(topicmap,"MemStore"); tm_topicmap_open(topicmap,NULL); tm_topicmap_require_model(topicmap,"http://purl.org/tm/Example/"); tm_topicmap_start_transaction(topicmap,"test-transaction"); for(i=1;i<100;i++) { int v = rand(); TM_SET_SUBJECT_N1(&sbj,"http://purl.org/tm/Example/","Weight",(void*)v); tm_topicmap_add_topic(topicmap,&sbj,NULL); } tm_topicmap_dump(topicmap,stdout); tm_topicmap_end_transaction(topicmap); tm_topicmap_close(topicmap); tm_topicmap_delete(&topicmap); tm_cleanup(&tm); return(OK); } int main(int argc,char **args) { EXPECT_OK( find_df ); EXPECT_OK( errstr_resize ); EXPECT_FAIL( not_find_df ); EXPECT_OK( pool_tree ); EXPECT_OK( build_list ); EXPECT_OK( sort_list ); EXPECT_OK( load_model ); EXPECT_OK( create_topicmap ); EXPECT_OK( setstore_topicmap ); EXPECT_OK( open_topicmap ); EXPECT_OK( empty_transaction ); EXPECT_OK( require_model ); tm_set_trace_mask("*"); EXPECT_OK( add_topic ); return (0); }