Symbolic Execution Testing with Jest
Symbolic execution is a powerful technique for testing software by exploring all possible execution paths. This challenge asks you to implement a simplified symbolic execution engine within a Jest testing environment to verify the correctness of a given function. By symbolically executing the function, you'll generate test cases that cover various input combinations and ensure the function behaves as expected.
Problem Description
You are tasked with creating a Jest test suite that utilizes symbolic execution to test a function named calculateDiscount. This function takes two arguments: price (a number representing the original price) and discountPercentage (a number representing the discount percentage, between 0 and 100). The function calculates the discounted price. Your symbolic execution engine should generate test cases with symbolic values for price and discountPercentage, then evaluate the function with concrete values derived from the symbolic execution to ensure correctness.
Specifically, you need to:
- Define Symbolic Variables: Represent
priceanddiscountPercentageas symbolic variables within your Jest test. - Generate Test Cases: Generate at least three test cases using symbolic execution. These cases should cover different scenarios, including:
- A case with a high discount percentage.
- A case with a low discount percentage.
- A case with a zero discount percentage.
- Concrete Value Assignment: For each test case, assign concrete numerical values to the symbolic variables
priceanddiscountPercentage. These values should be chosen to exercise the function's logic effectively. - Function Execution and Assertion: Execute the
calculateDiscountfunction with the assigned concrete values and assert that the result is correct. The correct discounted price is calculated asprice * (1 - discountPercentage / 100). - Test Suite Structure: Organize your tests within a Jest
describeblock for clarity.
Examples
Example 1:
Input: price = 100, discountPercentage = 20
Output: 80
Explanation: 100 * (1 - 20 / 100) = 100 * 0.8 = 80
Example 2:
Input: price = 50, discountPercentage = 0
Output: 50
Explanation: 50 * (1 - 0 / 100) = 50 * 1 = 50
Example 3:
Input: price = 200, discountPercentage = 50
Output: 100
Explanation: 200 * (1 - 50 / 100) = 200 * 0.5 = 100
Constraints
pricemust be a non-negative number.discountPercentagemust be a number between 0 and 100 (inclusive).- The test suite must include at least three test cases.
- The
calculateDiscountfunction is provided below and should not be modified. - The solution must be written in TypeScript and compatible with Jest.
Notes
- You don't need to implement a full-fledged symbolic execution engine. Instead, focus on simulating the process by manually generating test cases based on symbolic reasoning.
- Consider the potential for floating-point precision issues when performing calculations. Use
toBeCloseToin your assertions to account for these issues. - The goal is to demonstrate an understanding of symbolic execution principles and how they can be applied to test software.
// calculateDiscount.ts (Provided - Do not modify)
export function calculateDiscount(price: number, discountPercentage: number): number {
return price * (1 - discountPercentage / 100);
}