Hone logo
Hone
Problems

Concolic Testing with Jest: A Simple Calculator

Concolic testing combines concrete and symbolic execution to explore a wider range of program paths than traditional testing. This challenge asks you to implement a basic concolic testing framework using Jest and a symbolic execution library (we'll use js-concolic-executor). The goal is to test a simple calculator function by generating test cases that explore different execution paths, ensuring thorough code coverage.

Problem Description

You are tasked with creating a Jest test suite for a simple calculator function calculate that performs addition, subtraction, multiplication, and division based on a given operator. The test suite should utilize js-concolic-executor to perform concolic testing, generating test inputs that explore different execution paths within the calculate function. The test suite should aim for high branch coverage, ensuring that all possible execution paths are exercised.

The calculate function is defined as follows:

function calculate(a: number, b: number, operator: string): number | string {
  switch (operator) {
    case '+':
      return a + b;
    case '-':
      return a - b;
    case '*':
      return a * b;
    case '/':
      if (b === 0) {
        return "Division by zero error";
      }
      return a / b;
    default:
      return "Invalid operator";
  }
}

Your test suite should:

  1. Install Dependencies: Install jest and js-concolic-executor as development dependencies in your project.
  2. Concolic Execution: Use js-concolic-executor to symbolically execute the calculate function.
  3. Path Exploration: Generate test cases that explore all possible branches within the calculate function (addition, subtraction, multiplication, division, and the default case). Specifically, ensure the division by zero case is tested.
  4. Assertion Generation: Use the symbolic execution results to generate assertions that verify the correctness of the calculate function for each explored path.
  5. Jest Integration: Integrate the generated assertions into a Jest test suite.

Examples

Example 1:

Input: calculate(2, 3, '+')
Output: 5
Explanation: The function performs addition, returning the sum of 2 and 3.

Example 2:

Input: calculate(10, 2, '/')
Output: 5
Explanation: The function performs division, returning the quotient of 10 and 2.

Example 3:

Input: calculate(5, 0, '/')
Output: "Division by zero error"
Explanation: The function detects division by zero and returns an error message.

Example 4:

Input: calculate(4, 2, '%')
Output: "Invalid operator"
Explanation: The function receives an invalid operator and returns an error message.

Constraints

  • The calculate function must remain unchanged.
  • You must use js-concolic-executor for symbolic execution.
  • The test suite must cover all branches of the calculate function.
  • The generated test cases should be concise and focused on exploring different execution paths.
  • The test suite should be written in TypeScript.

Notes

  • js-concolic-executor provides functions for symbolic execution and path exploration. Refer to the library's documentation for details on how to use these functions.
  • Consider using Jest's describe and it blocks to organize your test suite.
  • The symbolic execution library will likely provide information about the possible values of variables at different points in the execution. Use this information to generate assertions that verify the correctness of the function.
  • Focus on generating test cases that explore different execution paths, rather than trying to cover every possible input value. The goal is to demonstrate concolic testing principles.
  • You may need to experiment with the symbolic execution options to achieve the desired level of path exploration.
  • Error handling (e.g., invalid operator) is an important aspect to test.
Loading editor...
typescript