int main() { // // make a list of even numbers // Node *test_list = make_node(2, NULL); // test_list->next = make_node(4, NULL); // test_list->next->next = make_node(6, NULL); // // insert the odd numbers // insert_by_index(&test_list, 1, 0); // insert_by_index(&test_list, 3, 2); // insert_by_index(&test_list, 5, 4); // insert_by_index(&test_list, 7, 6); // // this index is out of bounds; should return -1 // int res = insert_by_index(&test_list, 9, 8); // assert(res == -1); // printf("test_list\n"); // print_list(test_list); // free_everything(&test_list); // // make an empty list // printf("empty\n"); // Node *empty = NULL; // // add an element to the empty list // insert_by_index(&empty, 1, 0); // print_list(empty); // free_everything(&empty); Node *something = make_something(); print_list(something); free_everything(&something); return 0; }
void pipe_exec_cmd(t_state *state, char *line, int stdin, int stdout) { char **cmds; int fd_in; int fd_out; fd_in = 0; fd_out = 0; handle_dup2_pipe(state, stdin, stdout); cmds = pipe_get_cmds(line, &fd_out, &fd_in); handle_dup2_redirection(state, fd_in, fd_out); handle_cmds(state, line, cmds); free_everything(cmds, state); exit(0); }
// Any other main prototype will bug MinGW's SDL2 // Doesn't matter on *nix int main(int argc, char **argv) { // Init the structures Sdl *sdl = init_sdl(); Fractal *fractal = init_fractal(); // Init console print_verbose(fractal); // User can exit program using escape while (is_user_pressing_escape(sdl) == 0) { draw_mandelbrot(sdl, fractal); is_user_moving(sdl, fractal); SDL_RenderPresent(sdl->renderer); SDL_Delay(10); } free_everything(sdl, fractal); return 0; }
/// @brief executes uid hash test for specified client-ids (3 hosts) /// /// Creates three host structures, adds first host to the uid hash, /// then adds second host to the hash, then removes first host, /// then removed the second. Many checks are performed during all /// operations. /// /// @param clientid1 client-id of the first host /// @param clientid1_len client-id1 length (may be 0 for strings) /// @param clientid2 client-id of the second host /// @param clientid2_len client-id2 length (may be 0 for strings) /// @param clientid3 client-id of the second host /// @param clientid3_len client-id2 length (may be 0 for strings) void lease_hash_test_3hosts(unsigned char clientid1[], size_t clientid1_len, unsigned char clientid2[], size_t clientid2_len, unsigned char clientid3[], size_t clientid3_len) { printf("Checking hash operation for 3 hosts: clientid1-len=%lu" " clientid2-len=%lu clientid3-len=%lu\n", (unsigned long) clientid1_len, (unsigned long) clientid2_len, (unsigned long) clientid3_len); dhcp_db_objects_setup (); dhcp_common_objects_setup (); /* check that there is actually zero hosts in the hash */ /* @todo: host_hash_for_each() */ struct host_decl *host1 = 0, *host2 = 0, *host3 = 0; struct host_decl *check = 0; /* === step 1: allocate hosts === */ ATF_CHECK_MSG(host_allocate(&host1, MDL) == ISC_R_SUCCESS, "Failed to allocate host"); ATF_CHECK_MSG(host_allocate(&host2, MDL) == ISC_R_SUCCESS, "Failed to allocate host"); ATF_CHECK_MSG(host_allocate(&host3, MDL) == ISC_R_SUCCESS, "Failed to allocate host"); ATF_CHECK_MSG(host_new_hash(&host_uid_hash, HOST_HASH_SIZE, MDL) != 0, "Unable to create new hash"); ATF_CHECK_MSG(buffer_allocate(&host1->client_identifier.buffer, clientid1_len, MDL) != 0, "Can't allocate uid buffer for host1"); ATF_CHECK_MSG(buffer_allocate(&host2->client_identifier.buffer, clientid2_len, MDL) != 0, "Can't allocate uid buffer for host2"); ATF_CHECK_MSG(buffer_allocate(&host3->client_identifier.buffer, clientid3_len, MDL) != 0, "Can't allocate uid buffer for host3"); /* verify that our hosts are not in the hash yet */ ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid1, clientid1_len, MDL) == 0, "Host1 is not supposed to be in the uid_hash."); ATF_CHECK_MSG(!check, "Host1 is not supposed to be in the uid_hash."); ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid2, clientid2_len, MDL) == 0, "Host2 is not supposed to be in the uid_hash."); ATF_CHECK_MSG(!check, "Host2 is not supposed to be in the uid_hash."); ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid3, clientid3_len, MDL) == 0, "Host3 is not supposed to be in the uid_hash."); ATF_CHECK_MSG(!check, "Host3 is not supposed to be in the uid_hash."); /* === step 2: add hosts to the hash === */ host_hash_add(host_uid_hash, clientid1, clientid1_len, host1, MDL); host_hash_add(host_uid_hash, clientid2, clientid2_len, host2, MDL); host_hash_add(host_uid_hash, clientid3, clientid3_len, host3, MDL); ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid1, clientid1_len, MDL), "Host1 was supposed to be in the uid_hash."); /* Hey! That's not the host we were looking for! */ ATF_CHECK_MSG(check == host1, "Wrong host returned by host_hash_lookup"); host_dereference(&check, MDL); /* we don't need it now */ ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid2, clientid2_len, MDL), "Host2 was supposed to be in the uid_hash."); ATF_CHECK_MSG(check, "Host2 was supposed to be in the uid_hash."); /* Hey! That's not the host we were looking for! */ ATF_CHECK_MSG(check == host2, "Wrong host returned by host_hash_lookup"); host_dereference(&check, MDL); /* we don't need it now */ ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid3, clientid3_len, MDL), "Host3 was supposed to be in the uid_hash."); ATF_CHECK_MSG(check, "Host3 was supposed to be in the uid_hash."); /* Hey! That's not the host we were looking for! */ ATF_CHECK_MSG(check == host3, "Wrong host returned by host_hash_lookup"); host_dereference(&check, MDL); /* we don't need it now */ /* === step 4: remove first host from the hash === */ /* delete host from hash */ host_hash_delete(host_uid_hash, clientid1, clientid1_len, MDL); /* verify that host1 is no longer in the hash */ ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid1, clientid1_len, MDL) == 0, "Host1 is not supposed to be in the uid_hash."); ATF_CHECK_MSG(!check, "Host1 is not supposed to be in the uid_hash."); /* host2 and host3 should be still there, though */ ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid2, clientid2_len, MDL), "Host2 was supposed to still be in the uid_hash."); host_dereference(&check, MDL); ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid3, clientid3_len, MDL), "Host3 was supposed to still be in the uid_hash."); host_dereference(&check, MDL); /* === step 5: remove second host from the hash === */ host_hash_delete(host_uid_hash, clientid2, clientid2_len, MDL); ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid2, clientid2_len, MDL) == 0, "Host2 was not supposed to be in the uid_hash anymore."); ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid3, clientid3_len, MDL), "Host3 was supposed to still be in the uid_hash."); host_dereference(&check, MDL); /* === step 6: remove the last (third) host from the hash === */ host_hash_delete(host_uid_hash, clientid3, clientid3_len, MDL); ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid3, clientid3_len, MDL) == 0, "Host3 was not supposed to be in the uid_hash anymore."); host_dereference(&check, MDL); host_dereference(&host1, MDL); host_dereference(&host2, MDL); host_dereference(&host3, MDL); /* * No easy way to check if the host object were actually released. * We could run it in valgrind and check for memory leaks. */ #if defined (DEBUG_MEMORY_LEAKAGE) && defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT) /* @todo: Should be called in cleanup */ free_everything (); #endif }
/// @brief executes uid hash test for specified client-ids (2 hosts) /// /// Creates two host structures, adds first host to the uid hash, /// then adds second host to the hash, then removes first host, /// then removed the second. Many checks are performed during all /// operations. /// /// @param clientid1 client-id of the first host /// @param clientid1_len client-id1 length (may be 0 for strings) /// @param clientid2 client-id of the second host /// @param clientid2_len client-id2 length (may be 0 for strings) void lease_hash_test_2hosts(unsigned char clientid1[], size_t clientid1_len, unsigned char clientid2[], size_t clientid2_len) { printf("Checking hash operation for 2 hosts: clientid1-len=%lu" "clientid2-len=%lu\n", (unsigned long) clientid1_len, (unsigned long) clientid2_len); dhcp_db_objects_setup (); dhcp_common_objects_setup (); /* check that there is actually zero hosts in the hash */ /* @todo: host_hash_for_each() */ struct host_decl *host1 = 0, *host2 = 0; struct host_decl *check = 0; /* === step 1: allocate hosts === */ ATF_CHECK_MSG(host_allocate(&host1, MDL) == ISC_R_SUCCESS, "Failed to allocate host"); ATF_CHECK_MSG(host_allocate(&host2, MDL) == ISC_R_SUCCESS, "Failed to allocate host"); ATF_CHECK_MSG(host_new_hash(&host_uid_hash, HOST_HASH_SIZE, MDL) != 0, "Unable to create new hash"); ATF_CHECK_MSG(buffer_allocate(&host1->client_identifier.buffer, clientid1_len, MDL) != 0, "Can't allocate uid buffer for host1"); ATF_CHECK_MSG(buffer_allocate(&host2->client_identifier.buffer, clientid2_len, MDL) != 0, "Can't allocate uid buffer for host2"); /* setting up host1->client_identifier is actually not needed */ /* ATF_CHECK_MSG(lease_set_clientid(host1, clientid1, actual1_len) != 0, "Failed to set client-id for host1"); ATF_CHECK_MSG(lease_set_clientid(host2, clientid2, actual2_len) != 0, "Failed to set client-id for host2"); */ ATF_CHECK_MSG(host1->refcnt == 1, "Invalid refcnt for host1"); ATF_CHECK_MSG(host2->refcnt == 1, "Invalid refcnt for host2"); /* verify that our hosts are not in the hash yet */ ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid1, clientid1_len, MDL) == 0, "Host1 is not supposed to be in the uid_hash."); ATF_CHECK_MSG(!check, "Host1 is not supposed to be in the uid_hash."); ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid2, clientid2_len, MDL) == 0, "Host2 is not supposed to be in the uid_hash."); ATF_CHECK_MSG(!check, "Host2 is not supposed to be in the uid_hash."); /* === step 2: add first host to the hash === */ host_hash_add(host_uid_hash, clientid1, clientid1_len, host1, MDL); /* 2 pointers expected: ours (host1) and the one stored in hash */ ATF_CHECK_MSG(host1->refcnt == 2, "Invalid refcnt for host1"); /* 1 pointer expected: just ours (host2) */ ATF_CHECK_MSG(host2->refcnt == 1, "Invalid refcnt for host2"); /* verify that host1 is really in the hash and the we can find it */ ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid1, clientid1_len, MDL), "Host1 was supposed to be in the uid_hash."); ATF_CHECK_MSG(check, "Host1 was supposed to be in the uid_hash."); /* Hey! That's not the host we were looking for! */ ATF_CHECK_MSG(check == host1, "Wrong host returned by host_hash_lookup"); /* 3 pointers: host1, (stored in hash), check */ ATF_CHECK_MSG(host1->refcnt == 3, "Invalid refcnt for host1"); /* reference count should be increased because we not have a pointer */ host_dereference(&check, MDL); /* we don't need it now */ ATF_CHECK_MSG(check == NULL, "check pointer is supposed to be NULL"); /* 2 pointers: host1, (stored in hash) */ ATF_CHECK_MSG(host1->refcnt == 2, "Invalid refcnt for host1"); /* verify that host2 is not in the hash */ ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid2, clientid2_len, MDL) == 0, "Host2 was not supposed to be in the uid_hash[2]."); ATF_CHECK_MSG(check == NULL, "Host2 was not supposed to be in the hash."); /* === step 3: add second hot to the hash === */ host_hash_add(host_uid_hash, clientid2, clientid2_len, host2, MDL); /* 2 pointers expected: ours (host1) and the one stored in hash */ ATF_CHECK_MSG(host2->refcnt == 2, "Invalid refcnt for host2"); ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid2, clientid2_len, MDL), "Host2 was supposed to be in the uid_hash."); ATF_CHECK_MSG(check, "Host2 was supposed to be in the uid_hash."); /* Hey! That's not the host we were looking for! */ ATF_CHECK_MSG(check == host2, "Wrong host returned by host_hash_lookup"); /* 3 pointers: host1, (stored in hash), check */ ATF_CHECK_MSG(host2->refcnt == 3, "Invalid refcnt for host1"); host_dereference(&check, MDL); /* we don't need it now */ /* now we have 2 hosts in the hash */ /* verify that host1 is still in the hash and the we can find it */ ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid1, clientid1_len, MDL), "Host1 was supposed to be in the uid_hash."); ATF_CHECK_MSG(check, "Host1 was supposed to be in the uid_hash."); /* Hey! That's not the host we were looking for! */ ATF_CHECK_MSG(check == host1, "Wrong host returned by host_hash_lookup"); /* 3 pointers: host1, (stored in hash), check */ ATF_CHECK_MSG(host1->refcnt == 3, "Invalid refcnt for host1"); host_dereference(&check, MDL); /* we don't need it now */ /** * @todo check that there is actually two hosts in the hash. * Use host_hash_for_each() for that. */ /* === step 4: remove first host from the hash === */ /* delete host from hash */ host_hash_delete(host_uid_hash, clientid1, clientid1_len, MDL); ATF_CHECK_MSG(host1->refcnt == 1, "Invalid refcnt for host1"); ATF_CHECK_MSG(host2->refcnt == 2, "Invalid refcnt for host2"); /* verify that host1 is no longer in the hash */ ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid1, clientid1_len, MDL) == 0, "Host1 is not supposed to be in the uid_hash."); ATF_CHECK_MSG(!check, "Host1 is not supposed to be in the uid_hash."); /* host2 should be still there, though */ ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid2, clientid2_len, MDL), "Host2 was supposed to still be in the uid_hash."); host_dereference(&check, MDL); /* === step 5: remove second host from the hash === */ host_hash_delete(host_uid_hash, clientid2, clientid2_len, MDL); ATF_CHECK_MSG(host1->refcnt == 1, "Invalid refcnt for host1"); ATF_CHECK_MSG(host2->refcnt == 1, "Invalid refcnt for host2"); ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid2, clientid2_len, MDL) == 0, "Host2 was not supposed to be in the uid_hash anymore."); host_dereference(&host1, MDL); host_dereference(&host2, MDL); /* * No easy way to check if the host object were actually released. * We could run it in valgrind and check for memory leaks. */ #if defined (DEBUG_MEMORY_LEAKAGE) && defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT) /* @todo: Should be called in cleanup */ free_everything (); #endif }