Exemple #1
0
int main() {
Coef    coefs;   // a, b and c for the quadratic eqaution
Root    roots;   // Root struct with x1 and x1
int ret;     // return value from qsolve_roots
// double  a, b, c; // scratch variables
double  x1, x2;  // scratch variables


// initialize the unit testing framework
cunit_init();

//test the max value for b
coefs.a = 1.0;
coefs.b = DBL_MAX/2;
coefs.c = 1.0;
ret = qsolve_roots(&coefs, &roots);
assert_eq("ret",ret,-4);

//test the max value for a
x1 = 1.0;
x2 = 3.3;
coefs.a = DBL_MAX;
coefs.b = -x1 + -x2;
coefs.c = x1*x2;
ret = qsolve_roots(&coefs, &roots);
assert_eq("ret",ret,-2);

//test the max value for c
x1 = 3.1;
x2 = 3.3;
coefs.a = 1.0;
coefs.b = -x1 + -x2;
coefs.c = DBL_MAX;
ret = qsolve_roots(&coefs, &roots);
assert_eq("ret",ret,-2);


exit(0);
}
Exemple #2
0
int main() {
Coef    coefs;   // a, b and c for the quadratic eqaution
Root    roots;   // Root struct with x1 and x1
int ret;     // return value from qsolve_roots


// initialize the unit testing framework
cunit_init();

//  a = 0, not a true qudratic. should return -1
coefs.a = 0;
coefs.b = 2.0;
coefs.c = 1.0;
ret = qsolve_roots(&coefs, &roots);
assert_eq("ret",ret,-1);

// d = b^2 - 4ac < 0; no real roots. should return -2
coefs.a = 6.4;
coefs.b = 5.0;
coefs.c = 7.7;
ret = qsolve_roots(&coefs, &roots);
assert_eq("ret",ret,-2);

// NAN input; should return -3
coefs.a = NAN;
coefs.b = 2.0;
coefs.c = 1.0;
ret = qsolve_roots(&coefs, &roots);
assert_eq("ret",ret,-3);

// INF input; should return -3
coefs.a = 2.0;
coefs.b = INFINITY;
coefs.c = 1.0;
ret = qsolve_roots(&coefs, &roots);
assert_eq("ret",ret,-3);

exit(0);
}
int main() {
Coef	coefs;   // a, b and c for the quadratic eqaution
Root	roots;   // Root struct with x1 and x1
int	ret;     // return value from qsolve_roots
double  a, b, c; // scratch variables
double  x1, x2;  // scratch variables
int	count;	 // number of times the mock object qsolve_sqrt(0 is called.
double	x;	 // arguement passed to qsolve_sqrt()
char	str[100];// messsaage for assert() 
double	d;	 // scratch for discriminate
double	sqrtd;	 // scratch for sqrt(d) 

fprintf(stderr, "in t2\n" );
// initialize the unit testing framework
cunit_init();

//  A single test using the mock object for qsolve_sqrt()
//  Should have x1 = 3.0 and x2 = 1.0, except for roundoff
//   We expect  d =  b^2 - 4*a*c =  4.0 and 
//              sqrt(d) returned should be 2.0
//  In this special case we will ytry to have the error actually == 0.0.,
//  both in the qsove_sqrt(I) mock object and for the returned roots!
//  This is not often the case.
mock_setup_qsolve_sqrt( 4.0, 2.0, 0.0);
//
//// This looks like a check of qsolve_sqart()
coefs.a = 1.0;
coefs.b = -4.0;
coefs.c = 3.0;
ret = qsolve_roots(&coefs, &roots);
assert_eq("ret",ret,2);
assert_feq("x1",roots.x1,3.0);
assert_feq("x2",roots.x2,1.0);
//
ret= mock_check_qsolve_sqrt( &count, &x);
snprintf(str, 99, "count ret = %d x =%20.61e", count, x);
assert_eq(str, ret , 1);


//
// A "good" unit test, need to allow for round off!
// qsolve_roots() passes this one. ;-)
// This allows about one base 10 least significant digit of error
// (x - x1)*(x - x2) = 0
x1 = 3.1;
x2 = 3.3;
a = coefs.a = 1.0;
b = coefs.b = -x1 + -x2;
c = coefs.c = x1*x2;

d = b*b - 4.0*a*c;
sqrtd = sqrt(d);

//
mock_setup_qsolve_sqrt( d, sqrtd, cunit_dmacheps*2.0*d);
//
ret = qsolve_roots(&coefs, &roots);
assert_eq("ret",ret,2);
assert_feqrerr("x1",roots.x1, x2, 2.0*cunit_dmacheps*3.3 );
assert_feqrerr("x2",roots.x2, x1, 2.0*cunit_dmacheps*3.1 );
//
ret= mock_check_qsolve_sqrt( &count, &x);
snprintf(str, 99, "count ret = %d x =%20.61e", count, x);
assert_eq(str, ret , 1);

exit(0);
}