Advertisement
Guest User

Untitled

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