From fce2a94923da1903b4333f52d5617e084f549cb8 Mon Sep 17 00:00:00 2001 From: nip5 Date: Fri, 1 Jun 2018 13:11:59 -0700 Subject: [PATCH 01/25] Added testing file and made interpolate_monthlyValues testable --- Times.c | 5 ++++- Times.h | 2 +- test/test_Times.cc | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 test/test_Times.cc diff --git a/Times.c b/Times.c index 415671074..c30736b3d 100644 --- a/Times.c +++ b/Times.c @@ -271,6 +271,8 @@ TimeInt Time_get_lastdoy_y(TimeInt year) { * pretty independent of the current time, so I'm * removing the preceeding Time_ to shorten the calls in * the code + * + * @returns the month the given day falls under */ TimeInt doy2month(const TimeInt doy) { /* =================================================== */ @@ -334,7 +336,7 @@ Bool isleapyear(const TimeInt year) { } -void interpolate_monthlyValues(double monthlyValues[], double dailyValues[]) { +double* interpolate_monthlyValues(double monthlyValues[], double dailyValues[]) { /********************************************************************** PURPOSE: linear interpolation of monthly value; monthly values are assumed to representative for the 15th of a month @@ -369,6 +371,7 @@ void interpolate_monthlyValues(double monthlyValues[], double dailyValues[]) { dailyValues[doy] = monthlyValues[month] + sign * (monthlyValues[month2] - monthlyValues[month]) / (monthdays[month]) * (mday - 15.); } } + return dailyValues; } /* =================================================== */ diff --git a/Times.h b/Times.h index 23964ecbb..7e6ba5764 100644 --- a/Times.h +++ b/Times.h @@ -116,6 +116,6 @@ TimeInt yearto4digit(TimeInt yr); Bool isleapyear_now(void); Bool isleapyear(const TimeInt year); -void interpolate_monthlyValues(double monthlyValues[], double dailyValues[]); +double* interpolate_monthlyValues(double monthlyValues[], double dailyValues[]); #endif diff --git a/test/test_Times.cc b/test/test_Times.cc new file mode 100644 index 000000000..97a8c1983 --- /dev/null +++ b/test/test_Times.cc @@ -0,0 +1,42 @@ +#include "gtest/gtest.h" +#include +#include +#include +#include +#include +#include "../generic.h" +#include "../filefuncs.h" +#include "../myMemory.h" +#include "../SW_Defines.h" +#include "../SW_Files.h" +#include "../SW_Model.h" +#include "../SW_Site.h" +#include "../SW_SoilWater.h" +#include "../SW_VegProd.h" +#include "../SW_Site.h" +#include "../SW_Flow_lib.h" +#include "../Times.h" +#include "sw_testhelpers.h" + +namespace{ + // Test the 'Times.c' function 'interpolate_monthlyValues' + TEST(TimesTest, interpolateMonthlyValues){ + TimeInt day = 32; + // month which day falls under + TimeInt month = doy2month(day); + // day in the month the day falls under + // example: day = 32 means that mday would be 1 for febuary 1st + TimeInt mday = doy2mday(day); + double monthlyValues[12]; + double dailyValues[MAX_DAYS + 1]; + int i; + for (i = 0; i < 11; i++){ + + } + printf("month: %d\n", month); + printf("mday: %d\n", mday); + + // Reset to previous global states + Reset_SOILWAT2_after_UnitTest(); + } +} From 39d3bc36ccdb5b91492154afda6c08d5adbf3aa9 Mon Sep 17 00:00:00 2001 From: nip5 Date: Fri, 1 Jun 2018 16:19:15 -0700 Subject: [PATCH 02/25] Added macro for length of array --- Times.c | 1 - generic.h | 1 + test/test_Times.cc | 16 ++++++++++------ 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Times.c b/Times.c index c30736b3d..0df364fae 100644 --- a/Times.c +++ b/Times.c @@ -367,7 +367,6 @@ double* interpolate_monthlyValues(double monthlyValues[], double dailyValues[]) month2 = (month == Jan) ? Dec : month - 1; sign = -1; } - dailyValues[doy] = monthlyValues[month] + sign * (monthlyValues[month2] - monthlyValues[month]) / (monthdays[month]) * (mday - 15.); } } diff --git a/generic.h b/generic.h index c17e9b52c..f4389a3d1 100644 --- a/generic.h +++ b/generic.h @@ -209,6 +209,7 @@ extern int logged; /* REQUIRED */ // 06/26/2013 (dlm) powe(): an alternate definition of pow(x, y) for x>0... this is faster (ca. 20%) then the one in math.h, but comes with a cost as the precision is slightly lower. The measured precision drop I was getting was at max a relative error of about 100 billion percent (12 places after the decimal point) per calculation though so I don't think it's a big deal... (though it's hard to even accurately tell) #define powe(x, y) (exp((y) * log(x))) //x^y == exponential(y * ln(x)) or e^(y * ln(x)). NOTE: this will only work when x > 0 I believe #define squared(x) ((x) * (x)) // added for convenience +#define length(array) (sizeof(array) / sizeof(*(array))) //get length of an array /*************************************************** * Function definitions diff --git a/test/test_Times.cc b/test/test_Times.cc index 97a8c1983..8ab83f273 100644 --- a/test/test_Times.cc +++ b/test/test_Times.cc @@ -27,15 +27,19 @@ namespace{ // day in the month the day falls under // example: day = 32 means that mday would be 1 for febuary 1st TimeInt mday = doy2mday(day); - double monthlyValues[12]; + double monthlyValues[30]; double dailyValues[MAX_DAYS + 1]; - int i; - for (i = 0; i < 11; i++){ + int i; + //printf("monthly vals length: %d\n",length(monthlyValues) ); + for (i = 0; i < length(monthlyValues); i++){ + monthlyValues[i] = 10; } - printf("month: %d\n", month); - printf("mday: %d\n", mday); - + double* res = interpolate_monthlyValues(monthlyValues, dailyValues); + //printf("dailyValues: "); + //for(i = 1; i <= MAX_DAYS; i++){ + // printf("%d, ", doy2mday(i)); + //} // Reset to previous global states Reset_SOILWAT2_after_UnitTest(); } From b4a65b29abffb89dcbbd691963b682c4577fcc08 Mon Sep 17 00:00:00 2001 From: nip5 Date: Tue, 5 Jun 2018 10:16:25 -0700 Subject: [PATCH 03/25] Removed got here print statements for debugging --- test/test_SW_Flow_lib_temp.cc | 6 ------ 1 file changed, 6 deletions(-) diff --git a/test/test_SW_Flow_lib_temp.cc b/test/test_SW_Flow_lib_temp.cc index fab4ae808..98e2a1149 100644 --- a/test/test_SW_Flow_lib_temp.cc +++ b/test/test_SW_Flow_lib_temp.cc @@ -528,29 +528,23 @@ namespace { double theMaxDepth2 = 1; //swprintf("\n Depth of Layers %f, MaxDepth %f", width[nlyrs - 1], theMaxDepth2); - printf("GOT HERE\n"); EXPECT_DEATH(soil_temperature(airTemp, pet, aet, biomass, swc, swc_sat, bDensity, width, oldsTemp, sTemp, surfaceTemp, nlyrs, fc, wp, bmLimiter, t1Param1, t1Param2, t1Param3, csParam1, csParam2, shParam, snowdepth, sTconst, deltaX, theMaxDepth2, nRgr, snow, &ptr_stError), "@ generic.c LogError"); - printf("GOT HERE 2\n"); // ptr_stError should be set to TRUE if soil_temperature_today fails (i.e. unrealistic temp values) double sTemp2[nlyrs], oldsTemp2[nlyrs]; for (i = 0; i < nlyrs; i++) { sTemp2[i] = RandNorm(150, 1); oldsTemp2[i] = RandNorm(150, 1); } - printf("GOT HERE 3\n"); soil_temperature(airTemp, pet, aet, biomass, swc, swc_sat, bDensity, width, oldsTemp2, sTemp2, surfaceTemp, nlyrs, fc, wp, bmLimiter, t1Param1, t1Param2, t1Param3, csParam1, csParam2, shParam, snowdepth, sTconst, deltaX, theMaxDepth, nRgr, snow, &ptr_stError); - printf("GOT HERE 4\n"); // Check that ptr_stError is TRUE EXPECT_EQ(ptr_stError, 1); - printf("GOT HERE 5\n"); //Reset to global state Reset_SOILWAT2_after_UnitTest(); - printf("GOT HERE 6\n"); } } From ccfe28a7e0fb809d568757949b31a63a17cbfd98 Mon Sep 17 00:00:00 2001 From: nip5 Date: Fri, 8 Jun 2018 14:20:21 -0700 Subject: [PATCH 04/25] Added more tests to cover all conditionals of interpolate_monthlyValues --- Times.c | 6 ++++-- test/test_Times.cc | 17 ++++++++++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Times.c b/Times.c index 0df364fae..7172dc05a 100644 --- a/Times.c +++ b/Times.c @@ -359,11 +359,13 @@ double* interpolate_monthlyValues(double monthlyValues[], double dailyValues[]) if (mday == 15) { dailyValues[doy] = monthlyValues[month]; - } else { + } + else { if (mday >= 15) { month2 = (month == Dec) ? Jan : month + 1; sign = 1; - } else { + } + else { month2 = (month == Jan) ? Dec : month - 1; sign = -1; } diff --git a/test/test_Times.cc b/test/test_Times.cc index 8ab83f273..c7cdd6f5d 100644 --- a/test/test_Times.cc +++ b/test/test_Times.cc @@ -21,7 +21,7 @@ namespace{ // Test the 'Times.c' function 'interpolate_monthlyValues' TEST(TimesTest, interpolateMonthlyValues){ - TimeInt day = 32; + TimeInt day = 16; // month which day falls under TimeInt month = doy2month(day); // day in the month the day falls under @@ -30,12 +30,23 @@ namespace{ double monthlyValues[30]; double dailyValues[MAX_DAYS + 1]; - int i; - //printf("monthly vals length: %d\n",length(monthlyValues) ); + unsigned int i; + // function with monthlyValues all 10 for (i = 0; i < length(monthlyValues); i++){ monthlyValues[i] = 10; } double* res = interpolate_monthlyValues(monthlyValues, dailyValues); + // test final conditional + EXPECT_EQ(res[1], 10.0); + // should always be zero, regardless of input + EXPECT_EQ(res[0], 0.0); + // test top conditional + EXPECT_EQ(res[15], 10.0); + // test middle conditional + EXPECT_EQ(res[16], 10.0); + + printf("month, mday: %d, %d\n", month, mday); + printf("dailyValues[15]: %f", res[15]); //printf("dailyValues: "); //for(i = 1; i <= MAX_DAYS; i++){ // printf("%d, ", doy2mday(i)); From 788f32259a41a0d69ce0f0eb4e684785340a0369 Mon Sep 17 00:00:00 2001 From: nip5 Date: Mon, 11 Jun 2018 14:57:58 -0700 Subject: [PATCH 05/25] Increased accuracy in SWSWCbulk2SWPmatric test by calculating val in R --- test/test_SW_SoilWater.cc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/test_SW_SoilWater.cc b/test/test_SW_SoilWater.cc index 2a1d12081..ff49ed6a1 100644 --- a/test/test_SW_SoilWater.cc +++ b/test/test_SW_SoilWater.cc @@ -59,7 +59,7 @@ namespace{ RealD snowmelt = 1.2; // test 1, since TminAccu2 is < temp_ave, we expect SnowAccu to be 0 and thus rain is ppt - SnowAccu SW_SWC_adjust_snow(temp_min, temp_max, ppt, &rain, &snow, &snowmelt); - EXPECT_EQ(rain, 1); + EXPECT_EQ(rain, ppt); EXPECT_EQ(snow, 0); Reset_SOILWAT2_after_UnitTest(); @@ -68,8 +68,8 @@ namespace{ // Test the 'SW_SoilWater' function 'SW_SWCbulk2SWPmatric' TEST(SWSoilWaterTest, SWSWCbulk2SWPmatric){ // TODO, lacking info - RealD fractionGravel = 0.2; - RealD swcBulk = 0; + double fractionGravel = 0.2; + double swcBulk = 0; LyrIndex n = 1; // test missing and 0 for swc double res = SW_SWCbulk2SWPmatric(fractionGravel, swcBulk, n); @@ -87,9 +87,9 @@ namespace{ SW_Site.lyr[n] -> thetasMatric = 1; SW_Site.lyr[n] -> bMatric = 1; res = SW_SWCbulk2SWPmatric(fractionGravel, swcBulk, n); - double resExpect = .00013310902; // did math by hand to get this value + double resExpect = .000001953; // value calculated in R double actualExpectDiff = fabs(res - resExpect); - EXPECT_LT(actualExpectDiff, .0002); + EXPECT_LT(actualExpectDiff, .0000002); } // Test the 'SW_SoilWater' function 'SW_SWPmatric2VWCBulk' From 75d516f22ddd3ba319abd02b45878396de4e8938 Mon Sep 17 00:00:00 2001 From: nip5 Date: Tue, 12 Jun 2018 12:22:28 -0700 Subject: [PATCH 06/25] Added testing for snowmelt in SWSWCSadjustSnow --- SW_SoilWater.c | 4 ++-- {testing => test}/test_SW_Flow_lib_temp.cc | 0 test/test_SW_SoilWater.cc | 16 +++++++++------- 3 files changed, 11 insertions(+), 9 deletions(-) rename {testing => test}/test_SW_Flow_lib_temp.cc (100%) diff --git a/SW_SoilWater.c b/SW_SoilWater.c index 6b1ad0a74..bee10dd0c 100644 --- a/SW_SoilWater.c +++ b/SW_SoilWater.c @@ -803,8 +803,8 @@ void SW_SWC_adjust_snow(RealD temp_min, RealD temp_max, RealD ppt, RealD *rain, RealD *snow, RealD *snowmelt) { RealD *snowpack = &SW_Soilwat.snowpack[Today], - doy = SW_Model.doy, - temp_ave, Rmelt, SnowAccu = 0., SnowMelt = 0.; + doy = SW_Model.doy, temp_ave, + Rmelt, SnowAccu = 0., SnowMelt = 0.; static RealD snow_cov = 1.; diff --git a/testing/test_SW_Flow_lib_temp.cc b/test/test_SW_Flow_lib_temp.cc similarity index 100% rename from testing/test_SW_Flow_lib_temp.cc rename to test/test_SW_Flow_lib_temp.cc diff --git a/test/test_SW_SoilWater.cc b/test/test_SW_SoilWater.cc index 110a6ad3a..34ec9bad6 100644 --- a/test/test_SW_SoilWater.cc +++ b/test/test_SW_SoilWater.cc @@ -51,16 +51,18 @@ namespace{ TEST(SWSoilWaterTest, SWSWCSdjustSnow){ // setup mock variables SW_Site.TminAccu2 = 0; - RealD temp_min = 0; - RealD temp_max = 10; - RealD ppt = 1; - RealD rain = 1.5; - RealD snow = 1.5; - RealD snowmelt = 1.2; - // test 1, since TminAccu2 is < temp_ave, we expect SnowAccu to be 0 and thus rain is ppt - SnowAccu + double temp_min = 0; + double temp_max = 10; + double ppt = 1; + double rain = 1.5; + double snow = 1.5; + double snowmelt = 1.2; + + // since TminAccu2 is < temp_ave, we expect SnowAccu to be 0 and thus rain is ppt - SnowAccu SW_SWC_adjust_snow(temp_min, temp_max, ppt, &rain, &snow, &snowmelt); EXPECT_EQ(rain, ppt); EXPECT_EQ(snow, 0); + EXPECT_EQ(snowmelt, 0); Reset_SOILWAT2_after_UnitTest(); } From edd2201b86ca22f577229ff1fd75c38ab955cf86 Mon Sep 17 00:00:00 2001 From: nip5 Date: Tue, 12 Jun 2018 14:26:40 -0700 Subject: [PATCH 07/25] Implemented full coverage for function SW_SWCbulk2SWPmatric --- test/test_SW_SoilWater.cc | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/test/test_SW_SoilWater.cc b/test/test_SW_SoilWater.cc index 34ec9bad6..1b9eebc09 100644 --- a/test/test_SW_SoilWater.cc +++ b/test/test_SW_SoilWater.cc @@ -82,7 +82,7 @@ namespace{ EXPECT_EQ(res, 0.0); Reset_SOILWAT2_after_UnitTest(); - // test swp val + // test swp val when second conditional is true but third is false swcBulk = 4; SW_Site.lyr[n] -> width = 1; SW_Site.lyr[n] -> psisMatric = 1; @@ -92,6 +92,13 @@ namespace{ double resExpect = .000001953; // value calculated in R double actualExpectDiff = fabs(res - resExpect); EXPECT_LT(actualExpectDiff, .0000002); + Reset_SOILWAT2_after_UnitTest(); + + // when second and third conditional are true + fractionGravel = 1; + res = SW_SWCbulk2SWPmatric(fractionGravel, swcBulk, n); + EXPECT_DOUBLE_EQ(res, INFINITY); + } // Test the 'SW_SoilWater' function 'SW_SWPmatric2VWCBulk' @@ -126,4 +133,22 @@ namespace{ } Reset_SOILWAT2_after_UnitTest(); } + + // Death tests for SW_SWCbulk2SWPmatric function + TEST(SWSoilWaterTest, SW_SWCbulk2SWPmatricDeathTest) { + // test when swcBulk < 0 + LyrIndex n = 1; + double swcBulk = -1; + double fractionGravel = 1; + SW_Site.lyr[n] -> width = 1; + SW_Site.lyr[n] -> psisMatric = 1; + SW_Site.lyr[n] -> thetasMatric = 1; + SW_Site.lyr[n] -> bMatric = 1; + + EXPECT_DEATH_IF_SUPPORTED(SW_SWCbulk2SWPmatric(fractionGravel, swcBulk, n),"@ generic.c LogError"); // We expect death when max depth < last layer + + // Reset to previous global state + Reset_SOILWAT2_after_UnitTest(); + } + } From c21121f8e2992028cf2a2d36f64849652b382819 Mon Sep 17 00:00:00 2001 From: nip5 Date: Wed, 13 Jun 2018 10:29:10 -0700 Subject: [PATCH 08/25] Modified unit tests to work with structure pointer and no return --- Times.c | 3 +-- Times.h | 2 +- test/test_Times.cc | 36 ++++++++++++++---------------------- 3 files changed, 16 insertions(+), 25 deletions(-) diff --git a/Times.c b/Times.c index 7172dc05a..b47ec113a 100644 --- a/Times.c +++ b/Times.c @@ -336,7 +336,7 @@ Bool isleapyear(const TimeInt year) { } -double* interpolate_monthlyValues(double monthlyValues[], double dailyValues[]) { +void interpolate_monthlyValues(double monthlyValues[], double dailyValues[]) { /********************************************************************** PURPOSE: linear interpolation of monthly value; monthly values are assumed to representative for the 15th of a month @@ -372,7 +372,6 @@ double* interpolate_monthlyValues(double monthlyValues[], double dailyValues[]) dailyValues[doy] = monthlyValues[month] + sign * (monthlyValues[month2] - monthlyValues[month]) / (monthdays[month]) * (mday - 15.); } } - return dailyValues; } /* =================================================== */ diff --git a/Times.h b/Times.h index 7e6ba5764..23964ecbb 100644 --- a/Times.h +++ b/Times.h @@ -116,6 +116,6 @@ TimeInt yearto4digit(TimeInt yr); Bool isleapyear_now(void); Bool isleapyear(const TimeInt year); -double* interpolate_monthlyValues(double monthlyValues[], double dailyValues[]); +void interpolate_monthlyValues(double monthlyValues[], double dailyValues[]); #endif diff --git a/test/test_Times.cc b/test/test_Times.cc index c7cdd6f5d..f9acf6139 100644 --- a/test/test_Times.cc +++ b/test/test_Times.cc @@ -5,6 +5,7 @@ #include #include #include "../generic.h" +#include "../SW_Sky.h" #include "../filefuncs.h" #include "../myMemory.h" #include "../SW_Defines.h" @@ -21,36 +22,27 @@ namespace{ // Test the 'Times.c' function 'interpolate_monthlyValues' TEST(TimesTest, interpolateMonthlyValues){ - TimeInt day = 16; - // month which day falls under - TimeInt month = doy2month(day); - // day in the month the day falls under - // example: day = 32 means that mday would be 1 for febuary 1st - TimeInt mday = doy2mday(day); - double monthlyValues[30]; - double dailyValues[MAX_DAYS + 1]; + SW_SKY SW_Sky; + SW_SKY *interpolate = &SW_Sky; unsigned int i; + // function with monthlyValues all 10 - for (i = 0; i < length(monthlyValues); i++){ - monthlyValues[i] = 10; + for (i = 0; i < length(interpolate -> cloudcov); i++){ + interpolate -> cloudcov[i] = 10; } - double* res = interpolate_monthlyValues(monthlyValues, dailyValues); + interpolate_monthlyValues(interpolate -> cloudcov, interpolate -> cloudcov_daily); + // test final conditional - EXPECT_EQ(res[1], 10.0); - // should always be zero, regardless of input - EXPECT_EQ(res[0], 0.0); + EXPECT_EQ(interpolate -> cloudcov_daily[1], 10.0); + // should always be zero, regardless of input since dailyValues is never + // changed + EXPECT_EQ(interpolate -> cloudcov_daily[0], 0.0); // test top conditional - EXPECT_EQ(res[15], 10.0); + EXPECT_EQ(interpolate -> cloudcov_daily[15], 10.0); // test middle conditional - EXPECT_EQ(res[16], 10.0); + EXPECT_EQ(interpolate -> cloudcov_daily[16], 10.0); - printf("month, mday: %d, %d\n", month, mday); - printf("dailyValues[15]: %f", res[15]); - //printf("dailyValues: "); - //for(i = 1; i <= MAX_DAYS; i++){ - // printf("%d, ", doy2mday(i)); - //} // Reset to previous global states Reset_SOILWAT2_after_UnitTest(); } From 9ee7d98f50cf694154ff75f041763888f20f3c69 Mon Sep 17 00:00:00 2001 From: nip5 Date: Wed, 13 Jun 2018 13:31:59 -0700 Subject: [PATCH 09/25] Implemented more tests for different scenarios --- test/test_Times.cc | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/test/test_Times.cc b/test/test_Times.cc index f9acf6139..94642cd63 100644 --- a/test/test_Times.cc +++ b/test/test_Times.cc @@ -22,12 +22,13 @@ namespace{ // Test the 'Times.c' function 'interpolate_monthlyValues' TEST(TimesTest, interpolateMonthlyValues){ + // point to the structure that contains cloud coverage monthly values SW_SKY SW_Sky; SW_SKY *interpolate = &SW_Sky; unsigned int i; - // function with monthlyValues all 10 + // set all monthlyValues all 10 for (i = 0; i < length(interpolate -> cloudcov); i++){ interpolate -> cloudcov[i] = 10; } @@ -45,5 +46,16 @@ namespace{ // Reset to previous global states Reset_SOILWAT2_after_UnitTest(); + + // change first value to 20 and test the changes + interpolate -> cloudcov[0] = 20; + interpolate_monthlyValues(interpolate -> cloudcov, interpolate -> cloudcov_daily); + // calculated by hand + EXPECT_EQ(interpolate -> cloudcov_daily[15], 20); + EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[14], 19.67741935483871); + EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[32], 15); + + // Reset to previous global states + Reset_SOILWAT2_after_UnitTest(); } } From e6dec84712b60681661aaa8d8646d8f3a218e264 Mon Sep 17 00:00:00 2001 From: nip5 Date: Wed, 13 Jun 2018 15:43:17 -0700 Subject: [PATCH 10/25] Added test to SW_VWCBulkRes for when ideal inputs are entered --- test/test_SW_SoilWater.cc | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/test_SW_SoilWater.cc b/test/test_SW_SoilWater.cc index 1b9eebc09..b6f61c176 100644 --- a/test/test_SW_SoilWater.cc +++ b/test/test_SW_SoilWater.cc @@ -45,6 +45,14 @@ namespace{ // Reset to previous global states Reset_SOILWAT2_after_UnitTest(); + + // test using ideal inputs + clay = .1; + sand = .1; + porosity = .35; + res = SW_VWCBulkRes(fractionGravel, sand, clay, porosity); + double expected = 0.03469862; + EXPECT_LT(fabs(res - expected), .00000001); } // Test the 'SW_SoilWater' function 'SW_SWC_adjust_snow' From 15bf7ade0829c080f77ae48bd67a52209f42b1ac Mon Sep 17 00:00:00 2001 From: nip5 Date: Thu, 14 Jun 2018 09:08:08 -0700 Subject: [PATCH 11/25] Changed all doubles to RealD types --- test/test_SW_SoilWater.cc | 56 +++++++++++++++++------------------ test/test_Times.cc | 61 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 28 deletions(-) create mode 100644 test/test_Times.cc diff --git a/test/test_SW_SoilWater.cc b/test/test_SW_SoilWater.cc index b6f61c176..3ed99a686 100644 --- a/test/test_SW_SoilWater.cc +++ b/test/test_SW_SoilWater.cc @@ -32,7 +32,7 @@ namespace{ // test clay > .6 RealD res = SW_VWCBulkRes(fractionGravel, sand, clay, porosity); - EXPECT_DOUBLE_EQ(res, SW_MISSING); + EXPECT_RealD_EQ(res, SW_MISSING); // Reset to previous global states Reset_SOILWAT2_after_UnitTest(); @@ -41,7 +41,7 @@ namespace{ sand = .04; // test sand < .05 res = SW_VWCBulkRes(fractionGravel, sand, clay, porosity); - EXPECT_DOUBLE_EQ(res, SW_MISSING); + EXPECT_RealD_EQ(res, SW_MISSING); // Reset to previous global states Reset_SOILWAT2_after_UnitTest(); @@ -51,7 +51,7 @@ namespace{ sand = .1; porosity = .35; res = SW_VWCBulkRes(fractionGravel, sand, clay, porosity); - double expected = 0.03469862; + RealD expected = 0.03469862; EXPECT_LT(fabs(res - expected), .00000001); } @@ -59,12 +59,12 @@ namespace{ TEST(SWSoilWaterTest, SWSWCSdjustSnow){ // setup mock variables SW_Site.TminAccu2 = 0; - double temp_min = 0; - double temp_max = 10; - double ppt = 1; - double rain = 1.5; - double snow = 1.5; - double snowmelt = 1.2; + RealD temp_min = 0; + RealD temp_max = 10; + RealD ppt = 1; + RealD rain = 1.5; + RealD snow = 1.5; + RealD snowmelt = 1.2; // since TminAccu2 is < temp_ave, we expect SnowAccu to be 0 and thus rain is ppt - SnowAccu SW_SWC_adjust_snow(temp_min, temp_max, ppt, &rain, &snow, &snowmelt); @@ -77,12 +77,12 @@ namespace{ // Test the 'SW_SoilWater' function 'SW_SWCbulk2SWPmatric' TEST(SWSoilWaterTest, SWSWCbulk2SWPmatric){ - double fractionGravel = 0.2; - double swcBulk = 0; + RealD fractionGravel = 0.2; + RealD swcBulk = 0; LyrIndex n = 1; // test missing and 0 for swc - double res = SW_SWCbulk2SWPmatric(fractionGravel, swcBulk, n); + RealD res = SW_SWCbulk2SWPmatric(fractionGravel, swcBulk, n); EXPECT_EQ(res, 0.0); Reset_SOILWAT2_after_UnitTest(); @@ -97,31 +97,31 @@ namespace{ SW_Site.lyr[n] -> thetasMatric = 1; SW_Site.lyr[n] -> bMatric = 1; res = SW_SWCbulk2SWPmatric(fractionGravel, swcBulk, n); - double resExpect = .000001953; // value calculated in R - double actualExpectDiff = fabs(res - resExpect); + RealD resExpect = .000001953; // value calculated in R + RealD actualExpectDiff = fabs(res - resExpect); EXPECT_LT(actualExpectDiff, .0000002); Reset_SOILWAT2_after_UnitTest(); // when second and third conditional are true fractionGravel = 1; res = SW_SWCbulk2SWPmatric(fractionGravel, swcBulk, n); - EXPECT_DOUBLE_EQ(res, INFINITY); + EXPECT_RealD_EQ(res, INFINITY); } // Test the 'SW_SoilWater' function 'SW_SWPmatric2VWCBulk' TEST(SWSoilWaterTest, SWSWPmatric2VWCBulk){ // set up mock variables - double fractionGravel = .1; - double swpMatric = 15.0; - double p; - double tExpect; - double t; - double actualExpectDiff; - double psisMatric = 18.608013; - double binverseMatric = 0.188608; - double thetaMatric = 41.37; - double testNumber; + RealD fractionGravel = .1; + RealD swpMatric = 15.0; + RealD p; + RealD tExpect; + RealD t; + RealD actualExpectDiff; + RealD psisMatric = 18.608013; + RealD binverseMatric = 0.188608; + RealD thetaMatric = 41.37; + RealD testNumber; LyrIndex n = 0; SW_Site.lyr[n]->thetasMatric = thetaMatric; SW_Site.lyr[n]->psisMatric = psisMatric; @@ -137,8 +137,8 @@ namespace{ // Tolerance for error since division with RealD introcuces some error EXPECT_LT(actualExpectDiff, 0.0000001); - } + Reset_SOILWAT2_after_UnitTest(); } @@ -146,8 +146,8 @@ namespace{ TEST(SWSoilWaterTest, SW_SWCbulk2SWPmatricDeathTest) { // test when swcBulk < 0 LyrIndex n = 1; - double swcBulk = -1; - double fractionGravel = 1; + RealD swcBulk = -1; + RealD fractionGravel = 1; SW_Site.lyr[n] -> width = 1; SW_Site.lyr[n] -> psisMatric = 1; SW_Site.lyr[n] -> thetasMatric = 1; diff --git a/test/test_Times.cc b/test/test_Times.cc new file mode 100644 index 000000000..94642cd63 --- /dev/null +++ b/test/test_Times.cc @@ -0,0 +1,61 @@ +#include "gtest/gtest.h" +#include +#include +#include +#include +#include +#include "../generic.h" +#include "../SW_Sky.h" +#include "../filefuncs.h" +#include "../myMemory.h" +#include "../SW_Defines.h" +#include "../SW_Files.h" +#include "../SW_Model.h" +#include "../SW_Site.h" +#include "../SW_SoilWater.h" +#include "../SW_VegProd.h" +#include "../SW_Site.h" +#include "../SW_Flow_lib.h" +#include "../Times.h" +#include "sw_testhelpers.h" + +namespace{ + // Test the 'Times.c' function 'interpolate_monthlyValues' + TEST(TimesTest, interpolateMonthlyValues){ + // point to the structure that contains cloud coverage monthly values + SW_SKY SW_Sky; + SW_SKY *interpolate = &SW_Sky; + + unsigned int i; + + // set all monthlyValues all 10 + for (i = 0; i < length(interpolate -> cloudcov); i++){ + interpolate -> cloudcov[i] = 10; + } + interpolate_monthlyValues(interpolate -> cloudcov, interpolate -> cloudcov_daily); + + // test final conditional + EXPECT_EQ(interpolate -> cloudcov_daily[1], 10.0); + // should always be zero, regardless of input since dailyValues is never + // changed + EXPECT_EQ(interpolate -> cloudcov_daily[0], 0.0); + // test top conditional + EXPECT_EQ(interpolate -> cloudcov_daily[15], 10.0); + // test middle conditional + EXPECT_EQ(interpolate -> cloudcov_daily[16], 10.0); + + // Reset to previous global states + Reset_SOILWAT2_after_UnitTest(); + + // change first value to 20 and test the changes + interpolate -> cloudcov[0] = 20; + interpolate_monthlyValues(interpolate -> cloudcov, interpolate -> cloudcov_daily); + // calculated by hand + EXPECT_EQ(interpolate -> cloudcov_daily[15], 20); + EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[14], 19.67741935483871); + EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[32], 15); + + // Reset to previous global states + Reset_SOILWAT2_after_UnitTest(); + } +} From d7ef355fa6cd1e7c716e33777a39f06c98b8f849 Mon Sep 17 00:00:00 2001 From: nip5 Date: Thu, 14 Jun 2018 09:25:00 -0700 Subject: [PATCH 12/25] Fixed failing after replace --- {test => testing}/test_SW_Flow_lib_temp.cc | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {test => testing}/test_SW_Flow_lib_temp.cc (100%) diff --git a/test/test_SW_Flow_lib_temp.cc b/testing/test_SW_Flow_lib_temp.cc similarity index 100% rename from test/test_SW_Flow_lib_temp.cc rename to testing/test_SW_Flow_lib_temp.cc From 0d04dd6d880437528c2fad0ebf4486a5e3e8d698 Mon Sep 17 00:00:00 2001 From: nip5 Date: Thu, 14 Jun 2018 09:29:04 -0700 Subject: [PATCH 13/25] Replaced flow_lib_temp test --- {testing => test}/test_SW_Flow_lib_temp.cc | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {testing => test}/test_SW_Flow_lib_temp.cc (100%) diff --git a/testing/test_SW_Flow_lib_temp.cc b/test/test_SW_Flow_lib_temp.cc similarity index 100% rename from testing/test_SW_Flow_lib_temp.cc rename to test/test_SW_Flow_lib_temp.cc From 0ae687c6b2c8958d32243d460080f880bf63a73a Mon Sep 17 00:00:00 2001 From: nip5 Date: Mon, 9 Jul 2018 15:09:50 -0700 Subject: [PATCH 14/25] reverted back to version before changing array indexing --- Times.c | 9 +- test/test_SW_Flow_lib_temp.cc | 160 ++++++++++++++++++++++------------ test/test_Times.cc | 22 +++-- 3 files changed, 122 insertions(+), 69 deletions(-) diff --git a/Times.c b/Times.c index b47ec113a..415671074 100644 --- a/Times.c +++ b/Times.c @@ -271,8 +271,6 @@ TimeInt Time_get_lastdoy_y(TimeInt year) { * pretty independent of the current time, so I'm * removing the preceeding Time_ to shorten the calls in * the code - * - * @returns the month the given day falls under */ TimeInt doy2month(const TimeInt doy) { /* =================================================== */ @@ -359,16 +357,15 @@ void interpolate_monthlyValues(double monthlyValues[], double dailyValues[]) { if (mday == 15) { dailyValues[doy] = monthlyValues[month]; - } - else { + } else { if (mday >= 15) { month2 = (month == Dec) ? Jan : month + 1; sign = 1; - } - else { + } else { month2 = (month == Jan) ? Dec : month - 1; sign = -1; } + dailyValues[doy] = monthlyValues[month] + sign * (monthlyValues[month2] - monthlyValues[month]) / (monthdays[month]) * (mday - 15.); } } diff --git a/test/test_SW_Flow_lib_temp.cc b/test/test_SW_Flow_lib_temp.cc index fab4ae808..b3a3eb2d3 100644 --- a/test/test_SW_Flow_lib_temp.cc +++ b/test/test_SW_Flow_lib_temp.cc @@ -155,10 +155,30 @@ namespace { // Reset to previous global state Reset_SOILWAT2_after_UnitTest(); + } + +// Death tests for soil_temperature_init function + TEST(SWFlowTempTest, SoilTemperatureInitDeathTest) { + + // ***** Test when nlyrs = MAX_LAYERS (SW_Defines.h) ***** // + double deltaX = 15.0, sTconst = 4.15; + unsigned int nlyrs, nRgr = 65, i =0.; + Bool ptr_stError = swFALSE; + nlyrs = MAX_LAYERS; + double width2[] = {5, 5, 5, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 20}; + double oldsTemp2[] = {1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4}; + double bDensity2[nlyrs], fc2[nlyrs], wp2[nlyrs]; + + for (i = 0; i < nlyrs; i++) { + bDensity2[i] = RandNorm(1.,0.5); + fc2[i] = RandNorm(1.5, 0.5); + wp2[i] = fc2[i] - 0.6; // wp will always be less than fc + } + /// test when theMaxDepth is less than soil layer depth - function should fail double theMaxDepth2 = 70.0; - EXPECT_DEATH(soil_temperature_init(bDensity2, width2, oldsTemp2, sTconst, nlyrs, + EXPECT_DEATH_IF_SUPPORTED(soil_temperature_init(bDensity2, width2, oldsTemp2, sTconst, nlyrs, fc2, wp2, deltaX, theMaxDepth2, nRgr, &ptr_stError),"@ generic.c LogError"); // We expect death when max depth < last layer // Reset to previous global state @@ -347,13 +367,15 @@ namespace { EXPECT_EQ(sTempR[nRgr + 1], sTconst); //Check that ptr_stError is FALSE - EXPECT_EQ(ptr_stError, 0); + // EXPECT_EQ(ptr_stError, 0); hashed out as when soil temp fails, and sTempR values are irregular, this turns to one. - //Check that sTempR values are realisitic and pass check in code (between -100 and 100) - for (i = 0; i < nRgr + 1; i++) { - EXPECT_LT(sTempR[i], 100); - EXPECT_GT(sTempR[i], -100); + //Check that when ptr_stError is FALSE, sTempR values are realisitic and pass check in code (between -100 and 100) + if(ptr_stError == 0){ + for (i = 0; i < nRgr + 1; i++) { + EXPECT_LT(sTempR[i], 100); + EXPECT_GT(sTempR[i], -100); } + } // test that the ptr_stError is FALSE when it is supposed to double sTempR2[nRgr + 1], oldsTempR3[nRgr + 1]; @@ -456,11 +478,13 @@ namespace { double swc2[nlyrs2], swc_sat2[nlyrs2], bDensity2[nlyrs2], fc2[nlyrs2], wp2[nlyrs2]; for (i = 0; i < nlyrs2; i++) { - swc2[i] = fmaxf(RandNorm(1.,0.5), 0.1);; - swc_sat2[i] = swc2[i] + 0.5; - bDensity2[i] = fmaxf(RandNorm(1.,0.5), 0.1); - fc2[i] = fmaxf(RandNorm(1.5, 0.5), 0.1); - wp2[i] = fmaxf(fc2[i] - 0.6, 0.1); // wp will always be less than fc + bDensity2[i] = fmaxf(RandNorm(1.,0.5), 0.1); // greater than 0.1 + fc2[i] = fmaxf(RandNorm(1.5, 0.5), 0.1); // greater than 0.1 + swc_sat2[i] = fc2[i] + 0.2; //swc_sat > fc2 + swc2[i] = swc_sat2[i] - 0.3; // swc_sat > swc + wp2[i] = fmaxf(fc2[i] - 0.6, 0.1); // wp < fc + // swprintf("\n i %u, bDensity %f, swc_sat %f, fc %f, swc %f, wp %f", + // i, bDensity2[i], swc_sat2[i], fc2[i], swc2[i], wp2[i] ); } // copy initital oldsTempR values so we can check they have been updated @@ -504,53 +528,81 @@ namespace { EXPECT_NE(surfaceTemp[Today], airTemp + (t1Param1 * pet * (1. - (aet / pet)) * (1. - (biomass / bmLimiter)))); EXPECT_NE(surfaceTemp[Today], surface_temperature_under_snow(airTemp, snow)); - // checks for lyrTemp_to_lyrSoil_temperature - int resultValue2 = sizeof(sTemp3) / sizeof(sTemp3[0]); - EXPECT_EQ(MAX_LAYERS, resultValue2); // when the number of soil layers is MAX_LAYERS, sTemp should be MAX_LAYERS - - for(k = 1; k < nRgr +1; k++) { - if (ptr_stError == 0) { - //printf("\n k %u sTemp3 %f , newoldtemp %f,OLDSTEMPS2 %f", k, sTemp[k], stValues.oldsTempR[k], OLDTEMPS2[k] ); - EXPECT_GT(sTemp3[k], -100); // Sense check - EXPECT_LT(sTemp3[k], 100); // Sense check - // Test that oldsTempR is updated to sTempR for the next day - EXPECT_NE(stValues.oldsTempR[k], OLDTEMPS2[k]); - } + // checks for lyrTemp_to_lyrSoil_temperature + int resultValue2 = sizeof(sTemp3) / sizeof(sTemp3[0]); + EXPECT_EQ(MAX_LAYERS, resultValue2); // when the number of soil layers is MAX_LAYERS, sTemp should be MAX_LAYERS + + for(k = 1; k < nRgr +1; k++) { + if (ptr_stError == swFALSE) { + //swprintf("\n k %u, sTemp3 %f, newoldtemp %f ,OLDSTEMPS2 %f", k, sTemp3[k], stValues.oldsTempR[k], OLDTEMPS2[k] ); + EXPECT_GT(sTemp3[k], -100); // Sense check + EXPECT_LT(sTemp3[k], 100); // Sense check + // Test that oldsTempR is updated to sTempR for the next day + EXPECT_NE(stValues.oldsTempR[k], OLDTEMPS2[k]); } + } + // Reset to global state + Reset_SOILWAT2_after_UnitTest(); - // Reset to global state - Reset_SOILWAT2_after_UnitTest(); + // ptr_stError should be set to TRUE if soil_temperature_today fails (i.e. unrealistic temp values) + double sTemp2[nlyrs], oldsTemp2[nlyrs]; + for (i = 0; i < nlyrs; i++) { + sTemp2[i] = RandNorm(150, 1); + oldsTemp2[i] = RandNorm(150, 1); + } - // Test that function stops or throws an error when it is supposed to. + soil_temperature(airTemp, pet, aet, biomass, swc, swc_sat, bDensity, width, + oldsTemp2, sTemp2, surfaceTemp, nlyrs, fc, wp, bmLimiter, t1Param1, t1Param2, + t1Param3, csParam1, csParam2, shParam, snowdepth, sTconst, deltaX, theMaxDepth, + nRgr, snow, &ptr_stError); - // Should fail when soil_temperature_init fails - i.e. when theMaxDepth < depth of nlyrs + // Check that ptr_stError is TRUE + EXPECT_EQ(ptr_stError, 1); - double theMaxDepth2 = 1; - //swprintf("\n Depth of Layers %f, MaxDepth %f", width[nlyrs - 1], theMaxDepth2); - printf("GOT HERE\n"); - EXPECT_DEATH(soil_temperature(airTemp, pet, aet, biomass, swc, swc_sat, bDensity, width, - oldsTemp, sTemp, surfaceTemp, nlyrs, fc, wp, bmLimiter, t1Param1, t1Param2, - t1Param3, csParam1, csParam2, shParam, snowdepth, sTconst, deltaX, theMaxDepth2, - nRgr, snow, &ptr_stError), "@ generic.c LogError"); - printf("GOT HERE 2\n"); - // ptr_stError should be set to TRUE if soil_temperature_today fails (i.e. unrealistic temp values) - double sTemp2[nlyrs], oldsTemp2[nlyrs]; - for (i = 0; i < nlyrs; i++) { - sTemp2[i] = RandNorm(150, 1); - oldsTemp2[i] = RandNorm(150, 1); - } - printf("GOT HERE 3\n"); - soil_temperature(airTemp, pet, aet, biomass, swc, swc_sat, bDensity, width, - oldsTemp2, sTemp2, surfaceTemp, nlyrs, fc, wp, bmLimiter, t1Param1, t1Param2, - t1Param3, csParam1, csParam2, shParam, snowdepth, sTconst, deltaX, theMaxDepth, - nRgr, snow, &ptr_stError); - printf("GOT HERE 4\n"); - // Check that ptr_stError is TRUE - EXPECT_EQ(ptr_stError, 1); - printf("GOT HERE 5\n"); - //Reset to global state - Reset_SOILWAT2_after_UnitTest(); - printf("GOT HERE 6\n"); + //Reset to global state + Reset_SOILWAT2_after_UnitTest(); } -} + + + // Test that main soil temperature functions fails when it is supposed to + TEST(SWFlowTempTest, MainSoilTemperatureFunctionDeathTest) { + + // ***** Test when nlyrs = MAX_LAYERS ***** // + + // intialize values + unsigned int nRgr = 65, i = 0.; + double airTemp = 25.0, pet = 5.0, aet = 4.0, biomass = 100., surfaceTemp[] = {20.0, 15. ,14.}, + bmLimiter = 300., t1Param1 = 15., t1Param2 = -4., t1Param3 = 600., csParam1 =0.00070, + csParam2 = 0.00030, shParam = 0.18, snowdepth = 5, sTconst = 4.15, deltaX = 15, + snow = 1; + Bool ptr_stError = swFALSE; + + unsigned int nlyrs = MAX_LAYERS; + double width[] = {5, 5, 5, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 20}; + double oldsTemp[] = {1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4}; + double sTemp[] = {1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4}; + double swc[nlyrs], swc_sat[nlyrs], bDensity[nlyrs], fc[nlyrs], wp[nlyrs]; + + for (i = 0; i < nlyrs; i++) { + bDensity[i] = fmaxf(RandNorm(1.,0.5), 0.1); // greater than 0.1 + fc[i] = fmaxf(RandNorm(1.5, 0.5), 0.1); // greater than 0.1 + swc_sat[i] = fc[i] + 0.2; //swc_sat > fc2 + swc[i] = swc_sat[i] - 0.3; // swc_sat > swc + wp[i] = fmaxf(fc[i] - 0.6, 0.1); // wp < fc + } + + // Should fail when soil_temperature_init fails - i.e. when theMaxDepth < depth of nlyrs + + double theMaxDepth = 70; + + EXPECT_DEATH_IF_SUPPORTED(soil_temperature(airTemp, pet, aet, biomass, swc, swc_sat, bDensity, width, + oldsTemp, sTemp, surfaceTemp, nlyrs, fc, wp, bmLimiter, t1Param1, t1Param2, + t1Param3, csParam1, csParam2, shParam, snowdepth, sTconst, deltaX, theMaxDepth, + nRgr, snow, &ptr_stError), "@ generic.c LogError"); + + //Reset to global state + Reset_SOILWAT2_after_UnitTest(); + + } + } diff --git a/test/test_Times.cc b/test/test_Times.cc index 94642cd63..9d0419c3f 100644 --- a/test/test_Times.cc +++ b/test/test_Times.cc @@ -35,14 +35,13 @@ namespace{ interpolate_monthlyValues(interpolate -> cloudcov, interpolate -> cloudcov_daily); // test final conditional - EXPECT_EQ(interpolate -> cloudcov_daily[1], 10.0); - // should always be zero, regardless of input since dailyValues is never - // changed - EXPECT_EQ(interpolate -> cloudcov_daily[0], 0.0); + EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[0], 10.0); // test top conditional - EXPECT_EQ(interpolate -> cloudcov_daily[15], 10.0); + EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[14], 10.0); // test middle conditional - EXPECT_EQ(interpolate -> cloudcov_daily[16], 10.0); + EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[15], 10.0); + EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[365], 10.0); + // Reset to previous global states Reset_SOILWAT2_after_UnitTest(); @@ -51,9 +50,14 @@ namespace{ interpolate -> cloudcov[0] = 20; interpolate_monthlyValues(interpolate -> cloudcov, interpolate -> cloudcov_daily); // calculated by hand - EXPECT_EQ(interpolate -> cloudcov_daily[15], 20); - EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[14], 19.67741935483871); - EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[32], 15); + EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[14], 20); + EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[13], 19.67741935483871); + EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[31], 15); + EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[365], 15.483870967741935); + // change december monthly value to ensure meaningful final interpolation + interpolate -> cloudcov[11] = 12; + interpolate_monthlyValues(interpolate -> cloudcov, interpolate -> cloudcov_daily); + EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[365], 16.38709677419354838709); // Reset to previous global states Reset_SOILWAT2_after_UnitTest(); From aff41170931f49ddf5423bc1cf15337eaf1e13b5 Mon Sep 17 00:00:00 2001 From: nip5 Date: Tue, 10 Jul 2018 08:18:14 -0700 Subject: [PATCH 15/25] reverted to before array indexing changes --- test/test_Times.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/test_Times.cc b/test/test_Times.cc index 9d0419c3f..28c42e346 100644 --- a/test/test_Times.cc +++ b/test/test_Times.cc @@ -35,7 +35,7 @@ namespace{ interpolate_monthlyValues(interpolate -> cloudcov, interpolate -> cloudcov_daily); // test final conditional - EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[0], 10.0); + EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[0], 0); // test top conditional EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[14], 10.0); // test middle conditional @@ -50,14 +50,14 @@ namespace{ interpolate -> cloudcov[0] = 20; interpolate_monthlyValues(interpolate -> cloudcov, interpolate -> cloudcov_daily); // calculated by hand - EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[14], 20); - EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[13], 19.67741935483871); - EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[31], 15); - EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[365], 15.483870967741935); + EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[14], 19.6774193548387); + EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[13], 19.354838709677419); + EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[31], 14.838709677419356); + EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[365], 15.161290322580644); // change december monthly value to ensure meaningful final interpolation interpolate -> cloudcov[11] = 12; interpolate_monthlyValues(interpolate -> cloudcov, interpolate -> cloudcov_daily); - EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[365], 16.38709677419354838709); + EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[365], 16.129032258064516); // Reset to previous global states Reset_SOILWAT2_after_UnitTest(); From cbf7124d4e1f7600c1ad70c086bad26651fbc4d7 Mon Sep 17 00:00:00 2001 From: nip5 Date: Tue, 10 Jul 2018 08:43:36 -0700 Subject: [PATCH 16/25] merged with master --- Times.c | 2 +- googletest | 2 +- test/test_Times.cc | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Times.c b/Times.c index 415671074..51357ea60 100644 --- a/Times.c +++ b/Times.c @@ -365,7 +365,7 @@ void interpolate_monthlyValues(double monthlyValues[], double dailyValues[]) { month2 = (month == Jan) ? Dec : month - 1; sign = -1; } - + dailyValues[doy] = monthlyValues[month] + sign * (monthlyValues[month2] - monthlyValues[month]) / (monthdays[month]) * (mday - 15.); } } diff --git a/googletest b/googletest index ba96d0b11..08d5b1f33 160000 --- a/googletest +++ b/googletest @@ -1 +1 @@ -Subproject commit ba96d0b1161f540656efdaed035b3c062b60e006 +Subproject commit 08d5b1f33af8c18785fb8ca02792b5fac81e248f diff --git a/test/test_Times.cc b/test/test_Times.cc index 28c42e346..13f37050e 100644 --- a/test/test_Times.cc +++ b/test/test_Times.cc @@ -32,9 +32,10 @@ namespace{ for (i = 0; i < length(interpolate -> cloudcov); i++){ interpolate -> cloudcov[i] = 10; } + interpolate -> cloudcov_daily[0] = 0; interpolate_monthlyValues(interpolate -> cloudcov, interpolate -> cloudcov_daily); - // test final conditional + // inperpolate_monthlyValues should not change index 0 EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[0], 0); // test top conditional EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[14], 10.0); @@ -42,7 +43,6 @@ namespace{ EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[15], 10.0); EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[365], 10.0); - // Reset to previous global states Reset_SOILWAT2_after_UnitTest(); From 875785c52bbf1f1fb11702b3b2d18e01e1ee5c06 Mon Sep 17 00:00:00 2001 From: nip5 Date: Tue, 10 Jul 2018 10:01:41 -0700 Subject: [PATCH 17/25] Changed functioncomments to doxygen standards, added base1 note --- Times.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/Times.c b/Times.c index 51357ea60..376e8b6cc 100644 --- a/Times.c +++ b/Times.c @@ -333,21 +333,19 @@ Bool isleapyear(const TimeInt year) { return (Bool) (((yr % 4) == 0) && (((t) != yr) || ((yr % 400) == 0))); } +/** + @brief Linear interpolation of monthly value; monthly values are assumed to representative for the 15th of a month -void interpolate_monthlyValues(double monthlyValues[], double dailyValues[]) { - /********************************************************************** - PURPOSE: linear interpolation of monthly value; monthly values are assumed to representative for the 15th of a month - - HISTORY: - 09/22/2011 (drs) - - INPUTS: - monthlyValues - record with values for each month + @author drs + @date 09/22/2011 - OUTPUTS: - dailyValues - linear interpolation for each day - **********************************************************************/ + @param monthlyValues. Input, record with values for each month + @param dailyValues. Output, linear interpolation for each day + @note dailyValues[0] will always be 0 as the function does not modify it since there is no day 0, furthermore dailyValues is + only sub-setted by base1 objects in the model. + **/ +void interpolate_monthlyValues(double monthlyValues[], double dailyValues[]) { unsigned int doy, mday, month, month2 = NoMonth; double sign = 1.; @@ -365,7 +363,7 @@ void interpolate_monthlyValues(double monthlyValues[], double dailyValues[]) { month2 = (month == Jan) ? Dec : month - 1; sign = -1; } - + dailyValues[doy] = monthlyValues[month] + sign * (monthlyValues[month2] - monthlyValues[month]) / (monthdays[month]) * (mday - 15.); } } From 20cfaf171fe30a47203a207e446331fd85cddb26 Mon Sep 17 00:00:00 2001 From: nip5 Date: Tue, 10 Jul 2018 12:08:08 -0700 Subject: [PATCH 18/25] added expectation in test for the 0th and 1st element in dailyValues --- test/test_Times.cc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/test_Times.cc b/test/test_Times.cc index 13f37050e..e4d70662a 100644 --- a/test/test_Times.cc +++ b/test/test_Times.cc @@ -35,7 +35,8 @@ namespace{ interpolate -> cloudcov_daily[0] = 0; interpolate_monthlyValues(interpolate -> cloudcov, interpolate -> cloudcov_daily); - // inperpolate_monthlyValues should not change index 0 + // inperpolate_monthlyValues should not change index 0 because we used + // base1 indices EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[0], 0); // test top conditional EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[14], 10.0); @@ -50,6 +51,8 @@ namespace{ interpolate -> cloudcov[0] = 20; interpolate_monthlyValues(interpolate -> cloudcov, interpolate -> cloudcov_daily); // calculated by hand + EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[0], 0); + EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[1], 15.483870967741936); EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[14], 19.6774193548387); EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[13], 19.354838709677419); EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[31], 14.838709677419356); From 998601baab95ec1b5171e9965c33bbf61656f040 Mon Sep 17 00:00:00 2001 From: nip5 Date: Tue, 10 Jul 2018 13:27:20 -0700 Subject: [PATCH 19/25] added test for leap years --- test/test_Times.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/test_Times.cc b/test/test_Times.cc index e4d70662a..eb6da5673 100644 --- a/test/test_Times.cc +++ b/test/test_Times.cc @@ -38,6 +38,7 @@ namespace{ // inperpolate_monthlyValues should not change index 0 because we used // base1 indices EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[0], 0); + EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[1], 10.0); // test top conditional EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[14], 10.0); // test middle conditional @@ -57,11 +58,14 @@ namespace{ EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[13], 19.354838709677419); EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[31], 14.838709677419356); EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[365], 15.161290322580644); + // test last day on leap year + EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[MAX_DAYS], 15.161290322580644) // change december monthly value to ensure meaningful final interpolation interpolate -> cloudcov[11] = 12; interpolate_monthlyValues(interpolate -> cloudcov, interpolate -> cloudcov_daily); EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[365], 16.129032258064516); - + // test last day on leap year + EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[MAX_DAYS], 16.129032258064516); // Reset to previous global states Reset_SOILWAT2_after_UnitTest(); } From d75089953eb627929a2e0363e641f9ecebe1f355 Mon Sep 17 00:00:00 2001 From: nip5 Date: Tue, 10 Jul 2018 13:57:28 -0700 Subject: [PATCH 20/25] Fixed tests --- test/test_Times.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_Times.cc b/test/test_Times.cc index eb6da5673..8b90f0c7c 100644 --- a/test/test_Times.cc +++ b/test/test_Times.cc @@ -59,13 +59,13 @@ namespace{ EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[31], 14.838709677419356); EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[365], 15.161290322580644); // test last day on leap year - EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[MAX_DAYS], 15.161290322580644) + EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[MAX_DAYS], 15.483870967741936); // change december monthly value to ensure meaningful final interpolation interpolate -> cloudcov[11] = 12; interpolate_monthlyValues(interpolate -> cloudcov, interpolate -> cloudcov_daily); EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[365], 16.129032258064516); // test last day on leap year - EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[MAX_DAYS], 16.129032258064516); + EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[MAX_DAYS], 16.387096774193548); // Reset to previous global states Reset_SOILWAT2_after_UnitTest(); } From bcbc4b4bb8ee0ac8cad37726417754dd1d73ea37 Mon Sep 17 00:00:00 2001 From: dschlaep Date: Mon, 9 Jul 2018 14:42:25 -0400 Subject: [PATCH 21/25] cherry picked commit 5baeec0 to address issue #206 --- .gitmodules | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitmodules b/.gitmodules index 7e38e13e9..2d07bfe72 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,4 @@ [submodule "googletest"] path = googletest url = https://github.com/google/googletest + branch = master From cf7d9638240f766bab61e8b52f891efd15cac6a1 Mon Sep 17 00:00:00 2001 From: nip5 Date: Wed, 25 Jul 2018 12:36:37 -0700 Subject: [PATCH 22/25] Modify code styling as per unit testing guide --- test/test_SW_SoilWater.cc | 56 +++++++++++++++------------------------ 1 file changed, 21 insertions(+), 35 deletions(-) diff --git a/test/test_SW_SoilWater.cc b/test/test_SW_SoilWater.cc index 79bf44e98..4be628292 100644 --- a/test/test_SW_SoilWater.cc +++ b/test/test_SW_SoilWater.cc @@ -27,20 +27,17 @@ namespace{ // Test the 'SW_SoilWater' function 'SW_VWCBulkRes' TEST(SWSoilWaterTest, SWVWCBulkRes){ //declare mock INPUTS - RealD fractionGravel = .1; - RealD clay = .7; - RealD sand = .2; - RealD porosity = 1; + RealD fractionGravel = .1, clay = .7, sand = .2, porosity = 1; // test clay > .6 RealD res = SW_VWCBulkRes(fractionGravel, sand, clay, porosity); EXPECT_DOUBLE_EQ(res, SW_MISSING); - // Reset to previous global states Reset_SOILWAT2_after_UnitTest(); clay = .5; sand = .04; + // test sand < .05 res = SW_VWCBulkRes(fractionGravel, sand, clay, porosity); EXPECT_DOUBLE_EQ(res, SW_MISSING); @@ -53,45 +50,45 @@ namespace{ TEST(SWSoilWaterTest, SWSWCSdjustSnow){ // setup mock variables SW_Site.TminAccu2 = 0; - RealD temp_min = 0; - RealD temp_max = 10; - RealD ppt = 1; - RealD rain = 1.5; - RealD snow = 1.5; - RealD snowmelt = 1.2; - // test 1, since TminAccu2 is < temp_ave, we expect SnowAccu to be 0 and thus rain is ppt - SnowAccu + RealD temp_min = 0, temp_max = 10, ppt = 1, rain = 1.5, snow = 1.5, + snowmelt = 1.2; + + // test 1, since TminAccu2 is < temp_ave, we expect SnowAccu to be 0 and thus + // rain is ppt - SnowAccu SW_SWC_adjust_snow(temp_min, temp_max, ppt, &rain, &snow, &snowmelt); EXPECT_EQ(rain, 1); EXPECT_EQ(snow, 0); - // Reset to previous global states Reset_SOILWAT2_after_UnitTest(); } // Test the 'SW_SoilWater' function 'SW_SWCbulk2SWPmatric' TEST(SWSoilWaterTest, SWSWCbulk2SWPmatric){ - RealD fractionGravel = 0.2; - RealD swcBulk = 0; + RealD fractionGravel = 0.2, swcBulk = 0; LyrIndex n = 1; - // test missing and 0 for swc + + // test 0 for swc RealD res = SW_SWCbulk2SWPmatric(fractionGravel, swcBulk, n); EXPECT_EQ(res, 0.0); Reset_SOILWAT2_after_UnitTest(); + + // test missing for swc swcBulk = 999; + res = SW_SWCbulk2SWPmatric(fractionGravel, swcBulk, n); EXPECT_EQ(res, 0.0); - // test swp val + // test swp val math swcBulk = 4; SW_Site.lyr[n] -> width = 1; SW_Site.lyr[n] -> psisMatric = 1; SW_Site.lyr[n] -> thetasMatric = 1; SW_Site.lyr[n] -> bMatric = 1; + res = SW_SWCbulk2SWPmatric(fractionGravel, swcBulk, n); RealD resExpect = .00013310902; // did math by hand to get this value RealD actualExpectDiff = fabs(res - resExpect); EXPECT_LT(actualExpectDiff, .0002); - // Reset to previous global states Reset_SOILWAT2_after_UnitTest(); } @@ -99,36 +96,25 @@ namespace{ // Test the 'SW_SoilWater' function 'SW_SWPmatric2VWCBulk' TEST(SWSoilWaterTest, SWSWPmatric2VWCBulk){ // set up mock variables - RealD fractionGravel; - RealD swpMatric = 15.0; - RealD p; - RealD tExpect; - RealD t; - RealD psisMatric = 18.608013; - RealD binverseMatric = 0.188608; - RealD thetaMatric = 41.37; - + RealD p, tExpect, t, fractionGravel; + RealD swpMatric = 15.0, psisMatric = 18.608013, binverseMatric = 0.188608, + thetaMatric = 41.37; int i; LyrIndex n = 0; - SW_Site.lyr[n]->thetasMatric = thetaMatric; SW_Site.lyr[n]->psisMatric = psisMatric; SW_Site.lyr[n]->bMatric = binverseMatric; - - p = thetaMatric * 0.01 * \ - powe(psisMatric / (swpMatric * BARCONV), binverseMatric); + p = 0.11656662532982573; // done by hand // run tests for gravel fractions on the interval [.0, .8], step .05 - for (i = 0; i <= 16; i++) - { + for (i = 0; i <= 16; i++){ fractionGravel = i / 20.; + tExpect = p * (1 - fractionGravel); t = SW_SWPmatric2VWCBulk(fractionGravel, swpMatric, n); - // Tolerance for error since division with RealD introcuces some error EXPECT_NEAR(t, tExpect, tol); } - // Reset to previous global states Reset_SOILWAT2_after_UnitTest(); } From 91530cb12a02270789c59d28dcd06903fe27d979 Mon Sep 17 00:00:00 2001 From: nip5 Date: Wed, 25 Jul 2018 13:42:34 -0700 Subject: [PATCH 23/25] Remerge with branch #115 --- test/test_SW_SoilWater.cc | 75 +++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 35 deletions(-) diff --git a/test/test_SW_SoilWater.cc b/test/test_SW_SoilWater.cc index 4be628292..4d8f29f46 100644 --- a/test/test_SW_SoilWater.cc +++ b/test/test_SW_SoilWater.cc @@ -22,22 +22,23 @@ extern SW_SITE SW_Site; extern SW_VEGPROD SW_VegProd; namespace{ - float tol = 1e-6; - // Test the 'SW_SoilWater' function 'SW_VWCBulkRes' TEST(SWSoilWaterTest, SWVWCBulkRes){ //declare mock INPUTS - RealD fractionGravel = .1, clay = .7, sand = .2, porosity = 1; + RealD fractionGravel = .1; + RealD clay = .7; + RealD sand = .2; + RealD porosity = 1; // test clay > .6 RealD res = SW_VWCBulkRes(fractionGravel, sand, clay, porosity); EXPECT_DOUBLE_EQ(res, SW_MISSING); + // Reset to previous global states Reset_SOILWAT2_after_UnitTest(); clay = .5; sand = .04; - // test sand < .05 res = SW_VWCBulkRes(fractionGravel, sand, clay, porosity); EXPECT_DOUBLE_EQ(res, SW_MISSING); @@ -50,72 +51,76 @@ namespace{ TEST(SWSoilWaterTest, SWSWCSdjustSnow){ // setup mock variables SW_Site.TminAccu2 = 0; - RealD temp_min = 0, temp_max = 10, ppt = 1, rain = 1.5, snow = 1.5, - snowmelt = 1.2; - - // test 1, since TminAccu2 is < temp_ave, we expect SnowAccu to be 0 and thus - // rain is ppt - SnowAccu + RealD temp_min = 0; + RealD temp_max = 10; + RealD ppt = 1; + RealD rain = 1.5; + RealD snow = 1.5; + RealD snowmelt = 1.2; + // test 1, since TminAccu2 is < temp_ave, we expect SnowAccu to be 0 and thus rain is ppt - SnowAccu SW_SWC_adjust_snow(temp_min, temp_max, ppt, &rain, &snow, &snowmelt); EXPECT_EQ(rain, 1); EXPECT_EQ(snow, 0); - // Reset to previous global states Reset_SOILWAT2_after_UnitTest(); + } // Test the 'SW_SoilWater' function 'SW_SWCbulk2SWPmatric' TEST(SWSoilWaterTest, SWSWCbulk2SWPmatric){ - RealD fractionGravel = 0.2, swcBulk = 0; + RealD fractionGravel = 0.2; + RealD swcBulk = 0; LyrIndex n = 1; - - // test 0 for swc + // test missing and 0 for swc RealD res = SW_SWCbulk2SWPmatric(fractionGravel, swcBulk, n); EXPECT_EQ(res, 0.0); Reset_SOILWAT2_after_UnitTest(); - - // test missing for swc swcBulk = 999; - res = SW_SWCbulk2SWPmatric(fractionGravel, swcBulk, n); EXPECT_EQ(res, 0.0); + Reset_SOILWAT2_after_UnitTest(); - // test swp val math + // test swp val swcBulk = 4; SW_Site.lyr[n] -> width = 1; SW_Site.lyr[n] -> psisMatric = 1; SW_Site.lyr[n] -> thetasMatric = 1; SW_Site.lyr[n] -> bMatric = 1; - res = SW_SWCbulk2SWPmatric(fractionGravel, swcBulk, n); RealD resExpect = .00013310902; // did math by hand to get this value RealD actualExpectDiff = fabs(res - resExpect); EXPECT_LT(actualExpectDiff, .0002); - // Reset to previous global states - Reset_SOILWAT2_after_UnitTest(); } // Test the 'SW_SoilWater' function 'SW_SWPmatric2VWCBulk' TEST(SWSoilWaterTest, SWSWPmatric2VWCBulk){ // set up mock variables - RealD p, tExpect, t, fractionGravel; - RealD swpMatric = 15.0, psisMatric = 18.608013, binverseMatric = 0.188608, - thetaMatric = 41.37; - int i; + RealD fractionGravel = .1; + RealD swpMatric = 15.0; + RealD p; + RealD tExpect; + RealD t; + RealD actualExpectDiff; + RealD psisMatric = 18.608013; + RealD binverseMatric = 0.188608; + RealD thetaMatric = 41.37; + RealD testNumber; LyrIndex n = 0; SW_Site.lyr[n]->thetasMatric = thetaMatric; - SW_Site.lyr[n]->psisMatric = psisMatric; - SW_Site.lyr[n]->bMatric = binverseMatric; - p = 0.11656662532982573; // done by hand - - // run tests for gravel fractions on the interval [.0, .8], step .05 - for (i = 0; i <= 16; i++){ - fractionGravel = i / 20.; - - tExpect = p * (1 - fractionGravel); + SW_Site.lyr[n]->psisMatric = psisMatric; + SW_Site.lyr[n]->bMatric = binverseMatric; + + // run tests for gravel fractions on the interval [.05, .8], step .05 + for(testNumber = 1; testNumber <= 16; testNumber++){ + fractionGravel = (testNumber / 20); + p = powe(psisMatric / (swpMatric * BARCONV), binverseMatric); + tExpect = thetaMatric * p * 0.01 * (1 - fractionGravel); t = SW_SWPmatric2VWCBulk(fractionGravel, swpMatric, n); + actualExpectDiff = fabs(t - tExpect); + // Tolerance for error since division with RealD introcuces some error - EXPECT_NEAR(t, tExpect, tol); + EXPECT_LT(actualExpectDiff, 0.0000001); + } - // Reset to previous global states Reset_SOILWAT2_after_UnitTest(); } } From da922e4cdf2636a1f7b25048eabbc3b254d688d0 Mon Sep 17 00:00:00 2001 From: nip5 Date: Wed, 25 Jul 2018 13:49:39 -0700 Subject: [PATCH 24/25] Modify code style as per unit testing guidelines --- test/test_Times.cc | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/test/test_Times.cc b/test/test_Times.cc index 8b90f0c7c..123ddd336 100644 --- a/test/test_Times.cc +++ b/test/test_Times.cc @@ -25,31 +25,30 @@ namespace{ // point to the structure that contains cloud coverage monthly values SW_SKY SW_Sky; SW_SKY *interpolate = &SW_Sky; - unsigned int i; - // set all monthlyValues all 10 for (i = 0; i < length(interpolate -> cloudcov); i++){ interpolate -> cloudcov[i] = 10; } interpolate -> cloudcov_daily[0] = 0; - interpolate_monthlyValues(interpolate -> cloudcov, interpolate -> cloudcov_daily); + // test various obtained values + interpolate_monthlyValues(interpolate -> cloudcov, interpolate -> cloudcov_daily); // inperpolate_monthlyValues should not change index 0 because we used // base1 indices EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[0], 0); EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[1], 10.0); - // test top conditional + // test top conditional, day < 15 EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[14], 10.0); - // test middle conditional + // test middle conditional, day >= 15 EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[15], 10.0); EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[365], 10.0); - // Reset to previous global states Reset_SOILWAT2_after_UnitTest(); // change first value to 20 and test the changes interpolate -> cloudcov[0] = 20; + interpolate_monthlyValues(interpolate -> cloudcov, interpolate -> cloudcov_daily); // calculated by hand EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[0], 0); From 833cc72ebaafe680a5281a013a447cb2787c7001 Mon Sep 17 00:00:00 2001 From: nip5 Date: Thu, 26 Jul 2018 11:13:31 -0700 Subject: [PATCH 25/25] Modify spacing and comments for clearity --- test/test_Times.cc | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/test_Times.cc b/test/test_Times.cc index 123ddd336..059fff14a 100644 --- a/test/test_Times.cc +++ b/test/test_Times.cc @@ -26,7 +26,8 @@ namespace{ SW_SKY SW_Sky; SW_SKY *interpolate = &SW_Sky; unsigned int i; - // set all monthlyValues all 10 + // set all monthlyValues all 10 to ensure we know what monthlyValues + // are being used to interpolate dailyValues for (i = 0; i < length(interpolate -> cloudcov); i++){ interpolate -> cloudcov[i] = 10; } @@ -46,7 +47,7 @@ namespace{ // Reset to previous global states Reset_SOILWAT2_after_UnitTest(); - // change first value to 20 and test the changes + // change first value to 20 and test the changes to the interpolated daily values interpolate -> cloudcov[0] = 20; interpolate_monthlyValues(interpolate -> cloudcov, interpolate -> cloudcov_daily); @@ -59,8 +60,10 @@ namespace{ EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[365], 15.161290322580644); // test last day on leap year EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[MAX_DAYS], 15.483870967741936); + // change december monthly value to ensure meaningful final interpolation interpolate -> cloudcov[11] = 12; + interpolate_monthlyValues(interpolate -> cloudcov, interpolate -> cloudcov_daily); EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[365], 16.129032258064516); // test last day on leap year