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

📄 slp_attr_test.c

📁 SLP协议在linux下的实现。此版本为1.2.1版。官方网站为www.openslp.org
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <slp.h>#include <stdio.h>#include <stdlib.h>#include <assert.h>#include <string.h>#include <libslpattr.h>#define STR "a string of length."#define STR1 "hi \\there again"#define STR2 "yet another value"#define OP1 "aasdf"#define OP2 "ae\nl;adsf;\\y"#define SER1 "(abool=false),keyw,(anInt=51)"#define TERM_INT  (int)-2345#include <stdarg.h>/* Tests a serialized string to see if an attribute contains the named  * values.  * * Params: * str - The serialized attribute list.  * name - Name of the attribute to test.  * ... - List of strings the attribute must contain. Null terminated.  *  * Returns 1 iff the attributes contain all of the named values. 0 otherwise. */int test_serialized(char *str, char *name, ...) {	char *vl_start; /* The start of the serialized value list. */	char *vl_end; /* End of the value list. The ')'. */	int vl_size; /* The expected size of the value list. */	va_list ap;	char *current_value; /* Current member of ap. */		/***** Find value list. *****/	{		char *attr_name; /* The attribute name as it should appear in the serialized string. (ie, with preceeding '(' and trailing '=')*/				int len; /* The length of the name. */				/**** Build attribute as it should appear in str. ****/		len = strlen(name) + 2; /* +2 for '(' and '='. */		attr_name = (char *)malloc(len + 1); /* +1 for null. */		assert(attr_name);		/*** Add pre/post fix. ***/		*attr_name = '(';		strcpy(attr_name + 1, name);		attr_name[len - 1] = '=';		attr_name[len] = 0;				/**** Search in attribute list. ****/		vl_start = strstr(str, attr_name);		len = strlen(attr_name); /* Icky variable reuse. */		free(attr_name);		if (vl_start == NULL) {			return 0;		}		vl_start += len; /* Zip to the start of the attribute list. */		vl_end = strchr(vl_start, ')');		if (vl_end == NULL) {			return 0; /* No terminating ')'. Syntax error. */		}	}	vl_size = 0;		/***** Text contents of value list. *****/	va_start(ap, name);	current_value = va_arg(ap, char *);	while (current_value != NULL) { /* Foreach value... */		char *index; /* Index to current_value in value list. */				/* Check to see if the current value is in the value list. */		index = strstr(vl_start, current_value);		if (index == NULL) {			/* A value is missing. */			va_end(ap);			return 0;		}		/* Check that it's _within_ the list. */		if (index > vl_end) {			va_end(ap);			return 0;		}		/* TODO Check that it is properly delimited. */						/* Increment vl_size. */		vl_size += strlen(current_value) + 1; /* +1 for comma. */				current_value = va_arg(ap, char *); /* NEXT! */	}	va_end(ap);		/***** Check that all values in str are accounted for (ie, there are no extra). *****/	/**** Account for commas in value list. ****/	vl_size -= 1; /* -1 for lack of trailing comma. */	if (vl_size != (int) (vl_end - vl_start)) {		return 0;	}		return 1;	}/* Finds the number of attributes in an attribute list.  * * Returns the number of attributes in the passed list.  */int test_size(SLPAttributes attr) {	SLPAttrIterator iter;	SLPError err;	char const *tag; /* The tag. */	SLPType type; 	int count;		err = SLPAttrIteratorAlloc(attr, &iter);	count = 0;	while (SLPAttrIterNext(iter, &tag, &type) == SLP_TRUE) {		count++;	}	SLPAttrIteratorFree(iter);		return count;}/* Tests the named attribute to see if it has all of the named values.  * * Params: * name - Name of the attribute to test.  * ... - List of ints that must be associated with name. Terminated with a  * 		TERM_INT. *  * Returns 1 iff the attributes contain all of the named values. 0 otherwise. */int test_int(SLPAttributes attr, char *name, ...) {	int *int_arr; /* Values returned from SLPAttrGet_str(). */	int len; /* Number of values returned. */	int current_value; /* The passed value currently being tested. */	int values_seen; /* The count of passed values. */	int i;	va_list ap;	SLPError err;	err = SLPAttrGet_int(attr, name, &int_arr, &len); 	assert(err == SLP_OK);	/* Foreach value... */	values_seen = 0;	va_start(ap, name);	current_value = va_arg(ap, int);	while (current_value != TERM_INT) {		values_seen++;		/* Check to see if the current value is in the value list. */		for (i = 0; i < len; i++) {			if (int_arr[i] == current_value) {				break;			}		}				/* The current value was not found. */		if (i == len) {			/* TODO cleanup:  */			va_end(ap);			return 0;		}				current_value = va_arg(ap, int);	}	va_end(ap);	/* Cleanup memory. */	free(int_arr);		/* Check that all values in str_attr are accounted for (ie, there are no extra). */	if (values_seen != len) {		return 0;	}		return 1;	}/* Tests the named attribute to see if it has all of the named attributes.  * * Params: * name - Name of the attribute to test.  * ... - List of strings that must be associated with name. Terminated with a  * 		NULL. *  * Returns 1 iff the attributes contain all of the named values. 0 otherwise. */int test_string(SLPAttributes attr, char *name, ...) {	char **str_arr; /* Values returned from SLPAttrGet_str(). */	int len; /* Number of values returned. */	char *current_value; /* The passed value currently being tested. */	int values_seen; /* The count of passed values. */	int i;	va_list ap;	SLPError err;	err = SLPAttrGet_str(attr, name, &str_arr, &len); 	assert(err == SLP_OK);	/* Foreach value... */	values_seen = 0;	va_start(ap, name);	current_value = va_arg(ap, char *);	while (current_value != NULL) {		values_seen++;		/* Check to see if the current value is in the value list. */		for (i = 0; i < len; i++) {			if (strcmp(current_value, str_arr[i]) == 0) {				break;			}		}				/* The current value was not found. */		if (i == len) {			/* TODO cleanup:  */			va_end(ap);			return 0;		}				current_value = va_arg(ap, char *);	}	va_end(ap);	/* Cleanup memory. */	for (i = 0; i < len; i++) {		free(str_arr[i]);	}	free(str_arr);		/* Check that all values in str_attr are accounted for (ie, there are no extra). */	if (values_seen != len) {		return 0;	}		return 1;	}/* Forward declare. */int find_value_list_end(char *value, int *value_count, SLPType *type, int *unescaped_len, char **end);int main(int argc, char *argv[]) {	SLPAttributes attr;	SLPError err;	char *str, *str2;	int len;#ifdef ENABLE_PREDICATES		char data[] = STR;	SLPBoolean bool;	int *int_arr;	char **str_arr;	SLPType type;	char *tag;	SLPAttrIterator iter;		len = strlen(data);	data[4] = '\0';		printf("Predicates enabled. Performing full test for libslpattr.c\n");		/***** Test string parsing. *****/	{		int int_err; /* Somewhere to store integer return values. */		int value_count; 		SLPType type;		int unescaped_len;		char *cur;		char *val;		/*** Valid case: Single string. ***/		val = "value";		int_err = find_value_list_end(val, &value_count, &type, &unescaped_len, &cur);		assert(int_err == 1);		assert(value_count == 1);		assert(type == SLP_STRING);		assert(unescaped_len == strlen(val));		assert(cur == val + (strlen(val)));		/*** Valid case: paired string. ***/		val = "value,12";		int_err = find_value_list_end(val, &value_count, &type, &unescaped_len, &cur);		assert(int_err == 1);		assert(value_count == 2);		assert(type == SLP_STRING);		assert(unescaped_len == strlen(val) - 1);		assert(cur == val + (strlen(val)));		/*** Valid case: tripled string. ***/		val = "2,value,12";		int_err = find_value_list_end(val, &value_count, &type, &unescaped_len, &cur);		assert(int_err == 1);		assert(value_count == 3);		assert(type == SLP_STRING);		assert(unescaped_len == strlen(val) - 2);		assert(cur == val + (strlen(val)));		/*** Valid case: tripled string. ***/		val = "2,v,12";		int_err = find_value_list_end(val, &value_count, &type, &unescaped_len, &cur);		assert(int_err == 1);		assert(value_count == 3);		assert(type == SLP_STRING);		assert(unescaped_len == strlen(val) - 2);		assert(cur == val + (strlen(val)));		/*** Valid case: boolean. ***/		val = "true";		int_err = find_value_list_end(val, &value_count, &type, &unescaped_len, &cur);		assert(int_err == 1);		assert(value_count == 1);		assert(type == SLP_BOOLEAN);		assert(cur == val + (strlen(val)));		/*** Valid case: boolean. ***/		val = "false";		int_err = find_value_list_end(val, &value_count, &type, &unescaped_len, &cur);		assert(int_err == 1);		assert(value_count == 1);		assert(type == SLP_BOOLEAN);		assert(cur == val + (strlen(val)));		/*** Valid case: paired string (devolve from int to str). ***/		val = "false,true";		int_err = find_value_list_end(val, &value_count, &type, &unescaped_len, &cur);		assert(int_err == 1);		assert(value_count == 2);		assert(type == SLP_STRING);		assert(unescaped_len == strlen(val) - 1);		assert(cur == val + (strlen(val)));						/*** Valid case: Single opaque. ***/		val = "\\00\\AB";		int_err = find_value_list_end(val, &value_count, &type, &unescaped_len, &cur);		assert(int_err == 1);		assert(value_count == 1);		assert(type == SLP_OPAQUE);		assert(unescaped_len == 1);		assert(cur == val + (strlen(val)));						/*** Valid case: paired opaque. ***/		val = "\\00\\AB,\\00\\12";		int_err = find_value_list_end(val, &value_count, &type, &unescaped_len, &cur);		assert(int_err == 1);		assert(value_count == 2);		assert(type == SLP_OPAQUE);		assert(unescaped_len == 2);		assert(cur == val + (strlen(val)));						/*** Invalid case: Opaque and non-opaque. ***/		val = "\\00\\AB,\\00\\12,2";		int_err = find_value_list_end(val, &value_count, &type, &unescaped_len, &cur);		assert(int_err == 0);						/*** Invalid case: Opaque and non-opaque. ***/		val = "dog,\\00\\AB";		int_err = find_value_list_end(val, &value_count, &type, &unescaped_len, &cur);		assert(int_err == 0);	}	/***** Test Simple boolean set and get. *****/	err = SLPAttrAlloc("en", NULL, SLP_FALSE, &attr);	assert(err == SLP_OK);		err = SLPAttrSet_bool(attr, "shouldBeFalse", SLP_FALSE); /* Create. */	assert(err == SLP_OK);		err = SLPAttrGetType(attr, "shouldBeFalse", &type);	assert(err == SLP_OK);	assert(type == SLP_BOOLEAN);		err = SLPAttrGet_bool(attr, "shouldBeFalse", &bool); /* Test. */	assert(err == SLP_OK);	assert(bool == SLP_FALSE);	SLPAttrFree(attr);	/***** Test simple string set and get. *****/	err = SLPAttrAlloc("en", NULL, SLP_FALSE, &attr);	assert(err == SLP_OK);	/**** Create. ****/	err = SLPAttrSet_str(attr, "str", STR, SLP_ADD); 	assert(err == SLP_OK);	err = SLPAttrGetType(attr, "str", &type);	assert(err == SLP_OK);	assert(type == SLP_STRING);		/*** Test. ***/	err = SLPAttrGet_str(attr, "str", &str_arr, &len); 	assert(err == SLP_OK);	assert(len == 1);	assert(strcmp(str_arr[0], STR) == 0);	free(str_arr[0]);	free(str_arr);		/**** Append. ****/	err = SLPAttrSet_str(attr, "str", STR1, SLP_ADD); 	assert(err == SLP_OK);	err = SLPAttrGetType(attr, "str", &type);	assert(err == SLP_OK);	assert(type == SLP_STRING);		/*** Test. ***/	err = SLPAttrGet_str(attr, "str", &str_arr, &len); 	assert(err == SLP_OK);	assert(len == 2);		if (strcmp(str_arr[0], STR) == 0) {		assert(strcmp(str_arr[1], STR1) == 0);	} 	else {		assert(strcmp(str_arr[0], STR1) == 0);		assert(strcmp(str_arr[1], STR) == 0);	}		free(str_arr[0]);	free(str_arr[1]);	free(str_arr);	/**** Replace. ****/	err = SLPAttrSet_str(attr, "str", STR2, SLP_REPLACE); 	assert(err == SLP_OK);	err = SLPAttrGetType(attr, "str", &type);	assert(err == SLP_OK);	assert(type == SLP_STRING);		/*** Test. ***/	err = SLPAttrGet_str(attr, "str", &str_arr, &len); 	assert(err == SLP_OK);	assert(len == 1);	assert(strcmp(str_arr[0], STR2) == 0);	free(str_arr[0]);	free(str_arr);	SLPAttrFree(attr);	/***** Test ints. *****/	err = SLPAttrAlloc("en", NULL, SLP_FALSE, &attr);	assert(err == SLP_OK);		/**** Check an int. ****/	err = SLPAttrSet_int(attr, "anInt", (int)234, SLP_ADD);	assert(err == SLP_OK);	err = SLPAttrGet_int(attr, "anInt", &int_arr, &len);	assert(err == SLP_OK);	assert(len == 1);	assert(int_arr[0] == 234);	free(int_arr);		/**** Check a bunch of ints. ****/	err = SLPAttrSet_int(attr, "anInt", (int)345, SLP_ADD);	assert(err == SLP_OK);	err = SLPAttrGet_int(attr, "anInt", &int_arr, &len);	assert(err == SLP_OK);	assert(len == 2);	if (int_arr[0] == 234) {		assert(int_arr[1] == 345);	}	else {		assert(int_arr[0] == 345);		assert(int_arr[1] == 234);	}	free(int_arr);		/**** Check replacement. ****/	err = SLPAttrSet_int(attr, "anInt", (int)567, SLP_REPLACE);	assert(err == SLP_OK);	err = SLPAttrGet_int(attr, "anInt", &int_arr, &len);	assert(err == SLP_OK);	assert(len == 1);	assert(int_arr[0] == 567);	free(int_arr);		SLPAttrFree(attr);	/**** Check deserialization. ****/	err = SLPAttrAllocStr("en", NULL, SLP_FALSE, &attr, "(doc=12,34,56)");	assert(err == SLP_OK);		assert(test_int(attr, "doc", (int)12, (int)34, (int)56, TERM_INT));		SLPAttrFree(attr);		/****** Test keyword set and get. *****/	err = SLPAttrAlloc("en", NULL, SLP_FALSE, &attr);	assert(err == SLP_OK);		err = SLPAttrSet_keyw(attr, "keyw1"); /* Create. */	assert(err == SLP_OK);		err = SLPAttrGet_keyw(attr, "keyw1"); /* Test. */	assert(err == SLP_OK);	err = SLPAttrGet_keyw(attr, "keyw2"); /* Test. */	assert(err == SLP_TAG_ERROR);	SLPAttrFree(attr);

⌨️ 快捷键说明

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