Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.55 KB | None | 0 0
  1. #include <assert.h>
  2.  
  3. #include "array-utils5A.h"
  4.  
  5. int is_sorted(const int *a, int n)
  6. {
  7.   assert (n >= 1);
  8.  
  9.   int result = 0, i;
  10.   for (i = 1; i < n; i++) {
  11.     if (a[i - 1] <= a[i])
  12.       result = 1;
  13.     else
  14.       result = 0;
  15.   }
  16.   return result;
  17. }
  18.  
  19. int all_different(const int *a, int n)
  20. {
  21.   assert(n > 1);
  22.  
  23.   int i, j;
  24.   for (i = 0; i < n; i++) {
  25.     for (j = 0; j < i; j++) {
  26.       if (a[i] != a[j])
  27.         return 1;
  28.       else
  29.         return 0;
  30.     }
  31.   }
  32.   return 1;
  33. }
  34.  
  35. #ifdef UNIT_TESTS
  36. #include <stdio.h>
  37.  
  38. // This macro works for variables declared to be arrays. (DON'T try to
  39. // use it for function parameters declared to be arrays!)
  40. #define COUNT(x) (sizeof(x) / sizeof(x[0]))
  41.  
  42. void test_is_sorted(const char *tag,
  43.                             const int *a, int n, int expected_rv);
  44.  
  45. void test_all_different(const char *tag,
  46.                         const int *a, int n, int expected_rv);
  47.  
  48. int main(void)
  49. {
  50.   int test_01[] = {10, 10, 10, 10, 10};
  51.   int test_02[] = {11};
  52.   int test_03[] = {11, 12, 13, 14, 15};
  53.   int test_04[] = {16, 17, 18, 17, 18, 19};
  54.   int test_05[] = {20, 22, 24, 26, 25};
  55.   test_is_sorted("test_01", test_01, COUNT(test_01), 1);
  56.   test_is_sorted("test_02", test_02, COUNT(test_02), 1);
  57.   test_is_sorted("test_03", test_03, COUNT(test_03), 1);
  58.   test_is_sorted("test_04", test_04, COUNT(test_04), 0);
  59.   test_is_sorted("test_05", test_05, COUNT(test_05), 0);
  60.   fputc('\n', stdout);
  61.  
  62.   int test_06[] = { 30, 31 };
  63.   int test_07[] = { 32, 35, 34, 36, 31 };
  64.   int test_08[] = { 40, 41, 42, 43, 44, 40 };
  65.   int test_09[] = { 50, 50, 51, 52, 53};
  66.   int test_10[] = { 60, 61, 62, 63, 64, 64 };
  67.   test_all_different("test_06", test_06, COUNT(test_06), 1);
  68.   test_all_different("test_07", test_07, COUNT(test_07), 1);
  69.   test_all_different("test_08", test_08, COUNT(test_08), 0);
  70.   test_all_different("test_09", test_09, COUNT(test_09), 0);
  71.   test_all_different("test_10", test_10, COUNT(test_10), 0);
  72.   fputc('\n', stdout);
  73.  
  74.   return 0;
  75. }
  76.  
  77. void test_is_sorted(const char *tag,
  78.                      const int *a, int n, int expected_rv)
  79. {
  80.   printf("Testing is_sorted for case with tag \"%s\":", tag);
  81.   if (expected_rv == is_sorted(a, n))
  82.     printf(" Pass.\n");
  83.   else
  84.     printf(" FAIL!\n");
  85. }
  86.  
  87. void test_all_different(const char *tag,
  88.                         const int *a, int n, int expected_rv)
  89. {
  90.   printf("Testing all_different for case with tag \"%s\":", tag);
  91.   if (expected_rv == all_different(a, n))
  92.     printf(" Pass.\n");
  93.   else
  94.     printf(" FAIL!\n");
  95. }
  96.  
  97. #endif // #ifdef UNIT_TESTS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement