为什么我的测试用例会一直失败?

时间:2013-10-08 07:58:15

标签: c testing

我写了一些函数,我已多次检查,看看我是否写错了公式或任何已定义的变量,但这些似乎是正确的。 我的导师提供的测试案例因此,我认为必须能够工作!我不确定我的代码中的问题在哪里或者什么。顺便说一句,直到最后一个函数的所有函数的测试用例都通过了,它只是最后一个函数,即给出我问题的estimateYield。

注意:我已经宣布了很多常量变量,其中一些似乎现在似乎尚未使用,但你可以忽略它。

#include "grove.h"
#include <math.h>
#include <stdlib.h>

#define SOILQUALACONST 10 /* Number subtracted from both x and y in typeA. */
#define SOILQUALBCONST 10 /* Number subtracted from both x and y in typeB. */
#define SUNEXPXTERM 8   /* Number you subtract from x in exponent.*/
#define SUNEXPDIV1 10   /* First denominator term in first fraction in exp.*/
#define SUNEXPYTERM 12   /* Number you subtract from y in exponent.*/
#define SUNEXPDIV2 5   /* Second denominator term in second fraction in exp.*/
#define SUNEXPEMULT 10   /* The constant you are multiplying e^(exp.) by.*/
#define IRRIEXPONUM 10   /* The numerator in irrigation exposure function.*/
#define ESTYIELDNUM1 7   /* First term in fraction part of estimated yield.*/
#define ESTYIELDNUM2 7   /* Last term in fraction part of estimated yield.*/

double soilQuality(int x, int y) {
   double typeA, typeB, soilQual;

   typeA = 1 + (sqrt((pow(x - SOILQUALACONST, 2)) + (pow(y - SOILQUALACONST, 2))      * (1.0)));
   typeB = (1 + ((abs(x - SOILQUALBCONST) + abs(y - SOILQUALBCONST))/(2.0)));
   soilQual = (((x + y) % 2) * typeB) + ((1 - ((x + y) % 2)) * typeA);

   return soilQual;
}

double sunExposure(int x, int y) {
   double exponent, sunexp;

   exponent = (-0.5) * (((pow(x - SUNEXPXTERM, 2))/(SUNEXPDIV1)) + ((pow(y -
      SUNEXPYTERM, 2))/(SUNEXPDIV2)));
   sunexp = SUNEXPEMULT * exp(exponent);

   return sunexp;
}

double irrigationExposure(int x, int y) {
   double denominator, waterexp;

   denominator = (1 + abs(x - y)) * (1.0);
   waterexp = ((IRRIEXPONUM)/(denominator));

   return waterexp;
}

double estimateYield(int x, int y) {
   double waterexp, soilqual, sunexp, numerator, estyield;

   waterexp = irrigationExposure(x, y);
   soilqual = soilQuality(x, y);
   sunexp = sunExposure(x, y);

   numerator = ((ESTYIELDNUM1) - (abs(waterexp - ESTYIELDNUM2))) + 1;
   estyield = (soilqual) * (sunexp) * ((numerator)/(2.0));

   return estyield;
}

基本上,最后一个函数的一些测试用例一直在失败,我似乎无法弄清楚原因。以下是我的教师所涉及的测试用例:

#include <stdio.h>
#include "grove.h"
#include "checkit.h"

int main(){

   checkit_double(estimateYield(3,3), 0.023697  );
   checkit_double(estimateYield(1,19),0.067322 );
   checkit_double(estimateYield(7,8),  20.165240 );
   checkit_double(estimateYield(12,3),  0.007501);
   checkit_double(estimateYield(4,17), 2.371061);

   return(0);
}

以下是我运行时的结果:

Test passed on line 6.
Test FAILED on line 7.  estimateYield(1,19) is 0.088215, expected 0.067322.
Test passed on line 8.
Test passed on line 9.
Test FAILED on line 10.  estimateYield(4,17) is 2.766238, expected 2.371061.

如果你需要它,估计字段的公式是:

soilQuality(x,y)* sunExposure(x,y)*((7-(abs(irrigationExposure(x,y) - 7))+ 1)/(2))

1 个答案:

答案 0 :(得分:4)

问题在于:

numerator = ((ESTYIELDNUM1) - (abs(waterexp - ESTYIELDNUM2))) + 1;

您使用的abs整数函数值为double,因此会得到截断的int结果。将此行更改为使用fabs似乎可以解决问题:

numerator = ((ESTYIELDNUM1) - (fabs(waterexp - ESTYIELDNUM2))) + 1;

这是混合整数和浮点运算时的常见问题。我建议你制作所有常量和int变量double,并在整个过程中使用浮点运算(和fabs()!)。

另请注意gcc -Wall -Wconversion ...会为您带来错误:

$ gcc -Wall -Wconversion soil.c 
soil.c:58:48: warning: implicit conversion turns floating-point number into integer: 'double' to 'int' [-Wconversion]
   numerator = ((ESTYIELDNUM1) - (abs(waterexp - ESTYIELDNUM2))) + 1;
                                  ~~~ ~~~~~~~~~^~~~~~~~~~~~~~