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/Makefile | 87 ++++++++++++++++++++-------------------------- test/mppa/sort/README.md | 17 +++++++++ test/mppa/sort/insertion.c | 17 +++++---- test/mppa/sort/main.c | 34 ++++++++++++++++++ test/mppa/sort/merge.c | 25 +++++++------ test/mppa/sort/selection.c | 21 ++++++----- test/mppa/sort/test.c | 33 ------------------ 7 files changed, 125 insertions(+), 109 deletions(-) create mode 100644 test/mppa/sort/README.md create mode 100644 test/mppa/sort/main.c delete mode 100644 test/mppa/sort/test.c (limited to 'test/mppa/sort') diff --git a/test/mppa/sort/Makefile b/test/mppa/sort/Makefile index 26c597be..c0c9347d 100644 --- a/test/mppa/sort/Makefile +++ b/test/mppa/sort/Makefile @@ -1,64 +1,40 @@ -PRNG=../lib/prng.c -CCOMP=../../../ccomp +K1CC ?= k1-mbr-gcc +CC ?= gcc +CCOMP ?= ccomp +CFLAGS ?= -O2 -ALL= insertion-test-x86 insertion-test-k1c\ - selection-test-x86 selection-test-k1c\ - merge-test-x86 merge-test-k1c\ - test-x86 test-k1c\ - test-ccomp +PRNG=../prng/prng.c -all: $(ALL) - -%-test-x86: %.c $(PRNG) - gcc -g -D__UNIT_TEST_$$(echo $(basename $<) | tr a-z A-Z)__ -O2 -std=c99 $^ -o $@ +CFILES=insertion.c merge.c selection.c main.c -%-test-k1c: %.c $(PRNG) - k1-mbr-gcc -g -D__UNIT_TEST_$$(echo $(basename $<) | tr a-z A-Z)__ -O2 -std=c99 $^ -o $@ +ALL= insertion-test-gcc-x86 insertion-test-gcc-k1c\ + selection-test-gcc-x86 selection-test-gcc-k1c\ + merge-test-gcc-x86 merge-test-gcc-k1c\ + main-test-gcc-x86 main-test-gcc-k1c\ + main-test-ccomp-k1c -test-x86: selection.c merge.c insertion.c test.c $(PRNG) - gcc -g -O2 -std=c99 $^ -o $@ +all: $(ALL) -test-k1c: selection.c merge.c insertion.c test.c $(PRNG) - k1-mbr-gcc -g -O2 -std=c99 $^ -o $@ +main-test-gcc-x86: $(CFILES) $(PRNG) + $(CC) $(CFLAGS) $^ -o $@ -%.s: %.c $(CCOMP) - ccomp -O2 -S $< -o $@ +%-test-gcc-x86: %.c $(PRNG) + $(CC) -D__UNIT_TEST_$$(echo $(basename $<) | tr a-z A-Z)__ $(CFLAGS) $^ -o $@ -test-ccomp: selection.s merge.s insertion.s test.s $(subst .c,.s,$(PRNG)) - k1-mbr-gcc $^ -o $@ +main-test-gcc-k1c: $(CFILES) $(PRNG) + $(K1CC) $(CFLAGS) $^ -o $@ -.PHONY: -unittest: unittest-x86 unittest-k1c +%-test-gcc-k1c: %.c $(PRNG) + $(K1CC) -D__UNIT_TEST_$$(echo $(basename $<) | tr a-z A-Z)__ $(CFLAGS) $^ -o $@ -.PHONY: -check: check-x86 check-k1c +main-test-ccomp-k1c: $(CFILES) $(PRNG) + $(CCOMP) $(CFLAGS) $^ -o $@ -.PHONY: -compc-check: test-ccomp - @if ! k1-cluster -- ./$<; then\ - >&2 echo "ERROR k1c: sort $< failed";\ - else\ - echo "k1c: Test sort $< succeeded";\ - fi - -.PHONY: -check-x86: test-x86 - @if ! ./$<; then\ - >&2 echo "ERROR x86: $< failed";\ - else\ - echo "x86: Test $< succeeded";\ - fi +%-test-ccomp-k1c: %.c $(PRNG) + $(CCOMP) -D__UNIT_TEST_$$(echo $(basename $<) | tr a-z A-Z)__ $(CFLAGS) $^ -o $@ .PHONY: -check-k1c: test-k1c - @if ! k1-cluster -- ./$<; then\ - >&2 echo "ERROR k1c: $< failed";\ - else\ - echo "k1c: Test $< succeeded";\ - fi - -.PHONY: -unittest-x86: insertion-test-x86 selection-test-x86 merge-test-x86 +test-x86: insertion-test-gcc-x86 selection-test-gcc-x86 merge-test-gcc-x86 main-test-gcc-x86 @for test in $^; do\ if ! ./$$test; then\ >&2 echo "ERROR x86: $$test failed";\ @@ -68,7 +44,7 @@ unittest-x86: insertion-test-x86 selection-test-x86 merge-test-x86 done .PHONY: -unittest-k1c: insertion-test-k1c selection-test-k1c merge-test-k1c +test-k1c: insertion-test-gcc-k1c selection-test-gcc-k1c merge-test-gcc-k1c main-test-gcc-k1c @for test in $^; do\ if ! k1-cluster -- ./$$test; then\ >&2 echo "ERROR k1c: $$test failed";\ @@ -77,6 +53,17 @@ unittest-k1c: insertion-test-k1c selection-test-k1c merge-test-k1c fi;\ done +.PHONY: +test: test-x86 test-k1c + +.PHONY: +check: main-test-ccomp-k1c + @if ! k1-cluster -- ./$<; then\ + >&2 echo "ERROR k1c: sort $< failed";\ + else\ + echo "k1c: Test sort $< succeeded";\ + fi + .PHONY: clean: rm -f $(ALL) diff --git a/test/mppa/sort/README.md b/test/mppa/sort/README.md new file mode 100644 index 00000000..b4c2279b --- /dev/null +++ b/test/mppa/sort/README.md @@ -0,0 +1,17 @@ +PRNG +==== + +This is a simple Pseudo Random Number Generator. + +`prng.c` contains a simple unitary test that compares the sum of the "bytewise sum" +of 1000 generated numbers to a hardcoded result, that is the one obtained with +`gcc -O2` on a x86 processor, and returns 0 if the result is correct. + +The following commands can be run inside that folder: + +- `make`: produces the unitary test binaries + - `prng-test-gcc-x86` : binary from gcc on x86 + - `prng-test-k1c-x86` : binary from gcc on k1c + - `prng-test-ccomp-x86` : binary from ccomp on k1c +- `make test`: tests the return value of the binaries produced by gcc. +- `make check`: tests the return value of the binary produced by CompCert. diff --git a/test/mppa/sort/insertion.c b/test/mppa/sort/insertion.c index 88093b64..bca09599 100644 --- a/test/mppa/sort/insertion.c +++ b/test/mppa/sort/insertion.c @@ -1,5 +1,5 @@ -#include "../lib/prng.h" -#include "../lib/types.h" +#include "../prng/prng.h" +#include "../prng/types.h" #ifdef __UNIT_TEST_INSERTION__ #define SIZE 100 @@ -14,16 +14,18 @@ void swap_ins(uint64_t *a, uint64_t *b){ } int insert_sort(uint64_t *res, const uint64_t *T){ + int i, j; + if (SIZE <= 0) return -1; - for (int i = 0 ; i < SIZE ; i++) + for (i = 0 ; i < SIZE ; i++) res[i] = T[i]; - for (int i = 0 ; i < SIZE-1 ; i++){ + for (i = 0 ; i < SIZE-1 ; i++){ if (res[i] > res[i+1]){ swap_ins(&res[i], &res[i+1]); - for (int j = i ; j > 0 ; j--) + for (j = i ; j > 0 ; j--) if (res[j-1] > res[j]) swap_ins(&res[j-1], &res[j]); } @@ -36,9 +38,10 @@ int insert_sort(uint64_t *res, const uint64_t *T){ int main(void){ uint64_t T[SIZE]; uint64_t res[SIZE]; + int i; srand(42); - for (int i = 0 ; i < SIZE ; i++) + for (i = 0 ; i < SIZE ; i++) T[i] = randlong(); /* Sorting the table */ @@ -46,7 +49,7 @@ int main(void){ /* Computing max(T) */ uint64_t max = T[0]; - for (int i = 1 ; i < SIZE ; i++) + for (i = 1 ; i < SIZE ; i++) if (T[i] > max) max = T[i]; diff --git a/test/mppa/sort/main.c b/test/mppa/sort/main.c new file mode 100644 index 00000000..aef419aa --- /dev/null +++ b/test/mppa/sort/main.c @@ -0,0 +1,34 @@ +#include "../prng/prng.h" +#include "../prng/types.h" + +#include "test.h" +#include "insertion.h" +#include "selection.h" +#include "merge.h" + +int main(void){ + uint64_t T[SIZE]; + uint64_t res1[SIZE], res2[SIZE], res3[SIZE]; + int i; + srand(42); + + for (i = 0 ; i < SIZE ; i++) + T[i] = randlong(); + + /* insertion sort */ + if (insert_sort(res1, T) < 0) return -1; + + /* selection sort */ + if (select_sort(res2, T) < 0) return -2; + + /* merge sort */ + if (merge_sort(res3, T) < 0) return -3; + + /* We should have: res1[i] == res2[i] == res3[i] */ + for (i = 0 ; i < SIZE ; i++){ + if (!(res1[i] == res2[i] && res2[i] == res3[i])) + return -4; + } + + return 0; +} diff --git a/test/mppa/sort/merge.c b/test/mppa/sort/merge.c index b2d41ce3..99f8ba85 100644 --- a/test/mppa/sort/merge.c +++ b/test/mppa/sort/merge.c @@ -1,5 +1,5 @@ -#include "../lib/prng.h" -#include "../lib/types.h" +#include "../prng/prng.h" +#include "../prng/types.h" //https://en.wikipedia.org/wiki/Merge_sort @@ -15,8 +15,8 @@ int min(int a, int b){ void BottomUpMerge(const uint64_t *A, int iLeft, int iRight, int iEnd, uint64_t *B) { - int i = iLeft, j = iRight; - for (int k = iLeft; k < iEnd; k++) { + int i = iLeft, j = iRight, k; + for (k = iLeft; k < iEnd; k++) { if (i < iRight && (j >= iEnd || A[i] <= A[j])) { B[k] = A[i]; i = i + 1; @@ -30,18 +30,20 @@ void BottomUpMerge(const uint64_t *A, int iLeft, int iRight, int iEnd, uint64_t void CopyArray(uint64_t *to, const uint64_t *from) { const int n = SIZE; + int i; - for(int i = 0; i < n; i++) + for(i = 0; i < n; i++) to[i] = from[i]; } void BottomUpMergeSort(uint64_t *A, uint64_t *B) { const int n = SIZE; + int width, i; - for (int width = 1; width < n; width = 2 * width) + for (width = 1; width < n; width = 2 * width) { - for (int i = 0; i < n; i = i + 2 * width) + for (i = 0; i < n; i = i + 2 * width) { BottomUpMerge(A, i, min(i+width, n), min(i+2*width, n), B); } @@ -50,12 +52,14 @@ void BottomUpMergeSort(uint64_t *A, uint64_t *B) } int merge_sort(uint64_t *res, const uint64_t *T){ + int i; + if (SIZE <= 0) return -1; uint64_t B[SIZE]; uint64_t *A = res; - for (int i = 0 ; i < SIZE ; i++) + for (i = 0 ; i < SIZE ; i++) A[i] = T[i]; BottomUpMergeSort(A, B); @@ -67,9 +71,10 @@ int merge_sort(uint64_t *res, const uint64_t *T){ int main(void){ uint64_t T[SIZE]; uint64_t res[SIZE]; + int i; srand(42); - for (int i = 0 ; i < SIZE ; i++) + for (i = 0 ; i < SIZE ; i++) T[i] = randlong(); /* Sorting the table */ @@ -77,7 +82,7 @@ int main(void){ /* Computing max(T) */ uint64_t max = T[0]; - for (int i = 1 ; i < SIZE ; i++) + for (i = 1 ; i < SIZE ; i++) if (T[i] > max) max = T[i]; 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]; diff --git a/test/mppa/sort/test.c b/test/mppa/sort/test.c deleted file mode 100644 index e5e14fef..00000000 --- a/test/mppa/sort/test.c +++ /dev/null @@ -1,33 +0,0 @@ -#include "../lib/prng.h" -#include "../lib/types.h" - -#include "test.h" -#include "insertion.h" -#include "selection.h" -#include "merge.h" - -int main(void){ - uint64_t T[SIZE]; - uint64_t res1[SIZE], res2[SIZE], res3[SIZE]; - srand(42); - - for (int i = 0 ; i < SIZE ; i++) - T[i] = randlong(); - - /* insertion sort */ - if (insert_sort(res1, T) < 0) return -1; - - /* selection sort */ - if (select_sort(res2, T) < 0) return -2; - - /* merge sort */ - if (merge_sort(res3, T) < 0) return -3; - - /* We should have: res1[i] == res2[i] == res3[i] */ - for (int i = 0 ; i < SIZE ; i++){ - if (!(res1[i] == res2[i] && res2[i] == res3[i])) - return -4; - } - - return 0; -} -- cgit