We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
static get_expected_result(): number {
return 1;
}
}
function minimum_index(seq: number[]): number {
if (seq.length === 0) {
throw new Error("Cannot get the minimum value index from an empty sequence");
}
let min_idx: number = 0;
for (let i: number = 1; i < seq.length; i++) {
if (seq[i] < seq[min_idx]) {
min_idx = i;
}
}
return min_idx;
}
function TestWithEmptyArray(): void {
try {
const seq: number[] = TestDataEmptyArray.get_array();
const result: number = minimum_index(seq);
} catch (e) {
return;
}
throw new Error("Exception wasn't thrown as expected");
}
function TestWithUniqueValues(): void {
const seq: number[] = TestDataUniqueValues.get_array();
const result: number = minimum_index(seq);
if (result !== TestDataUniqueValues.get_expected_result()) {
throw new Error("Result doesn't match the expected value for unique values array");
}
}
function TestWithExactlyTwoDifferentMinimums(): void {
const seq: number[] = TestDataExactlyTwoDifferentMinimums.get_array();
const result: number = minimum_index(seq);
if (result !== TestDataExactlyTwoDifferentMinimums.get_expected_result()) {
throw new Error("Result doesn't match the expected value for array with two minimums");
}
}
function main(): void {
try {
TestWithEmptyArray();
TestWithUniqueValues();
TestWithExactlyTwoDifferentMinimums();
console.log("OK");
} catch (e) {
console.log((e as Error).message);
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 27: Testing
You are viewing a single comment's thread. Return to all comments →
'use strict';
process.stdin.resume(); process.stdin.setEncoding('utf-8');
let inputString: string = ''; let inputLines: string[] = []; let currentLine: number = 0;
process.stdin.on('data', function(inputStdin: string): void { inputString += inputStdin; });
process.stdin.on('end', function(): void { inputLines = inputString.split('\n'); inputString = '';
});
function readLine(): string { return inputLines[currentLine++]; }
class TestDataEmptyArray { static get_array(): number[] { return []; } }
class TestDataUniqueValues { static get_array(): number[] { return [3, 1, 2]; }
}
class TestDataExactlyTwoDifferentMinimums { static get_array(): number[] { return [2, 1, 3, 1]; }
}
function minimum_index(seq: number[]): number { if (seq.length === 0) { throw new Error("Cannot get the minimum value index from an empty sequence"); } let min_idx: number = 0; for (let i: number = 1; i < seq.length; i++) { if (seq[i] < seq[min_idx]) { min_idx = i; } } return min_idx; }
function TestWithEmptyArray(): void { try { const seq: number[] = TestDataEmptyArray.get_array(); const result: number = minimum_index(seq); } catch (e) { return; } throw new Error("Exception wasn't thrown as expected"); }
function TestWithUniqueValues(): void { const seq: number[] = TestDataUniqueValues.get_array(); const result: number = minimum_index(seq); if (result !== TestDataUniqueValues.get_expected_result()) { throw new Error("Result doesn't match the expected value for unique values array"); } }
function TestWithExactlyTwoDifferentMinimums(): void { const seq: number[] = TestDataExactlyTwoDifferentMinimums.get_array(); const result: number = minimum_index(seq); if (result !== TestDataExactlyTwoDifferentMinimums.get_expected_result()) { throw new Error("Result doesn't match the expected value for array with two minimums"); } }
function main(): void { try { TestWithEmptyArray(); TestWithUniqueValues(); TestWithExactlyTwoDifferentMinimums(); console.log("OK"); } catch (e) { console.log((e as Error).message); } }