void intPerm ( INT * const permtab, /*+ Permutation array to build +*/ const INT permnbr) /*+ Number of entries in array +*/ { INT * permptr; INT permrmn; for (permptr = permtab, permrmn = permnbr; /* Perform random permutation */ permrmn > 0; permptr ++, permrmn --) { INT permnum; INT permtmp; permnum = intRandVal (permrmn); /* Select index to swap */ permtmp = permptr[0]; /* Swap it with current index */ permptr[0] = permptr[permnum]; permptr[permnum] = permtmp; } }
int main ( int argc, char * argv[]) { INT * randtab; int randnbr; int randnum; struct stat statdat; FILE * fileptr; int passnum; if (argc != 3) { errorPrint ("usage: %s file passnum", argv[0]); return (1); } if ((randtab = malloc (RANDNBR * sizeof (INT))) == NULL) { errorPrint ("main: out of memory"); return (1); } intRandInit (); /* Initialize random generator */ for (randnum = 0; randnum < RANDNBR; randnum ++) randtab[randnum] = intRandVal (INTVALMAX); intRandReset (); passnum = (atoi (argv[2]) == 0); /* First pass to write file; second pass to read it */ if ((fileptr = fopen (argv[1], (passnum) ? "w+" : "r")) == NULL) { errorPrint ("main: cannot open file"); return (1); } if (passnum) { /* If first pass */ for (randnum = 0; randnum < RANDNBR; randnum ++) { if (randtab[randnum] != intRandVal (INTVALMAX)) { errorPrint ("main: cannot replay random sequence"); return (1); } } if (fwrite (randtab, sizeof (INT), RANDNBR, fileptr) < RANDNBR) { errorPrint ("main: cannot write to file"); return (1); } sleep (1); /* Next run will not get the same time() value */ } else { /* Second pass */ const char * const bufftab = ""; char * charptr; int o; if (fread (randtab, sizeof (INT), RANDNBR, fileptr) < RANDNBR) { errorPrint ("main: cannot read from file"); return (1); } for (randnum = 0; randnum < RANDNBR; randnum ++) { if (randtab[randnum] != intRandVal (INTVALMAX)) break; } o = (randnum == RANDNBR); charptr = (o) ? "same" : "different"; #if ((defined COMMON_DEBUG) || (defined COMMON_RANDOM_FIXED_SEED) || (defined SCOTCH_DETERMINISTIC)) o ^= 1; #endif /* ((defined COMMON_DEBUG) || (defined COMMON_RANDOM_FIXED_SEED) || (defined SCOTCH_DETERMINISTIC)) */ if (o) { errorPrint ("main: two consecutive runs yield %s values.", charptr); return (1); } printf ("Two consecutive runs yield %s values.\n", charptr); } fclose (fileptr); free (randtab); return (0); }
int main ( int argc, char * argv[]) { FiboHeap fibodat; TestFibo * nodetab; TestFibo * nodeptr; int nodesiz; int nodemax; int nodenbr; int nodenum; int nodetmp; int randval; int randtmp; int passnbr; int passnum; SCOTCH_errorProg (argv[0]); if (fiboHeapInit (&fibodat, testFiboCmpFunc) != 0) { SCOTCH_errorPrint ("main: cannot initialize Fibonacci heap"); exit (EXIT_FAILURE); } intRandInit (); /* Initialize random generator */ nodesiz = 100; passnbr = -1; switch (argc) { case 4 : intRandSeed (MAX (0, atoi (argv[3]))); case 3 : passnbr = MAX (1, atoi (argv[2])); case 2 : nodesiz = MAX (1, atoi (argv[1])); case 1 : break; default : SCOTCH_errorPrint ("usage: %s [nodenbr [passnbr [seed]]]", argv[0]); exit (EXIT_FAILURE); } if (passnbr < 0) passnbr = 10 * nodesiz; if ((nodetab = malloc (nodesiz * sizeof (TestFibo))) == NULL) { SCOTCH_errorPrint ("main: out of memory"); exit (EXIT_FAILURE); } for (nodenum = 0; nodenum < nodesiz; nodenum ++) /* Initialize node array */ nodetab[nodenum].randval = -1; nodemax = nodesiz - 1; /* Set maximum index */ nodenbr = 0; /* Array is empty */ for (passnum = 0; passnum < passnbr; passnum ++) { switch (intRandVal (6)) { case 0 : /* Add node */ case 1 : case 2 : /* More additions than deletions on average */ if (nodenbr >= nodemax) break; for (nodenum = 0; nodenum <= nodemax; nodenum ++) { /* Search for a free slot */ if (nodetab[nodenum].randval < 0) break; } if (nodenum > nodemax) { SCOTCH_errorPrint ("main: invalid node array (1)"); exit (EXIT_FAILURE); } nodetab[nodenum].randval = abs (intRandVal (INTVALMAX)); fiboHeapAdd (&fibodat, (FiboNode *) &nodetab[nodenum]); nodenbr ++; break; case 3 : /* Remove arbitrary node */ if (nodenbr <= 0) break; nodetmp = intRandVal (nodenbr); for (nodenum = 0; ; nodenum ++) { /* Search for non-empty slot */ if (nodenum > nodemax) { SCOTCH_errorPrint ("main: invalid node array (2)"); exit (EXIT_FAILURE); } if (nodetab[nodenum].randval >= 0) { if (-- nodetmp <= 0) break; } } fiboHeapDel (&fibodat, (FiboNode *) &nodetab[nodenum]); nodetab[nodenum].randval = -1; nodenbr --; break; case 4 : /* Remove minimum node */ if (nodenbr <= 0) break; nodeptr = (TestFibo *) fiboHeapMin (&fibodat); randval = nodeptr->randval; /* Keep node key value */ fiboHeapDel (&fibodat, (FiboNode *) nodeptr); /* Remove node first */ nodeptr->randval = -1; nodenbr --; for (nodenum = 0; nodenum <= nodemax; nodenum ++) { /* Check if smaller node exists */ if ((nodetab[nodenum].randval >= 0) && (nodetab[nodenum].randval < randval)) { SCOTCH_errorPrint ("main: node is not of minimum key"); } } break; case 5 : /* Decrease value of arbitrary node */ if (nodenbr <= 0) break; nodetmp = intRandVal (nodenbr); for (nodenum = 0; ; nodenum ++) { /* Search for non-empty slot */ if (nodenum > nodemax) { SCOTCH_errorPrint ("main: invalid node array (3)"); exit (EXIT_FAILURE); } if (nodetab[nodenum].randval >= 0) { if (-- nodetmp <= 0) break; } } if (nodetab[nodenum].randval <= 0) /* Cannot decrease smallest value */ break; randtmp = intRandVal (nodetab[nodenum].randval + 1); if (randtmp > nodetab[nodenum].randval) break; nodetab[nodenum].randval -= randtmp; fiboHeapDecrease (&fibodat, (FiboNode *) &nodetab[nodenum]); break; } } fiboHeapExit (&fibodat); free (nodetab); exit (EXIT_SUCCESS); }