示例#1
0
/**
 * @brief Entry point for program.
 *
 * Coordinates all modules.
 */
int main()
{
	#ifdef DEBUG
		FILE * logFile;
		logFile = fopen("log.txt","w+");
		fprintf(logFile, "----LOGFILE FOR QUADRATIC EQUATION SOLVER----\n");
		fclose(logFile);
	#endif
	int rootNum = 0;
	Coef coef;
	Root root;
	char *buf;
	char *input;
	input = get_input();
	if (!input_val(&coef, input)) {
		output("Invalid input");
		exit(1);
	}
	#ifdef DEBUG
		logFile = fopen("log.txt","a");
		fprintf(logFile, "\nReturn parameters to input_val():\n");
		fprintf(logFile, "\tCoef.a: %lf Coef.b:%lf Coef.c: %lf \n"
			,coef.a,coef.b,coef.c);
		fclose(logFile);
	#endif
	rootNum = num_roots(coef);
	calc_roots(coef, rootNum, &root);
	buf = format(rootNum, root);
	output(buf);
	free(input);
	free(buf);
}
示例#2
0
int main()
{
	Coef coefs;		// a, b and c for the quadratic eqaution
	Root roots;		// Root struct with x1 and x1
	int num, ret;		// return value from num_roots() and get_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)

	cunit_init();

	//
	// 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);

	setup_mock_sqrt(d, sqrtd, cunit_dmacheps * 2.0 * d);
	num = num_roots(coefs);
	ret = get_roots(coefs, num, &roots);
	assert_eq("ret", ret, true);
	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 = check_mock_sqrt(&count, &x);
	snprintf(str, 99, "count ret = %d x =%20.61e", count, x);
	assert_eq(str, ret, 0);

	exit(0);
}