From: <ro...@us...> - 2011-03-11 15:20:02
|
Revision: 1829 http://nsclspectcl.svn.sourceforge.net/nsclspectcl/?rev=1829&view=rev Author: ron-fox Date: 2011-03-11 15:19:56 +0000 (Fri, 11 Mar 2011) Log Message: ----------- Add/test functions for querying spectrum types and type information. Modified Paths: -------------- trunk/nextgen/primitives/Makefile.am trunk/nextgen/primitives/spectcl_experiment.h trunk/nextgen/primitives/spectcl_workspace_base.c Added Paths: ----------- trunk/nextgen/primitives/spectcl_workspace_spectypes.c trunk/nextgen/primitives/spectcl_wssptypetest.c Modified: trunk/nextgen/primitives/Makefile.am =================================================================== --- trunk/nextgen/primitives/Makefile.am 2011-03-11 01:08:39 UTC (rev 1828) +++ trunk/nextgen/primitives/Makefile.am 2011-03-11 15:19:56 UTC (rev 1829) @@ -13,7 +13,8 @@ spectcl_errors.c \ spectcl_experimentInternal.c \ spectcl_events.c \ - spectcl_workspace_base.c + spectcl_workspace_base.c \ + spectcl_workspace_spectypes.c libExperiment_la_LIBADD=@SQLITE3_LDFLAGS@ -luuid @@ -24,12 +25,13 @@ INCLUDES = -I. -I@top_srcdir@/NSCLException @SQLITE3_CFLAGS@ -TESTS= c_workspacebase c_eventstest \ +TESTS= c_workspacesptypes c_workspacebase c_eventstest \ c_exptests c_runtests c_uuidtests c_paramtests -check_PROGRAMS=c_workspacebase c_exptests c_paramtests c_runtests c_uuidtests \ +check_PROGRAMS=c_workspacesptypes \ + c_workspacebase c_exptests c_paramtests c_runtests c_uuidtests \ c_eventstest c_exptests_SOURCES=spectcl_experiment_check.c @@ -59,3 +61,6 @@ +c_workspacesptypes_SOURCES=spectcl_wssptypetest.c +c_workspacesptypes_CFLAGS=@CHECK_CFLAGS@ @SQLITE3_CFLAGS@ +c_workspacesptypes_LDADD=@CHECK_LIBS@ libExperiment.la Modified: trunk/nextgen/primitives/spectcl_experiment.h =================================================================== --- trunk/nextgen/primitives/spectcl_experiment.h 2011-03-11 01:08:39 UTC (rev 1828) +++ trunk/nextgen/primitives/spectcl_experiment.h 2011-03-11 15:19:56 UTC (rev 1829) @@ -120,6 +120,13 @@ pParameterData s_pData; } AugmentResult, *pAugmentResult; +/* Informatino gotten for a spectrum type: */ + +typedef struct _spectcl_spectrum_type { + char* s_type; + char* s_description; +} spectcl_spectrum_type; + typedef pAugmentResult (AugmentCallback)(size_t, pParameterData, void*); /** ** Function definitions; and globals: @@ -220,6 +227,16 @@ int spectcl_workspace_isCorrectExperiment(spectcl_experiment expdb, spectcl_workspace ws); + /** Functions used to query the spectrum types table. */ + + void spectcl_workspace_free_typelist(spectcl_spectrum_type** typeList); + spectcl_spectrum_type** spectcl_workspace_spectrumTypes(spectcl_workspace ws); + int spectcl_workspace_isValidType(spectcl_workspace ws, + const char* type); + char* spectcl_workspace_getDescription(spectcl_workspace ws, + const char* type); + + #ifdef __cplusplus } #endif Modified: trunk/nextgen/primitives/spectcl_workspace_base.c =================================================================== --- trunk/nextgen/primitives/spectcl_workspace_base.c 2011-03-11 01:08:39 UTC (rev 1828) +++ trunk/nextgen/primitives/spectcl_workspace_base.c 2011-03-11 15:19:56 UTC (rev 1829) @@ -84,6 +84,33 @@ } /** + ** Create and populate the spectrum types database: + ** @param ws - The workspace database handle + ** @return int + ** @retval 0 - Everything worked. + ** @retval -1 - Something failed. + ** + */ +static +int createTypesTable(sqlite3* ws) +{ + /* Create the table: */ + + do_non_select(ws, + "CREATE TABLE spectrum_types ( \ + id INTEGER PRIMARY KEY, \ + type VARCHAR(10), \ + description VARCHAR(256))"); + + /* Insert known spectrum types: */ + + do_non_select(ws, + "INSERT INTO spectrum_types (type,description) \ + VALUES ('1', '1-D')"); +} + + +/** ** Determine if an sqlite3 handle is open on a workspace database. ** this requires that we be able to get the 'type' config_item from ** the configuration_values table and that it have the value 'workspace'. @@ -151,6 +178,8 @@ status = create_config_values(ws, uuid); free(uuid); + createTypesTable(ws); + spectcl_workspace_close(ws); if (status) { Added: trunk/nextgen/primitives/spectcl_workspace_spectypes.c =================================================================== --- trunk/nextgen/primitives/spectcl_workspace_spectypes.c (rev 0) +++ trunk/nextgen/primitives/spectcl_workspace_spectypes.c 2011-03-11 15:19:56 UTC (rev 1829) @@ -0,0 +1,216 @@ +/* + This software is Copyright by the Board of Trustees of Michigan + State University (c) Copyright 2008 + + You may use this software under the terms of the GNU public license + (GPL). The terms of this license are described at: + + http://www.gnu.org/licenses/gpl.txt + + Author: + Ron Fox + NSCL + Michigan State University + East Lansing, MI 48824-1321 +*/ + +#include <config.h> +#include <sqlite3.h> + +#include "spectcl_experiment.h" +#include "spectcl_experimentInternal.h" + +#include <stdlib.h> + +/*------------------------ Private functions ----------------------------------*/ + +/** + ** Get the data from a type row; The non-exception case is: + ** - allocate a spectcl_spectrum_type struct. + ** - get the values of the two fields and put them in the struct. + ** - return the struct. + ** + ** @param stmt - the sqlit3_stmt* that represents the statement at the step + ** being retrieved. + ** @return spectcl_spectrum_type* + ** @retval NULL -- Memory allocation failure. + ** @retval non_NULL - Pointer to the allocated spectcl_spectrum_type filled in + ** with our data. + ** + */ +static spectcl_spectrum_type* +getTypeRow(sqlite3_stmt* stmt) +{ + spectcl_spectrum_type* result = (spectcl_spectrum_type*)malloc(sizeof(spectcl_spectrum_type)); + + /* Type code */ + + + if (!( result->s_type = getTextField(stmt, 0))) { + free(result); + return NULL; + } + + /* description field */ + + if(!(result->s_description= getTextField(stmt, 1))) { + free(result->s_type); + free(result); + return NULL; + } + + return result; + +} + +/*------------------------- public functions ---------------------------------*/ + +/** + ** Get a description of a spectrum type given the type name. + ** @param ws - Work space handle. + ** @param type - Abbreviated type. + ** @return char* + ** @retval NULL - Could not get the type. The error is put in spectcl_experiment_errno + ** @retval other- Pointer to dynamically allocated storage containing the description + ** of the spectrum type. + ** + ** ERRORS: + ** - SPEXP_NOT_WORKSPACE - The ws parameters is not a workspace. + ** - SPEXP_NOSUCH - there is no matchto the specified 'type' parameter. + */ +char* +spectcl_workspace_getDescription(spectcl_workspace ws, const char* type) +{ + char* result; + if (! spectcl_workspace_isWorkspace(ws)) { + spectcl_experiment_errno = SPEXP_NOT_WORKSPACE; + return NULL; + } + + result = getfirst(ws, "spectrum_types", + "description", "type", type); + if (!result) { + spectcl_experiment_errno = SPEXP_NOSUCH; + return NULL; + } + else { + return result; + } + +} +/** + ** Determine if a spectrum type is valid. + ** @param ws - workspace. + ** @param tyep - Spectrum type to check. + ** @return int + ** @retval SPEXP_OK - normal completion.type exists. + ** @retval SPEXP_NOT_WORKSPACE - ws was not a workspace + ** @retval SPEXP_NOSUCH - No such type. + */ +int +spectcl_workspace_isValidType(spectcl_workspace ws, const char* type) +{ + char* descrip = spectcl_workspace_getDescription(ws, type); + free(descrip); + return descrip ? SPEXP_OK : spectcl_experiment_errno; + +} +/** + ** Free the storage associated with a spectcl_spectrum_type** returned by + ** spectcl_workstation_spectrumTypes() call. + ** @param pList - pointer to the data to be freed. + */ +void +spectcl_workspace_free_typelist(spectcl_spectrum_type** pList) +{ + spectcl_spectrum_type** p = pList; + if(!p) return; /* nothing to free. */ + while(*p) { + + /* free each entry and its chars. */ + + free((*p)->s_type); + free((*p)->s_description); + free(*p); + + /* On to the next one until we hit the null terminator. */ + + p++; + + } + free(pList); /* Free the whole list. */ +} + +/** + ** Retrieve a list of spectrum types and their descriptions. + ** @param ws - Workspace. + ** @return spectcl_spectrum_type** + ** @retval NULL -some problem described in spectcl_experiment_errno occured. + ** @retval not-null - A pointer to a null terminated array of spectcl_spectrum pointers. + ** each spectcl_spectrum element contains the fields: + ** s_type - char* the spectrum type (encoded into spectrum definitions). + ** s_description - char* the Human readable spectrum type description. + ** ERRORS (spectcl_experiment_errno) + ** - SPEXP_NOT_WORKSPACE - ws is not a workspace. + ** - SPEXP_NOMEM - Dynamic memory allocation failure. + ** - SPEXP_SQLFAIL - Some call to sqlite failed. + */ +spectcl_spectrum_type** +spectcl_workspace_spectrumTypes(spectcl_workspace ws) +{ + sqlite3_stmt* stmt; + const char* sql = "SELECT type,description FROM spectrum_types"; + spectcl_spectrum_type** result = NULL; + spectcl_spectrum_type* entry; + int status; + size_t nItems = 0; /* number of types gotten so far. */ + + if(!spectcl_workspace_isWorkspace(ws)) { + spectcl_experiment_errno = SPEXP_NOT_WORKSPACE; + return NULL; + } + + /** Create the statement */ + + status = sqlite3_prepare_v2(ws, sql, -1, &stmt, NULL); + if(status != SQLITE_OK) { + spectcl_experiment_errno = SPEXP_SQLFAIL; + return NULL; + } + + while (status = sqlite3_step(stmt)) { + if (status != SQLITE_ROW) { + break; + } + entry = getTypeRow(stmt); /* get one entry of the type row. */ + if (!entry) { + spectcl_workspace_free_typelist(result); + spectcl_experiment_errno = SPEXP_NOMEM; + return NULL; + } + nItems++; + result = (spectcl_spectrum_type**)realloc(result, nItems+1); + if (!result) { + free(entry); + spectcl_workspace_free_typelist(result); + spectcl_experiment_errno = SPEXP_NOMEM; + return NULL; + } + result[nItems -1] = entry; + result[nItems] = NULL; /* null termination always. */ + } + /* Finalize the statement and then analyze the result */ + + sqlite3_finalize(stmt); + + if (status != SQLITE_DONE) { + spectcl_workspace_free_typelist(result); + + spectcl_experiment_errno = SPEXP_SQLFAIL; + return NULL; + } + + return result; + + +} Added: trunk/nextgen/primitives/spectcl_wssptypetest.c =================================================================== --- trunk/nextgen/primitives/spectcl_wssptypetest.c (rev 0) +++ trunk/nextgen/primitives/spectcl_wssptypetest.c 2011-03-11 15:19:56 UTC (rev 1829) @@ -0,0 +1,230 @@ +/* + This software is Copyright by the Board of Trustees of Michigan + State University (c) Copyright 2008 + + You may use this software under the terms of the GNU public license + (GPL). The terms of this license are described at: + + http://www.gnu.org/licenses/gpl.txt + + Author: + Ron Fox + NSCL + Michigan State University + East Lansing, MI 48824-1321 +*/ + +/** Unit tests for the + ** spectcl_experiment.c module + */ +#include <check.h> +#include "spectcl_experiment.h" +#include <sqlite3.h> +#include "spectcl_experimentInternal.h" + +#include <stdlib.h> +#include <unistd.h> +#include <stdio.h> +#include <time.h> +#include <uuid/uuid.h> +#include <string.h> +#include <limits.h> + + + +static char dbName[PATH_MAX]; +static char wsName[PATH_MAX]; +static char wsName1[PATH_MAX]; + + +spectcl_experiment db; +spectcl_workspace ws; + +/*----------------------------------- fixture code ----------------------------------------*/ + +static void setup() +{ + strcpy(dbName, tmpnam(NULL)); + db = spectcl_experiment_create(dbName); + strcpy(wsName, tmpnam(NULL)); + spectcl_workspace_create(db, wsName); + ws = spectcl_workspace_open(wsName); +} + +static void teardown() +{ + spectcl_workspace_close(ws); + spectcl_experiment_close(db); + unlink(dbName); + unlink(wsName); +} + +/*---------------------------------- test cases -------------------------------------------*/ + +/* +** When the workspace is created it should have spectrum_types created and stocked: +*/ + +START_TEST(test_schema) +{ + char* description; + + description = getfirst(ws, "spectrum_types", + "description", "type", "1"); + fail_if(description == NULL); + if(description) { + fail_unless(strcmp(description, "1-D") == 0); + } + free(description); +} +END_TEST +/** + * getDescription should fail with SPEXP_NOT_WORKSPACE if the ws parameter is not a workspace. + */ +START_TEST(test_descr_notws) +{ + char* description; + + description = spectcl_workspace_getDescription(db, "1"); + fail_unless(description == NULL); + fail_unless(spectcl_experiment_errno == SPEXP_NOT_WORKSPACE); + +} +END_TEST + +/** + ** getDescription should fail with SPEXP_NOSUCH if there is no such + ** spectrum type. + */ +START_TEST(test_descr_nosuch) +{ + char* description; + description = spectcl_workspace_getDescription(ws, "no such"); + fail_unless(description == NULL); + fail_unless(spectcl_experiment_errno == SPEXP_NOSUCH); + +} +END_TEST + +/* Ought to be able to get the descriptionfor '1' as 1-D + */ +START_TEST(test_descr_ok) +{ + char* description; + + description = spectcl_workspace_getDescription(ws, "1"); + fail_if(description == NULL); + if (description) { + fail_unless(strcmp(description, "1-D") == 0); + } +} +END_TEST + +/** + ** isValidType - NOT_WORKSPACE if ws parameter is invalid. + */ +START_TEST(test_valid_notws) +{ + fail_unless(spectcl_workspace_isValidType(db, "1") == SPEXP_NOT_WORKSPACE ); + +} +END_TEST +/** + * isValidtype - NOSUCH if no such type: + */ +START_TEST(test_valid_nosuch) +{ + fail_unless(spectcl_workspace_isValidType(ws, "no such") == SPEXP_NOSUCH); +} +END_TEST +/* +** isValidtype - SPEXP_OK if there is a type like this +*/ +START_TEST(test_valid_ok) +{ + fail_unless(spectcl_workspace_isValidType(ws, "1") == SPEXP_OK); +} +END_TEST +/** + ** List for not workspace reports that + */ +START_TEST(test_list_notws) +{ + fail_unless(spectcl_workspace_spectrumTypes(db) ==NULL); + fail_unless(spectcl_experiment_errno == SPEXP_NOT_WORKSPACE); +} +END_TEST + +/** + ** Get the list of types: + */ +START_TEST(test_list_ok) +{ + spectcl_spectrum_type** types; + + types = spectcl_workspace_spectrumTypes(ws); + fail_if(types == NULL); + /* + ** First one shouild be "1", "1-D", second one NULL for now. + */ + if (types) { + spectcl_spectrum_type* type= *types; + fail_unless(strcmp(type->s_type, "1") == 0); + fail_unless(strcmp(type->s_description, "1-D") == 0); + + type = types[1]; + fail_unless(type == NULL); + + /* Free storage */ + + spectcl_workspace_free_typelist(types); + } +} +END_TEST + +/* +** This should just not bomb +*/ +START_TEST(test_free_null) +{ + spectcl_spectrum_type** types= NULL; + spectcl_workspace_free_typelist(types); +} +END_TEST +/*------------------------------------ final setup ---------------------------------------------*/ +int main(void) +{ + int failures; + + Suite* s = suite_create("spectcl_workspace_spectypes"); + SRunner* sr = srunner_create(s); + TCase* tc_types = tcase_create("spectypes"); + + tcase_add_checked_fixture(tc_types, setup, teardown); + suite_add_tcase(s, tc_types); + + + tcase_add_test(tc_types, test_schema); + + tcase_add_test(tc_types, test_descr_notws); + tcase_add_test(tc_types, test_descr_nosuch); + tcase_add_test(tc_types, test_descr_ok); + + tcase_add_test(tc_types, test_valid_notws); + tcase_add_test(tc_types, test_valid_nosuch); + tcase_add_test(tc_types, test_valid_ok); + + tcase_add_test(tc_types, test_list_notws); + tcase_add_test(tc_types, test_list_ok); + + tcase_add_test(tc_types, test_free_null); + + + srunner_set_fork_status(sr, CK_NOFORK); + + + srunner_run_all(sr, CK_NORMAL); + failures = srunner_ntests_failed(sr); + srunner_free(sr); + return (failures == 0) ? EXIT_SUCCESS : EXIT_FAILURE; +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |