|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { Runnable } from '../../../../src/index.js';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MultiplierRunnable extends Runnable {
|
|
|
constructor(factor) {
|
|
|
super();
|
|
|
|
|
|
|
|
|
if (typeof factor !== 'number') {
|
|
|
throw new Error('Factor must be a number');
|
|
|
}
|
|
|
|
|
|
this.factor = factor;
|
|
|
}
|
|
|
|
|
|
async _call(input, config) {
|
|
|
|
|
|
if (typeof input !== 'number') {
|
|
|
throw new Error('Input must be a number');
|
|
|
}
|
|
|
|
|
|
|
|
|
return input * this.factor;
|
|
|
}
|
|
|
|
|
|
|
|
|
toString() {
|
|
|
return `MultiplierRunnable(Γ${this.factor})`;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function runTests() {
|
|
|
console.log('π§ͺ Testing MultiplierRunnable Solution...\n');
|
|
|
|
|
|
try {
|
|
|
|
|
|
console.log('Test 1: Basic multiplication');
|
|
|
const times3 = new MultiplierRunnable(3);
|
|
|
const result1 = await times3.invoke(5);
|
|
|
console.assert(result1 === 15, `Expected 15, got ${result1}`);
|
|
|
console.log('β
3 Γ 5 = 15');
|
|
|
console.log(` Runnable: ${times3.toString()}\n`);
|
|
|
|
|
|
|
|
|
console.log('Test 2: Different factor');
|
|
|
const times10 = new MultiplierRunnable(10);
|
|
|
const result2 = await times10.invoke(7);
|
|
|
console.assert(result2 === 70, `Expected 70, got ${result2}`);
|
|
|
console.log('β
10 Γ 7 = 70\n');
|
|
|
|
|
|
|
|
|
console.log('Test 3: Negative numbers');
|
|
|
const times2 = new MultiplierRunnable(2);
|
|
|
const result3 = await times2.invoke(-5);
|
|
|
console.assert(result3 === -10, `Expected -10, got ${result3}`);
|
|
|
console.log('β
2 Γ -5 = -10\n');
|
|
|
|
|
|
|
|
|
console.log('Test 4: Decimal numbers');
|
|
|
const times1_5 = new MultiplierRunnable(1.5);
|
|
|
const result4 = await times1_5.invoke(4);
|
|
|
console.assert(result4 === 6, `Expected 6, got ${result4}`);
|
|
|
console.log('β
1.5 Γ 4 = 6\n');
|
|
|
|
|
|
|
|
|
console.log('Test 5: Multiply by zero');
|
|
|
const times0 = new MultiplierRunnable(0);
|
|
|
const result5 = await times0.invoke(100);
|
|
|
console.assert(result5 === 0, `Expected 0, got ${result5}`);
|
|
|
console.log('β
0 Γ 100 = 0\n');
|
|
|
|
|
|
|
|
|
console.log('Test 6: Error handling - invalid factor');
|
|
|
try {
|
|
|
new MultiplierRunnable('not a number');
|
|
|
console.error('β Should have thrown error');
|
|
|
} catch (error) {
|
|
|
console.log('β
Correctly throws error for invalid factor\n');
|
|
|
}
|
|
|
|
|
|
|
|
|
console.log('Test 7: Error handling - invalid input');
|
|
|
try {
|
|
|
const times5 = new MultiplierRunnable(5);
|
|
|
await times5.invoke('not a number');
|
|
|
console.error('β Should have thrown error');
|
|
|
} catch (error) {
|
|
|
console.log('β
Correctly throws error for invalid input\n');
|
|
|
}
|
|
|
|
|
|
|
|
|
console.log('Test 8: Batch processing');
|
|
|
const times2_batch = new MultiplierRunnable(2);
|
|
|
const results = await times2_batch.batch([1, 2, 3, 4, 5]);
|
|
|
console.log(` Input: [1, 2, 3, 4, 5]`);
|
|
|
console.log(` Output: [${results.join(', ')}]`);
|
|
|
console.assert(JSON.stringify(results) === JSON.stringify([2, 4, 6, 8, 10]));
|
|
|
console.log('β
Batch processing works\n');
|
|
|
|
|
|
console.log('π All tests passed!');
|
|
|
} catch (error) {
|
|
|
console.error('β Test failed:', error.message);
|
|
|
console.error(error.stack);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
|
runTests();
|
|
|
}
|
|
|
|
|
|
export { MultiplierRunnable }; |