示例#1
0
//=========================================================================
// test matrix copy constructor (copy & view)
int matrixCpyCtr(bool verbose, bool debug) {
	const int m1rows = 5;
	const int m1cols = 4;
	const int m2rows = 2;
	const int m2cols = 6;

	int ierr = 0;
	int returnierr = 0;
	if(verbose) printHeading("Testing matrix copy constructors");

	if(verbose) cout << "checking copy constructor (view)" << endl;
	double* m1rand = getRandArray(m1rows * m1cols);
	if(debug) printArray(m1rand, m1rows * m1cols);
	Epetra_SerialDenseMatrix m1(View, m1rand, m1rows, m1rows, m1cols);
	if(debug) {
		cout << "original matrix:" << endl;
		printMat("m1",m1);
	}
	Epetra_SerialDenseMatrix m1clone(m1);
	if(debug) {
		cout << "clone matrix:" << endl;
		printMat("m1clone",m1clone);
	}
	if(verbose) cout << "making sure signatures match" << endl;
	EPETRA_TEST_ERR(!identicalSignatures(m1, m1clone), ierr);
	delete[] m1rand;
	returnierr += ierr;
	if(ierr == 0)
		if(verbose) cout << "Checked OK." << endl;
	ierr = 0;
	
	if(verbose) cout << "\nchecking copy constructor (copy)" << endl;
	double* m2rand = getRandArray(m2rows * m2cols);
	if(debug) printArray(m2rand, m2rows * m2cols);
	Epetra_SerialDenseMatrix m2(Copy, m2rand, m2rows, m2rows, m2cols);
	if(debug) {
		cout << "original matrix:" << endl;
		printMat("m2",m2);
	}
	Epetra_SerialDenseMatrix m2clone(m2);
	if(debug) {
		cout << "clone matrix:" << endl;
		printMat("m2clone",m2clone);
	}
	if(verbose) cout << "checking that signatures match" << endl;
	EPETRA_TEST_ERR(!identicalSignatures(m2, m2clone), ierr);
	returnierr += ierr;
	if(ierr == 0)
		if(verbose) cout << "Checked OK." << endl;
	ierr = 0;

	if(verbose) cout << "\nmodifying entry in m2, m2clone should be unchanged" << endl;
	EPETRA_TEST_ERR(!seperateData(m2, m2clone), ierr);
	if(debug) {
		printArray(m2rand, m2rows * m2cols);
		cout << "orig:" << endl;
		printMat("m2",m2);
		cout << "clone:" << endl;
		printMat("m2clone",m2clone);
	}
	delete[] m2rand;
	returnierr += ierr;
	if(ierr == 0)
		if(verbose) cout << "Checked OK." << endl;
	ierr = 0;
	
	return(returnierr);
}
示例#2
0
//=========================================================================
// test matrix operator= (copy & view)
int matrixAssignment(bool verbose, bool debug) {
	int ierr = 0;
	int returnierr = 0;
	if(verbose) printHeading("Testing matrix operator=");

	// each section is in its own block so we can reuse variable names
	// lhs = left hand side, rhs = right hand side
	
	{
		// copy->copy (more space needed)
		// orig and dup should have same signature
		// modifying orig or dup should have no effect on the other
		if(verbose) cout << "Checking copy->copy (new alloc)" << endl;
		Epetra_SerialDenseMatrix lhs(2,2);
		double* rand1 = getRandArray(25);
		Epetra_SerialDenseMatrix rhs(Copy, rand1, 5, 5, 5);
		if(debug) {
			cout << "before assignment:" << endl;
			printMat("rhs",rhs);
			printMat("lhs",lhs);
		}
		lhs = rhs;
		if(debug) {
			cout << "after assignment:" << endl;
			printMat("rhs",rhs);
			printMat("lhs",lhs);
		}
		EPETRA_TEST_ERR(!identicalSignatures(rhs,lhs), ierr);
		EPETRA_TEST_ERR(!seperateData(rhs,lhs), ierr);
		delete[] rand1;
	}
	returnierr += ierr;
	if(ierr == 0)
		if(verbose) cout << "Checked OK." << endl;
	ierr = 0;
	{
		// copy->copy (have enough space)
		// orig and dup should have same signature
		// modifying orig or dup should have no effect on the other
		if(verbose) cout << "\nChecking copy->copy (no alloc)" << endl;
		double* rand1 = getRandArray(25);
		double* rand2 = getRandArray(20);
		Epetra_SerialDenseMatrix lhs(Copy, rand1, 5, 5, 5);
		Epetra_SerialDenseMatrix rhs(Copy, rand2, 4, 4, 5);
		double* origA = lhs.A();
		int origLDA = lhs.LDA();
		if(debug) {
			cout << "before assignment:" << endl;
			printMat("rhs",rhs);
			printMat("lhs",lhs);
		}
		lhs = rhs;
		if(debug) {
			cout << "after assignment:" << endl;
			printMat("rhs",rhs);
			printMat("lhs",lhs);
		}
		// in this case, instead of doing a "normal" LDA test in identSig,
		// we do our own. Since we had enough space already, A and LDA should
		// not have been changed by the assignment. (The extra parameter to
		// identicalSignatures tells it not to test LDA).
		EPETRA_TEST_ERR((lhs.A() != origA) || (lhs.LDA() != origLDA), ierr);
		EPETRA_TEST_ERR(!identicalSignatures(rhs,lhs,false), ierr);
		EPETRA_TEST_ERR(!seperateData(rhs,lhs), ierr);
	}
	returnierr += ierr;
	if(ierr == 0)
		if(verbose) cout << "Checked OK." << endl;
	ierr = 0;
	{
		// view->copy
		// orig and dup should have same signature
		// modifying orig or dup should have no effect on the other
		if(verbose) cout << "\nChecking view->copy" << endl;
		double* rand1 = getRandArray(25);
		double* rand2 = getRandArray(64);
		Epetra_SerialDenseMatrix lhs(View, rand1, 5, 5, 5);
		Epetra_SerialDenseMatrix rhs(Copy, rand2, 8, 8, 8);
		if(debug) {
			cout << "before assignment:" << endl;
			printMat("rhs",rhs);
			printMat("lhs",lhs);
		}
		lhs = rhs;
		if(debug) {
			cout << "after assignment:" << endl;
			printMat("rhs",rhs);
			printMat("lhs",lhs);
		}
		EPETRA_TEST_ERR(!identicalSignatures(rhs,lhs), ierr);
		EPETRA_TEST_ERR(!seperateData(rhs,lhs), ierr);
		delete[] rand1;
		delete[] rand2;
	}
	returnierr += ierr;
	if(ierr == 0)
		if(verbose) cout << "Checked OK." << endl;
	ierr = 0;
	{
	  // copy->view
		// orig and dup should have same signature
		// modifying orig or dup should change the other
		if(verbose) cout << "\nChecking copy->view" << endl;
		double* rand1 = getRandArray(10);
		Epetra_SerialDenseMatrix lhs(4,4);
		Epetra_SerialDenseMatrix rhs(View, rand1, 2, 2, 5);
		if(debug) {
			cout << "before assignment:" << endl;
			printMat("rhs",rhs);
			printMat("lhs",lhs);
		}
		lhs = rhs;
		if(debug) {
			cout << "after assignment:" << endl;
			printMat("rhs",rhs);
			printMat("lhs",lhs);
		}
		EPETRA_TEST_ERR(!identicalSignatures(rhs,lhs), ierr);
		EPETRA_TEST_ERR(seperateData(rhs,lhs), ierr);
		delete[] rand1;
	}
	returnierr += ierr;
	if(ierr == 0)
		if(verbose) cout << "Checked OK." << endl;
	ierr = 0;	
	{
		// view->view
		// orig and dup should have same signature
		// modifying orig or dup should change the other
		if(verbose) cout << "\nChecking view->view" << endl;
		double* rand1 = getRandArray(9);
		double* rand2 = getRandArray(18);
		Epetra_SerialDenseMatrix lhs(View, rand1, 3, 3, 3);
		Epetra_SerialDenseMatrix rhs(View, rand2, 3, 3, 6);
		if(debug) {
			cout << "before assignment:" << endl;
			printMat("rhs",rhs);
			printMat("lhs",lhs);
		}
		lhs = rhs;
		if(debug) {
			cout << "after assignment:" << endl;
			printMat("rhs",rhs);
			printMat("lhs",lhs);
		}
		EPETRA_TEST_ERR(!identicalSignatures(rhs,lhs), ierr);
		EPETRA_TEST_ERR(seperateData(rhs,lhs), ierr);
		delete[] rand1;
		delete[] rand2;
	}
	returnierr += ierr;
	if(ierr == 0)
		if(verbose) cout << "Checked OK." << endl;
	ierr = 0;

	return(returnierr);
}
示例#3
0
/* Print n strings that are hashed to 0 by universal_hash seeded with seed
 * although character can be anything <255, only 'pretty' characters are chosen
 * for the 'prettiness' of the output */
void collide_dumb(unsigned int size, unsigned int seed, int n) {
    static bool   first    = true;
    int           *randArr = getRandArray(seed, size);
    unsigned int  randSize, i, try;
    unsigned char *line    = malloc(sizeof(*line) * MAX_LEN);

#if I_DUNNO_STATS
    unsigned char **used   = malloc(sizeof(*used) * n);
    unsigned int useCount  = 0;
    // index randsize will be '\0'. Therefore, randsize < MAXLEN.
#endif

    if(first == true) { // if virgin, initialize universal hash
        first = false;
        srand(seed);
        universal_hash(line, size);
    }

#if GET_TIME
    clock_t start = clock();
    fprintf(stderr, "N: %d\nSIZE: %d\n", n, size);
#endif

    while(n > 0) {
        try = 0;
        randSize = rand() % (MAX_LEN - INIT_SIZE - 1) + INIT_SIZE;
        // -1 for \0 slot

        while(try <= MAX_TRY) {
            for(i = 0; i < randSize; i++)
                line[i] = rand() % (MAX_ASCII - START_ASCII - 1) + START_ASCII;


            line[randSize] = '\0';

            if(universal_hash(line, size) == 0) {
#if I_DUNNO_STATS
                for(i = 0; i < useCount; i++) {
                    // make sure it is not duplicate
                    if(strcmp((char*) used[i], (char*) line) == 0)
                        break;
                }

                used[useCount] = malloc(sizeof(unsigned char) * (strlen((char*)line) + 1));
                strcpy((char*)used[useCount], (char*)line);
                // fprintf(stderr, "USED %d: %s\n", useCount, used[useCount]);
                useCount++;
#endif

                fprintf(stdout, "%s\n", line);
                n--;
                break; // now generate new random size!
            }
        }
    }

#if GET_TIME
    clock_t end = clock();
    fprintf(stderr, "TIME TAKEN: %.5f s\n", ((double) end - start) / CLOCKS_PER_SEC);
#endif

#if I_DUNNO_STATS
    for(i = 0; i < useCount; i++) {
        free(used[i]);
    }
    free(used);
#endif
    free(randArr);
    free(line);
}
示例#4
0
/* Print n strings that are hashed to 0 by universal_hash seeded with seed
 * although character can be anything <255, only 'pretty' characters are chosen
 * for the 'prettiness' of the output */
void collide_clever(unsigned int size, unsigned int seed, int n) {
    static bool   first = true;
    int           randSize, sum, gen, i, x, toChange, *randArr = NULL;
    unsigned char y, *line = malloc(sizeof(*line) * MAX_LEN);
    // index randsize will be '\0'. Therefore, randsize < MAXLEN.

#if I_DUNNO_STATS
    unsigned char **used   = malloc(sizeof(*used) * n);
    int           useCount = 0;
#endif

    if(size > 255) {
        fprintf(stderr, "Size is too big. Outside assignment scope!\n");
        fprintf(stderr, "I had spent ages addressing that but after nerf,\n");
        fprintf(stderr, "and reached the state where generated string hash to one index for any size.\n");
        fprintf(stderr, "But no point working further on and submitting risky long code.\n");
        fprintf(stderr, "Anyway, terminating in sass :P\n");
        exit(EXIT_FAILURE);
    }

    // initialize universal hash's random array
    if(first == true) { // some say there are still first == true for most CompSc students
        first = false;
        srand(seed);
        universal_hash(line, size);
    }

    // find out which index to work on
    randArr = getRandArray(seed, size);
    i = 0;
    while(randArr[i] == 0)
        i++;
    toChange = i;

#if GET_TIME
    clock_t start = clock();
    fprintf(stderr, "N: %d\nSIZE: %d\n", n, size);
#endif

    // initialize my random array
    while(n > 0) {
        sum = 0;

        randSize       = rand() % (MAX_LEN - INIT_SIZE - 1) + INIT_SIZE;
        line[randSize] = '\0';

        for(i = 0; i < randSize; i++) {
            if(i != toChange) {
                line[i] = rand() % (MAX_ASCII - START_ASCII - 1) + START_ASCII;
                sum += (line[i] * randArr[i]);
            }
        }

        // explanation of how and why this works and the algorithm is in the report, question 7.
        gen       = size - (sum % size);
        tuple_t t = extEuclid(size, randArr[toChange]);

        x = t.b * gen / t.c;

        if(x < 0)
            y = size - ((-x) % size);
        else
            y = x % size;

        if(y < START_ASCII && size + y < MAX_ASCII) {
            y += size * (1 + (START_ASCII - y) / size);
            // minimum multiple of size to add to make y fall within given range
        } else if(y == '\n' || y == 127 || y <= 32) { // 127 = DEL ASCII
            // if the string genrated has newline character, solution is not valid since
            // strings are loaded with fgets().
            // this happens is about 0.3% of the time.
            // Forum post requirement: avoid characters from 0-32.
            continue;
            // had attempted to add minimum multiple of size,
            // faster to generate new rather than resolve
        }

        line[toChange] = y;

#if I_DUNNO_STATS
        for(i = 0; i < useCount; i++) {
            // make sure it is not duplicate
            if(strcmp((char*) used[i], (char*) line) == 0)
                break;
        }

        used[useCount] = malloc(sizeof(unsigned char) * (strlen((char*)line) + 1));
        strcpy((char*)used[useCount], (char*)line);
        // fprintf(stderr, "USED %d: %s\n", useCount, used[useCount]);
        useCount++;
#endif

        fprintf(stdout, "%s\n", line);
        n--;
    }

#if GET_TIME
    clock_t end = clock();
    fprintf(stderr, "TIME TAKEN: %.5f s\n", ((double) end - start) / CLOCKS_PER_SEC);
#endif

#if I_DUNNO_STATS
    for(i = 0; i < useCount; i++) {
        free(used[i]);
    }
    free(used);
#endif
    free(randArr);
    free(line);
}
示例#5
0
void launchGambling() {
    getRandArray();
}