Hone logo
Hone
Problems

Implement a Test Oracle for a Simple Calculator in Jest

This challenge focuses on creating a robust test oracle using Jest for a fundamental calculator function. Understanding how to define and implement test oracles is crucial for ensuring the reliability and correctness of your code, especially when dealing with complex logic or scenarios where the expected output might be non-trivial to determine manually.

Problem Description

You are tasked with implementing a set of unit tests for a simple Calculator class in TypeScript using the Jest testing framework. The Calculator class will have methods for addition, subtraction, multiplication, and division.

Your primary goal is to implement a sophisticated test oracle that verifies the correctness of these calculator methods. A test oracle is a mechanism that determines the expected output for a given input, allowing you to compare it against the actual output of the function under test. In this challenge, you will define the expected outcomes based on predefined mathematical rules and then assert that the Calculator's methods produce these expected outcomes.

Key Requirements:

  1. Calculator Class: Create a Calculator class with the following methods:

    • add(a: number, b: number): number
    • subtract(a: number, b: number): number
    • multiply(a: number, b: number): number
    • divide(a: number, b: number): number
  2. Jest Tests: Write comprehensive unit tests for each method of the Calculator class using Jest.

  3. Test Oracle Implementation: For each test case, you must explicitly define the expected result before running the Calculator method. This means:

    • For addition, the expected result is simply a + b.
    • For subtraction, the expected result is a - b.
    • For multiplication, the expected result is a * b.
    • For division, the expected result is a / b.
  4. Assertions: Use Jest's assertion library (expect) to compare the actual output of the Calculator methods with the expected outputs determined by your test oracle.

  5. Edge Cases: Include tests for common edge cases such as:

    • Zero as an operand.
    • Negative numbers.
    • Division by zero (you should define how your calculator handles this, e.g., by throwing an error or returning Infinity or NaN). For this challenge, let's assume division by zero should result in Infinity for positive numerators, -Infinity for negative numerators, and NaN for 0/0.

Expected Behavior:

  • The Calculator methods should perform their respective arithmetic operations accurately for valid number inputs.
  • Division by zero should be handled as specified in the edge cases.
  • All tests should pass if the Calculator class is implemented correctly and the test oracle accurately defines the expected outcomes.

Examples

Example 1: Addition

Input: calculator.add(5, 3)
Expected Output (from oracle): 8

Example 2: Division with Positive Numbers

Input: calculator.divide(10, 2)
Expected Output (from oracle): 5

Example 3: Division by Zero

Input: calculator.divide(7, 0)
Expected Output (from oracle): Infinity

Example 4: Zero Divided by Zero

Input: calculator.divide(0, 0)
Expected Output (from oracle): NaN

Example 5: Negative Numbers and Subtraction

Input: calculator.subtract(-5, -3)
Expected Output (from oracle): -2

Constraints

  • All inputs to calculator methods will be numbers.
  • The Calculator class and its tests should be written in TypeScript.
  • You must use Jest for testing.
  • The divide method should handle division by zero as described in the "Expected Behavior" section.

Notes

  • The core of this challenge is not the complexity of the calculator itself, but the discipline of defining your expected results separately from calling the function under test. Think of your test oracle as a ground truth against which the code's output is measured.
  • For division by zero, the standard JavaScript behavior aligns with what's requested. You can directly assert against Infinity, -Infinity, and NaN.
  • Consider organizing your tests using describe blocks for each calculator method.
Loading editor...
typescript