From 9aa08d20a74fdd31dc086540ec2580f318f1a021 Mon Sep 17 00:00:00 2001 From: dschlaep Date: Wed, 22 Nov 2017 16:09:16 -0500 Subject: [PATCH] We still cannot include SW_Output.c when unit testing - now close #82 We still cannot include SW_Output.c when unit testing: * "error: cannot increment expression of enum type" (e.g., OutKey, OutPeriod) * "error: assigning to 'OutKey' from incompatible type 'int'", and similar cases --> for now, exclude SW_Output.c (see new issue #85), work on fixing the errors under c++ and then - address c++ compile "warning: ISO C++11 does not allow conversion from string literal to 'char *'" --> don't define such variables as "char *" but instead as "char const *" (see https://stackoverflow.com/questions/20944784/why-is-conversion-from-stri ng-constant-to-char-valid-in-c-but-invalid-in-c#20944858) --- SW_Main_lib.c | 3 ++- SW_Output.c | 24 ++++++++++++------------ makefile | 19 +++++++++++++++---- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/SW_Main_lib.c b/SW_Main_lib.c index b625ef22e..0c874a668 100644 --- a/SW_Main_lib.c +++ b/SW_Main_lib.c @@ -89,7 +89,8 @@ void init_args(int argc, char **argv) { * -q=quiet, don't print "Check logfile" * at end of program. */ - char str[1024], *opts[] = { "-d", "-f", "-e", "-q" }; /* valid options */ + char str[1024]; + char const *opts[] = { "-d", "-f", "-e", "-q" }; /* valid options */ int valopts[] = { 1, 1, 0, 0 }; /* indicates options with values */ /* 0=none, 1=required, -1=optional */ int i, /* looper through all cmdline arguments */ diff --git a/SW_Output.c b/SW_Output.c index c2e22eced..22986b9d4 100644 --- a/SW_Output.c +++ b/SW_Output.c @@ -238,7 +238,7 @@ static TimeInt tOffset; /* 1 or 0 means we're writing previous or current period /* These MUST be in the same order as enum OutKey in * SW_Output.h */ -static char *key2str[] = +static char const *key2str[] = { SW_WETHR, SW_TEMP, SW_PRECIP, SW_SOILINF, SW_RUNOFF, SW_ALLH2O, SW_VWCBULK, SW_VWCMATRIC, SW_SWCBULK, SW_SWABULK, SW_SWAMATRIC, SW_SWPMATRIC, SW_SURFACEW, SW_TRANSP, SW_EVAPSOIL, SW_EVAPSURFACE, SW_INTERCEPTION, @@ -252,9 +252,9 @@ static ObjType key2obj[] = { eWTH, eWTH, eWTH, eWTH, eWTH, eSWC, eSWC, eSWC, eSWC, eSWC, eSWC, eSWC, eSWC, eSWC, eSWC, eSWC, eSWC, eSWC, eSWC, eSWC, eSWC, eSWC, eSWC, eSWC, eSWC, eSWC, eVES, eVES }; -static char *pd2str[] = +static char const *pd2str[] = { SW_DAY, SW_WEEK, SW_MONTH, SW_YEAR }; -static char *styp2str[] = +static char const *styp2str[] = { SW_SUM_OFF, SW_SUM_SUM, SW_SUM_AVG, SW_SUM_FNL }; /* =================================================== */ @@ -301,7 +301,7 @@ static OutPeriod str2period(char *s) { /* --------------------------------------------------- */ IntUS pd; - for (pd = 0; Str_CompareI(s, pd2str[pd]) && pd < SW_OUTNPERIODS; pd++) ; + for (pd = 0; Str_CompareI(s, (char *)pd2str[pd]) && pd < SW_OUTNPERIODS; pd++) ; return (OutPeriod) pd; } @@ -311,7 +311,7 @@ static OutKey str2key(char *s) /* --------------------------------------------------- */ IntUS key; - for (key = 0; key < SW_OUTNKEYS && Str_CompareI(s, key2str[key]); key++) ; + for (key = 0; key < SW_OUTNKEYS && Str_CompareI(s, (char *)key2str[key]); key++) ; if (key == SW_OUTNKEYS) { LogError(logfp, LOGFATAL, "%s : Invalid key (%s) in %s", SW_F_name(eOutput), s); @@ -324,7 +324,7 @@ static OutSum str2stype(char *s) /* --------------------------------------------------- */ IntUS styp; - for (styp = eSW_Off; styp < SW_NSUMTYPES && Str_CompareI(s, styp2str[styp]); styp++) ; + for (styp = eSW_Off; styp < SW_NSUMTYPES && Str_CompareI(s, (char *)styp2str[styp]); styp++) ; if (styp == SW_NSUMTYPES) { LogError(logfp, LOGFATAL, "%s : Invalid summary type (%s)\n", SW_F_name(eOutput), s); @@ -528,7 +528,7 @@ void SW_OUT_read(void) x = sscanf(inbuf, "%s %s %s %d %s %s", keyname, sumtype, period, &first, last, outfile); - if (Str_CompareI(keyname, "TIMESTEP") == 0) // condition to read in the TIMESTEP line in outsetup.in + if (Str_CompareI(keyname, (char *)"TIMESTEP") == 0) // condition to read in the TIMESTEP line in outsetup.in { numPeriod = sscanf(inbuf, "%s %s %s %s %s", keyname, timeStep[0], timeStep[1], timeStep[2], timeStep[3]); // need to rescan the line because you are looking for all strings, unlike the original scan @@ -541,7 +541,7 @@ void SW_OUT_read(void) { // If the line TIMESTEP is present, only need to read in five variables not six, so re read line. if (x < 6) { - if (Str_CompareI(keyname, "OUTSEP") == 0) + if (Str_CompareI(keyname, (char *)"OUTSEP") == 0) { switch ((int) *sumtype) { @@ -627,7 +627,7 @@ void SW_OUT_read(void) SW_Output[k].period = str2period(Str_ToUpper(period, ext)); SW_Output[k].first_orig = first; SW_Output[k].last_orig = - !Str_CompareI("END", last) ? 366 : atoi(last); + !Str_CompareI("END", (char *)last) ? 366 : atoi(last); if (SW_Output[k].last_orig == 0) { CloseFile(&f); @@ -1642,7 +1642,7 @@ static void get_vwcBulk(void) LyrIndex i; SW_SOILWAT *v = &SW_Soilwat; OutPeriod pd = SW_Output[eSW_VWCBulk].period; - RealD *val = malloc(sizeof(RealD) * SW_Site.n_layers); + RealD *val = (RealD *) malloc(sizeof(RealD) * SW_Site.n_layers); ForEachSoilLayer(i) val[i] = SW_MISSING; @@ -1740,7 +1740,7 @@ static void get_vwcMatric(void) SW_SOILWAT *v = &SW_Soilwat; OutPeriod pd = SW_Output[eSW_VWCMatric].period; RealD convert; - RealD *val = malloc(sizeof(RealD) * SW_Site.n_layers); + RealD *val = (RealD *) malloc(sizeof(RealD) * SW_Site.n_layers); ForEachSoilLayer(i) val[i] = SW_MISSING; @@ -2343,7 +2343,7 @@ static void get_transp(void) LyrIndex i; SW_SOILWAT *v = &SW_Soilwat; OutPeriod pd = SW_Output[eSW_Transp].period; - RealD *val = malloc(sizeof(RealD) * SW_Site.n_layers); + RealD *val = (RealD *) malloc(sizeof(RealD) * SW_Site.n_layers); #if !defined(STEPWAT) && !defined(RSOILWAT) char str[OUTSTRLEN]; #elif defined(STEPWAT) diff --git a/makefile b/makefile index 3a25f4e4e..dc863f67a 100644 --- a/makefile +++ b/makefile @@ -36,6 +36,17 @@ sources = SW_Main_lib.c SW_VegEstab.c SW_Control.c generic.c \ SW_VegProd.c SW_Flow_lib.c SW_Flow.c objects = $(sources:.c=.o) +# Unfortunately, we cannot include 'SW_Output.c' currently because +# - cannot increment expression of enum type (e.g., OutKey, OutPeriod) +# - assigning to 'OutKey' from incompatible type 'int' +sources_tests = SW_Main_lib.c SW_VegEstab.c SW_Control.c generic.c \ + rands.c Times.c mymemory.c filefuncs.c \ + SW_Files.c SW_Model.c SW_Site.c SW_SoilWater.c \ + SW_Markov.c SW_Weather.c SW_Sky.c\ + SW_VegProd.c SW_Flow_lib.c SW_Flow.c +objects_tests = $(sources_tests:.c=.o) + + bin_sources = SW_Main.c bin_objects = $(bin_sources:.c=.o) @@ -70,8 +81,8 @@ $(lib_target) : @rm -f $(objects) $(lib_target++) : - $(CXX) $(CXXFLAGS) -c $(sources) - ar -rcsu $(lib_target++) $(objects) + $(CXX) $(CXXFLAGS) -c $(sources_tests) + ar -rcsu $(lib_target++) $(objects_tests) @rm -f $(objects) @@ -81,7 +92,7 @@ $(target) : $(lib_target) $(CC) $(CFLAGS) $(LDFLAGS) -o $(target) $(bin_sources) $(LDLIBS) .PHONY : bint -bint : $(target) +bint : bin cp $(target) testing/$(target) .PHONY : bint_run @@ -104,7 +115,7 @@ test : $(lib_gtest) $(lib_target++) $(CXX) $(CXXFLAGS) $(LDFLAGS) -isystem ${GTEST_DIR}/include -pthread \ test/*.cc -o $(bin_test) $(gtest_LDLIBS) -test_run : +test_run : test ./$(bin_test)