/** * generic-testing-tools.c * A variety of tools to make testing easier (or so I hope). * Note: TYPE, PREFIX(FUN), MAY_PRECEDE(X,Y) and EQUALS(X,Y) must be * defined in order for this to work. */ int PREFIX(is_in_order) (TYPE values[], int size) { int i; for (i = 1; i < size; i++) if (! MAY_PRECEDE(values[i-1], values[i])) return 0; return 1; } // is_in_order int PREFIX(is_permutation) (TYPE a1[], TYPE a2[], int size) { int i; for (i = 0; i < size; i++) { // This is made a little bit more step-by-step than normal for // clarity. Basically, we make sure that every element of a2 // appears the same number of times in a2 and a1. TYPE val = a2[i]; int c1 = PREFIX(count) (a1, size, val); int c2 = PREFIX(count) (a2, size, val); if (c1 != c2) return 0; } // for return 1; } // is_permutation int PREFIX(index_of) (TYPE a[], int limit, TYPE value) { int i; for (i = 0; i < limit; i++) if (EQUALS(a[i], value)) return i; return -1; } // index_of int PREFIX(count) (TYPE a[], int limit, TYPE value) { int i; int count = 0; for (i = 0; i < limit; i++) if (EQUALS(a[i], value)) ++count; return count; } // count