init geek calc
Some checks failed
Deploy to GitHub Pages / build-and-deploy (push) Has been cancelled

This commit is contained in:
2025-10-04 10:53:41 +08:00
commit 54f427ea21
45 changed files with 4878 additions and 0 deletions

98
tests/unit/math.test.js Normal file
View File

@@ -0,0 +1,98 @@
// Unit tests for core math operations
// These tests should fail initially since the calculator module doesn't exist yet
function testMathOperations() {
try {
// This will fail since calculator.js doesn't exist yet
const calc = new Calculator();
// Test addition
if (calc.add(2, 3) !== 5) {
throw new Error('Addition failed: 2 + 3 should equal 5');
}
if (calc.add(-1, 1) !== 0) {
throw new Error('Addition failed: -1 + 1 should equal 0');
}
if (calc.add(0, 0) !== 0) {
throw new Error('Addition failed: 0 + 0 should equal 0');
}
// Test subtraction
if (calc.subtract(5, 3) !== 2) {
throw new Error('Subtraction failed: 5 - 3 should equal 2');
}
if (calc.subtract(0, 5) !== -5) {
throw new Error('Subtraction failed: 0 - 5 should equal -5');
}
// Test multiplication
if (calc.multiply(4, 5) !== 20) {
throw new Error('Multiplication failed: 4 * 5 should equal 20');
}
if (calc.multiply(-2, 3) !== -6) {
throw new Error('Multiplication failed: -2 * 3 should equal -6');
}
if (calc.multiply(0, 100) !== 0) {
throw new Error('Multiplication failed: 0 * 100 should equal 0');
}
// Test division
if (calc.divide(10, 2) !== 5) {
throw new Error('Division failed: 10 / 2 should equal 5');
}
if (calc.divide(7, 2) !== 3.5) {
throw new Error('Division failed: 7 / 2 should equal 3.5');
}
// Test division by zero (should return Infinity)
const divByZero = calc.divide(5, 0);
if (!isFinite(divByZero)) {
// This is expected, division by zero returns Infinity
} else {
throw new Error('Division by zero should return Infinity');
}
// Test percentage
if (calc.percentage(50) !== 0.5) {
throw new Error('Percentage failed: 50% should equal 0.5');
}
if (calc.percentage(100) !== 1) {
throw new Error('Percentage failed: 100% should equal 1');
}
if (calc.percentage(0) !== 0) {
throw new Error('Percentage failed: 0% should equal 0');
}
// Test toggle sign
if (calc.toggleSign(5) !== -5) {
throw new Error('Toggle sign failed: 5 should become -5');
}
if (calc.toggleSign(-5) !== 5) {
throw new Error('Toggle sign failed: -5 should become 5');
}
if (calc.toggleSign(0) !== 0) {
throw new Error('Toggle sign failed: 0 should remain 0');
}
return true; // All tests passed
} catch (error) {
return { error: error.message };
}
}
// Add test to global test collection
if (typeof addTest !== 'undefined') {
addTest('Math Operations Unit Tests', testMathOperations);
} else {
console.log('Test harness not loaded. Run with test runner.');
}