From ddc7eee87d6e43dc731eafdfc178a7d87006b272 Mon Sep 17 00:00:00 2001 From: Edward A Maxedon Date: Mon, 2 Apr 2018 19:44:21 -0400 Subject: [PATCH] fix quicksort test --- src/test/scala/ExamplesNoCompressed.scala | 12 +++ tests/quicksort64.c | 103 ++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 tests/quicksort64.c diff --git a/src/test/scala/ExamplesNoCompressed.scala b/src/test/scala/ExamplesNoCompressed.scala index b8e2627..c41e342 100644 --- a/src/test/scala/ExamplesNoCompressed.scala +++ b/src/test/scala/ExamplesNoCompressed.scala @@ -73,4 +73,16 @@ class ExamplesNoCompressed extends FreeSpec with PropertyChecks with Matchers { """.trim.stripMargin } + "quicksort" in { + Run( "tests/quicksort.hex" ) shouldBe + """ + |[1, 2, 3, 7, 7, 7, 7, 8, 9, 10] + |[1, 2, 3, 5, 7, 7, 7, 8, 9, 10] + |[1, 2, 3, 4, 5, 7, 7, 8, 9, 10] + |[9, 10] + |[10] + |[] + """.trim.stripMargin + } + } \ No newline at end of file diff --git a/tests/quicksort64.c b/tests/quicksort64.c new file mode 100644 index 0000000..9197e16 --- /dev/null +++ b/tests/quicksort64.c @@ -0,0 +1,103 @@ +void +out( char c ) { + *((char*) 0x20000) = c; +} + +//#include +//#define out( c ) putchar( c ) + +void +print( char* s ) { + while (*s) + out( *s++ ); +} + +void +println( char* s ) { + print( s ); + out( '\n' ); +} + +char* +bin2str( long n, int radix, char* buf ) { + char digits[] = "0123456789ABCDEF"; + char* p = &buf[33]; + long quo = n; + + if (n < 0) + quo = -quo; + + *p-- = 0; + + while (quo >= radix) { + *p-- = digits[(quo%radix)]; + quo /= radix; + } + + *p = digits[quo]; + + if (n < 0) + *--p = '-'; + + return p; +} + +void +sort( long a[], int left, int right ) { + if (right > left) { + int i = left; + int j = right; + long tmp = 0; + long p = a[right]; + + do { + while (a[i] < p) + i++; + + while (a[j] > p) + j--; + + if (i <= j) { + tmp = a[i]; + a[i] = a[j]; + a[j] = tmp; + i++; + j--; + } + } while (i <= j); + + if (left < j) + sort( a, left, j ); + + if (i < right) + sort( a, i, right ); + } +} + +void +run( long array[], int size ) { + sort( array, 0, size - 1 ); + print( "[" ); + + for (int i = 0; i < size; i++) { + char buf[34]; + char* s = bin2str( array[i], 10, buf ); + + print( s ); + + if (i < size - 1) + print( ", " ); + } + + println( "]" ); +} + +void +main() { + run( (long[]){10, 9, 8, 7, 7, 7, 7, 3, 2, 1}, 10 ); + run( (long[]){10, 9, 8, 7, 7, 5, 7, 3, 2, 1}, 10 ); + run( (long[]){10, 9, 8, 7, 5, 7, 4, 3, 2, 1}, 10 ); + run( (long[]){10, 9}, 2 ); + run( (long[]){10}, 1 ); + run( (long[]){}, 0 ); +} \ No newline at end of file