// Test put and get functions int test_hmap_2() { hmap_handle* hmap; hmap = hmap_create(); int* data1; int* data2; int* data3; data1 = new(int); *data1 = 0; hmap_put(hmap, (long) (*data1), data1); data2 = new(int); *data2 = 1; hmap_put(hmap, (long) (*data2), data2); data3 = new(int); *data3 = 2; hmap_put(hmap, (long) (*data3), data3); if (hmap_get(hmap, (long) (*data1)) != data1 || hmap_get(hmap, (long) (*data2)) != data2 || hmap_get(hmap, (long) (*data3)) != data3) { kfree(data1); kfree(data2); kfree(data3); hmap_free(hmap); return TEST_FAIL; } return TEST_OK; }
/** * repad_net_init - Initialize all structures used to receive packets from network * * @param id The id of thread which call this function * * @return <0 for errors */ static int repad_net_init(long id) { int result_raw = 0, result_udp = 0, result_overlay = 0; repad_attr.th_network_threads[TH_RAW_SOCKET] = 0; repad_attr.th_network_threads[TH_UDP_SOCKET] = 0; repad_attr.th_network_threads[TH_OVERLAY_NET] = 0; /* Create a new prod_cons for receive packets from network */ repad_attr.pc_receive_packets = pc_create(10, NULL, NULL, NULL, (void(*)(void*))skb_release); if (repad_attr.pc_receive_packets == NULL) { repad_printf(LOG_INFO, "Th%ld: ERROR: On create prod_cons for received packets\n"); return -1; } /* Create HashMap for memory message */ repad_attr.hm_memory_message = hmap_create(hmap_hash_mem_fn, hmap_eq_mem_fn, NULL, (void (*)(hmap_key_t))dll_destroy_all, 0, 0.0); if (repad_attr.hm_memory_message == NULL) { repad_printf(LOG_INFO, "Th%ld: ERROR: On create map for memory message\n"); return -1; } /* Create list of packet with no match */ repad_attr.list_pkt_without_match = dll_create(upwm_compare_packet, NULL); if (repad_attr.list_pkt_without_match == NULL) { repad_printf(LOG_INFO, "Th%ld: ERROR: On create list for packets without match\n"); return -1; } /* Load ifaces informations */ load_all_ifaces_info(&repad_attr.list_all_ifaces); repad_printf(LOG_INFO, "Th%ld: It was found %d interface(s)...\n", id, dll_get_num_elements(repad_attr.list_all_ifaces)); /* Init raw socket */ repad_printf(LOG_INFO, "Th%ld: raw_socket_enable=%s - udp_socket_enable=%s" " - overlay_enable=%s\n", id, strbool(repad_attr.bl_raw_socket_enable), strbool(repad_attr.bl_udp_socket_enable), strbool(repad_attr.bl_overlay_enable)); if (repad_attr.bl_raw_socket_enable) { result_raw = repad_net_init_raw_socket(id); } /* Init UDP socket */ if (repad_attr.bl_udp_socket_enable) { result_udp = repad_net_init_udp_socket(id); } /* Init overlay */ if (repad_attr.bl_overlay_enable) { result_overlay = repad_net_init_overlay(id); } return ((result_raw >= 0) ? 0 : (result_udp >= 0) ? 0 : (result_overlay >= 0) ? 0 : -1); }
//Tests the create function int test_hmap_1() { hmap_handle* hmap1 = hmap_create(); hmap_handle* hmap2 = hmap_create_fixed(3); if (!hmap1) { os_printf("expected value"); return TEST_FAIL; } if (!hmap2) { os_printf("expected value"); return TEST_FAIL; } return TEST_OK; }
// Test remove function int test_hmap_3() { hmap_handle* hmap; hmap = hmap_create(); int* data1; int* data2; int* data3; data1 = new(int); *data1 = 0; hmap_put(hmap, (long) (*data1), data1); data2 = new(int); *data2 = 1; hmap_put(hmap, (long) (*data2), data2); data3 = new(int); *data3 = 2; hmap_put(hmap, (long) (*data3), data3); int* tmp1 = hmap_remove(hmap, (long) (*data1)); int* tmp2 = hmap_remove(hmap, (long) (*data2)); int* tmp3 = hmap_remove(hmap, (long) (*data3)); if (tmp1 != data1 || tmp2 != data2 || tmp3 != data3 || hmap_count(hmap) != 0) { kfree(tmp1); kfree(tmp2); kfree(tmp3); hmap_free(hmap); return TEST_FAIL; } kfree(tmp1); kfree(tmp2); kfree(tmp3); hmap_free(hmap); return TEST_OK; }
// Test count function int test_hmap_4() { hmap_handle* hmap; hmap = hmap_create(); int* data1; int* data2; int* data3; int* data4; data1 = new(int); *data1 = 0; hmap_put(hmap, (long) (*data1), data1); if (hmap_count(hmap) != 1) { kfree(data1); hmap_free(hmap); return TEST_FAIL; } data2 = new(int); *data2 = 1; hmap_put(hmap, (long) (*data2), data2); if (hmap_count(hmap) != 2) { kfree(data1); kfree(data2); hmap_free(hmap); return TEST_FAIL; } data3 = new(int); *data3 = 2; hmap_put(hmap, (long) (*data3), data3); if (hmap_count(hmap) != 3) { kfree(data1); kfree(data2); kfree(data3); hmap_free(hmap); return TEST_FAIL; } kfree(hmap_remove(hmap, (long) (*data2))); if (hmap_count(hmap) != 2) { kfree(data1); kfree(data3); hmap_free(hmap); return TEST_FAIL; } data4 = new(int); *data4 = 3; hmap_put(hmap, (long) (*data4), data4); if (hmap_count(hmap) != 3) { kfree(data1); kfree(data3); kfree(data4); hmap_free(hmap); return TEST_FAIL; } kfree(data1); kfree(data2); kfree(data3); kfree(data4); hmap_free(hmap); return TEST_OK; }