Comp 110 Function Testing

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)

Test Functions

testArray, testBoolean, and testNumber are functions that 110 has created for your use! 

They each take in:

  • A string (that describes what's being tested for your own reference)
  • An 'actual' value
    • This will be an array/boolean/number depending on what you are testing
    • This argument should be a function call of the function that you are testing! 
  • An 'expected' value
    • This will be an array/boolean/number depending on what you are testing
    • This parameter is what the case you're testing SHOULD evaluate to be (assuming your code works the way it should)

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.


Examples of test function usage

// 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!

Test Function Implementations

/*
 * 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);
    }
};