Index: trunk/psLib/test/math/tst_psStats07.c
===================================================================
--- trunk/psLib/test/math/tst_psStats07.c	(revision 6304)
+++ trunk/psLib/test/math/tst_psStats07.c	(revision 6316)
@@ -13,423 +13,427 @@
 #include <math.h>
 
-#define NUM_DATA 10000
+#define NUM_DATA 1000
 #define MEAN 32.0
 #define STDEV 2.0
-#define ERROR_TOLERANCE 0.15
-#define PERCENT_OUTLIERS 10
-
-psS32 t00()
+#define PERCENT_OUTLIERS 1
+#define OUTLIER_MAGNITUDE (NUM_DATA * 10)
+#define ERROR_TOLERANCE .10
+#define ERRORS 1000.0
+#define SEED 1995
+#define VERBOSE 0
+
+#define TST_IN_NULL             0x00000001
+#define TST_IN_F32              0x00000002
+#define TST_IN_F64              0x00000004
+#define TST_IN_S8               0x00000008
+#define TST_IN_U16              0x00000010
+#define TST_IN_S32              0x00000020
+#define TST_ERRORS_NULL         0x00000040
+#define TST_ERRORS_F32          0x00000080
+#define TST_ERRORS_F64          0x00000100
+#define TST_ERRORS_S8           0x00000200
+#define TST_ERRORS_U16          0x00000400
+#define TST_ERRORS_S32          0x00000800
+#define TST_MASK_NULL           0x00001000
+#define TST_MASK_U8             0x00002000
+#define TST_MASK_S32            0x00004000
+
+psBool genericRobustStatsTest(
+    unsigned int flags,
+    psS32 numData,
+    psU32 maskValue,
+    psBool expectedRC)
 {
-
-    psStats * myStats = NULL;
-    psS32 testStatus = true;
-    psS32 globalTestStatus = true;
-    psS32 i = 0;
-    psVector *myVector = NULL;
-    psVector *maskVector = NULL;
-    psS32 count = 0;
     psS32 currentId = psMemGetId();
+    psBool testStatus = true;
     psS32 memLeaks = 0;
-    float realMeanNoMask = MEAN;
-    float realMedianNoMask = MEAN;
-    float realStdevNoMask = STDEV;
-    float realLQNoMask = MEAN - ( 0.6 * STDEV );
-    float realUQNoMask = MEAN + ( 0.6 * STDEV );
-    psS32 realN50NoMask = NUM_DATA / 4;
-
-    /*************************************************************************/
-    /*  Allocate and initialize data structures                              */
-    /*************************************************************************/
-    maskVector = psVectorAlloc( NUM_DATA, PS_TYPE_U8 );
-    maskVector->n = NUM_DATA;
-    myVector = p_psGaussianDev( MEAN, STDEV, NUM_DATA );
-    // Set the mask vector and calculate the expected maximum.
-    for ( i = 0;i < NUM_DATA;i++ ) {
-        if ( i < ( NUM_DATA / 2 ) ) {
-            maskVector->data.U8[ i ] = 0;
-            count++;
+    psVector *in = NULL;
+    psVector *errors = NULL;
+    psVector *mask = NULL;
+    srand(SEED);
+    printPositiveTestHeader(stdout, "psMathUtils functions", "psVectorStats Robust Stats Routine");
+
+    if (expectedRC == true) {
+        printf("This test should not generate any errors.\n");
+    }
+    if (expectedRC == false) {
+        printf("This test should generate an error message, and return NULL.\n");
+    }
+    psVector *gaussVector = p_psGaussianDev(MEAN, STDEV, numData);
+
+
+    if (flags & TST_IN_NULL) {
+        printf("        using a NULL in vector\n");
+    }
+
+    if (flags & TST_IN_F32) {
+        printf("        using a psF32 in vector\n");
+        in = psVectorAlloc(numData, PS_TYPE_F32);
+        for (psS32 i=0;i<numData;i++) {
+            in->data.F32[i] = gaussVector->data.F32[i];
+        }
+
+        if (VERBOSE) {
+            for (psS32 i=0;i<numData;i++) {
+                printf("Original in data %d: (%.1f)\n", i, in->data.F32[i]);
+            }
+        }
+    }
+
+    if (flags & TST_IN_F64) {
+        printf("        using a psF64 in vector\n");
+        in = psVectorAlloc(numData, PS_TYPE_F64);
+        for (psS32 i=0;i<numData;i++) {
+            in->data.F64[i] = (psF64) gaussVector->data.F32[i];
+        }
+
+        if (VERBOSE) {
+            for (psS32 i=0;i<numData;i++) {
+                printf("Original in data %d: (%.1f)\n", i, in->data.F64[i]);
+            }
+        }
+    }
+
+    if (flags & TST_IN_S8) {
+        printf("        using a psS8 in vector\n");
+        in = psVectorAlloc(numData, PS_TYPE_S8);
+        for (psS32 i=0;i<numData;i++) {
+            in->data.S8[i] = (psS8) gaussVector->data.F32[i];
+        }
+
+        if (VERBOSE) {
+            for (psS32 i=0;i<numData;i++) {
+                printf("Original in data %d: (%d)\n", i, in->data.S8[i]);
+            }
+        }
+    }
+
+    if (flags & TST_IN_U16) {
+        printf("        using a psU16 in vector\n");
+        in = psVectorAlloc(numData, PS_TYPE_U16);
+        for (psS32 i=0;i<numData;i++) {
+            in->data.U16[i] = (psU16) gaussVector->data.F32[i];
+        }
+
+        if (VERBOSE) {
+            for (psS32 i=0;i<numData;i++) {
+                printf("Original in data %d: (%d)\n", i, in->data.U16[i]);
+            }
+        }
+    }
+
+    if (flags & TST_IN_S32) {
+        printf("        using a psS32 in vector\n");
+        in = psVectorAlloc(numData, PS_TYPE_S32);
+        for (psS32 i=0;i<numData;i++) {
+            in->data.S32[i] = (psS32) gaussVector->data.F32[i];
+        }
+
+        if (VERBOSE) {
+            for (psS32 i=0;i<numData;i++) {
+                printf("Original in data %d: (%d)\n", i, in->data.S32[i]);
+            }
+        }
+    }
+    psFree(gaussVector);
+
+    if (flags & TST_ERRORS_NULL) {
+        printf("        using a NULL errors vector\n");
+    }
+
+    if (flags & TST_ERRORS_F32) {
+        printf("        using a psF32 errors vector\n");
+        errors = psVectorAlloc(numData, PS_TYPE_F32);
+        for (psS32 i=0;i<numData;i++) {
+            errors->data.F32[i] = ERRORS;
+        }
+
+        if (VERBOSE) {
+            for (psS32 i=0;i<numData;i++) {
+                printf("Original errors data %d: (%.1f)\n", i, errors->data.F32[i]);
+            }
+        }
+    }
+
+    if (flags & TST_ERRORS_F64) {
+        printf("        using a psF64 errors vector\n");
+        errors = psVectorAlloc(numData, PS_TYPE_F64);
+        for (psS32 i=0;i<numData;i++) {
+            errors->data.F64[i] = ERRORS;
+        }
+
+        if (VERBOSE) {
+            for (psS32 i=0;i<numData;i++) {
+                printf("Original errors data %d: (%.1f)\n", i, errors->data.F64[i]);
+            }
+        }
+    }
+
+    if (flags & TST_ERRORS_S8) {
+        printf("        using a psS8 errors vector\n");
+        errors = psVectorAlloc(numData, PS_TYPE_S8);
+        for (psS32 i=0;i<numData;i++) {
+            errors->data.S8[i] = (psS8) ERRORS;
+        }
+
+        if (VERBOSE) {
+            for (psS32 i=0;i<numData;i++) {
+                printf("Original errors data %d: (%d)\n", i, errors->data.S8[i]);
+            }
+        }
+    }
+
+    if (flags & TST_ERRORS_U16) {
+        printf("        using a psU16 errors vector\n");
+        errors = psVectorAlloc(numData, PS_TYPE_U16);
+        for (psS32 i=0;i<numData;i++) {
+            errors->data.U16[i] = (psU16) ERRORS;
+        }
+
+        if (VERBOSE) {
+            for (psS32 i=0;i<numData;i++) {
+                printf("Original errors data %d: (%d)\n", i, errors->data.U16[i]);
+            }
+        }
+    }
+
+    if (flags & TST_ERRORS_S32) {
+        printf("        using a psS32 errors vector\n");
+        errors = psVectorAlloc(numData, PS_TYPE_S32);
+        for (psS32 i=0;i<numData;i++) {
+            errors->data.S32[i] = (psS32) ERRORS;
+        }
+
+        if (VERBOSE) {
+            for (psS32 i=0;i<numData;i++) {
+                printf("Original errors data %d: (%d)\n", i, errors->data.S32[i]);
+            }
+        }
+    }
+
+
+    if (flags & TST_MASK_NULL) {
+        printf("        using a NULL mask vector\n");
+    }
+
+    if (flags & TST_MASK_U8) {
+        printf("        using a psU8 mask vector\n");
+        mask = psVectorAlloc(numData, PS_TYPE_U8);
+        for (psS32 i=0;i<numData;i++) {
+            mask->data.U8[i] = (psU8) 0;
+        }
+
+        if (VERBOSE) {
+            for (psS32 i=0;i<numData;i++) {
+                printf("Original mask data %d: (%d)\n", i, mask->data.U8[i]);
+            }
+        }
+    }
+
+    if (flags & TST_MASK_S32) {
+        printf("        using a psS32 mask vector\n");
+        mask = psVectorAlloc(numData, PS_TYPE_S32);
+        for (psS32 i=0;i<numData;i++) {
+            mask->data.S32[i] = (psS32) 0;
+        }
+
+        if (VERBOSE) {
+            for (psS32 i=0;i<numData;i++) {
+                printf("Original mask data %d: (%d)\n", i, mask->data.S32[i]);
+            }
+        }
+    }
+
+
+    //
+    // We calculate the sample mean and stdev without and outliers in the data.
+    // We will use this later in determining if the clipped stats are correct.
+    //
+    psF32 sampleMean;
+    psF32 sampleStdev;
+    psF32 sampleMedian;
+    psF32 sampleLQ;
+    psF32 sampleUQ;
+    if (expectedRC == true) {
+        psStats *myStats = psStatsAlloc(PS_STAT_SAMPLE_MEAN | PS_STAT_SAMPLE_STDEV | PS_STAT_SAMPLE_MEDIAN | PS_STAT_SAMPLE_QUARTILE);
+        psStats *rc = psVectorStats(myStats, in, NULL, NULL, maskValue);
+        if (rc == NULL) {
+            printf("TEST ERROR: the psVectorStats() function returned NULL.\n");
+            testStatus = false;
         } else {
-            maskVector->data.U8[ i ] = 1;
-        }
-    }
-
-    //
-    // We calculate the exact mean, median, stdev and quartiles with no mask.
-    //
-    psStats *mySampleStats = psStatsAlloc( PS_STAT_SAMPLE_MEAN |
-                                           PS_STAT_SAMPLE_MEDIAN |
-                                           PS_STAT_SAMPLE_STDEV |
-                                           PS_STAT_SAMPLE_QUARTILE );
-    mySampleStats = psVectorStats( mySampleStats, myVector, NULL, NULL, 0 );
-    printf("The Sample Mean is %.2f\n", mySampleStats->sampleMean);
-    printf("The Sample Median is %.2f\n", mySampleStats->sampleMedian);
-    printf("The Sample stdev is %.2f\n", mySampleStats->sampleStdev);
-    printf("The Sample quartiles are (%.2f %.2f)\n", mySampleStats->sampleLQ, mySampleStats->sampleUQ);
-    psFree(mySampleStats);
-
-    //
-    // We calculate the exact mean, median, stdev and quartiles with mask.
-    //
-    psStats *mySampleStatsWithMask = psStatsAlloc( PS_STAT_SAMPLE_MEAN |
-                                     PS_STAT_SAMPLE_MEDIAN |
-                                     PS_STAT_SAMPLE_STDEV |
-                                     PS_STAT_SAMPLE_QUARTILE );
-    mySampleStatsWithMask = psVectorStats( mySampleStatsWithMask, myVector, NULL, maskVector, 1 );
-    printf("The Sample Mean is %.2f\n", mySampleStatsWithMask->sampleMean);
-    printf("The Sample Median is %.2f\n", mySampleStatsWithMask->sampleMedian);
-    printf("The Sample stdev is %.2f\n", mySampleStatsWithMask->sampleStdev);
-    printf("The Sample quartiles are (%.2f %.2f)\n", mySampleStatsWithMask->sampleLQ, mySampleStatsWithMask->sampleUQ);
-    psFree(mySampleStatsWithMask);
-
-    myStats = psStatsAlloc(PS_STAT_ROBUST_MEDIAN |
+            sampleMean = myStats->sampleMean;
+            sampleStdev = myStats->sampleStdev;
+            sampleMedian = myStats->sampleMedian;
+            sampleLQ = myStats->sampleLQ;
+            sampleUQ = myStats->sampleUQ;
+        }
+        psFree(myStats);
+    }
+
+    //
+    // We add a few outliers to the input data.
+    //
+    if (flags & TST_IN_F32) {
+        for (psS32 i=0;i<numData;i++) {
+            if (PERCENT_OUTLIERS > (random() % 100)) {
+                in->data.F32[i] = (psF32) OUTLIER_MAGNITUDE;
+            }
+        }
+
+        if (VERBOSE) {
+            for (psS32 i=0;i<numData;i++) {
+                printf("Original in data %d: (%.1f)\n", i, in->data.F32[i]);
+            }
+        }
+    }
+    if (flags & TST_IN_F64) {
+        for (psS32 i=0;i<numData;i++) {
+            if (PERCENT_OUTLIERS > (random() % 100)) {
+                in->data.F64[i] = (psF64) OUTLIER_MAGNITUDE;
+            }
+        }
+
+        if (VERBOSE) {
+            for (psS32 i=0;i<numData;i++) {
+                printf("Original in data %d: (%.1f)\n", i, in->data.F64[i]);
+            }
+        }
+    }
+    if (flags & TST_IN_S8) {
+        for (psS32 i=0;i<numData;i++) {
+            if (PERCENT_OUTLIERS > (random() % 100)) {
+                in->data.S8[i] = (psS8) OUTLIER_MAGNITUDE;
+            }
+        }
+
+        if (VERBOSE) {
+            for (psS32 i=0;i<numData;i++) {
+                printf("Original in data %d: (%d)\n", i, in->data.S8[i]);
+            }
+        }
+    }
+    if (flags & TST_IN_U16) {
+        for (psS32 i=0;i<numData;i++) {
+            if (PERCENT_OUTLIERS > (random() % 100)) {
+                in->data.U16[i] = (psU16) OUTLIER_MAGNITUDE;
+            }
+        }
+
+        if (VERBOSE) {
+            for (psS32 i=0;i<numData;i++) {
+                printf("Original in data %d: (%d)\n", i, in->data.U16[i]);
+            }
+        }
+    }
+    if (flags & TST_IN_S32) {
+        for (psS32 i=0;i<numData;i++) {
+            if (PERCENT_OUTLIERS > (random() % 100)) {
+                in->data.S32[i] = (psS32) OUTLIER_MAGNITUDE;
+            }
+        }
+
+        if (VERBOSE) {
+            for (psS32 i=0;i<numData;i++) {
+                printf("Original in data %d: (%d)\n", i, in->data.S32[i]);
+            }
+        }
+    }
+
+    //
+    // We call psVectorStats() and calculate the clipped stats.
+    //
+    psStats *myStats = psStatsAlloc(
+                           PS_STAT_ROBUST_MEDIAN |
                            PS_STAT_ROBUST_STDEV |
                            PS_STAT_ROBUST_QUARTILE |
                            PS_STAT_FITTED_MEAN |
                            PS_STAT_FITTED_STDEV);
-    // Create a full outliers:
-    for (psS32 i = 0 ; i < NUM_DATA ; i++) {
-        if (PERCENT_OUTLIERS > (random() % 100)) {
-            myVector->data.F32[i] = 1000.0 * MEAN;
-        }
-    }
-    /*************************************************************************/
-    /*  Call psVectorStats() with no vector mask.                            */
-    /*************************************************************************/
-    printPositiveTestHeader( stdout,
-                             "psStats functions",
-                             "PS_STAT_ROBUST_STATS: robust mean: no vector mask" );
-
-    myStats = psVectorStats( myStats, myVector, NULL, NULL, 0 );
-    if (myStats == NULL) {
-        printf("TEST ERROR: psVectorStats() returned NULL.\n");
-        return(false);
-    }
-
-    printf( "The expected Mean was %.2f; the calculated Mean was %.2f\n",
-            realMeanNoMask, myStats->fittedMean );
-
-    if ( fabs( myStats->fittedMean - realMeanNoMask ) < ( ERROR_TOLERANCE * realMeanNoMask ) ) {
-        testStatus = true;
+    psStats *rc = psVectorStats(myStats, in, errors, mask, maskValue);
+
+    if (rc == NULL) {
+        if (expectedRC == true) {
+            printf("TEST ERROR: the psVectorStats() function returned NULL.\n");
+            testStatus = false;
+        }
     } else {
-        testStatus = false;
-        globalTestStatus = false;
-    }
-    printFooter( stdout,
-                 "psVector functions",
-                 "PS_STAT_ROBUST_STATS: robust mean: no vector mask",
-                 testStatus );
-
-
-    printPositiveTestHeader( stdout,
-                             "psStats functions",
-                             "PS_STAT_ROBUST_STATS: robust Median: no vector mask" );
-
-    printf( "The expected Median was %.2f; the calculated Median was %.2f\n",
-            realMedianNoMask, myStats->robustMedian );
-    if ( fabs( myStats->robustMedian - realMedianNoMask ) < ( ERROR_TOLERANCE * realMedianNoMask ) ) {
-        testStatus = true;
-    } else {
-        testStatus = false;
-        globalTestStatus = false;
-    }
-    printFooter( stdout,
-                 "psVector functions",
-                 "PS_STAT_ROBUST_STATS: robust Median: no vector mask",
-                 testStatus );
-
-    printPositiveTestHeader( stdout,
-                             "psStats functions",
-                             "PS_STAT_ROBUST_STATS: robust Stdev: no vector mask" );
-
-    printf( "The expected Stdev was %.2f; the calculated Stdev was %.2f\n",
-            realStdevNoMask, myStats->fittedStdev );
-    if ( fabs( myStats->fittedStdev - realStdevNoMask ) < ( ERROR_TOLERANCE * realStdevNoMask ) ) {
-        testStatus = true;
-    } else {
-        testStatus = false;
-        globalTestStatus = false;
-    }
-    printFooter( stdout,
-                 "psVector functions",
-                 "PS_STAT_ROBUST_STATS: robust Stdev: no vector mask",
-                 testStatus );
-
-
-    printPositiveTestHeader( stdout,
-                             "psStats functions",
-                             "PS_STAT_ROBUST_STATS: lower quartile: no vector mask" );
-
-    printf( "The expected LQ was %.2f; the calculated LQ was %.2f\n",
-            realLQNoMask, myStats->robustLQ );
-
-    if ( fabs( myStats->robustLQ - realLQNoMask ) < ( ERROR_TOLERANCE * realLQNoMask ) ) {
-        testStatus = true;
-    } else {
-        testStatus = false;
-        globalTestStatus = false;
-    }
-    printFooter( stdout,
-                 "psVector functions",
-                 "PS_STAT_ROBUST_STATS: lower quartile: no vector mask",
-                 testStatus );
-
-    printPositiveTestHeader( stdout,
-                             "psStats functions",
-                             "PS_STAT_ROBUST_STATS: upper quartile: no vector mask" );
-
-    printf( "The expected UQ was %.2f; the calculated UQ was %.2f\n",
-            realUQNoMask, myStats->robustUQ );
-    if ( fabs( myStats->robustUQ - realUQNoMask ) < ( ERROR_TOLERANCE * realUQNoMask ) ) {
-        testStatus = true;
-    } else {
-        testStatus = false;
-        globalTestStatus = false;
-    }
-    printFooter( stdout,
-                 "psVector functions",
-                 "PS_STAT_ROBUST_STATS: lower quartile: no vector mask",
-                 testStatus );
-
-    printPositiveTestHeader( stdout,
-                             "psStats functions",
-                             "PS_STAT_ROBUST_STATS: robust N50: no vector mask" );
-    // XXX:
-    realN50NoMask = myStats->robustN50;
-
-    printf( "The expected N50 was %d; the calculated N50 was %d\n",
-            realN50NoMask, myStats->robustN50 );
-    /* XXX: fix
-        if ( fabs( myStats->robustN50 - realN50NoMask ) < ( ERROR_TOLERANCE * realN50NoMask ) ) {
-            testStatus = true;
-        } else {
-            testStatus = false;
-            globalTestStatus = false;
-        }
-    */
-    printFooter( stdout,
-                 "psVector functions",
-                 "PS_STAT_ROBUST_STATS: robust N50: no vector mask",
-                 testStatus );
-
-
-    /*************************************************************************/
-    /*  Deallocate data structures                                           */
-    /*************************************************************************/
-    printPositiveTestHeader( stdout,
-                             "psStats functions",
-                             "psStats(): deallocating memory" );
-
-    psFree( myStats );
-    psFree( myVector );
-    psFree( maskVector );
-
-    psMemCheckCorruption( 1 );
+        if (expectedRC == false) {
+            printf("TEST ERROR: the psVectorStats() function returned non-NULL.\n");
+            testStatus = false;
+        }
+
+        //
+        // Fitted Mean
+        //
+        if (fabs(myStats->fittedMean - sampleMean) > (ERROR_TOLERANCE * sampleMean)) {
+            printf("TEST ERROR: the fitted mean was %.2f, should have been %.2f\n", myStats->fittedMean, sampleMean);
+            testStatus = false;
+        } else if (VERBOSE) {
+            printf("GOOD: the fitted mean was %.2f, should have been %.2f\n", myStats->fittedMean, sampleMean);
+        }
+
+        //
+        // Fitted Stdev
+        //
+        if (fabs(myStats->fittedStdev - sampleStdev) > (ERROR_TOLERANCE * sampleStdev)) {
+            printf("TEST ERROR: the fitted stdev was %.2f, should have been %.2f\n", myStats->fittedStdev, sampleStdev);
+            testStatus = false;
+        } else if (VERBOSE) {
+            printf("GOOD: the fitted stdev was %.2f, should have been %.2f\n", myStats->fittedStdev, sampleStdev);
+        }
+
+        //
+        // Robust Stdev
+        //
+        if (fabs(myStats->robustStdev - sampleStdev) > (ERROR_TOLERANCE * sampleStdev)) {
+            printf("TEST ERROR: the robust stdev was %.2f, should have been %.2f\n", myStats->robustStdev, sampleStdev);
+            testStatus = false;
+        } else if (VERBOSE) {
+            printf("GOOD: the robust stdev was %.2f, should have been %.2f\n", myStats->robustStdev, sampleStdev);
+        }
+
+        //
+        // Robust Median
+        //
+        if (fabs(myStats->robustMedian - sampleMedian) > (ERROR_TOLERANCE * sampleMedian)) {
+            printf("TEST ERROR: the robust median was %.2f, should have been %.2f\n", myStats->robustMedian, sampleMedian);
+            testStatus = false;
+        } else if (VERBOSE) {
+            printf("GOOD: the robust median was %.2f, should have been %.2f\n", myStats->robustMedian, sampleMedian);
+        }
+
+        //
+        // Robust LQ
+        //
+        if (fabs(myStats->robustLQ - sampleLQ) > (ERROR_TOLERANCE * sampleLQ)) {
+            printf("TEST ERROR: the robust LQ was %.2f, should have been %.2f\n", myStats->robustLQ, sampleLQ);
+            testStatus = false;
+        } else if (VERBOSE) {
+            printf("GOOD: the robust LQ was %.2f, should have been %.2f\n", myStats->robustLQ, sampleLQ);
+        }
+
+        //
+        // Robust UQ
+        //
+        if (fabs(myStats->robustUQ - sampleUQ) > (ERROR_TOLERANCE * sampleUQ)) {
+            printf("TEST ERROR: the robust UQ was %.2f, should have been %.2f\n", myStats->robustUQ, sampleUQ);
+            testStatus = false;
+        } else if (VERBOSE) {
+            printf("GOOD: the robust UQ was %.2f, should have been %.2f\n", myStats->robustUQ, sampleUQ);
+        }
+    }
+
+    psFree(myStats);
+    psFree(in);
+    psFree(errors);
+    psFree(mask);
+    psMemCheckCorruption(1);
     memLeaks = psMemCheckLeaks(currentId,NULL,stderr,false);
-    if ( 0 != memLeaks ) {
-        psAbort( __func__, "Memory Leaks! (%d leaks)", memLeaks );
-    }
-
-    printFooter( stdout,
-                 "psVector functions",
-                 "psStats(): deallocating memory",
-                 testStatus );
-
-    return ( globalTestStatus );
+    if (0 != memLeaks) {
+        psAbort(__func__,"Memory Leaks! (%d leaks)", memLeaks);
+    }
+
+    return(testStatus);
 }
 
-
-psS32 t01()
-{
-    psStats * myStats = NULL;
-    psS32 testStatus = true;
-    psS32 globalTestStatus = true;
-    psS32 i = 0;
-    psVector *myVector = NULL;
-    psVector *maskVector = NULL;
-    // NOTE: These values were calculated by running the function on the data.
-    // A: They must be changed if we adjust the number of data points.
-    // B: We don't really know that they are correct.
-    psS32 count = 0;
-    psS32 currentId = psMemGetId();
-    psS32 memLeaks = 0;
-    float realMeanWithMask = MEAN;
-    float realMedianWithMask = MEAN;
-    float realStdevWithMask = STDEV;
-    float realLQWithMask = MEAN;
-    float realUQWithMask = MEAN;
-    psS32 realN50WithMask = NUM_DATA / 4;
-    /*************************************************************************/
-    /*  Allocate and initialize data structures                              */
-    /*************************************************************************/
-    myStats = psStatsAlloc(PS_STAT_ROBUST_MEDIAN |
-                           PS_STAT_ROBUST_STDEV |
-                           PS_STAT_ROBUST_QUARTILE |
-                           PS_STAT_FITTED_MEAN |
-                           PS_STAT_FITTED_STDEV);
-
-    maskVector = psVectorAlloc( NUM_DATA, PS_TYPE_U8 );
-    maskVector->n = NUM_DATA;
-    myVector = p_psGaussianDev( MEAN, STDEV, NUM_DATA );
-    // Set the mask vector and calculate the expected maximum.
-    for ( i = 0;i < NUM_DATA;i++ ) {
-        if ( i < ( NUM_DATA / 2 ) ) {
-            maskVector->data.U8[ i ] = 0;
-            count++;
-        } else {
-            maskVector->data.U8[ i ] = 1;
-        }
-    }
-    for (psS32 i = 0 ; i < NUM_DATA ; i++) {
-        if (PERCENT_OUTLIERS < (random() % 100)) {
-            myVector->data.F32[i] = 1000.0 * MEAN;
-        }
-    }
-
-    /*************************************************************************/
-    /*  Call psVectorStats() with vector mask.                               */
-    /*************************************************************************/
-    printPositiveTestHeader( stdout,
-                             "psStats functions",
-                             "PS_STAT_ROBUST_STATS: robust mean: with vector mask" );
-
-    printf( "Calling psVectorStats() on a vector with elements masked.\n" );
-    myStats = psVectorStats( myStats, myVector, NULL, maskVector, 1 );
-    if (myStats == NULL) {
-        printf("TEST ERROR: psVectorStats() returned NULL.\n");
-        return(false);
-    }
-    printf( "Called psVectorStats() on a vector with elements masked.\n" );
-    printf( "The expected Mean was %.2f; the calculated Mean was %.2f\n",
-            realMeanWithMask, myStats->fittedMean );
-    if ( fabs( myStats->fittedMean - realMeanWithMask ) < ( ERROR_TOLERANCE * realMeanWithMask ) ) {
-        testStatus = true;
-    } else {
-        testStatus = false;
-        globalTestStatus = false;
-    }
-    printFooter( stdout,
-                 "psVector functions",
-                 "PS_STAT_ROBUST_STATS: robust mean: with vector mask",
-                 testStatus );
-
-    printPositiveTestHeader( stdout,
-                             "psStats functions",
-                             "PS_STAT_ROBUST_STATS: robust Median: with vector mask" );
-
-    printf( "The expected Median was %.2f; the calculated Median was %.2f\n",
-            realMedianWithMask, myStats->robustMedian );
-    if ( fabs( myStats->robustMedian - realMedianWithMask ) < ( ERROR_TOLERANCE * realMedianWithMask ) ) {
-        testStatus = true;
-    } else {
-        testStatus = false;
-        globalTestStatus = false;
-    }
-    printFooter( stdout,
-                 "psVector functions",
-                 "PS_STAT_ROBUST_STATS: robust Median: with vector mask",
-                 testStatus );
-
-
-    printPositiveTestHeader( stdout,
-                             "psStats functions",
-                             "PS_STAT_ROBUST_STATS: robust Stdev: with vector mask" );
-
-    printf( "The expected Stdev was %.2f; the calculated Stdev was %.2f\n",
-            realStdevWithMask, myStats->fittedStdev );
-    if ( fabs( myStats->fittedStdev - realStdevWithMask ) < ( ERROR_TOLERANCE * realStdevWithMask ) ) {
-        testStatus = true;
-    } else {
-        testStatus = false;
-        globalTestStatus = false;
-    }
-    printFooter( stdout,
-                 "psVector functions",
-                 "PS_STAT_ROBUST_STATS: robust Stdev: with vector mask",
-                 testStatus );
-
-
-
-    printPositiveTestHeader( stdout,
-                             "psStats functions",
-                             "PS_STAT_ROBUST_STATS: lower quartile: with vector mask" );
-
-    printf( "The expected LQ was %.2f; the calculated LQ was %.2f\n",
-            realLQWithMask, myStats->robustLQ );
-    if ( fabs( myStats->robustLQ - realLQWithMask ) < ( ERROR_TOLERANCE * realLQWithMask ) ) {
-        testStatus = true;
-    } else {
-        testStatus = false;
-        globalTestStatus = false;
-    }
-    printFooter( stdout,
-                 "psVector functions",
-                 "PS_STAT_ROBUST_STATS: lower quartile: with vector mask",
-                 testStatus );
-
-
-
-    printPositiveTestHeader( stdout,
-                             "psStats functions",
-                             "PS_STAT_ROBUST_STATS: upper quartile: with vector mask" );
-
-    printf( "The expected UQ was %.2f; the calculated UQ was %.2f\n",
-            realUQWithMask, myStats->robustUQ );
-    if ( fabs( myStats->robustUQ - realUQWithMask ) < ( ERROR_TOLERANCE * realUQWithMask ) ) {
-        testStatus = true;
-    } else {
-        testStatus = false;
-        globalTestStatus = false;
-    }
-    printFooter( stdout,
-                 "psVector functions",
-                 "PS_STAT_ROBUST_STATS: lower quartile: with vector mask",
-                 testStatus );
-
-
-
-    printPositiveTestHeader( stdout,
-                             "psStats functions",
-                             "PS_STAT_ROBUST_STATS: robust N50: with vector mask" );
-
-    printf( "The expected N50 was %d; the calculated N50 was %d\n",
-            realN50WithMask, myStats->robustN50 );
-    /* XXX: fix
-        if ( fabs( myStats->robustN50 - realN50WithMask ) < ( ERROR_TOLERANCE * realN50WithMask ) ) {
-            testStatus = true;
-        } else {
-            testStatus = false;
-            globalTestStatus = false;
-        }
-    */
-    printFooter( stdout,
-                 "psVector functions",
-                 "PS_STAT_ROBUST_STATS: robust N50: with vector mask",
-                 testStatus );
-
-
-    /*************************************************************************/
-    /*  Deallocate data structures                                           */
-    /*************************************************************************/
-    printPositiveTestHeader( stdout,
-                             "psStats functions",
-                             "psStats(): deallocating memory" );
-
-    psFree( myStats );
-    psFree( myVector );
-    psFree( maskVector );
-
-    psMemCheckCorruption( 1 );
-    memLeaks = psMemCheckLeaks(currentId,NULL,stderr,false);
-    if ( 0 != memLeaks ) {
-        psAbort( __func__, "Memory Leaks! (%d leaks)", memLeaks );
-    }
-
-    printFooter( stdout,
-                 "psVector functions",
-                 "psStats(): deallocating memory",
-                 testStatus );
-
-    return ( globalTestStatus );
-}
 
 psS32 main()
@@ -437,4 +441,5 @@
     psLogSetFormat("HLNM");
     psLogSetLevel(PS_LOG_INFO);
+    psBool testStatus = true;
     //
     // We list pertinent psStats.c functions here for debugging ease.
@@ -462,13 +467,14 @@
     psTraceSetLevel("p_psConvertToF32", TRACE_LEVEL);
     psTraceSetLevel("psVectorStats", TRACE_LEVEL);
-    psBool rc0 = t00();
-    psBool rc1 = t01();
-
-    if (rc0 & rc1) {
+
+    testStatus &= genericRobustStatsTest(TST_IN_F32 | TST_ERRORS_NULL | TST_MASK_NULL, NUM_DATA, 1, true);
+    testStatus &= genericRobustStatsTest(TST_IN_F32 | TST_ERRORS_NULL | TST_MASK_U8, NUM_DATA, 1, true);
+
+    if (testStatus) {
         printf("TEST PASSED.\n");
     } else {
         printf("TEST FAILED.\n");
     }
-
-    return(!(rc0 & rc1));
 }
+
+//This code will
