Ejemplo n.º 1
0
int main()
{
	Coef coefs;
	int ret;
	char *input = "1 a 1";
	cunit_init();
	ret = input_val(&coefs, input);
	assert_eq("ret", ret, 0);
}
Ejemplo n.º 2
0
int main()
{
	FILE *f = openLog("unitTestLog.txt");
	cunit_init();	// Initiate CUnit
	Roots root;
	Terms terms;

	// First unit test, should
	//terms.a = "1", terms.b = "2", terms.c = "1";
	terms = validateInput("1", "2", "1");
	if (terms.input == 1)
	{
		root = eqnControl(terms.a, terms.b, terms.c);
		
		//fwrite(assert_neq("x1", root.x1, -2.0), f);
		assert_neq("x1", root.x1, -2.0);
		fprintf(f, "x1: %f assert not equal %f\n", (double)root.x1, -2.0);
		assert_neq("x2", root.x2, -2.0);
		fprintf(f, "x2: %f assert not equal %f\n", (double)root.x2, -2.0);
	}

	printf("-------------------------------\n");

	//terms.a = "2"; terms.b = "5"; terms.c = "-3";
	terms = validateInput("2", "5", "-3");
	if (terms.input == 1)
	{
		root = eqnControl(terms.a, terms.b, terms.c);
		assert_eq("x1", root.x1, 0.5);
		fprintf(f, "x1: %f assert equal %f\n", (double)root.x1, 0.5);
		assert_eq("x2", root.x2, -3.0);
		fprintf(f, "x2: %f assert equal %f\n", (double)root.x2, -3.0);
	}	

	printf("-------------------------------\n");
	//char terms.a, terms.b, terms.c;
	//terms.a = "a"; terms.b = "b"; terms.c = "c";
	terms = validateInput("a", "b", "c");
	if (terms.input == 1)
	{
		root = eqnControl(terms.a, terms.b, terms.c);
		assert_eq("x1", root.x1, 0.5);
		fprintf(f, "x1: %f assert equal %f\n", (double)root.x1, 0.5);
		assert_eq("x2", root.x2, -3.0);
		fprintf(f, "x2: %f assert equal %f\n", (double)root.x2, -3.0);
	}
	fclose(f);
}
Ejemplo n.º 3
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);
}
Ejemplo n.º 4
0
int main()
{
	cunit_init();
	int length = 10;
	int input[length];
	int i = 0;
	for(i = 0; i < length; i++)
	{
		input[i] = i;
	}
	
	assert_eq("Testing old with n=1, l=0", largest(input, 1, 0), input[length - 1]);
	assert_eq("Testing old with n=10, l=20", largest(input, 10, 20), input[length - 1]);
	assert_eq("Testing old with n=202, l=0", largest(input, 202, 0), input[length - 1]);
	assert_eq("Testing old with n=10, l=-1", largest(input, 10, -1), input[length - 1]);
	assert_eq("Testing old with n=-1, l=10", largest(input, 10, -1), input[length - 1]);
	
	assert_eq("Testing fixed with standardized n, l=500", largestFixed(input, length, 500), input[length - 1]);
	assert_eq("Testing fixed with standardized n, l=700", largestFixed(input, length, 700), input[length - 1]);
	assert_eq("Testing fixed with standardized n, l=200", largestFixed(input, length, 200), input[length - 1]);
	return 0;
}
Ejemplo n.º 5
0
Archivo: t3.c Proyecto: SlimeQ/qsolver
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);
}
Ejemplo n.º 6
0
Archivo: t4.c Proyecto: SlimeQ/qsolver
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);
}
Ejemplo n.º 7
0
int main(){
	/*Declare variables*/
	char *comp = "4";
	char *promptString = "Please enter a value: ";
	char *userInputReturn = (char *)malloc(BUFFERSIZE);
	int i = 0;

	/*initialize the unit testing framework*/
	cunit_init();

	/*Get user input*/
	userInputReturn = getUserInput(promptString);

	/*Replace first \n with null terminator for later comparison*/
	userInputReturn[strcspn(userInputReturn,"\n")] = '\0';

	/*Make comparison and place \n back to userInputReturn variable*/
	i = strcmp(comp, userInputReturn);
	userInputReturn[strcspn(userInputReturn,"\n")] = '\n';
	assert_eq("Input not in correct format", i, 0);

	return 0;
}
Ejemplo n.º 8
0
Archivo: t3.c Proyecto: cuzox/quadratic
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;
}
Ejemplo n.º 9
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);
}