77 }
78
79 bool QuickSort::compare_arrays(int* actual, int* expected, int length) {
80 for (int i = 0; i < length; i++) {
81 if (actual[i] != expected[i]) {
82 print_array("Sorted array ", actual, length);
83 print_array("Expected array", expected, length);
84 return false;
85 }
86 }
87 return true;
88 }
89
90 template <class C>
91 bool QuickSort::sort_and_compare(int* arrayToSort, int* expectedResult, int length, C comparator, bool idempotent) {
92 sort<int, C>(arrayToSort, length, comparator, idempotent);
93 return compare_arrays(arrayToSort, expectedResult, length);
94 }
95
96 bool QuickSort::test_quick_sort() {
97 tty->print_cr("test_quick_sort\n");
98 {
99 int* test_array = NULL;
100 int* expected_array = NULL;
101 assert(sort_and_compare(test_array, expected_array, 0, test_comparator), "Empty array not handled");
102 }
103 {
104 int test_array[] = {3};
105 int expected_array[] = {3};
106 assert(sort_and_compare(test_array, expected_array, 1, test_comparator), "Single value array not handled");
107 }
108 {
109 int test_array[] = {3,2};
110 int expected_array[] = {2,3};
111 assert(sort_and_compare(test_array, expected_array, 2, test_comparator), "Array with 2 values not correctly sorted");
112 }
113 {
114 int test_array[] = {3,2,1};
115 int expected_array[] = {1,2,3};
116 assert(sort_and_compare(test_array, expected_array, 3, test_comparator), "Array with 3 values not correctly sorted");
117 }
|
77 }
78
79 bool QuickSort::compare_arrays(int* actual, int* expected, int length) {
80 for (int i = 0; i < length; i++) {
81 if (actual[i] != expected[i]) {
82 print_array("Sorted array ", actual, length);
83 print_array("Expected array", expected, length);
84 return false;
85 }
86 }
87 return true;
88 }
89
90 template <class C>
91 bool QuickSort::sort_and_compare(int* arrayToSort, int* expectedResult, int length, C comparator, bool idempotent) {
92 sort<int, C>(arrayToSort, length, comparator, idempotent);
93 return compare_arrays(arrayToSort, expectedResult, length);
94 }
95
96 bool QuickSort::test_quick_sort() {
97 tty->print_cr("test_quick_sort");
98 {
99 int* test_array = NULL;
100 int* expected_array = NULL;
101 assert(sort_and_compare(test_array, expected_array, 0, test_comparator), "Empty array not handled");
102 }
103 {
104 int test_array[] = {3};
105 int expected_array[] = {3};
106 assert(sort_and_compare(test_array, expected_array, 1, test_comparator), "Single value array not handled");
107 }
108 {
109 int test_array[] = {3,2};
110 int expected_array[] = {2,3};
111 assert(sort_and_compare(test_array, expected_array, 2, test_comparator), "Array with 2 values not correctly sorted");
112 }
113 {
114 int test_array[] = {3,2,1};
115 int expected_array[] = {1,2,3};
116 assert(sort_and_compare(test_array, expected_array, 3, test_comparator), "Array with 3 values not correctly sorted");
117 }
|