API Docs for: 1.0

src/js/initTest.js

/*! @file initTest.js */

/*
 * part of the 'Transfinite Ordinal Calculator'
 * author: Claudio Kressibucher
 * license: GNU LGPL
 */

// check correctness of calculations

/* JSHINT global declaration */
/*global go:true, util:true, config:true, Calculation:true, model:true, oGui:true, Ord:true, OrdSummand:true */

/*
 * This function executes some calculation tests.
 * It is supposed to be called at initialization
 * of the app to ensure correct calculations.
 */
go.initTest = function (){
	var calc;
	var opnd1, opnd2, res, exp, tmpExp;
	var ordSumd1, ordSumd2;
	
	// checks if the result ordinal (res) equals to the expected ordinal (exp)
	// and shows a message if that isn't true
	var check = function (res, exp){
		var success = util.assert(Calculation.compare(res, exp) === 0);
		if (!success) {
			oGui.showHint("A problem has occurred. The program doesn't behave as expected. Please restart it and try again.");
			return false;
		}
		return true;
	};
	
	// Test Addition
	opnd1 = 3;
	opnd2 = new Ord(4);
	calc = new Calculation(opnd1, opnd2, new model.Operator('+'));
	res = calc.simplify();
	exp = new Ord(7);
	if (!check(res, exp)){
		return false;
	}
	
	// Test Addition with omega
	opnd2 = new Ord( new OrdSummand(1, 1) );
	calc = new Calculation(opnd1, opnd2, new model.Operator('+'));
	res = calc.simplify();
	exp = opnd2;
	if (!check(res, exp)){
		return false;
	}
	
	// Test Addition with omega^omega
	ordSumd1 = new OrdSummand(opnd2, 3); // w^w*3
	opnd1 = new Ord(ordSumd1);
	ordSumd2 = new OrdSummand(0, 2); // 2
	opnd1.addSummand(ordSumd2);
	// opnd2 = omega
	calc = new Calculation(opnd1, opnd2, new model.Operator('+'));
	res = calc.simplify(); // (w^w*3 + 2) + w = w^w*3 + w
	exp = new Ord(ordSumd1);
	exp.addSummand( new OrdSummand(1, 1) );
	if (!check(res, exp)){
		return false;
	}
	
	// Test multiplication
	// a * b | b with natural term
	opnd1 = new Ord( new OrdSummand(2, 2) );
	opnd1.addSummand( new OrdSummand(1, 3) );
	opnd2 = new Ord( new OrdSummand(3, 3) );
	opnd2.addSummand( new OrdSummand(2, 4) );
	opnd2.addSummand( new OrdSummand(0, 5) );
	// (w^2*2 + w*3) * (w^3*3 + w^2*4 + 5) = w^5*3 + w^4*4 + w^2*10 + w*3
	calc = new Calculation(opnd1, opnd2, new model.Operator('*'));
	res = calc.simplify();
	exp = new Ord( new OrdSummand(5, 3) );
	exp.addSummand( new OrdSummand(4, 4) );
	exp.addSummand( new OrdSummand(2, 2*5) );
	exp.addSummand( new OrdSummand(1, 3) );
	if (!check(res, exp)){
		return false;
	}
	ordSumd1 = new OrdSummand(0, 4);
	opnd1.addSummand(ordSumd1);
	// (w^2*2 + w*3 + 4) * (w^3*3 + w^2*4 + 5) = w^5*3 + w^4*4 + w^2*10 + w*3 + 4
	calc = new Calculation(opnd1, opnd2, new model.Operator('*'));
	res = calc.simplify();
	exp.addSummand(ordSumd1);
	if (!check(res, exp)){
		return false;
	}
	
	// Test exponentiation
	// base = omega + 3
	opnd1 = new Ord(new OrdSummand(1, 1)); // omega
	opnd1.addSummand( new OrdSummand(0, 3) ); // omega + 3
	
	// exp = omega+2
	opnd2 = new Ord(new OrdSummand(1, 1));
	opnd2.addSummand( new OrdSummand(0, 2) );

	calc = new Calculation(opnd1, opnd2, new model.Operator('^'));
	res = calc.simplify();
	
	// omega^(omega+2) + omega^(omega+1)*3 + omega^omega*3
	tmpExp = new Ord( new OrdSummand(1, 1) );
	tmpExp.addSummand( new OrdSummand(0, 2) );
	exp = new Ord( new OrdSummand(tmpExp, 1) );
	tmpExp = new Ord( new OrdSummand(1, 1) );
	tmpExp.addSummand( new OrdSummand(0, 1) );
	exp.addSummand( new OrdSummand(tmpExp, 3) );
	tmpExp = new Ord( new OrdSummand(1, 1) );
	exp.addSummand( new OrdSummand(tmpExp, 3) );
	
	if (!check(res, exp)){
		return false;
	}

	// oGui.showHint("Tests passed..."); // alert that all tests passed
	oGui.dbgLog("init Tests passed successfully...");
	
	if (config.debug > 0){
		// Hint should not be used in productive state.
		// It just reminds us to deactivate logging for productive state!
		oGui.showHint("Init tests passed. Logging mode is active...");
	}
	
	return true;
};