Better memory management of arrays and test sample generation
This commit is contained in:
8
test/include/samples.h
Normal file
8
test/include/samples.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#ifndef LSORTTEST_SAMPLES_H
|
||||
#define LSORTTEST_SAMPLES_H
|
||||
|
||||
#include "../../include/types.h"
|
||||
|
||||
lsort_array* generate_samples(int number);
|
||||
|
||||
#endif
|
||||
15
test/samples.c
Normal file
15
test/samples.c
Normal file
@@ -0,0 +1,15 @@
|
||||
#include "../include/types.h"
|
||||
#include "include/samples.h"
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
lsort_array* generate_samples(int number) {
|
||||
srand(time(NULL));
|
||||
lsort_array* array = lsort_array_create();
|
||||
|
||||
for (int i = 0; i < number; i++) {
|
||||
lsort_array_append(array, lsort_item_create(rand(), NULL));
|
||||
}
|
||||
|
||||
return array;
|
||||
};
|
||||
135
test/test.c
135
test/test.c
@@ -1,105 +1,94 @@
|
||||
#include "../include/types.h"
|
||||
#include "../include/radix.h"
|
||||
#include "../include/quicksort.h"
|
||||
#include "../include/lsort.h"
|
||||
#include "include/samples.h"
|
||||
#include <stdio.h>
|
||||
|
||||
int test_array_i_append(lsort_array_i* array) {
|
||||
printf("lsort_array_i_append()...\n");
|
||||
#define sample_count 64*64*64*64
|
||||
|
||||
lsort_array_i_append(array, 25);
|
||||
lsort_array_i_append(array, 64);
|
||||
lsort_array_i_append(array, 34);
|
||||
lsort_array_i_append(array, 74);
|
||||
lsort_array_i_append(array, 99);
|
||||
lsort_array_i_append(array, 23);
|
||||
int test_array_append() {
|
||||
printf("lsort_array_append()...\n");
|
||||
|
||||
if (array->items[5] != 23) return 1;
|
||||
int return_code = 0;
|
||||
lsort_array* array = lsort_array_create();
|
||||
|
||||
return 0;
|
||||
lsort_array_append(array, lsort_item_create(25, NULL));
|
||||
lsort_array_append(array, lsort_item_create(64, NULL));
|
||||
lsort_array_append(array, lsort_item_create(34, NULL));
|
||||
lsort_array_append(array, lsort_item_create(74, NULL));
|
||||
lsort_array_append(array, lsort_item_create(99, NULL));
|
||||
lsort_array_append(array, lsort_item_create(23, NULL));
|
||||
|
||||
|
||||
if (array->items[5]->key != 23) return_code = 1;
|
||||
|
||||
lsort_array_destroy(array, 1);
|
||||
return return_code;
|
||||
}
|
||||
|
||||
int test_array_i_swap(lsort_array_i* array) {
|
||||
printf("lsort_array_i_swap()...\n");
|
||||
int test_array_swap() {
|
||||
printf("lsort_array_swap()...\n");
|
||||
|
||||
lsort_array_i_swap(array, 0, 5);
|
||||
lsort_array_i_swap(array, 4, 2);
|
||||
lsort_array_i_swap(array, 2, 3);
|
||||
int return_code = 0;
|
||||
lsort_array* array = lsort_array_create();
|
||||
|
||||
lsort_array_append(array, lsort_item_create(25, NULL));
|
||||
lsort_array_append(array, lsort_item_create(64, NULL));
|
||||
lsort_array_append(array, lsort_item_create(34, NULL));
|
||||
lsort_array_append(array, lsort_item_create(74, NULL));
|
||||
lsort_array_append(array, lsort_item_create(99, NULL));
|
||||
lsort_array_append(array, lsort_item_create(23, NULL));
|
||||
|
||||
int index_0 = array->items[0]->key;
|
||||
int index_1 = array->items[1]->key;
|
||||
int index_2 = array->items[2]->key;
|
||||
int index_3 = array->items[3]->key;
|
||||
int index_4 = array->items[4]->key;
|
||||
int index_5 = array->items[5]->key;
|
||||
|
||||
lsort_array_swap(array, 0, 5);
|
||||
lsort_array_swap(array, 4, 2);
|
||||
lsort_array_swap(array, 2, 3);
|
||||
|
||||
if (array->items[0] != 23 || array->items[5] != 25 || array->items[4] != 34 || array->items[2] != 74 || array->items[3] != 99) return 1;
|
||||
if (
|
||||
array->items[0]->key != index_5 ||
|
||||
array->items[5]->key != index_0 ||
|
||||
array->items[4]->key != index_2 ||
|
||||
array->items[3]->key != index_4 ||
|
||||
array->items[2]->key != index_3 ||
|
||||
array->items[1]->key != index_1
|
||||
) return_code = 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
lsort_array_destroy(array, 1);
|
||||
|
||||
int test_append_swap() {
|
||||
printf("test_append_swap()...\n");
|
||||
|
||||
lsort_array_i array_append_swap = {0};
|
||||
|
||||
if (test_array_i_append(&array_append_swap)) return 1;
|
||||
if (test_array_i_swap(&array_append_swap)) return 1;
|
||||
|
||||
lsort_array_i array_sort_check = {0};
|
||||
lsort_array_i_append(&array_sort_check, 1);
|
||||
lsort_array_i_append(&array_sort_check, 5);
|
||||
lsort_array_i_append(&array_sort_check, 7);
|
||||
lsort_array_i_append(&array_sort_check, 8);
|
||||
lsort_array_i_append(&array_sort_check, 15);
|
||||
lsort_array_i_append(&array_sort_check, 28);
|
||||
|
||||
if (check_sorted(&array_sort_check) == 1) return 1;
|
||||
lsort_array_i_swap(&array_sort_check, 2, 4);
|
||||
lsort_array_i_swap(&array_sort_check, 0, 3);
|
||||
if (check_sorted(&array_sort_check) == 0) return 1;
|
||||
|
||||
return 0;
|
||||
return return_code;
|
||||
}
|
||||
|
||||
int test_radix_sort() {
|
||||
printf("lsort_radix()...\n");
|
||||
|
||||
lsort_array_i array_radix = {0};
|
||||
lsort_array_i_append(&array_radix, -2);
|
||||
lsort_array_i_append(&array_radix, -15);
|
||||
lsort_array_i_append(&array_radix, 25);
|
||||
lsort_array_i_append(&array_radix, 56);
|
||||
lsort_array_i_append(&array_radix, 23);
|
||||
lsort_array_i_append(&array_radix, -222);
|
||||
lsort_array_i_append(&array_radix, -58);
|
||||
lsort_array* array_radix = generate_samples(sample_count);
|
||||
lsort_array* array_radix_return = lsort_array_create();
|
||||
int return_code = lsort_radix(array_radix, array_radix_return);
|
||||
|
||||
lsort_array_i array_radix_return = {0};
|
||||
lsort_radix(&array_radix, &array_radix_return);
|
||||
|
||||
if (check_sorted(&array_radix_return) != 0) return 1;
|
||||
|
||||
return 0;
|
||||
lsort_array_destroy(array_radix, 1);
|
||||
return return_code;
|
||||
}
|
||||
|
||||
int test_quicksort() {
|
||||
printf("lsort_quicksort()...\n");
|
||||
lsort_array_i array_quicksort = {0};
|
||||
|
||||
lsort_array_i_append(&array_quicksort,6);
|
||||
lsort_array_i_append(&array_quicksort, 5);
|
||||
lsort_array_i_append(&array_quicksort, 4);
|
||||
lsort_array_i_append(&array_quicksort, 3);
|
||||
lsort_array_i_append(&array_quicksort, 2);
|
||||
lsort_array_i_append(&array_quicksort, 5);
|
||||
lsort_array_i_append(&array_quicksort, 1);
|
||||
lsort_array_i_append(&array_quicksort, 0);
|
||||
|
||||
lsort_quicksort(&array_quicksort, NULL);
|
||||
lsort_array* array_quicksort = generate_samples(sample_count);
|
||||
int return_code = lsort_quicksort(array_quicksort);
|
||||
|
||||
if (check_sorted(&array_quicksort)) return 1;
|
||||
|
||||
|
||||
return 0;
|
||||
lsort_array_destroy(array_quicksort, 1);
|
||||
return return_code;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
|
||||
if (
|
||||
test_append_swap() ||
|
||||
test_array_append() ||
|
||||
test_array_swap() ||
|
||||
test_radix_sort() ||
|
||||
test_quicksort()
|
||||
) return 1;
|
||||
@@ -107,4 +96,4 @@ int main(void) {
|
||||
|
||||
printf("\nTest completed successfully.\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user