Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New function 'Reset_SOILWAT2_after_UnitTest' for unit testing #114

Merged
merged 1 commit into from
Dec 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions test/sw_maintest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

#include "../SW_Control.h"


#include "sw_testhelpers.h"


/* The unit test code is using the SOILWAT2-standalone input files from testing/ as
Expand All @@ -55,9 +55,9 @@ extern Bool QuietMode, EchoInits;
extern char _firstfile[];



int main(int argc, char **argv) {
int res;

/*--- Imitate 'SW_Main.c/main()': we need to initialize and take down SOILWAT2 variables
because SOILWAT2 uses (global) states. This is otherwise not comptable with the c++
approach used by googletest.
Expand All @@ -74,8 +74,7 @@ int main(int argc, char **argv) {
EchoInits = FALSE;

// Initialize SOILWAT2 variables and read values from example input file
SW_CTL_init_model(_firstfile);
SW_CTL_obtain_inputs();
Reset_SOILWAT2_after_UnitTest();

//--- Setup and call unit tests
::testing::InitGoogleTest(&argc, argv);
Expand Down
46 changes: 46 additions & 0 deletions test/sw_testhelpers.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "gtest/gtest.h"
#include <assert.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <float.h>
#include <math.h>
#include <memory.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

#include "../generic.h"
#include "../myMemory.h"
#include "../filefuncs.h"
#include "../rands.h"
#include "../Times.h"
#include "../SW_Defines.h"
#include "../SW_Times.h"
#include "../SW_Files.h"
#include "../SW_Carbon.h"
#include "../SW_Site.h"
#include "../SW_VegProd.h"
#include "../SW_VegEstab.h"
#include "../SW_Model.h"
#include "../SW_SoilWater.h"
#include "../SW_Weather.h"
#include "../SW_Markov.h"
#include "../SW_Sky.h"
#include "../SW_Control.h"

#include "sw_testhelpers.h"

extern char _firstfile[];

/** Initialize SOILWAT2 variables and read values from example input file
*/
void Reset_SOILWAT2_after_UnitTest(void) {
SW_CTL_init_model(_firstfile);
SW_CTL_obtain_inputs();
}
4 changes: 4 additions & 0 deletions test/sw_testhelpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include "gtest/gtest.h"


void Reset_SOILWAT2_after_UnitTest(void);
18 changes: 5 additions & 13 deletions test/test_SW_Carbon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include "../SW_Markov.h"
#include "../SW_Sky.h"

#include "sw_testhelpers.h"


extern SW_CARBON SW_Carbon;
extern SW_MODEL SW_Model;
Expand All @@ -58,8 +60,7 @@ namespace {
EXPECT_EQ(typeid(x), typeid(c->use_bio_mult));

// Reset to previous global state
SW_CBN_read();
calculate_CO2_multipliers();
Reset_SOILWAT2_after_UnitTest();
}


Expand Down Expand Up @@ -95,9 +96,7 @@ namespace {
}

// Reset to previous global state
SW_CBN_construct();
SW_CBN_read();
calculate_CO2_multipliers();
Reset_SOILWAT2_after_UnitTest();
}


Expand Down Expand Up @@ -126,15 +125,8 @@ namespace {
EXPECT_GT(v->tree.co2_multipliers[WUE_INDEX][year], 0.);
}


// Reset to previous global states
SW_CBN_construct();
SW_VPD_construct();

SW_CBN_read();
SW_VPD_read();

calculate_CO2_multipliers();
Reset_SOILWAT2_after_UnitTest();
}

} // namespace
6 changes: 3 additions & 3 deletions test/test_SW_VegProd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#include "../SW_Markov.h"
#include "../SW_Sky.h"

#include "sw_testhelpers.h"


extern SW_MODEL SW_Model;
extern SW_VEGPROD SW_VegProd;
Expand Down Expand Up @@ -64,9 +66,7 @@ namespace {
EXPECT_DOUBLE_EQ(1., v->forb.co2_multipliers[WUE_INDEX][MAX_NYEAR - 1]);

// Reset to previous global state
SW_VPD_construct();
SW_VPD_read();
calculate_CO2_multipliers();
Reset_SOILWAT2_after_UnitTest();
}


Expand Down