From eb1e1c79fa3fc882b68c67d781f7b64e74e00828 Mon Sep 17 00:00:00 2001 From: Cyril SIX Date: Tue, 17 Apr 2018 12:01:18 +0200 Subject: MPPA - tests - added insertion sort and selection sort --- test/mppa/sort/selection.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 test/mppa/sort/selection.c (limited to 'test/mppa/sort/selection.c') diff --git a/test/mppa/sort/selection.c b/test/mppa/sort/selection.c new file mode 100644 index 00000000..432bbf49 --- /dev/null +++ b/test/mppa/sort/selection.c @@ -0,0 +1,55 @@ +#include "../lib/prng.h" +#include "../lib/types.h" + +void swap(uint64_t *a, uint64_t *b){ + uint64_t tmp = *a; + *a = *b; + *b = tmp; +} + +int selection_sort(uint64_t *res, const uint64_t *T, int size){ + if (size <= 0) + return -1; + + for (int i = 0 ; i < size ; i++) + res[i] = T[i]; + + for (int j = 0 ; j < size ; j++){ + int i; + int iMin = j; + for (i = j+1 ; i < size ; i++) + if (res[i] < res[iMin]) + iMin = i; + + if (iMin != j) + swap (&res[j], &res[iMin]); + } + + return 0; +} + +#ifdef __UNIT_TEST_SELECTION__ +#define SIZE 100 + +int main(void){ + uint64_t T[SIZE]; + uint64_t res[SIZE]; + srand(42); + + for (int i = 0 ; i < SIZE ; i++) + T[i] = randlong(); + + /* Sorting the table */ + if (selection_sort(res, T, SIZE) < 0) return -1; + + /* Computing max(T) */ + uint64_t max = T[0]; + for (int i = 1 ; i < SIZE ; i++) + if (T[i] > max) + max = T[i]; + + /* We should have: max(T) == res[SIZE] */ + return !(max == res[SIZE-1]); +} +#endif // __UNIT_TEST_SELECTION__ + -- cgit From 1b8cf73abc25b1bb167db770a622704f0d672691 Mon Sep 17 00:00:00 2001 From: Cyril SIX Date: Tue, 17 Apr 2018 15:33:14 +0200 Subject: MPPA - added merge sort + corrected bug in insertion + testing them together --- test/mppa/sort/selection.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'test/mppa/sort/selection.c') diff --git a/test/mppa/sort/selection.c b/test/mppa/sort/selection.c index 432bbf49..89bc2c65 100644 --- a/test/mppa/sort/selection.c +++ b/test/mppa/sort/selection.c @@ -1,36 +1,40 @@ #include "../lib/prng.h" #include "../lib/types.h" -void swap(uint64_t *a, uint64_t *b){ +#ifdef __UNIT_TEST_SELECTION__ +#define SIZE 100 +#else +#include "test.h" +#endif + +void swap_sel(uint64_t *a, uint64_t *b){ uint64_t tmp = *a; *a = *b; *b = tmp; } -int selection_sort(uint64_t *res, const uint64_t *T, int size){ - if (size <= 0) +int select_sort(uint64_t *res, const uint64_t *T){ + if (SIZE <= 0) return -1; - for (int i = 0 ; i < size ; i++) + for (int i = 0 ; i < SIZE ; i++) res[i] = T[i]; - for (int j = 0 ; j < size ; j++){ + for (int j = 0 ; j < SIZE ; j++){ int i; int iMin = j; - for (i = j+1 ; i < size ; i++) + for (i = j+1 ; i < SIZE ; i++) if (res[i] < res[iMin]) iMin = i; if (iMin != j) - swap (&res[j], &res[iMin]); + swap_sel (&res[j], &res[iMin]); } return 0; } #ifdef __UNIT_TEST_SELECTION__ -#define SIZE 100 - int main(void){ uint64_t T[SIZE]; uint64_t res[SIZE]; @@ -40,7 +44,7 @@ int main(void){ T[i] = randlong(); /* Sorting the table */ - if (selection_sort(res, T, SIZE) < 0) return -1; + if (select_sort(res, T) < 0) return -1; /* Computing max(T) */ uint64_t max = T[0]; -- cgit From f24d303df6cb125ca19b953bb364955cc6e8c246 Mon Sep 17 00:00:00 2001 From: Cyril SIX Date: Fri, 9 Nov 2018 17:07:13 +0100 Subject: Fixed consistency between the different tests mmult, prng and sort --- test/mppa/sort/selection.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'test/mppa/sort/selection.c') diff --git a/test/mppa/sort/selection.c b/test/mppa/sort/selection.c index 89bc2c65..df4be04f 100644 --- a/test/mppa/sort/selection.c +++ b/test/mppa/sort/selection.c @@ -1,5 +1,5 @@ -#include "../lib/prng.h" -#include "../lib/types.h" +#include "../prng/prng.h" +#include "../prng/types.h" #ifdef __UNIT_TEST_SELECTION__ #define SIZE 100 @@ -14,15 +14,16 @@ void swap_sel(uint64_t *a, uint64_t *b){ } int select_sort(uint64_t *res, const uint64_t *T){ + int i, j, iMin; + if (SIZE <= 0) return -1; - for (int i = 0 ; i < SIZE ; i++) + for (i = 0 ; i < SIZE ; i++) res[i] = T[i]; - for (int j = 0 ; j < SIZE ; j++){ - int i; - int iMin = j; + for (j = 0 ; j < SIZE ; j++){ + iMin = j; for (i = j+1 ; i < SIZE ; i++) if (res[i] < res[iMin]) iMin = i; @@ -38,17 +39,19 @@ int select_sort(uint64_t *res, const uint64_t *T){ int main(void){ uint64_t T[SIZE]; uint64_t res[SIZE]; + uint64_t max; + int i; srand(42); - for (int i = 0 ; i < SIZE ; i++) + for (i = 0 ; i < SIZE ; i++) T[i] = randlong(); /* Sorting the table */ if (select_sort(res, T) < 0) return -1; /* Computing max(T) */ - uint64_t max = T[0]; - for (int i = 1 ; i < SIZE ; i++) + max = T[0]; + for (i = 1 ; i < SIZE ; i++) if (T[i] > max) max = T[i]; -- cgit