⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hashtable_test.c

📁 mobile ip 在linux下的一种实现
💻 C
📖 第 1 页 / 共 2 页
字号:
/* $Id: hashtable_test.c,v 1.18 2000/04/06 07:26:53 jm Exp $ * Hashtable module tests. * * Dynamic hierarchial IP tunnel * Copyright (C) 1998-2000, Dynamics group * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. See README and COPYING for * more details. */#include <stdlib.h>#include <stdio.h>#include <sys/types.h> /* required for netinet/in.h in AMIGAOS */#include <netinet/in.h>#include <string.h>#include <assert.h>#include "hashtable.h"#include "list.h"/* defines */#define ASSERT assert#ifndef TRUE#define TRUE 1#endif#ifndef FALSE#define FALSE 0#endif/* NOTE: * The test cases have been numbered according to the module test codes. * hashtable module uses list module, so even the module testing for hashtable * is sort of integration testing. *//* structures *//* NOTE: YOU ONLY CAN ADD THIS STRUCTURE TO ONE LIST AT A TIME! *//* SO DO NOT TRY TO ADD THIS KIND OF A STRUCTURE INTO TWO HASHTABLES! */struct testdata {	struct node node;	int keyval;	char *text;	float x;};struct hashtable * test_hashtable_init(int size){	struct hashtable *table = hashtable_init(size);	if (table == NULL) {		printf("Test hashtable_init: Init failed, table was NULL\n");		return NULL;	}	printf("test_hashtable_init: Init successful!\n");	return table;}/* A hashfunc to be given as an argument. *//* Returns "key mod size". */int test_hash(void *key, const int size){	int i;	struct testdata *data;	data = (struct testdata *) key;	i = data->keyval;	printf("Key mod size = %d mod %d = %d \n", i, size, (i % size));	ASSERT(i % size >= 0);	ASSERT(i % size < size);	return (i % size);}/* A comparison for the testdata. *//* Returns 1 if the fields of the structs key and cmpd are the same. * Otherwise will return 0. */int test_cmp(void *key, struct node *cmpd){	struct testdata *data1, *data2;	if (key == NULL || cmpd == NULL) {		printf("\ntest_cmp: one of the given arguments was NULL !\n"		       "No match! Returning 0 !\n");		return 0;	}	data1 = (struct testdata *) key;	data2 = (struct testdata *) cmpd;	if (data1->keyval == data2->keyval &&	    data1->text == data2->text &&	    data1->x == data2->x) {                printf("test_cmp: the given key matches to the compared "		       "struct\n");		return 1;	}	printf("test_cmp: no match (yet)...\n");	return 0;}/* An invalid hash function */int hashfunc_invalid(void *key, const int size){	return -5;}/* A valid hash function */int hashfunc_valid(void *key, const int size){	struct testdata *data;	data = (struct testdata *) key;	return data->keyval % size;}/* A valid compare function */int cmpfunc_valid(void *key, struct node *cmpd){	struct testdata *data1, *data2;	data1 = (struct testdata *) key;	data2 = (struct testdata *) cmpd;	printf("cmpfunc_valid: data1 = 0x%08x, data2 = 0x%08x\n",	       (int) data1, (int) data2);	return data1->keyval == data2->keyval;}static int iterfunc_return = TRUE;/* An iterator function that prints some information */int iterfunc_print(struct node *node, void *dummy){	struct testdata *data;	data = (struct testdata *) node;	printf("iterfunc_print: keyval = %d, text = '%s', x = %f\n",	       data->keyval, data->text, data->x);	return iterfunc_return;}/* An iterator function that removes the node */int iterfunc_remove(struct node *node, void *dummy){	struct testdata *data;	data = (struct testdata *) node;	printf("iterfunc_remove: keyval = %d, text = '%s', x = %f\n",	       data->keyval, data->text, data->x);	hashtable_remove(&data->node);	return iterfunc_return;}/* An iterator function that verifies the data */int iterfunc_verify(struct node *node, void *data){	if (data == (void *) iterfunc_verify) {		return 1;	}	return 0;}/****************************************** *                                        * *     Hashtable test main function       * *                                        * ******************************************/int main(void){	struct testdata *data1, *data2, *data3, *data4, *data5;	int i, n, size, primesize;	int result1, result2, result3, result4, result5;	struct hashtable *A, *B;	struct testdata *data1_out, *data2_out;	struct hashtable *table;	struct testdata *data;	printf("\n\t **********************************\n"	       "\t  STARTING HASHTABLE MODULE TESTS! \n"	       "\n"	       "\t ***********************************\n");	/* ASSIGNING TEST DATA VALUES: */	printf("\n\t ASSIGNING TEST DATA VALUES:\n\n");	data1 = malloc(sizeof(struct testdata));	ASSERT(data1 != NULL);	list_init_node(&data1->node);	data1->keyval = 5;	data1->text = "Matti Meikalaenen";	data1->x = 123.4444;	printf("Testdata1: %d, %s, %f\n\n",	       data1->keyval, data1->text, data1->x);	data2 = malloc(sizeof(struct testdata));	ASSERT(data2 != NULL);	list_init_node(&data2->node);	data2->keyval = 111257;	data2->text = "Jooseppi Ikaeheimo";	data2->x = 1999.25;	printf("Testdata2: %d, %s, %f\n\n",	       data2->keyval, data2->text, data2->x);	data3 = malloc(sizeof(struct testdata));	ASSERT(data3 != NULL);	list_init_node(&data3->node);	data3->keyval = 111257;	data3->text = "Hemmo Heikinheimo";	data3->x = 0.1212122;	printf("Testdata3: %d, %s, %f\n\n",	       data3->keyval, data3->text, data3->x);	data4 = malloc(sizeof(struct testdata));	ASSERT(data4 != NULL);	list_init_node(&data4->node);	data4->keyval = 123450;	data4->text = "Kimmon data4";	data4->x = 3.14159265;	printf("Testdata4: %d, %s, %f\n\n",	       data4->keyval, data4->text, data4->x);	data5 = malloc(sizeof(struct testdata));	ASSERT(data5 != NULL);	list_init_node(&data5->node);	data5->keyval = 123456;	data5->text = "Kimmon data5";	data5->x = 3.14159266;	printf("Testdata5: %d, %s, %f\n\n",	       data5->keyval, data5->text, data5->x);	printf("Testing hashtable-module...\n");	/* M01 */	printf("Test case M01: Initialize, size is zero\n");	ASSERT(hashtable_init(0) == NULL);	printf("M01 test cases (1) OK\n");	/* M02 */	printf("Test case M02: Initialize, size is negative\n");	ASSERT(hashtable_init(-5) == NULL);	printf("M02 test cases (1) OK\n");	/* M03 */	printf("Test case M03 requires manual testing\n");	/* M04 */	printf("Test case M04: Initialize, size is too large\n");	ASSERT(hashtable_init(20000000) == NULL);	printf("M04 test cases (1) OK\n");	/* M05 */	printf("Test case M05: Initialize, realistic values\n");	table = hashtable_init(200);	ASSERT(table != NULL);	printf("M05 test cases (1) OK\n");	/* M06 */	printf("Test case M06: Destroy hashtable when it's not empty\n");	ASSERT(hashtable_add(table, hashfunc_valid, data1,			     &data1->node) == TRUE);	ASSERT(hashtable_destroy(table) == FALSE);	printf("M06 test cases (1) OK\n");	table = hashtable_init(200);	ASSERT(table != NULL);	/* M07 */	printf("Test case M07: Destroy hashtable, table is empty\n");	ASSERT(hashtable_destroy(table) == TRUE);	printf("M07 test cases (1) OK\n");	table = hashtable_init(200);	ASSERT(table != NULL);	/* M08 */	printf("Test case M08: Add, hash function invalid\n");	ASSERT(hashtable_add(table, hashfunc_invalid, data1,			     &data1->node) == FALSE);	printf("M08 test cases (1) OK\n");	/* M09 */	printf("Test case M09: Add, hash function valid\n");	ASSERT(hashtable_add(table, hashfunc_valid, data1,			     &data1->node) == TRUE);	printf("M09 test cases (1) OK\n");	/* M10 */	printf("Test case M10: Fetch, hash function invalid\n");	data1->keyval = 5;	data = (struct testdata *) hashtable_fetch(table, hashfunc_invalid,						   data1, cmpfunc_valid);	ASSERT(data == NULL);	printf("M10 test cases (1) OK\n");	/* M11 */	printf("Test case M11: Fetch, hash function valid, node found\n");	data = (struct testdata *) hashtable_fetch(table, hashfunc_valid,						   data1, cmpfunc_valid);	ASSERT(&data->node == &data1->node);	printf("M11 test cases (1) OK\n");	/* M12 */	printf("Test case M12: Fetch, hash function valid, node not found\n");	data = (struct testdata *) hashtable_fetch(table, hashfunc_valid,						   data2, cmpfunc_valid);	ASSERT(data == NULL);	printf("M12 test cases (1) OK\n");	/* M13 */	printf("Test case M13: Remove from table\n");	hashtable_remove(&data1->node);	printf("M13 test cases (1) OK\n");	/* M14 */	printf("Test case M14: Find and remove from table, node found\n");	data1->keyval = 5;	ASSERT(hashtable_add(table, hashfunc_valid, data1,			     &data1->node) == TRUE);	ASSERT(hashtable_find_and_remove(table, hashfunc_valid, data1,					 cmpfunc_valid) == TRUE);	printf("M14 test cases (1) OK\n");	/* M15 */	printf("Test case M15: Find and remove from table, node not found\n");	data1->keyval = 5;	ASSERT(hashtable_add(table, hashfunc_valid, data1,			     &data1->node) == TRUE);	data1->keyval = 6;	ASSERT(hashtable_find_and_remove(table, hashfunc_valid, data1,					 cmpfunc_valid) == FALSE);	printf("M15 test cases (1) OK\n");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -