Note: these are not built-into the introcs package - in order to use them they need to be imported from test-util.ts or you need to include your own implementation of them in your file that you are testing (see below)
testArray, testBoolean, and testNumber are functions that 110 has created for your use!
They each take in:
They are void functions (so they won't return anything) however, a string will be printed to the browser containing either "PASS" or "FAIL" depending on whether the expected and actual values match.
// this is our function that we want to test!
let countOs = (str: string): number => {
let count = 0;
for (let i = 0; i < str.length; i++) {
if (str[i] === "o") {
count++;
}
}
return count;
};
// assuming we've imported our test functions lets come up with some test use cases!
//general use cases
testNumber("countOs piano lowercase", 1, countOs("piano"));
testNumber("countOs oboe uppercase", 2, countOs("obOe"));
//edge cases
testNumber("countOs empty", 0, countOs(""));
testNumber("countOs all Os", 5, countOs("oOoOo"));
If you are fuzzy on how test cases work, check out Test-Driven Programming!
Based on what's printed below, we've failed a couple of tests! We can tell that the tests that include a capital "O" fail, and that we need to make some edits to our function to accommodate these cases.
//new and improved function!
let countOs = (str: string): number => {
let count = 0;
for (let i = 0; i < str.length; i++) {
if (str[i] === "o" || str[i] === "O") { // we've added another condition here
count++;
}
}
return count;
};
When we run the same tests again, all of them pass! Success!
/*
* You should not need to change the functions in this file.
*
* This file contains two simplistic functions to help you
* test your array utility functions programmatically.
*/
import { print } from "introcs";
/**
* This function is given a test name for diagnostic purposes.
*
* It is then given an expected number. This should be what your
* test is *expecting* to be the correct result.
*
* It is then given the actual number in question. This will be
* compared with the expected number for equality.
*
* If the two numbers are equal, then the test passes. If they are
* different in any way, then the test fails.
*/
export let testNumber = (name: string, expected: number, actual: number): void => {
let passed = expected === actual;
if (passed) {
print("PASS: " + name);
} else {
print("FAIL: " + name);
print("-- Expected: " + expected);
print("-- Actually: " + actual);
}
};
/*
* This function is given a test name for diagnostic purposes.
*
* It is then given an expected number array. This should be what your
* test is *expecting* to be the correct result.
*
* It is then given the actual number array in question. This will be
* compared with the expected array for equality.
*
* If the two number arrays are equal, then the test passes. If they are
* different in any way, then the test fails.
*/
export let testArray = (name: string, expected: number[], actual: number[]): void => {
let passed = true;
if (expected.length === actual.length) {
for (let i = 0; i < expected.length; i++) {
passed = passed && expected[i] === actual[i];
}
} else {
passed = false;
}
if (passed) {
print("PASS: " + name);
} else {
print("FAIL: " + name);
print("-- Expected: " + expected);
print("-- Actually: " + actual);
}
};
/*
* This function is given a test name for diagnostic purposes.
*
* It is then given an expected boolean value. This should be what your
* test is *expecting* to be the correct result.
*
* It is then given the actual boolean in question. This will be
* compared with the expected array for equality.
*
* If the two booleans are equal, then the test passes. If they are
* different in any way, then the test fails.
*/
export let testBoolean = (name: string, expected: boolean, actual: boolean): void => {
let passed = expected === actual;
if (passed) {
print("PASS: " + name);
} else {
print("FAIL: " + name);
print("-- Expected: " + expected);
print("-- Actually: " + actual);
}
};