Automated Test Generation for a Simple Utility Function
Automated test generation is a powerful technique to increase test coverage and reduce the manual effort required for writing tests. This challenge focuses on implementing a basic test generation strategy for a simple utility function using Jest in TypeScript. You'll create a function and then automatically generate a suite of tests for it, covering various input scenarios.
Problem Description
You are tasked with creating a utility function called safeDivide that takes two numbers as input and returns the result of dividing the first number by the second. However, the function should handle the case where the second number is zero to prevent division by zero errors. In such cases, it should return null. Your goal is to write a Jest test suite that automatically generates tests for safeDivide covering normal division, division by zero, and potentially other edge cases.
What needs to be achieved:
- Implement the
safeDividefunction in TypeScript. - Create a Jest test suite that automatically generates tests for
safeDivide. The test suite should include tests for:- Normal division (e.g., 10 / 2)
- Division by zero (e.g., 10 / 0)
- Negative numbers (e.g., -10 / 2, 10 / -2, -10 / -2)
- Floating-point numbers (e.g., 5.5 / 2, 10 / 2.5)
- Zero divided by a non-zero number (e.g., 0 / 5)
Key Requirements:
- The
safeDividefunction must be implemented correctly. - The Jest test suite must be able to automatically generate tests for the specified scenarios. You can use loops, arrays, or other techniques to generate the test cases programmatically.
- The tests should clearly assert the expected output for each scenario.
- The code should be well-structured and readable.
Expected Behavior:
The safeDivide function should return the result of the division if the second number is not zero. If the second number is zero, it should return null. The Jest test suite should pass all generated tests, verifying that the function behaves as expected in all specified scenarios.
Edge Cases to Consider:
- Large numbers (to check for potential overflow issues, though not strictly required for this challenge).
- Very small numbers (close to zero) to ensure accurate floating-point calculations.
- Input types: While the function signature specifies numbers, consider how it might behave if non-numeric values are passed (though error handling for this is not required).
Examples
Example 1:
Input: safeDivide(10, 2)
Output: 5
Explanation: Simple division with positive numbers.
Example 2:
Input: safeDivide(10, 0)
Output: null
Explanation: Division by zero should return null.
Example 3:
Input: safeDivide(-10, 2)
Output: -5
Explanation: Division with a negative numerator.
Example 4:
Input: safeDivide(5.5, 2)
Output: 2.75
Explanation: Division with floating-point numbers.
Constraints
- The
safeDividefunction must be implemented in TypeScript. - The Jest test suite must be written in TypeScript.
- The test generation should be automated; manually writing each test case is discouraged.
- The solution should be reasonably efficient. Generating a large number of tests is not required, but the generation process should not be excessively slow.
- The code should be well-commented and easy to understand.
Notes
Consider using a loop or array to define the test cases programmatically. Think about how to structure your test suite to make it easy to add new test cases in the future. Focus on generating a representative set of tests that cover the key scenarios described in the problem description. You can use expect.resolves if you want to test asynchronous behavior (not required for this problem).