int main() { Hash_map hash_map; Data data= (Data) 123; Key key= (Key) 456; mcheck_pedantic(NULL); hash_map= create_example_hash_map(); /* calling some of the functions with an empty Hash_map */ if (!get(&hash_map, (Key) (long) 1, &data) && !get_smallest(hash_map, &key, &data) && num_keys(hash_map, 0) == 0 && index_of(hash_map, (Key) (long) 5) == -1 && data == (Data) 123 && key == (Key) 456) printf("Your functions can handle an empty hash_map correctly!\n"); else printf("Oops! Error in handling anempty hash_map.\n"); clear(&hash_map); if (mallinfo().uordblks != 0) printf("Memory leak of %d bytes. :(\n", mallinfo().uordblks); else printf("No memory leak detected. :)\n"); return 0; }
static void memory_leak_check() { unsigned int memory_used; mcheck_pedantic(NULL); memory_used= mallinfo().uordblks; test_function(); memory_used= mallinfo().uordblks - memory_used; if (memory_used != 0) printf("Memory leak detected (%d bytes)\n", memory_used); else printf("No memory leak detected.\n"); }
int main() { Hash_map hash_map; Data data; Iterator iter; int i, correct= 1; int sorted_offsets[NUM]= {7, 8, 9, 2, 0, 3, 4, 6, 1, 5}; mcheck_pedantic(NULL); hash_map= create_example_hash_map(); iter= create_iterator(hash_map); for (i= 0; i < 5 && correct; i++) { data= next(&iter); if ((int) *((long *) data) != sorted_offsets[i]) correct= 0; } if (!get(&hash_map, (Key) (long) 'a', &data) || data != (Data) 7 || !get(&hash_map, (Key) (long) 'q', &data) || data != (Data) 0 || !get(&hash_map, (Key) (long) 'c', &data) || data != (Data) 9) correct= 0; reset(&iter); for (i= 0; i < NUM && correct; i++) { data= next(&iter); if ((int) *((long *) data) != sorted_offsets[i]) correct= 0; } if (correct && !has_next(iter)) printf("Correct values were accessed when iterating! :)\n"); else printf("Incorrect results occurred when iterating. :(\n"); clear(&hash_map); clear_iterator(&iter); if (mallinfo().uordblks != 0) printf("Memory leak of %d bytes. :(\n", mallinfo().uordblks); else printf("No memory leak detected. :)\n"); return 0; }
int main() { Hash_map hash_map; Data data; Fraction sorted_fractions[NUM]= {{1, 10}, {2, 10}, {1, 2}, {3, 6}, {5, 9}, {3, 5}, {5, 8}, {2, 3}, {5, 3}, {7, 4}, {4, 5}, {8, 9}}; int i, correct= 1; Fraction *fp; mcheck_pedantic(NULL); hash_map= create_example_hash_map(); for (i= 0; i < NUM; i += 3) { fp= malloc(sizeof(*fp)); if (fp == NULL) die("Memory could not be allocated."); *fp= sorted_fractions[i]; put(&hash_map, (Key) fp, (Data) (long) 0); /* existing key, new value */ } for (i= 0; i < NUM && correct; i += 3) { get(&hash_map, (Key) &sorted_fractions[i], &data); if ((int) (long) data != 0) correct= 0; } if (size(hash_map) == 13 && correct == 1) printf("A new key/value pair was not created! :)\n"); else printf("A new key/value pair was created. :(\n"); clear(&hash_map); if (mallinfo().uordblks != 0) printf("Memory leak of %d bytes. :(\n", mallinfo().uordblks); else printf("No memory leak detected. :)\n"); return 0; }
/** * Test program looping and reading LLDP/CDP packets and exercising most of the packet * send/receive mechanism and a good bit of nanoprobe and CMA basic infrastructure. * * It plays both sides of the game - the CMA and the nanoprobe. * * It leaves most of the work of starting up the nanoprobe code to nano_start_full() */ int main(int argc, char **argv) { const guint8 loopback[] = CONST_IPV6_LOOPBACK; //const guint8 mcastaddrstring[] = CONST_ASSIM_DEFAULT_V4_MCAST; //NetAddr* mcastaddr; const guint8 otheradstring[] = {127,0,0,1}; const guint8 otheradstring2[] = {10,10,10,4}; const guint8 anyadstring[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; guint16 testport = TESTPORT; SignFrame* signature = signframe_glib_new(G_CHECKSUM_SHA256, 0); Listener* otherlistener; ConfigContext* config = configcontext_new(0); PacketDecoder* decoder = nano_packet_decoder(); AuthListener* listentonanoprobes; ReliableUDP* rtransport; #if 0 # ifdef HAVE_MCHECK_PEDANTIC g_assert(mcheck_pedantic(NULL) == 0); # else # ifdef HAVE_MCHECK g_assert(mcheck(NULL) == 0); # endif # endif #endif g_setenv("G_MESSAGES_DEBUG", "all", TRUE); #if 0 proj_class_incr_debug(NULL); proj_class_incr_debug(NULL); proj_class_incr_debug(NULL); proj_class_incr_debug(NULL); #endif g_log_set_fatal_mask(NULL, G_LOG_LEVEL_ERROR); if (argc > 1) { maxpkts = atol(argv[1]); g_debug("Max packet count is "FMT_64BIT"d", maxpkts); } if (netio_is_dual_ipv4v6_stack()) { g_message("Our OS supports dual ipv4/v6 sockets. Hurray!"); }else{ g_warning("Our OS DOES NOT support dual ipv4/v6 sockets - this may not work!!"); } config->setframe(config, CONFIGNAME_OUTSIG, &signature->baseclass); // Create a network transport object for normal UDP packets rtransport = reliableudp_new(0, config, decoder, 0); rtransport->_protocol->window_size = 8; nettransport = &(rtransport->baseclass.baseclass); g_return_val_if_fail(NULL != nettransport, 2); // Set up the parameters the 'CMA' is going to send to our 'nanoprobe' // in response to their request for configuration data. nanoconfig = configcontext_new(0); nanoconfig->setint(nanoconfig, CONFIGNAME_INTERVAL, 1); nanoconfig->setint(nanoconfig, CONFIGNAME_TIMEOUT, 3); nanoconfig->setint(nanoconfig, CONFIGNAME_CMAPORT, testport); // Construct the NetAddr we'll talk to (i.e., ourselves) and listen from destaddr = netaddr_ipv6_new(loopback, testport); g_return_val_if_fail(NULL != destaddr, 3); config->setaddr(config, CONFIGNAME_CMAINIT, destaddr); nanoconfig->setaddr(nanoconfig, CONFIGNAME_CMAADDR, destaddr); nanoconfig->setaddr(nanoconfig, CONFIGNAME_CMAFAIL, destaddr); nanoconfig->setaddr(nanoconfig, CONFIGNAME_CMADISCOVER, destaddr); // Construct another couple of NetAddrs to talk to and listen from // for good measure... otheraddr = netaddr_ipv4_new(otheradstring, testport); g_return_val_if_fail(NULL != otheraddr, 4); otheraddr2 = netaddr_ipv4_new(otheradstring2, testport); g_return_val_if_fail(NULL != otheraddr2, 4); // Construct another NetAddr to bind to (anything) anyaddr = netaddr_ipv6_new(anyadstring, testport); g_return_val_if_fail(NULL != destaddr, 5); // Bind to ANY address (as noted above) g_return_val_if_fail(nettransport->bindaddr(nettransport, anyaddr, FALSE),16); //g_return_val_if_fail(nettransport->bindaddr(nettransport, destaddr),16); g_message("NOT Joining multicast address."); #if 0 // We can't do this because of encryption and we will likely screw up // others on our network even if that weren't a problem... mcastaddr = netaddr_ipv4_new(mcastaddrstring, testport); g_return_val_if_fail(nettransport->mcastjoin(nettransport, mcastaddr, NULL), 17); UNREF(mcastaddr); g_message("multicast join succeeded."); #endif // Connect up our network transport into the g_main_loop paradigm // so we get dispatched when packets arrive netpkt = netgsource_new(nettransport, NULL, G_PRIORITY_HIGH, FALSE, NULL, 0, NULL); // Set up so that we can observe all unclaimed packets otherlistener = listener_new(config, 0); otherlistener->got_frameset = gotnetpkt; netpkt->addListener(netpkt, 0, otherlistener); otherlistener->associate(otherlistener,netpkt); // Unref the "other" listener - we hold other references to it UNREF(otherlistener); // Pretend to be the CMA... // Listen for packets from our nanoprobes - scattered throughout space... listentonanoprobes = authlistener_new(0, cmalist, config, TRUE, NULL); listentonanoprobes->baseclass.associate(&listentonanoprobes->baseclass, netpkt); nano_start_full("netconfig", 900, netpkt, config, test_cma_authentication); g_timeout_add_seconds(1, timeout_agent, NULL); mainloop = g_main_loop_new(g_main_context_default(), TRUE); /******************************************************************** * Start up the main loop - run our test program... * (the one pretending to be both the nanoprobe and the CMA) ********************************************************************/ g_main_loop_run(mainloop); /******************************************************************** * We exited the main loop. Shut things down. ********************************************************************/ nano_shutdown(TRUE); // Tell it to shutdown and print stats g_message("Count of 'other' pkts received:\t%d", wirepktcount); UNREF(nettransport); // Main loop is over - shut everything down, free everything... g_main_loop_unref(mainloop); mainloop=NULL; // Unlink misc dispatcher - this should NOT be necessary... netpkt->addListener(netpkt, 0, NULL); // Dissociate packet actions from the packet source. listentonanoprobes->baseclass.dissociate(&listentonanoprobes->baseclass); // Unref the AuthListener object UNREF2(listentonanoprobes); g_source_destroy(&netpkt->baseclass); g_source_unref(&netpkt->baseclass); //g_main_context_unref(g_main_context_default()); // Free signature frame UNREF2(signature); // Free misc addresses UNREF(destaddr); UNREF(otheraddr); UNREF(otheraddr2); UNREF(anyaddr); // Free config object UNREF(config); UNREF(nanoconfig); // At this point - nothing should show up - we should have freed everything if (proj_class_live_object_count() > 0) { g_warning("Too many objects (%d) alive at end of test.", proj_class_live_object_count()); proj_class_dump_live_objects(); ++errcount; }else{ g_message("No objects left alive. Awesome!"); } proj_class_finalize_sys(); // Shut down object system to make valgrind happy :-D return(errcount <= 127 ? errcount : 127); }
static int do_test (void) { mcheck_pedantic (mcheck_abort); CHAR_T *buf = (CHAR_T *) 1l; size_t len = 12345; FILE *fp = OPEN_MEMSTREAM (&buf, &len); if (fp == NULL) { printf ("%s failed\n", S(OPEN_MEMSTREAM)); return 1; } for (int outer = 0; outer < 800; ++outer) { for (int inner = 0; inner < 100; ++inner) if (fputc (W('a') + (outer * 100 + inner) % 26, fp) == EOF) { printf ("fputc at %d:%d failed\n", outer, inner); return 1; } if (fflush (fp) != 0) { puts ("fflush failed"); return 1; } if (len != (outer + 1) * 100) { printf ("string in round %d not %d bytest long\n", outer + 1, (outer + 1) * 100); return 1; } if (buf == (CHAR_T *) 1l) { printf ("round %d: buf not updated\n", outer + 1); return 1; } for (int inner = 0; inner < (outer + 1) * 100; ++inner) if (buf[inner] != W('a') + inner % 26) { printf ("round %d: buf[%d] != '%c'\n", outer + 1, inner, (char) (W('a') + inner % 26)); return 1; } } buf = (CHAR_T *) 1l; len = 12345; if (fclose (fp) != 0) { puts ("fclose failed"); return 1; } if (len != 800 * 100) { puts ("string after close not 80000 bytes long"); return 1; } if (buf == (CHAR_T *) 1l) { puts ("buf not updated"); return 1; } for (int inner = 0; inner < 800 * 100; ++inner) if (buf[inner] != W('a') + inner % 26) { printf ("after close: buf[%d] != %c\n", inner, (char) (W('a') + inner % 26)); return 1; } free (buf); return 0; }