static void testutil_newrand(const size_t bit_sz, const unsigned int seed) { // If we somehow managed to avoid freeing test_bitarray after a previous // test, go free it now. if (test_bitarray != NULL) { bitarray_free(test_bitarray); } test_bitarray = bitarray_new(bit_sz); assert(test_bitarray != NULL); // Reseed the RNG with whatever we were passed; this ensures that we can // repeat the test deterministically by specifying the same seed. srand(seed); for (size_t i = 0; i < bit_sz; i++) { bitarray_set(test_bitarray, i, randbit()); } // If we were asked to be verbose, go ahead and show the bit array and // the random seed. if (test_verbose) { bitarray_fprint(stdout, test_bitarray); fprintf(stdout, " newrand sz=%zu, seed=%u\n", bit_sz, seed); } }
/** Run unit tests for bitarray code */ static void test_container_bitarray(void *arg) { bitarray_t *ba = NULL; int i, j, ok=1; (void)arg; ba = bitarray_init_zero(1); tt_assert(ba); tt_assert(! bitarray_is_set(ba, 0)); bitarray_set(ba, 0); tt_assert(bitarray_is_set(ba, 0)); bitarray_clear(ba, 0); tt_assert(! bitarray_is_set(ba, 0)); bitarray_free(ba); ba = bitarray_init_zero(1023); for (i = 1; i < 64; ) { for (j = 0; j < 1023; ++j) { if (j % i) bitarray_set(ba, j); else bitarray_clear(ba, j); } for (j = 0; j < 1023; ++j) { if (!bool_eq(bitarray_is_set(ba, j), j%i)) ok = 0; } tt_assert(ok); if (i < 7) ++i; else if (i == 28) i = 32; else i += 7; } done: if (ba) bitarray_free(ba); }
/** Run unit tests for bitarray code */ static void test_container_bitarray(void *unused) { bitarray_t *ba = NULL; int i, j; ba = bitarray_init_zero(1); tt_assert(ba); tt_assert(! bitarray_is_set(ba, 0)); bitarray_set(ba, 0); tt_assert(bitarray_is_set(ba, 0)); bitarray_clear(ba, 0); tt_assert(! bitarray_is_set(ba, 0)); bitarray_free(ba); ba = bitarray_init_zero(1023); for (i = 1; i < 64; ) { for (j = 0; j < 1023; ++j) { if (j % i) bitarray_set(ba, j); else bitarray_clear(ba, j); } for (j = 0; j < 1023; ++j) { tt_bool_op(bitarray_is_set(ba, j), ==, j%i); } if (i < 7) ++i; else if (i == 28) i = 32; else i += 7; } end: if (ba) bitarray_free(ba); }
/** * Take a list of uint16_t *, and remove every port in the list from the * current list of predicted ports. */ void rep_hist_remove_predicted_ports(const smartlist_t *rmv_ports) { /* Let's do this on O(N), not O(N^2). */ bitarray_t *remove_ports = bitarray_init_zero(UINT16_MAX); SMARTLIST_FOREACH(rmv_ports, const uint16_t *, p, bitarray_set(remove_ports, *p)); SMARTLIST_FOREACH_BEGIN(predicted_ports_list, predicted_port_t *, pp) { if (bitarray_is_set(remove_ports, pp->port)) { tor_free(pp); predicted_ports_total_alloc -= sizeof(*pp); SMARTLIST_DEL_CURRENT(predicted_ports_list, pp); } } SMARTLIST_FOREACH_END(pp); bitarray_free(remove_ports); }
/* Create a new bitarray in test_ba of the specified size and fill it with random data based on the seed given. For a given seed number, the pseudorandom data will be the same (at least on the same glibc implementation). */ static void testutil_newrand(size_t bit_sz, unsigned int seed) { size_t i; if (test_ba != NULL) bitarray_free(test_ba); test_ba = bitarray_new(bit_sz); assert(test_ba != NULL); srand(seed); for (i = 0; i < bit_sz; i++) { bool temp = randbit() ; bitarray_set(test_ba, i, temp); } if (test_verbose) { bitarray_fprint(stdout, test_ba); fprintf(stdout, "\n"); fprintf(stdout, " newrand sz=%llu, seed=%u\n", (unsigned long long) bit_sz, seed); } }
/* Create a new bitarray in test_ba by parsing a string of 0s and 1s, e.g. "0101011011". */ static void testutil_frmstr(const char *bitstr) { size_t sl = strlen(bitstr), i; if (test_ba != NULL) bitarray_free(test_ba); test_ba = bitarray_new(sl); assert(test_ba != NULL); size_t myflipcount = 0; bool curbit, prevbit = false; for (i = 0; i < sl; i++) { curbit = boolfromchar(bitstr[i]); if (i != 0 && curbit != prevbit) myflipcount++; bitarray_set(test_ba, i, curbit); prevbit = curbit; } bitarray_fprint(stdout, test_ba); if (test_verbose) { fprintf(stdout, " newstr lit=%s\n", bitstr); testutil_expect(bitstr, myflipcount); } }
void testutil_frmstr(const char *const bitstring) { const size_t bitstring_length = strlen(bitstring); // If we somehow managed to avoid freeing test_bitarray after a previous // test, go free it now. if (test_bitarray != NULL) { bitarray_free(test_bitarray); } test_bitarray = bitarray_new(bitstring_length); assert(test_bitarray != NULL); bool current_bit; for (size_t i = 0; i < bitstring_length; i++) { current_bit = boolfromchar(bitstring[i]); bitarray_set(test_bitarray, i, current_bit); } bitarray_fprint(stdout, test_bitarray); if (test_verbose) { fprintf(stdout, " newstr lit=%s\n", bitstring); testutil_expect(bitstring); } }
/** Update the routerset's <b>countries</b> bitarray_t. Called whenever * the GeoIP IPv4 database is reloaded. */ void routerset_refresh_countries(routerset_t *target) { int cc; bitarray_free(target->countries); if (!geoip_is_loaded(AF_INET)) { target->countries = NULL; target->n_countries = 0; return; } target->n_countries = geoip_get_n_countries(); target->countries = bitarray_init_zero(target->n_countries); SMARTLIST_FOREACH_BEGIN(target->country_names, const char *, country) { cc = geoip_get_country(country); if (cc >= 0) { tor_assert(cc < target->n_countries); bitarray_set(target->countries, cc); } else { log_warn(LD_CONFIG, "Country code '%s' is not recognized.", country); } } SMARTLIST_FOREACH_END(country); }
int main(int argc, char **argv) { unsigned long nbits = 20, *ba = bitarray_alloc(nbits); printf("\nSetting bit 2\n"); bit_test_and_set(ba, 2); test_all_bits(ba, nbits); printf("\nChanging bit 5\n"); bit_test_and_change(ba, 5); test_all_bits(ba, nbits); printf("\nChanging bit 5\n"); bit_test_and_change(ba, 5); test_all_bits(ba, nbits); printf("\nResetting bit 2\n"); bit_test_and_reset(ba, 2); test_all_bits(ba, nbits); bitarray_free(ba); return 0; }
static void test_pick_circid(void *arg) { bitarray_t *ba = NULL; channel_t *chan1, *chan2; circid_t circid; int i; (void) arg; chan1 = tor_malloc_zero(sizeof(channel_t)); chan2 = tor_malloc_zero(sizeof(channel_t)); chan2->wide_circ_ids = 1; chan1->circ_id_type = CIRC_ID_TYPE_NEITHER; tt_int_op(0, OP_EQ, get_unique_circ_id_by_chan(chan1)); /* Basic tests, with no collisions */ chan1->circ_id_type = CIRC_ID_TYPE_LOWER; for (i = 0; i < 50; ++i) { circid = get_unique_circ_id_by_chan(chan1); tt_uint_op(0, OP_LT, circid); tt_uint_op(circid, OP_LT, (1<<15)); } chan1->circ_id_type = CIRC_ID_TYPE_HIGHER; for (i = 0; i < 50; ++i) { circid = get_unique_circ_id_by_chan(chan1); tt_uint_op((1<<15), OP_LT, circid); tt_uint_op(circid, OP_LT, (1<<16)); } chan2->circ_id_type = CIRC_ID_TYPE_LOWER; for (i = 0; i < 50; ++i) { circid = get_unique_circ_id_by_chan(chan2); tt_uint_op(0, OP_LT, circid); tt_uint_op(circid, OP_LT, (1u<<31)); } chan2->circ_id_type = CIRC_ID_TYPE_HIGHER; for (i = 0; i < 50; ++i) { circid = get_unique_circ_id_by_chan(chan2); tt_uint_op((1u<<31), OP_LT, circid); } /* Now make sure that we can behave well when we are full up on circuits */ chan1->circ_id_type = CIRC_ID_TYPE_LOWER; chan2->circ_id_type = CIRC_ID_TYPE_LOWER; chan1->wide_circ_ids = chan2->wide_circ_ids = 0; ba = bitarray_init_zero((1<<15)); for (i = 0; i < (1<<15); ++i) { circid = get_unique_circ_id_by_chan(chan1); if (circid == 0) { tt_int_op(i, OP_GT, (1<<14)); break; } tt_uint_op(circid, OP_LT, (1<<15)); tt_assert(! bitarray_is_set(ba, circid)); bitarray_set(ba, circid); channel_mark_circid_unusable(chan1, circid); } tt_int_op(i, OP_LT, (1<<15)); /* Make sure that being full on chan1 does not interfere with chan2 */ for (i = 0; i < 100; ++i) { circid = get_unique_circ_id_by_chan(chan2); tt_uint_op(circid, OP_GT, 0); tt_uint_op(circid, OP_LT, (1<<15)); channel_mark_circid_unusable(chan2, circid); } done: tor_free(chan1); tor_free(chan2); bitarray_free(ba); circuit_free_all(); }
int main(int argc, char** argv) { /*Check arguments to make sure you got a file*/ //There must be at least some arguments to get a file if(errno != 0){ perror("Unknown Error at start"); errno = 0; } const char* filename = NULL; const char* outfile = NULL; bool gui = false; float nodeCharge = 3; bool output = true; bool isFile = true; layout_params* params = (layout_params*) malloc(sizeof(layout_params)); params->ke = 500; params->kh = 0.0005; params->kl = -0.05; params->width = 1920; params->height = 1080; params->iterations = 10000; params->mass = 1; params->time = 1; params->coefficientOfRestitution = -0.9; params->mus = 0.2; params->muk = 0.04; params->kw = 3; params->kg = 0.06; params->wellMass = 1; params->edgeCharge = nodeCharge; params->forcemode = COULOMBS_LAW | HOOKES_LAW_SPRING | FRICTION | DRAG | BOUNCY_WALLS; params->cpuLoop = false; if (argc < 2) { usage(); return EXIT_FAILURE; } for (int i = 1; i < argc; i++) { if(errno != 0){ fprintf(stderr, "On Iter: %d ", i); perror("Unknown Error"); errno = 0; } if (strcmp(argv[i], "-f") == 0) { if (filename != NULL) { printf( "An input directory has already been provided (%s).\nCan only use one of -f and -d.\n", filename); exit(-1); } filename = readString(argc, argv, ++i); isFile = true; } else if (strcmp(argv[i], "-d") == 0) { if (filename != NULL) { printf( "An input file has already been provided (%s).\nCan only use one of -f and -d.\n", filename); exit(-1); } filename = readString(argc, argv, ++i); isFile = false; } else if (strcmp(argv[i], "-o") == 0) { outfile = readString(argc, argv, ++i); } else if (strcmp(argv[i], "-Ke") == 0) { params->ke = readFloat(argc, argv, ++i); } else if (strcmp(argv[i], "-Kh") == 0) { params->kh = readFloat(argc, argv, ++i); } else if (strcmp(argv[i], "-Kl") == 0) { params->kl = readFloat(argc, argv, ++i); } else if (strcmp(argv[i], "-nodeCharge") == 0) { nodeCharge = readFloat(argc, argv, ++i); } else if (strcmp(argv[i], "-edgeCharge") == 0) { params->edgeCharge = readFloat(argc, argv, ++i); } else if (strcmp(argv[i], "-Kg") == 0) { params->kg = readFloat(argc, argv, ++i); } else if (strcmp(argv[i], "-nm") == 0) { params->wellMass = readFloat(argc, argv, ++i); } else if (strcmp(argv[i], "-mus") == 0) { params->mus = readFloat(argc, argv, ++i); } else if (strcmp(argv[i], "-muk") == 0) { params->muk = readFloat(argc, argv, ++i); } else if (strcmp(argv[i], "-i") == 0) { params->iterations = readInt(argc, argv, ++i); } else if (strcmp(argv[i], "-width") == 0) { params->width = readInt(argc, argv, ++i); } else if (strcmp(argv[i], "-height") == 0) { params->height = readInt(argc, argv, ++i); } else if (strcmp(argv[i], "-gui") == 0) { gui = true; } else if (strcmp(argv[i], "-cpuLoop") == 0) { params->cpuLoop = true; } else if (strcmp(argv[i], "-noOutput") == 0) { output = false; } else if (strcmp(argv[i], "-t") == 0) { params->time = readFloat(argc, argv, ++i); } else if (strcmp(argv[i], "-m") == 0) { params->mass = readFloat(argc, argv, ++i); } else if (strcmp(argv[i], "-cRest") == 0) { params->coefficientOfRestitution = readFloat(argc, argv, ++i); } else if (strcmp(argv[i], "-friction") == 0) { int fricForce = readInt(argc, argv, ++i); params->forcemode = params->forcemode & ~(FRICTION | DRAG); params->forcemode = params->forcemode | (fricForce << 2); } else if (strcmp(argv[i], "-spring") == 0) { int springForce = readInt(argc, argv, ++i); params->forcemode = params->forcemode & ~(HOOKES_LAW_SPRING | LOG_SPRING); params->forcemode = params->forcemode | (springForce); } else if (strcmp(argv[i], "-walls") == 0) { int wallForce = readInt(argc, argv, ++i); params->forcemode = params->forcemode & ~(BOUNCY_WALLS | CHARGED_WALLS | GRAVITY_WELL); params->forcemode = params->forcemode | (wallForce << 4); } else if (strcmp(argv[i], "-forces") == 0) { int primForce = readInt(argc, argv, ++i); params->forcemode = params->forcemode & ~(COULOMBS_LAW | DEGREE_BASED_CHARGE | CHARGED_EDGE_CENTERS | WRAP_AROUND_FORCES); params->forcemode = params->forcemode | (primForce << 7); } else { fprintf(stderr, "Unknown option %s\n", argv[i]); return EXIT_FAILURE; } } if (filename == NULL) { fprintf(stderr, "You must include a filename\n"); usage(); return EXIT_FAILURE; } graph** g; int graphCount = 0; if (isFile) { debug("Reading graph: %s\n", filename); g = (graph**) malloc(sizeof(graph*)); g[0] = readFile(filename); graphCount = 1; } else { g = readDir(filename, &graphCount); } if (g == NULL || graphCount < 1) { fprintf(stderr, "Creating a graph failed. Terminating\n"); usage(); return EXIT_FAILURE; } for (int i = 0; i < graphCount; i++) { graph_initRandom(g[i], 20, 10, params->width, params->height, nodeCharge); } if (gui && isFile) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); glutInitWindowSize(params->width, params->height); glutCreateWindow("Force Directed Layout"); glutDisplayFunc(display); //glutReshapeFunc(reshape); glutIdleFunc(idle); setLight(); initCamera(params->width, params->height); glGraph = g; glParams = params; glutMainLoop(); } else if (gui) { printf("Cannot use gui mode with a directory.\n"); exit(-1); } /* The graph is now is a legal state. * It is not using the GUI. * It is possible to lay it out now */ struct timeval tstart, tend; gettimeofday(&tstart, NULL); graph_layout(g, graphCount, params); gettimeofday(&tend, NULL); long start = tstart.tv_sec * 1000000 + tstart.tv_usec; long end = tend.tv_sec * 1000000 + tend.tv_usec; long msElapsed = end - start; debug("Elapsed Time (us): %ld\n", msElapsed); if (outfile == NULL && isFile) { outfile = "after.svg"; } if (output && (outfile != NULL)) { for (int i = 0; i < graphCount; i++) { #define BUFF_SIZE 1024 char thisOutFile[BUFF_SIZE]; strncpy(thisOutFile,outfile,BUFF_SIZE); if(!isFile){ //Does it end on a / int len = strlen(thisOutFile); if(thisOutFile[len-1] != '/'){ if(len < BUFF_SIZE - 2){ //Keep enough space for the '\0' thisOutFile[len++] = '/'; thisOutFile[len] = '\0'; }else{ fprintf(stderr,"File name too long (%s [/] %s [/] %s)\n. Not creating svg.\n", outfile, (g[i])->dir, g[i]->filename); continue; } } //Now concatenate the dir name if(g[i]->dir != NULL){ int len2 = strlen(g[i]->dir); if(len + len2 +1 > BUFF_SIZE){ fprintf(stderr,"File name too long (%s [/] %s [/] %s)\n. Not creating svg.\n", outfile, (g[i])->dir, g[i]->filename); continue; } strcat(thisOutFile, g[i]->dir + (g[i]->dir[0]=='/'? 1: 0)); } //Now the file name int len2 = strlen(g[i]->filename); if(len + len2 + 1 > BUFF_SIZE){ fprintf(stderr,"File name too long (%s [/] %s [/] %s)\n. Not creating svg.\n", outfile, (g[i])->dir, g[i]->filename); continue; } strcat(thisOutFile,g[i]->filename); char* extension = strstr(thisOutFile, ".graphml"); if(extension ==NULL){ fprintf(stderr,"Something went wrong. Could not find .graphml in %s\nTerminating\n", thisOutFile); exit(EXIT_FAILURE); } strcpy(extension, ".svg"); //Now the filename is finally right! } graph_toSVG(g[i], thisOutFile, params->width, params->height, (params->forcemode & (BOUNCY_WALLS | CHARGED_WALLS)) != 0, msElapsed, params); } } //Free all the graphs and the structure that holds it free(g[0]->nodes); bitarray_free(g[0]->edges); for (int i = 0; i < graphCount; i++) { graph_free(g[i]); } free(g); free(params); return EXIT_SUCCESS; }