Selaa lähdekoodia

tests; refractor

master
Robin Thoni 8 vuotta sitten
vanhempi
commit
33563473fd
19 muutettua tiedostoa jossa 147 lisäystä ja 46 poistoa
  1. 1
    0
      .gitignore
  2. 1
    1
      Makefile
  3. 8
    43
      common.c
  4. 0
    2
      common.h
  5. 6
    0
      d2p.c
  6. 1
    0
      d2s.c
  7. 41
    0
      mergepar.c
  8. 6
    0
      mergepar.h
  9. 41
    0
      mergeseq.c
  10. 6
    0
      mergeseq.h
  11. 24
    0
      test.sh
  12. 2
    0
      tests/01.in
  13. 1
    0
      tests/01.out
  14. 2
    0
      tests/02.in
  15. 1
    0
      tests/02.out
  16. 2
    0
      tests/03.in
  17. 1
    0
      tests/03.out
  18. 2
    0
      tests/04.in
  19. 1
    0
      tests/04.out

+ 1
- 0
.gitignore Näytä tiedosto

@@ -1,3 +1,4 @@
1 1
 omp-merge-sort-seq
2 2
 omp-merge-sort-par
3 3
 *.o
4
+*.swp

+ 1
- 1
Makefile Näytä tiedosto

@@ -1,7 +1,7 @@
1 1
 CFLAGS = -Wall -Wextra -Werror -pedantic -std=c99 -fopenmp
2 2
 LDLIBS = -lrt
3 3
 CC = gcc
4
-SOURCES_COMMON = common.c
4
+SOURCES_COMMON = common.c mergeseq.c mergepar.c
5 5
 SOURCES_SEQ = $(SOURCES_COMMON) d2s.c
6 6
 OUT_SEQ = omp-merge-sort-seq
7 7
 OBJS_SEQ = $(SOURCES_SEQ:.c=.o)

+ 8
- 43
common.c Näytä tiedosto

@@ -1,6 +1,7 @@
1 1
 #include "common.h"
2 2
 #include <stdio.h>
3 3
 #include <stdlib.h>
4
+#include <unistd.h>
4 5
 
5 6
 struct timespec get_time(void)
6 7
 {
@@ -48,56 +49,20 @@ int* read_int_array(unsigned n)
48 49
 
49 50
 void read_input(int** array, unsigned* n)
50 51
 {
51
-  printf("Enter n: ");
52
+  if (isatty(0)) {
53
+    printf("Enter n: ");
54
+  }
52 55
   scanf("%u", n);
53
-  printf("Enter %u integers: ", 2 * *n);
56
+  if (isatty(0)) {
57
+    printf("Enter %u integers: ", 2 * *n);
58
+  }
54 59
   *array = read_int_array(2 * *n);
55 60
 }
56 61
 
57 62
 void print_array(int* array, unsigned n)
58 63
 {
59 64
   for (unsigned i = 0; i < n; ++i) {
60
-    printf("%i ", array[i]);
65
+    printf("%i%s", array[i], i == n - 1 ? "" : " ");
61 66
   }
62 67
   printf("\n");
63 68
 }
64
-
65
-void merge_sort_merge_seq(int* array, unsigned l, unsigned m, unsigned h)
66
-{
67
-  unsigned i = l, j = m + 1, k = 0;
68
-  int tmp[h - l + 1];
69
-  while (i <= m && j <= h) {
70
-    if (array[i] <= array[j]) {
71
-      tmp[k++] = array[i++];
72
-    }
73
-    else {
74
-      tmp[k++] = array[j++];
75
-    }
76
-  }
77
-
78
-  while (i <= m) {
79
-    tmp[k++] = array[i++];
80
-  }
81
-  while (j <= h) {
82
-    tmp[k++] = array[j++];
83
-  }
84
-
85
-  for (--k; k != (unsigned)-1; --k) {
86
-    array[l + k] = tmp[k];
87
-  }
88
-}
89
-
90
-void merge_sort_sub_seq(int* array, unsigned l, unsigned h)
91
-{
92
-  if (l < h) {
93
-    unsigned m = (l + h) / 2;
94
-    merge_sort_sub_seq(array, l, m);
95
-    merge_sort_sub_seq(array, m + 1, h);
96
-    merge_sort_merge_seq(array, l, m, h);
97
-  }
98
-}
99
-
100
-void merge_sort_seq(int* array, unsigned s)
101
-{
102
-  merge_sort_sub_seq(array, 0, s - 1);
103
-}

+ 0
- 2
common.h Näytä tiedosto

@@ -13,6 +13,4 @@ int* read_int_array(unsigned n);
13 13
 void read_input(int** array, unsigned* n);
14 14
 void print_array(int* array, unsigned n);
15 15
 
16
-void merge_sort_seq(int* array, unsigned s);
17
-
18 16
 #endif

+ 6
- 0
d2p.c Näytä tiedosto

@@ -1,6 +1,12 @@
1 1
 #include "common.h"
2
+#include "mergepar.h"
2 3
 
3 4
 int main(void)
4 5
 {
6
+  int* array;
7
+  unsigned n;
8
+  read_input(&array, &n);
9
+  merge_sort_par(array, 2 * n);
10
+  print_array(array, 2 * n);
5 11
   return 0;
6 12
 }

+ 1
- 0
d2s.c Näytä tiedosto

@@ -1,4 +1,5 @@
1 1
 #include "common.h"
2
+#include "mergeseq.h"
2 3
 
3 4
 int main(void)
4 5
 {

+ 41
- 0
mergepar.c Näytä tiedosto

@@ -0,0 +1,41 @@
1
+#include "common.h"
2
+
3
+void merge_sort_merge_par(int* array, unsigned l, unsigned m, unsigned h)
4
+{
5
+  unsigned i = l, j = m + 1, k = 0;
6
+  int tmp[h - l + 1];
7
+  while (i <= m && j <= h) {
8
+    if (array[i] <= array[j]) {
9
+      tmp[k++] = array[i++];
10
+    }
11
+    else {
12
+      tmp[k++] = array[j++];
13
+    }
14
+  }
15
+
16
+  while (i <= m) {
17
+    tmp[k++] = array[i++];
18
+  }
19
+  while (j <= h) {
20
+    tmp[k++] = array[j++];
21
+  }
22
+
23
+  for (--k; k != (unsigned)-1; --k) {
24
+    array[l + k] = tmp[k];
25
+  }
26
+}
27
+
28
+void merge_sort_sub_par(int* array, unsigned l, unsigned h)
29
+{
30
+  if (l < h) {
31
+    unsigned m = (l + h) / 2;
32
+    merge_sort_sub_par(array, l, m);
33
+    merge_sort_sub_par(array, m + 1, h);
34
+    merge_sort_merge_par(array, l, m, h);
35
+  }
36
+}
37
+
38
+void merge_sort_par(int* array, unsigned s)
39
+{
40
+  merge_sort_sub_par(array, 0, s - 1);
41
+}

+ 6
- 0
mergepar.h Näytä tiedosto

@@ -0,0 +1,6 @@
1
+#ifndef MERGEPAR_H
2
+#define MERGEPAR_H
3
+
4
+void merge_sort_par(int* array, unsigned s);
5
+
6
+#endif

+ 41
- 0
mergeseq.c Näytä tiedosto

@@ -0,0 +1,41 @@
1
+#include "common.h"
2
+
3
+void merge_sort_merge_seq(int* array, unsigned l, unsigned m, unsigned h)
4
+{
5
+  unsigned i = l, j = m + 1, k = 0;
6
+  int tmp[h - l + 1];
7
+  while (i <= m && j <= h) {
8
+    if (array[i] <= array[j]) {
9
+      tmp[k++] = array[i++];
10
+    }
11
+    else {
12
+      tmp[k++] = array[j++];
13
+    }
14
+  }
15
+
16
+  while (i <= m) {
17
+    tmp[k++] = array[i++];
18
+  }
19
+  while (j <= h) {
20
+    tmp[k++] = array[j++];
21
+  }
22
+
23
+  for (--k; k != (unsigned)-1; --k) {
24
+    array[l + k] = tmp[k];
25
+  }
26
+}
27
+
28
+void merge_sort_sub_seq(int* array, unsigned l, unsigned h)
29
+{
30
+  if (l < h) {
31
+    unsigned m = (l + h) / 2;
32
+    merge_sort_sub_seq(array, l, m);
33
+    merge_sort_sub_seq(array, m + 1, h);
34
+    merge_sort_merge_seq(array, l, m, h);
35
+  }
36
+}
37
+
38
+void merge_sort_seq(int* array, unsigned s)
39
+{
40
+  merge_sort_sub_seq(array, 0, s - 1);
41
+}

+ 6
- 0
mergeseq.h Näytä tiedosto

@@ -0,0 +1,6 @@
1
+#ifndef MERGESEQ_H
2
+#define MERGESEQ_H
3
+
4
+void merge_sort_seq(int* array, unsigned s);
5
+
6
+#endif

+ 24
- 0
test.sh Näytä tiedosto

@@ -0,0 +1,24 @@
1
+#! /usr/bin/env sh
2
+
3
+for exe in omp-merge-sort-seq omp-merge-sort-par
4
+do
5
+
6
+echo "Running ${exe}"
7
+
8
+for tIn in tests/*.in
9
+do
10
+  tOut=$(echo "${tIn}" | sed -re 's/(.+).in/\1/')".out"
11
+  tmpfile=$(mktemp)
12
+  cat "${tIn}" | "./${exe}" > "${tmpfile}"
13
+  cmp "${tmpfile}" "${tOut}"
14
+  res=$?
15
+  if [ ${res} -ne 0 ]
16
+  then
17
+    echo "${tIn} failed"
18
+  else
19
+    echo "${tIn} succeded"
20
+    rm "${tmpfile}"
21
+  fi
22
+done
23
+
24
+done

+ 2
- 0
tests/01.in Näytä tiedosto

@@ -0,0 +1,2 @@
1
+1
2
+2 1

+ 1
- 0
tests/01.out Näytä tiedosto

@@ -0,0 +1 @@
1
+1 2

+ 2
- 0
tests/02.in Näytä tiedosto

@@ -0,0 +1,2 @@
1
+2
2
+4 3 2 1

+ 1
- 0
tests/02.out Näytä tiedosto

@@ -0,0 +1 @@
1
+1 2 3 4

+ 2
- 0
tests/03.in Näytä tiedosto

@@ -0,0 +1,2 @@
1
+2
2
+4 2 3 1

+ 1
- 0
tests/03.out Näytä tiedosto

@@ -0,0 +1 @@
1
+1 2 3 4

+ 2
- 0
tests/04.in Näytä tiedosto

@@ -0,0 +1,2 @@
1
+5
2
+9 5 3 6 2 1 4 6 6 9

+ 1
- 0
tests/04.out Näytä tiedosto

@@ -0,0 +1 @@
1
+1 2 3 4 5 6 6 6 9 9

Loading…
Peruuta
Tallenna