1
0
Fork 0

More test fixes, yet more needs to be done

This commit is contained in:
wael 2021-09-08 21:53:31 +03:00
parent 514471a92b
commit 29d32a612f
7 changed files with 100 additions and 12 deletions

View File

@ -16,16 +16,14 @@ Linked list.
Double linked list.
Stack.
Safe string copy (not tested - not a priority).
Bubble Sort (not tested).
Bubble Sort (not tested - fix tests).
Quicksort (not tested - fix tests, and fix comparison function!).
To be implemented:
Quicksort.
Merge Sort.
Introspective sort (Uses both quicksort and merge sort).
Heapsort (after implementing heaps).
Insertion sort.
Queue.
Double-ended queue.
Hashmap.
Heap.
BST - Binary Search Tree.

View File

@ -14,14 +14,14 @@
* equal to the first, otherwise zero. */
/* The array's length should be passed in the length argument. */
/* The function returns a NULL pointer on error, caller must check! */
void** bubble_sort(const void** array, const void (*comparison_function)(), const int length, const int copy) {
void** bubble_sort(void** array, int (*comparison_function)(const void*, const void*), const int length, const int copy) {
/* Sanity check. */
if (array == NULL)
return NULL;
/* Local variables. */
int i, j;
void **ptr, void *tmp;
void **ptr, *tmp;
/* Check if to allocate a copy. */
if (copy) {

View File

@ -1,5 +1,9 @@
/* This header defines the function for bubble sort. */
#ifndef BUBBLE_SORT_H
#define BUBBLE_SORT_H
void** bubble_sort(const void** array, const void (*comparison_function)(), const int length, const int copy);
/* Include statements. */
#include <stdlib.h>
/* Function definitions. */
void** bubble_sort(void** array, int (*comparison_function)(const void*, const void*), const int length, const int copy);
#endif /* BUBBLE_SORT_H */

View File

@ -3,7 +3,7 @@
#ifndef QUCIK_SORT_C
#define QUICK_SORT_C
#include <string.h>
#include "quick_sort.h"
#include "quicksort.h"
#endif /* QUICK_SORT_C */
/* Function implementations. */
@ -18,7 +18,7 @@ int standard_compare_quick_sort(const void* first, const void* second) {
else if (first == NULL && second == NULL)
return 0;
else
return *first <= *second;
return *((int*)first) <= *((int*)second); /* THIS IS WRONG AND SHOULD BE FIXED! */
}
/* Note that if the comparison function is NULL, then the regular comparison operator is used <. */
@ -29,7 +29,7 @@ int standard_compare_quick_sort(const void* first, const void* second) {
/* The array's length should be passed in the length argument. */
/* Size specifies a single element's size. */
/* The function returns a NULL pointer on error, caller must check! */
void** quick_sort(const void** array, int (*comparison_function)(const void*, const void*), const size_t length, const size_t size, const int copy) {
void** quick_sort(void** array, int (*comparison_function)(const void*, const void*), const size_t length, const size_t size, const int copy) {
/* Sanity check. */
if (array == NULL)
return NULL;

View File

@ -5,5 +5,5 @@
/* This is the standard (<) comparison function. */
int standard_compare_quick_sort(const void* first, const void* second);
/* The quicksort function's definition. */
void** quick_sort(const void** array, int (*comparison_function)(const void*, const void*), const size_t length, const size_t size, const int copy);
void** quick_sort(void** array, int (*comparison_function)(const void*, const void*), const size_t length, const size_t size, const int copy);
#endif /* QUICK_SORT_H */

View File

@ -2,7 +2,7 @@
#include <stdio.h>
/*#include <assert.h>*/
#include <time.h>
#include "../sorting_algorithms/bubble_sort/bubble_sort.h"
#include "../sorting_algorithms/bubble_sort/bubble_sort.c"
/* Include the struct to store the measurements and function name. */
#include "../utils/time_function.h"

86
tests/quick_sort_tests.c Normal file
View File

@ -0,0 +1,86 @@
/* Include statements. */
#include <stdio.h>
#include <assert.h>
#include <time.h>
#include <stdlib.h>
#include "../sorting_algorithms/quicksort/quicksort.c"
/* Include the struct to store the measurements and function name. */
#include "../utils/time_function.h"
/* Define constants. */
#define PRINT_INFO_TESTING_START "Starting tests.\n"
#define PRINT_INFO_TESTING_END "Done testing.\n"
#define SOME_TEST_FAILED "Some test failed!\n"
#define TESTED_FUNCTION_NAME "quicksort()"
#define TESTING_ALLOCATION_ERROR "Testing allocation error, free some memory and try again later.\n"
#define TESTING_WITH_COPY "Testing with array copying.\n"
#define TESTING_NO_COPY "Testing without array copying.\n"
#define RUNNING_TIME "Ran in %lf seconds.\n"
#define ARRAY_START_POINT 0
#define ARRAY_LENGTH 15
#define ALLOCATION_ERROR_CODE 75
/* Constants. */
const int unsorted[ARRAY_LENGTH] = {3, 5, 1, 2, 6, 4, 9, 7, 11, 10, 8, 15, 13, 14, 12};
const int sorted[ARRAY_LENGTH] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
/* Define functions. */
int comparison_function_int(const void *first, const void *second);
int test_array_sort();
/* Starting point. */
int main() {
printf(PRINT_INFO_TESTING_START);
if (test_array_sort()) {
printf(SOME_TEST_FAILED);
}
printf(PRINT_INFO_TESTING_END);
}
/* This function is used to compare two integers (through pointers). */
int comparison_function_int(const void *first, const void *second) {
return (*((int*)(first)) <= *((int*)(second)));
}
/* This function does the actual testing. */
int test_array_sort() {
/* Local variables. */
clock_t start, end;
function_run_time *runtime;
int **result;
int i;
/* Attempt to allocate the struct. */
runtime = (function_run_time*) malloc(sizeof(function_run_time));
if (runtime == NULL) {
printf(TESTING_ALLOCATION_ERROR);
exit(ALLOCATION_ERROR_CODE);
}
/* Set the timestamp. */
start = clock();
/* Run. */
result = (int**) quick_sort(((void*)unsorted), comparison_function_int, ARRAY_LENGTH, sizeof(int), 1);
/* Set another timestamp. */
end = clock();
/* Print start. */
printf(TESTING_WITH_COPY);
/* Record the results. */
runtime->name = TESTED_FUNCTION_NAME;
runtime->time = end - start;
/* Print the results. */
printf(RUNNING_TIME, runtime->time);
/* Free the struct. */
free(runtime);
/* Check if the array is sorted. */
for (i = 0; i < ARRAY_LENGTH; i++)
assert(((int*)result)[i] == sorted[i]);
/* Ran successfully. */
return 0;
}