Skip to content

Commit

Permalink
We still cannot include SW_Output.c when unit testing
Browse files Browse the repository at this point in the history
- 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)
  • Loading branch information
dschlaep committed Nov 22, 2017
1 parent 83a6429 commit 9aa08d2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
3 changes: 2 additions & 1 deletion SW_Main_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
24 changes: 12 additions & 12 deletions SW_Output.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 };

/* =================================================== */
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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)
Expand Down
19 changes: 15 additions & 4 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)


Expand All @@ -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
Expand All @@ -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)


Expand Down

0 comments on commit 9aa08d2

Please sign in to comment.