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

Feature enhancement#118 times unit test #192

Merged
merged 38 commits into from
Jul 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
fce2a94
Added testing file and made interpolate_monthlyValues testable
nip5 Jun 1, 2018
39d3bc3
Added macro for length of array
nip5 Jun 1, 2018
b4a65b2
Removed got here print statements for debugging
nip5 Jun 5, 2018
9327536
Merge branch 'master' into Enchancement#115_SW_SoilWater_UnitTests
nip5 Jun 8, 2018
ccfe28a
Added more tests to cover all conditionals of interpolate_monthlyValues
nip5 Jun 8, 2018
788f322
Increased accuracy in SWSWCbulk2SWPmatric test by calculating val in R
nip5 Jun 11, 2018
80364e3
Merged with branch Enhancement#171 to get updates to test_SW_Flow_lib…
nip5 Jun 12, 2018
75d516f
Added testing for snowmelt in SWSWCSadjustSnow
nip5 Jun 12, 2018
edd2201
Implemented full coverage for function SW_SWCbulk2SWPmatric
nip5 Jun 12, 2018
c21121f
Modified unit tests to work with structure pointer and no return
nip5 Jun 13, 2018
9ee7d98
Implemented more tests for different scenarios
nip5 Jun 13, 2018
b830b95
Resolved conflicts with master
nip5 Jun 13, 2018
2ef7de7
Merge branch 'Enhancement#171_SW_VWCBulkRes_InvalidVals' into feature…
nip5 Jun 13, 2018
32462ee
Merge branch 'Enhancement#171_SW_VWCBulkRes_InvalidVals' into Enchanc…
nip5 Jun 13, 2018
e6dec84
Added test to SW_VWCBulkRes for when ideal inputs are entered
nip5 Jun 13, 2018
15bf7ad
Changed all doubles to RealD types
nip5 Jun 14, 2018
4ebfd27
#
nip5 Jun 14, 2018
1dc6951
Merge branch 'feature_Enhancement#118_TimesUnitTests' into Enchanceme…
nip5 Jun 14, 2018
d7ef355
Fixed failing after replace
nip5 Jun 14, 2018
0d04dd6
Replaced flow_lib_temp test
nip5 Jun 14, 2018
0ae687c
reverted back to version before changing array indexing
nip5 Jul 9, 2018
81dd645
Merge branch 'feature_Enhancement#118_TimesUnitTests' of https://gith…
nip5 Jul 9, 2018
aff4117
reverted to before array indexing changes
nip5 Jul 10, 2018
bf7569f
Merge branch 'master' into feature_Enhancement#118_TimesUnitTests
nip5 Jul 10, 2018
cbf7124
merged with master
nip5 Jul 10, 2018
875785c
Changed functioncomments to doxygen standards, added base1 note
nip5 Jul 10, 2018
bf5483b
Merge branch 'master' into feature_Enhancement#118_TimesUnitTests
nip5 Jul 10, 2018
20cfaf1
added expectation in test for the 0th and 1st element in dailyValues
nip5 Jul 10, 2018
998601b
added test for leap years
nip5 Jul 10, 2018
d750899
Fixed tests
nip5 Jul 10, 2018
bcbc4b4
cherry picked commit 5baeec0 to address issue #206
dschlaep Jul 9, 2018
acc9b44
Merge branch 'master' into Enchancement#115_SW_SoilWater_UnitTests
nip5 Jul 25, 2018
5360de4
Merge branch 'feature_Enhancement#118_TimesUnitTests' into Enchanceme…
nip5 Jul 25, 2018
54549fa
remerged with branches #118 and master
nip5 Jul 25, 2018
cf7d963
Modify code styling as per unit testing guide
nip5 Jul 25, 2018
91530cb
Remerge with branch #115
nip5 Jul 25, 2018
da922e4
Modify code style as per unit testing guidelines
nip5 Jul 25, 2018
833cc72
Modify spacing and comments for clearity
nip5 Jul 26, 2018
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
4 changes: 2 additions & 2 deletions SW_SoilWater.c
Original file line number Diff line number Diff line change
Expand Up @@ -858,8 +858,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.;

Expand Down
22 changes: 10 additions & 12 deletions Times.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
@author drs
@date 09/22/2011

INPUTS:
monthlyValues - record with values for each month

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.;

Expand Down
1 change: 1 addition & 0 deletions generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,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
Expand Down
2 changes: 1 addition & 1 deletion googletest
Submodule googletest updated 42 files
+3 −1 .travis.yml
+8 −0 ci/travis.sh
+93 −112 googlemock/docs/CookBook.md
+73 −17 googlemock/include/gmock/gmock-actions.h
+261 −67 googlemock/include/gmock/gmock-generated-actions.h
+44 −5 googlemock/include/gmock/gmock-generated-actions.h.pump
+650 −366 googlemock/include/gmock/gmock-generated-function-mockers.h
+76 −20 googlemock/include/gmock/gmock-generated-function-mockers.h.pump
+317 −250 googlemock/include/gmock/gmock-generated-matchers.h
+18 −16 googlemock/include/gmock/gmock-generated-matchers.h.pump
+87 −26 googlemock/include/gmock/gmock-generated-nice-strict.h
+33 −16 googlemock/include/gmock/gmock-generated-nice-strict.h.pump
+281 −159 googlemock/include/gmock/gmock-matchers.h
+17 −0 googlemock/include/gmock/gmock-more-matchers.h
+128 −58 googlemock/include/gmock/gmock-spec-builders.h
+1 −1 googlemock/include/gmock/internal/custom/gmock-generated-actions.h.pump
+30 −1 googlemock/include/gmock/internal/gmock-internal-utils.h
+3 −1 googlemock/src/gmock-internal-utils.cc
+52 −6 googlemock/src/gmock-matchers.cc
+95 −42 googlemock/src/gmock-spec-builders.cc
+161 −1 googlemock/test/gmock-actions_test.cc
+8 −6 googlemock/test/gmock-generated-actions_test.cc
+23 −0 googlemock/test/gmock-generated-function-mockers_test.cc
+11 −9 googlemock/test/gmock-generated-internal-utils_test.cc
+59 −4 googlemock/test/gmock-generated-matchers_test.cc
+1,121 −75 googlemock/test/gmock-matchers_test.cc
+8 −9 googlemock/test/gmock-more-actions_test.cc
+68 −3 googlemock/test/gmock-nice-strict_test.cc
+73 −3 googlemock/test/gmock-spec-builders_test.cc
+15 −0 googlemock/test/gmock_output_test_.cc
+8 −1 googlemock/test/gmock_output_test_golden.txt
+1 −1 googletest/include/gtest/gtest-death-test.h
+15 −0 googletest/include/gtest/gtest.h
+2 −2 googletest/include/gtest/internal/gtest-internal.h
+29 −30 googletest/include/gtest/internal/gtest-port.h
+202 −11 googletest/src/gtest-death-test.cc
+24 −0 googletest/src/gtest-port.cc
+13 −4 googletest/src/gtest.cc
+4 −3 googletest/test/gtest-death-test_test.cc
+6 −0 googletest/test/gtest-options_test.cc
+4 −4 googletest/test/gtest-port_test.cc
+23 −1 googletest/test/gtest_unittest.cc
41 changes: 16 additions & 25 deletions test/test_SW_SoilWater.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ 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
Expand Down Expand Up @@ -63,9 +61,8 @@ namespace{
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'
Expand All @@ -80,6 +77,7 @@ namespace{
swcBulk = 999;
res = SW_SWCbulk2SWPmatric(fractionGravel, swcBulk, n);
EXPECT_EQ(res, 0.0);
Reset_SOILWAT2_after_UnitTest();

// test swp val
swcBulk = 4;
Expand All @@ -91,45 +89,38 @@ namespace{
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 fractionGravel;
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;

int i;
RealD testNumber;
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);

// 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();
}
}
74 changes: 74 additions & 0 deletions test/test_Times.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "gtest/gtest.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#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 to ensure we know what monthlyValues
// are being used to interpolate dailyValues
for (i = 0; i < length(interpolate -> cloudcov); i++){
interpolate -> cloudcov[i] = 10;
}
interpolate -> cloudcov_daily[0] = 0;

// 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, day < 15
EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[14], 10.0);
// 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 to the interpolated daily values
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);
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
EXPECT_DOUBLE_EQ(interpolate -> cloudcov_daily[MAX_DAYS], 16.387096774193548);
// Reset to previous global states
Reset_SOILWAT2_after_UnitTest();
}
}