Skip to content

Instantly share code, notes, and snippets.

/Test_program_v2 Secret
Created Mar 31, 2014

Embed
What would you like to do?
Test program v2
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#define BOOL int
#define FALSE 0
#define TRUE 1
// Following parameters average about 1GB mem per test set
#define TEST_COUNT 3000
#define TEST_AVG_LENGTH 100000
#define TEST_AVG_VALUE 1000
#include "user.c"
typedef struct {
int Count;
float* Values;
} Test;
float randFloat() {
return ((float) rand()) / RAND_MAX;
}
// exponent must be strictly > 2 to allow finite mean.
float powerLawInverseDist(double average, double exponent)
{
if(exponent <= 2.0)
return NAN;
float uniRand = randFloat();
float unNormalised = powf((1.0 - uniRand), 1.0/(1.0 - exponent)) - 1.0;
float naturalAverage = 1.0/(exponent - 2.0);
return unNormalised/naturalAverage*average;
}
// exponent must be strictly > 2 to allow finite mean.
float powerLawInverseDistInt(double average, double exponent)
{
return (int)round(powerLawInverseDist(average, exponent));
}
int compareAsc(const void * a, const void * b)
{
if ( *(float*)a < *(float*)b ) return -1;
if ( *(float*)a > *(float*)b ) return 1;
return 0;
}
int compareDesc(const void * a, const void * b)
{
return compareAsc(b, a);
}
int generateRandomArray(float** pResult, int length, float averageValue)
{
int i;
float central = (randFloat() < 0.5) ? averageValue : -averageValue;
int dups = (randFloat() < 0.25);
int n = length;
float* result = NULL;
if(n > 0)
result = (float*) malloc(n * sizeof(float));
for (i = 0; i < n; i++)
{
if(dups && i > 0 && (randFloat() < 0.01)) {
result[i] = result[i-1];
continue;
}
float value = powerLawInverseDist(averageValue, 3);
if (randFloat() < 0.25)
// Truncate
value = (int) value;
if (randFloat() < 0.5)
value = -value;
result[i] = value + central;
}
if(n > 0 && randFloat() < 0.33) {
//printf("Sorting;;;\n");
int (*compar)(const void*,const void*) = (randFloat() < 0.5) ? compareAsc : compareDesc;
int base = (n > 0) ? (rand() % n) : 0;
int nSort = (n > base) ? (rand() % (n-base)) : 0;
if(randFloat() < 0.033) { // 1% overall, complete sort
base = 0;
nSort = n;
}
qsort((void*)(result+base), nSort, sizeof(float), compar);
}
*pResult = result;
return n;
}
BOOL isSorted(float* values, int n)
{
int i;
for (i = 0; i < n-1; i++)
if(values[i] > values[i+1])
return FALSE;
return TRUE;
}
Test generateTest(int length, int averageValue) {
Test result;
result.Count = generateRandomArray(&result.Values, length, averageValue);
//if(isSorted(result.Values, result.Count)) printf("Sorted! (%d)\n", result.Count);
return result;
}
Test* generateTests(int nTests) {
Test* pResult = (Test*) malloc(nTests * sizeof(Test));
int i;
for(i = 0; i< nTests; i++) {
int testLength = i/2;
if(i >= 10) {
testLength = powerLawInverseDistInt(TEST_AVG_LENGTH, 3);
if(testLength > 0x100000) testLength = 0x100000; // 2^20
}
pResult[i] = generateTest(testLength, TEST_AVG_VALUE);
}
return pResult;
}
void runTests(Test* pTests, int nTests) {
int i;
for(i = 0; i< nTests; i++)
sort(pTests[i].Values, pTests[i].Count);
}
BOOL verifyTests(Test* pTests, int nTests) {
int i;
for(i = 0; i < nTests; i++)
if(!isSorted(pTests[i].Values, pTests[i].Count))
return FALSE;
return TRUE;
}
void deleteTests(Test** ppTests, int nTests) {
int i;
for(i = 0; i < nTests; i++)
if((*ppTests)[i].Count > 0)
free((*ppTests)[i].Values);
free(*ppTests);
*ppTests = NULL;
}
unsigned long timespecDiff(struct timespec *timeA_p, struct timespec *timeB_p)
{
return
((timeA_p->tv_sec * 1000000000) + timeA_p->tv_nsec) -
((timeB_p->tv_sec * 1000000000) + timeB_p->tv_nsec);
}
int main(int argc, char* argv[]) {
struct timespec start, end;
unsigned long elapsedGen = 0, elapsedRun = 0, elapsedVer = 0, elapsedDel = 0;
srand(1657841943);
int nRuns = 5;
int nTests = TEST_COUNT;
int ri;
for(ri = 0; ri < nRuns; ri++) {
printf("%d: Generating tests.\n", ri);
clock_gettime(CLOCK_MONOTONIC, &start);
Test* pTests = generateTests(nTests);
clock_gettime(CLOCK_MONOTONIC, &end);
elapsedGen += timespecDiff(&end, &start);
printf("%d: Running tests.\n", ri);
clock_gettime(CLOCK_MONOTONIC, &start);
runTests(pTests, nTests);
clock_gettime(CLOCK_MONOTONIC, &end);
elapsedRun += timespecDiff(&end, &start);
printf("%d: Verifying tests.\n", ri);
clock_gettime(CLOCK_MONOTONIC, &start);
if(!verifyTests(pTests, nTests))
printf("*** VERIFICATION FAILED. ***\n");
clock_gettime(CLOCK_MONOTONIC, &end);
elapsedVer += timespecDiff(&end, &start);
printf("%d: Deleting tests.\n", ri);
clock_gettime(CLOCK_MONOTONIC, &start);
deleteTests(&pTests, nTests);
clock_gettime(CLOCK_MONOTONIC, &end);
elapsedDel += timespecDiff(&end, &start);
}
printf("Total generation time: %f s.\n", elapsedGen/1000000000.0);
printf("Total run time: %f s.\n", elapsedRun/1000000000.0);
printf("Total verification time: %f s.\n", elapsedVer/1000000000.0);
printf("Total deletion time: %f s.\n", elapsedDel/1000000000.0);
getchar();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.