为给定 Python 函数生成单元测试。
在对 Cloud 控制台使用不同参数值时,或通过直接调用 Vertex AI API,您可以直接查询模型,并测试返回的结果。
|
|||||||||||
提示自由格式Create unit tests for the following quick_sort function. ```python def quick_sort(arr): if len(arr) <= 1: return arr else: pivot = arr[0] left = [x for x in arr[1:] if x < pivot] right = [x for x in arr[1:] if x >= pivot] return quick_sort(left) + [pivot] + quick_sort(right) ``` 响应```python import unittest class QuickSortTest(unittest.TestCase): def test_empty_array(self): self.assertEqual(quick_sort([]), []) def test_single_element_array(self): self.assertEqual(quick_sort([1]), [1]) def test_sorted_array(self): self.assertEqual(quick_sort([1, 2, 3, 4, 5]), [1, 2, 3, 4, 5]) def test_unsorted_array(self): self.assertEqual(quick_sort([5, 2, 1, 3, 4]), [1, 2, 3, 4, 5]) def test_array_with_duplicates(self): self.assertEqual(quick_sort([5, 2, 1, 3, 4, 5]), [1, 2, 3, 4, 5, 5]) def test_large_array(self): arr = [i for i in range(1000)] self.assertEqual(quick_sort(arr), arr) ``` |
|