Commit ff50a3db authored by Vesa Oikonen's avatar Vesa Oikonen
Browse files

added parSelectByAnother()

parent 4ca7b9c9
......@@ -4,7 +4,7 @@ set(libtpcpar_HEADER_FILES "${CMAKE_CURRENT_SOURCE_DIR}" PARENT_SCOPE)
# This needs to be edited each time a par .c is added
add_library (tpcpar STATIC
par.c parcsv.c parift.c parxmlio.c pario.c
parres.c parfit.c parcomp.c
parres.c parfit.c parcomb.c parcomp.c
parselect.c parorder.c parexample.c
)
# Dependencies on other libraries
......@@ -37,7 +37,7 @@ install (
# Compile executable for testing
add_executable (libtpcpar libtpcpar.c
test_par.c test_parcsv.c test_parift.c test_parxmlio.c test_pario.c
test_parres.c test_parfit.c test_parcomp.c
test_parres.c test_parfit.c test_parcomb.c test_parcomp.c
test_parselect.c test_parorder.c test_parexample.c
)
# Link the executable to the libraries.
......@@ -46,12 +46,6 @@ target_link_libraries (
tpcmodels tpcextensions
m
)
# Install the executable
#install (
# PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/libtpcpar${CMAKE_EXECUTABLE_SUFFIX}
# DESTINATION bin
# COMPONENT librarytests
#)
# Copy test data folder
file(COPY test DESTINATION ${CMAKE_CURRENT_BINARY_DIR} )
......
......@@ -34,13 +34,13 @@ static char *info[] = {
/*****************************************************************************/
/** Run unit tests to the library functions
* @author Vesa Oikonen
* @return 0 if all tests pass, otherwise >0.
* */
@author Vesa Oikonen
@return 0 if all tests pass, otherwise >0.
*/
int main(
/** Nr of arguments */
/** Nr of arguments. */
int argc,
/** Pointer to arrays of argument string */
/** Pointer to arrays of argument string. */
char *argv[ ]
) {
int i, help=0, version=0, verbose=1, error=0, test=0;
......@@ -283,6 +283,12 @@ int main(
statusPrint(stderr, &status); statusFree(&status);
return(i);
}
/* parcomb */
i++; if((ret=test_parSelectByAnother(&status))!=0) {
fprintf(stderr, "failed (%d).\n", ret);
statusPrint(stderr, &status); statusFree(&status);
return(i);
}
/* parcomp */
i++; if((ret=test_parCompareParameterNames(&status))!=0) {
fprintf(stderr, "failed (%d).\n", ret);
......
/** @file parcomb.c
* @brief Combinations of data in PAR structures.
*/
/*****************************************************************************/
#include "tpcclibConfig.h"
/*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
/*****************************************************************************/
#include "tpcpar.h"
/*****************************************************************************/
/*****************************************************************************/
/** Select parameters and regions in a PAR structure that are available in another PAR structure.
@return enum tpcerror (TPCERROR_OK when successful).
@sa parSelectTACs, parEnsureNames, parCompareParameterNames, parCompareTacNames,
parSelectedParameters, parSelectedTACs
*/
int parSelectByAnother(
/** Pointer to target PAR structure, in which the PARN and PARR switches are set. */
PAR *d1,
/** Pointer to PAR structure to which the first is compared to; not modified. */
PAR *d2,
/** Pointer to store the number of available parameters; enter NULL, if not needed. */
int *pn,
/** Pointer to store the number of available regions; enter NULL, if not needed. */
int *pr,
/** Pointer to status data; enter NULL if not needed. */
TPCSTATUS *status
) {
int verbose=0; if(status!=NULL) verbose=status->verbose;
if(verbose>0) printf("%s()\n", __func__);
if(pn!=NULL) *pn=0;
if(pr!=NULL) *pr=0;
/* Check that required data exists */
if(d1==NULL || d1->parNr<1 || d1->tacNr<1) {
statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
return TPCERROR_NO_DATA;
}
/* Unselect all */
for(int i=0; i<d1->parNr; i++) d1->n[i].sw=0;
for(int i=0; i<d1->tacNr; i++) d1->r[i].sw=0;
/* If no data to compare to, then that is it */
if(d2==NULL || d2->parNr<1 || d2->tacNr<1) {
statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
return TPCERROR_OK;
}
/* Search for parameter names */
for(int i=0; i<d1->parNr; i++) {
if(d1->n[i].name==NULL || strnlen(d1->n[i].name, 1)<1) continue;
for(int j=0; j<d2->parNr; j++) {
if(d2->n[j].name==NULL || strnlen(d2->n[j].name, 1)<1) continue;
if(strcasecmp(d1->n[i].name, d2->n[j].name)==0) {d1->n[i].sw=1; continue;}
/* Allow mixed '-' and '_' */
char *buf1, *buf2;
buf1=strdup(d1->n[i].name); strReplaceChar(buf1, '-', '_');
buf2=strdup(d2->n[j].name); strReplaceChar(buf2, '-', '_');
int ret=strcasecmp(buf1, buf2);
free(buf1); free(buf2);
if(ret==0) {d1->n[i].sw=1; continue;}
}
}
/* Search for TAC names */
for(int i=0; i<d1->tacNr; i++) {
if(d1->r[i].name==NULL || strnlen(d1->r[i].name, 1)<1) continue;
for(int j=0; j<d2->tacNr; j++) {
if(d2->r[i].name==NULL || strnlen(d2->r[j].name, 1)<1) continue;
if(strcasecmp(d1->r[i].name, d2->r[j].name)==0) {d1->r[i].sw=1; continue;}
/* Allow mixed '-' and '_' */
char *buf1, *buf2;
buf1=strdup(d1->r[i].name); strReplaceChar(buf1, '-', '_');
buf2=strdup(d2->r[j].name); strReplaceChar(buf2, '-', '_');
int ret=strcasecmp(buf1, buf2);
free(buf1); free(buf2);
if(ret==0) {d1->r[i].sw=1; continue;}
}
}
/* Count the selected numbers, if needed */
if(pn!=NULL) *pn=parSelectedParameters(d1);
if(pr!=NULL) *pr=parSelectedTACs(d1);
// if(pn!=NULL) for(int i=0; i<d1->parNr; i++) if(d1->n[i].sw) *pn=*pn+1;
// if(pr!=NULL) for(int i=0; i<d1->tacNr; i++) if(d1->r[i].sw) *pr=*pr+1;
statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
return TPCERROR_OK;
}
/*****************************************************************************/
/*****************************************************************************/
......@@ -19,7 +19,7 @@
/** Select the TAC(s) in PAR structure that have matching ID name or number.
@return the number of newly selected VOIs, or <=0 in case of an error.
@author Vesa Oikonen
@sa parSelectedTACs, parSelectParameters, parSortByName
@sa parSelectedTACs, parSelectParameters, parSortByName, parSelectByAnother, parEnsureNames
*/
int parSelectTACs(
/** Pointer to PAR structure. */
......@@ -95,7 +95,7 @@ int parSelectTACs(
/** Get the number of selected TACs (sw!=0) in PAR structure.
@return the number of selected TACs.
@author Vesa Oikonen
@sa parSelectTACs, parSortByName
@sa parSelectTACs, parSortByName, parSelectByAnother
*/
int parSelectedTACs(
/** Pointer to PAR data. */
......@@ -115,7 +115,7 @@ int parSelectedTACs(
/** Select the parameter(s) in PAR struct that have matching name or number.
@return the number of newly selected parameters, or <=0 in case of an error.
@author Vesa Oikonen
@sa parSelectedParameters, parFindParameter, parSelectTACs
@sa parSelectedParameters, parFindParameter, parSelectTACs, parSelectByAnother, parEnsureNames
*/
int parSelectParameters(
/** Pointer to PAR structure. */
......@@ -194,7 +194,7 @@ int parSelectParameters(
/** Get the number of selected parameters (sw!=0) in PAR structure.
@return the number of selected parameters.
@author Vesa Oikonen
@sa parSelectParameters, parFindParameter
@sa parSelectParameters, parFindParameter, parSelectByAnother
*/
int parSelectedParameters(
/** Pointer to PAR data */
......
/*****************************************************************************/
#include "tpcclibConfig.h"
/*****************************************************************************/
#include "tpcextensions.h"
#include "test_tpcpar.h"
/*****************************************************************************/
/*****************************************************************************/
int test_parSelectByAnother(
TPCSTATUS *status
) {
int verbose=0; if(status!=NULL) verbose=status->verbose;
statusSet(status, __func__, __FILE__, __LINE__, 0);
if(verbose>0) {
printf("\n=====================================\n");
printf("\n%s\n", __func__);
printf("\n=====================================\n");
}
if(verbose>1) printf("\n should not crash with NULL input\n");
if(parSelectByAnother(NULL, NULL, NULL, NULL, NULL)==TPCERROR_OK) return(1);
PAR par; parInit(&par);
int n1=9, n2=9;
if(verbose>1) printf("\n should not crash with empty PAR struct\n");
if(parSelectByAnother(NULL, &par, &n1, &n2, NULL)==TPCERROR_OK) return(2);
if(n1!=0 || n2!=0) return(3);
n1=n2=8;
if(parSelectByAnother(&par, NULL, &n1, &n2, NULL)==TPCERROR_OK) return(4);
if(n1!=0 || n2!=0) return(5);
int ret;
if(verbose>1) printf("\n create test data\n");
ret=create_par(&par); if(ret!=0) {parFree(&par); return(10);}
if(verbose>1) parWrite(&par, stdout, PAR_FORMAT_CSV_UK, 0, NULL);
if(verbose>1) printf("\n test against NULL\n");
ret=parSelectByAnother(&par, NULL, &n1, &n2, NULL);
if(ret!=TPCERROR_OK) {parFree(&par); return(101);}
if(n1!=0 || n2!=0) {parFree(&par); return(102);}
if(verbose>1) printf("\n test against itself\n");
ret=parSelectByAnother(&par, &par, &n1, &n2, NULL);
if(ret!=TPCERROR_OK) {parFree(&par); return(111);}
if(n1!=par.parNr || n2!=par.tacNr) {parFree(&par); return(112);}
if(verbose>1) printf("\n create test data\n");
PAR par2; parInit(&par2);
ret=create_par(&par2); if(ret!=0) {parFree(&par); return(20);}
if(verbose>1) printf("\n one parameter less\n");
par2.parNr--;
if(verbose>1) parWrite(&par2, stdout, PAR_FORMAT_CSV_UK, 0, NULL);
ret=parSelectByAnother(&par, &par2, &n1, &n2, NULL);
if(ret!=TPCERROR_OK) {parFree(&par); parFree(&par2); return(201);}
if(verbose>2) printf("parNr=%d n1=%d\n", par.parNr, n1);
if(n1!=par.parNr-1 || n2!=par.tacNr) {parFree(&par); parFree(&par2); return(202);}
ret=parSelectByAnother(&par2, &par, &n1, &n2, NULL);
if(ret!=TPCERROR_OK) {parFree(&par); parFree(&par2); return(211);}
if(verbose>2) printf("parNr=%d n1=%d\n", par2.parNr, n1);
if(n1!=par2.parNr || n2!=par2.tacNr) {parFree(&par); parFree(&par2); return(212);}
par2.parNr++;
if(verbose>1) printf("\n one region less\n");
par2.tacNr--;
if(verbose>1) parWrite(&par2, stdout, PAR_FORMAT_CSV_UK, 0, NULL);
ret=parSelectByAnother(&par, &par2, &n1, &n2, NULL);
if(ret!=TPCERROR_OK) {parFree(&par); parFree(&par2); return(221);}
if(verbose>2) printf("tacNr=%d n2=%d\n", par.tacNr, n2);
if(n1!=par.parNr || n2!=par.tacNr-1) {parFree(&par); parFree(&par2); return(222);}
ret=parSelectByAnother(&par2, &par, &n1, &n2, NULL);
if(ret!=TPCERROR_OK) {parFree(&par); parFree(&par2); return(231);}
if(verbose>2) printf("tacNr=%d n1=%d\n", par2.tacNr, n2);
if(n1!=par2.parNr || n2!=par2.tacNr) {parFree(&par); parFree(&par2); return(232);}
par2.tacNr++;
if(verbose>1) printf("\n different region and parameter name\n");
strcpy(par2.n[1].name, "p");
strcpy(par2.r[0].name, "tac1");
if(verbose>1) parWrite(&par2, stdout, PAR_FORMAT_CSV_UK, 0, NULL);
ret=parSelectByAnother(&par, &par2, &n1, &n2, NULL);
if(ret!=TPCERROR_OK) {parFree(&par); parFree(&par2); return(301);}
if(verbose>2) printf("n1=%d n2=%d\n", n1, n2);
if(n1!=par.parNr-1 || n2!=par.tacNr-1) {parFree(&par); parFree(&par2); return(302);}
if(verbose>1) printf("\n no matching region and parameter names\n");
for(int i=0; i<par2.parNr; i++) sprintf(par2.n[i].name, "p%d", 1+i);
for(int i=0; i<par2.tacNr; i++) sprintf(par2.r[i].name, "tac%d", 1+i);
if(verbose>1) parWrite(&par2, stdout, PAR_FORMAT_CSV_UK, 0, NULL);
ret=parSelectByAnother(&par, &par2, &n1, &n2, NULL);
if(ret!=TPCERROR_OK) {parFree(&par); parFree(&par2); return(311);}
if(verbose>2) printf("n1=%d n2=%d\n", n1, n2);
if(n1!=0 || n2!=0) {parFree(&par); parFree(&par2); return(312);}
parFree(&par); parFree(&par2);
statusSet(status, __func__, __FILE__, __LINE__, 0);
return(0);
}
/*****************************************************************************/
/*****************************************************************************/
......@@ -116,7 +116,7 @@ int test_parSelectedTACs(
printf("\n=====================================\n");
}
if(verbose>0) printf("tested with test_parSelectTACs()\n");
if(verbose>1) printf("tested with test_parSelectTACs()\n");
statusSet(status, __func__, __FILE__, __LINE__, 0);
return(0);
......@@ -239,7 +239,7 @@ int test_parSelectedParameters(
printf("\n=====================================\n");
}
if(verbose>0) printf("tested with test_parSelectParameters()\n");
if(verbose>1) printf("tested with test_parSelectParameters()\n");
statusSet(status, __func__, __FILE__, __LINE__, 0);
return(0);
......
......@@ -85,6 +85,11 @@ extern int test_parWriteFIT(TPCSTATUS *status);
extern int test_parReadFIT(TPCSTATUS *status);
/*****************************************************************************/
/*****************************************************************************/
/* parcomb */
extern int test_parSelectByAnother(TPCSTATUS *status);
/*****************************************************************************/
/*****************************************************************************/
/* parcomp */
extern int test_parCompareParameterNames(TPCSTATUS *status);
......
......@@ -216,6 +216,11 @@ extern int parReadFIT(PAR *par, CSV *csv, IFT *ift, TPCSTATUS *status);
extern int parWriteXML(PAR *par, FILE *fp, TPCSTATUS *status);
/*****************************************************************************/
/*****************************************************************************/
/* parcomb */
int parSelectByAnother(PAR *d1, PAR *d2, int *pn, int *pr, TPCSTATUS *status);
/*****************************************************************************/
/*****************************************************************************/
/* parcomp */
extern int parCompareParameterNames(
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment