Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <assert.h>
- #include "array-utils5A.h"
- int is_sorted(const int *a, int n)
- {
- assert (n >= 1);
- int result = 0, i;
- for (i = 1; i < n; i++) {
- if (a[i - 1] <= a[i])
- result = 1;
- else
- result = 0;
- }
- return result;
- }
- int all_different(const int *a, int n)
- {
- assert(n > 1);
- int i, j;
- for (i = 0; i < n; i++) {
- for (j = 0; j < i; j++) {
- if (a[i] != a[j])
- return 1;
- else
- return 0;
- }
- }
- return 1;
- }
- #ifdef UNIT_TESTS
- #include <stdio.h>
- // This macro works for variables declared to be arrays. (DON'T try to
- // use it for function parameters declared to be arrays!)
- #define COUNT(x) (sizeof(x) / sizeof(x[0]))
- void test_is_sorted(const char *tag,
- const int *a, int n, int expected_rv);
- void test_all_different(const char *tag,
- const int *a, int n, int expected_rv);
- int main(void)
- {
- int test_01[] = {10, 10, 10, 10, 10};
- int test_02[] = {11};
- int test_03[] = {11, 12, 13, 14, 15};
- int test_04[] = {16, 17, 18, 17, 18, 19};
- int test_05[] = {20, 22, 24, 26, 25};
- test_is_sorted("test_01", test_01, COUNT(test_01), 1);
- test_is_sorted("test_02", test_02, COUNT(test_02), 1);
- test_is_sorted("test_03", test_03, COUNT(test_03), 1);
- test_is_sorted("test_04", test_04, COUNT(test_04), 0);
- test_is_sorted("test_05", test_05, COUNT(test_05), 0);
- fputc('\n', stdout);
- int test_06[] = { 30, 31 };
- int test_07[] = { 32, 35, 34, 36, 31 };
- int test_08[] = { 40, 41, 42, 43, 44, 40 };
- int test_09[] = { 50, 50, 51, 52, 53};
- int test_10[] = { 60, 61, 62, 63, 64, 64 };
- test_all_different("test_06", test_06, COUNT(test_06), 1);
- test_all_different("test_07", test_07, COUNT(test_07), 1);
- test_all_different("test_08", test_08, COUNT(test_08), 0);
- test_all_different("test_09", test_09, COUNT(test_09), 0);
- test_all_different("test_10", test_10, COUNT(test_10), 0);
- fputc('\n', stdout);
- return 0;
- }
- void test_is_sorted(const char *tag,
- const int *a, int n, int expected_rv)
- {
- printf("Testing is_sorted for case with tag \"%s\":", tag);
- if (expected_rv == is_sorted(a, n))
- printf(" Pass.\n");
- else
- printf(" FAIL!\n");
- }
- void test_all_different(const char *tag,
- const int *a, int n, int expected_rv)
- {
- printf("Testing all_different for case with tag \"%s\":", tag);
- if (expected_rv == all_different(a, n))
- printf(" Pass.\n");
- else
- printf(" FAIL!\n");
- }
- #endif // #ifdef UNIT_TESTS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement