int main(int argc, char *argvp[]){ /* initialize cunit framework */ cunit_init(); /* temporary variables */ ROOTS *roots; /* struct to be returned by solveQ */ COEFS *coefs = malloc(sizeof(COEFS)); /* struct to be passed to solveQ */ double x1, x2; /* scratch variables */ /* test mock object */ setExpect(3.0); assert_feq("mockSqrt", 3.0, mockSqrt()); /* test mock object for correctness through repetitive calls */ setExpect(7.0); assert_feq("mockSqrt", 7.0, mockSqrt()); assert_feq("mockSqrt", 7.0, mockSqrt()); assert_feq("mockSqrt", 7.0, mockSqrt()); /* * Test for equation with real solutions * Allows for one base 10 least significant digit of error */ setExpect(.2); x1 = 3.1; x2 = 3.3; coefs->a = 1.0; coefs->b = -x1 + -x2; coefs->c = x1 * x2; roots = solveQ(coefs); assert_eq("ret", roots->real, 1); assert_feqrerr("x1", roots->root[0], x2, 10.0 * cunit_dmacheps); assert_feqrerr("x2", roots->root[1], x1, 10.0 * cunit_dmacheps); /* * Test for equation with one real solution * Allows for one base 10 least significant digit of error */ setExpect(0); x1 = 3; x2 = 3; coefs->a = 1.0; coefs->b = -x1 + -x2; coefs->c = x1 * x2; roots = solveQ(coefs); assert_eq("ret", roots->equal, 1); assert_feqrerr("x1", roots->root[0], x2, 10.0 * cunit_dmacheps); assert_feqrerr("x2", roots->root[1], x1, 10.0 * cunit_dmacheps); /* * Test for equation with complex solutions * Allows for one base 10 least significant digit of error */ setExpect(1.732050808); x1 = -0.633974596; x2 = -2.366025404; coefs->a = 1.0; coefs->b = -x1 + -x2; coefs->c = 3; roots = solveQ(coefs); assert_eq("ret", roots->complex, 1); assert_feqrerr("x1", roots->root[0] + roots->root[1], x1, 10.0 * cunit_dmacheps); assert_feqrerr("x2", roots->root[0] - roots->root[1], x2, 10.0 * cunit_dmacheps); /* notify testing complete */ printf("Test t3 complete\n"); return 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); }