Beispiel #1
0
int
test_text_split(test_case *test)
{
	int i, rc;
	char **token;
	Vector tokens;

	rc = 0;

	printf("s=\"%s\" d=\"%s\" f=0x%02X ", test->string, test->delims, test->flags);
	tokens = TextSplit(test->string, test->delims, test->flags);
	if (tokens == NULL) {
		return -1;
	}

	printf("e=%d l=%ld ", test->expect_length, VectorLength(tokens));
	if (test->expect_length != VectorLength(tokens)) {
		rc = -1;
	}

	for (i = 0, token = (char **)VectorBase(tokens); *token != NULL; token++, i++) {
		if (test->expect_items[i] == NULL || strcmp(*token, test->expect_items[i]) != 0)
			rc = -1;
		printf("[%s]", *token);
	}
	if (test->expect_items[i] != NULL)
		rc = -1;

	printf("... %s\n", rc == 0 ? "OK" : "FAIL");
	VectorDestroy(tokens);

	return rc;
}
Beispiel #2
0
void
TestTextSplit(Text string, Text delims, int empty, long expect)
{
	int i;
	Text s;
	Vector v;

	printf("string=[%s] delims=[%s] return-empty-tokens=%d\n",TextString(string),TextString(delims), empty);
	v = TextSplitOn(string, delims, empty);

	if (v == NULL) {
		printf("vector is null\n");
		return;
	}

	printf("  length=%ld expected=%ld", VectorLength(v), expect);
	for (i = 0; i < VectorLength(v); i++) {
		s = VectorGet(v, i);
		printf("[%s]", s == NULL ? "" : s->string(s));
	}

	printf(" ...%s\n", VectorLength(v) == expect ? "OK" : "FAIL");
	VectorDestroy(v);

	string->destroy(string);
	delims->destroy(delims);
}
Beispiel #3
0
static void
at_exit_cleanup(void)
{
	listFini(&pattern_rules);
	VectorDestroy(report);
	LogClose();
	fini_db();
}
Beispiel #4
0
void GenericSortEmptyVector(ADTErr(*SortFunc)(Vector* _vec))
{
	Vector* vec = NULL; 
	
	vec = VectorCreate(100, 100);
	
	SortFunc(vec);
	printf("SortEmptyVector..................OK\n");
	
	VectorDestroy(vec);
}
Beispiel #5
0
void MassiveRadixSortTest()
{
	int vectorSize, i, j;
	int num1, num2;
	Vector* vec;
	srand(time(NULL));
	
	for(j = 0; j < RUNS; j++)
	{
		
		vectorSize = rand() % MAX_VECTOR_SIZE;

		vec = VectorCreate(vectorSize, 100);

		for(i = 0; i < vectorSize; i++)
		{
			VectorAdd(vec, rand() % MAX_NUMBER);
		}

		RadixSort(vec, 3);

		
		VectorDelete(vec, &num1);

		for(i = 0; i < vectorSize - 1; i++)
		{
			VectorDelete(vec, &num2);
			if(num2 > num1)
			{
				printf("MassiveSortTest..................FAIL\n");
				VectorDestroy(vec);
				return;
			}
			num1 = num2;
		}

		VectorDestroy(vec);
	}

	printf("MassiveSortTest..................OK\n");
}
Beispiel #6
0
void MassiveGenericSortTestWithNegatives(ADTErr(*SortFunc)(Vector* _vec))
{
	int vectorSize, i, j;
	int num1, num2;
	Vector* vec;
	srand(time(NULL));
	
	for(j = 0; j < RUNS; j++)
	{
		
		vectorSize = rand() % MAX_VECTOR_SIZE;

		vec = VectorCreate(vectorSize, 100);
		

		for(i = 0; i < vectorSize; i++)
		{
			i % 2 == 0 ? VectorAdd(vec, rand() % MAX_NUMBER) : VectorAdd(vec, - rand() % MAX_NUMBER);
		}

		SortFunc(vec);

		VectorDelete(vec, &num1);

		for(i = 0; i < vectorSize - 1; i++)
		{
			VectorDelete(vec, &num2);
			if(num2 > num1)
			{
				printf("MassiveSortTestWithNegatives..................FAIL\n");
				VectorDestroy(vec);
				return;
			}
			num1 = num2;
		}

		VectorDestroy(vec);
	}

	printf("MassiveSortTestWithNegatives..................OK\n");
}
Beispiel #7
0
void RadixSortEmptyVector()
{
	Vector* vec = NULL; 
	
	vec = VectorCreate(100, 100);
	
	RadixSort(vec, 1);
	
	printf("SortEmptyVector..................OK\n");
	
	VectorDestroy(vec);
}
Beispiel #8
0
void CountingSortEmptyVector()
{
	Vector* vec = NULL; 
	
	vec = VectorCreate(100, 100);
	
	CountingSort(vec, 100);
	
	printf("SortEmptyVector..................OK\n");
	
	VectorDestroy(vec);
}
Beispiel #9
0
int
main(int argc, char **argv)
{
	Vector v;
	char *delims;
	int i, ch, argi, flags;

	flags = 0;
	delims = NULL;
	
	while ((ch = getopt(argc, argv, "d:f:t")) != -1) {
		switch (ch) {
		case 'd':
			delims = optarg;
			break;
			
		case 'f':
			flags = (int) strtol(optarg, NULL, 0);
			break;
			
		case 't':
			return test_suite();
		
		default:
			optind = argc;
		}
	}
	
	if (argc <= optind) {
		(void) fputs(usage, stderr);
		return EXIT_FAILURE;
	}
	
	for (argi = optind; argi < argc; argi++) {		
		if ((v = TextSplit(argv[argi], delims, flags)) == NULL) {
			(void) fprintf(stderr, "out of memory\n");
			return EXIT_FAILURE;
		}
		
		for (i = 0; i < VectorLength(v); i++)
			(void) printf("%s\n", (char *)VectorGet(v, i));			

		VectorDestroy(v);
	}

	return EXIT_SUCCESS;
}
Beispiel #10
0
void SimpleGenericSort(ADTErr(*SortFunc)(Vector* _vec))
{
	Vector* vec; 
	int a;
	
	vec = VectorCreate(100, 100);
	
	VectorAdd(vec, 46);
	VectorAdd(vec, 2);
	VectorAdd(vec, 83);
	VectorAdd(vec, 41);
	VectorAdd(vec, 102);
	VectorAdd(vec, 5);
	VectorAdd(vec, 17);
	VectorAdd(vec, 31);
	VectorAdd(vec, 64);
	VectorAdd(vec, 49);
	VectorAdd(vec, 18);

	/*VectorPrint(vec);*/

	SortFunc(vec);
	
	/*VectorPrint(vec);*/
	
	VectorDelete(vec, &a);
	
	if(a == 102)
	{
		printf("SimpleSort..................OK\n");
	}	
	else
	{
		printf("SimpleSort..................FAIL\n");
	}
	
	VectorDestroy(vec);
}
Beispiel #11
0
void SimpleRadixSort()
{
	Vector* vec; 
	int a;
	
	vec = VectorCreate(100, 100);
	
	VectorAdd(vec, 46);
	VectorAdd(vec, 2);
	VectorAdd(vec, 83);
	VectorAdd(vec, 41);
	VectorAdd(vec, 102);
	VectorAdd(vec, 5);
	VectorAdd(vec, 17);
	VectorAdd(vec, 31);
	VectorAdd(vec, 64);
	VectorAdd(vec, 49);
	VectorAdd(vec, 18);

	RadixSort(vec, 3);
	


	VectorDelete(vec, &a);
	VectorDelete(vec, &a);
	VectorDelete(vec, &a);
	
	if(a == 64)
	{
		printf("SimpleSort..................OK\n");
	}	
	else
	{
		printf("SimpleSort..................FAIL\n");
	}
	
	VectorDestroy(vec);
}
Beispiel #12
0
Vector
TextSplitOnC(Text self, const char *delims, int returnEmptyTokens)
{
	Text token;
	Vector list;

	if ((list = VectorCreate(5)) == NULL)
		return NULL;

	TextResetTokens(self);

	while ((token = TextNextTokenC(self, delims, returnEmptyTokens)) != NULL) {
		if (VectorAdd(list, token)) {
			VectorDestroy(list);
			/*@-kepttrans@*/
			TextDestroy(token);
			/*@=kepttrans@*/
			return NULL;
		}
	}

	return list;
}
Beispiel #13
0
void SimpleRadixSortForSingleMember()
{
	Vector* vec; 
	int a;
	
	vec = VectorCreate(100, 100);
	
	VectorAdd(vec, 5);

	RadixSort(vec, 1);
	
	VectorDelete(vec, &a);
	
	if(a == 5)
	{
		printf("SimpleSortForSingleMember..................OK\n");
	}	
	else
	{
		printf("SimpleSortForSingleMember..................FAIL\n");
	}
	
	VectorDestroy(vec);
}
Beispiel #14
0
void SimpleGenericSortForSingleMember(ADTErr(*SortFunc)(Vector* _vec))
{
	Vector* vec; 
	int a;
	
	vec = VectorCreate(100, 100);
	
	VectorAdd(vec, 5);

	SortFunc(vec);
	
	VectorDelete(vec, &a);
	
	if(a == 5)
	{
		printf("SimpleSortForSingleMember..................OK\n");
	}	
	else
	{
		printf("SimpleSortForSingleMember..................FAIL\n");
	}
	
	VectorDestroy(vec);
}
Beispiel #15
0
static void
limit_report(struct pattern_rule *rule, const char *action, struct limit *limit, const char *line, regmatch_t *parens)
{
        SMTP2 *smtp;
        Vector rcpts;
        int smtp_flags;
	const unsigned char *text;
        char **table, buffer[SMTP_PATH_LENGTH];

	if (0 < debug)
		(void) fprintf(
			stdout, "/%s/ %s: limit exceeded (%d)\n",
			rule->pattern, action, limit->reported + limit->counter
		);

	smtp = NULL;
	rcpts = NULL;
        if (report_to != NULL) {
		rcpts = TextSplit(rule->report != NULL ? strchr(rule->report, '=')+1 : report_to, ",; ", 0);
		 if (VectorLength(rcpts) <= 0) {
			(void) fprintf(stderr, "no report-to mail addresses\n");
			goto error0;
		}

		/* Try to connect to local smart host. */
		smtp_flags = SMTP_FLAG_LOG | (1 < debug ? SMTP_FLAG_DEBUG : 0);
		smtp = smtp2Open(smtp_host, SMTP_CONNECT_TO * UNIT_MILLI, SMTP_COMMAND_TO * UNIT_MILLI, smtp_flags);
		if (smtp == NULL) {
			(void) fprintf(stderr, "%s: %s (%d)\n", smtp_host, strerror(errno), errno);
			goto error1;
		}
		if (smtp2Mail(smtp, report_from) != SMTP_OK) {
			(void) fprintf(stderr, "%s: null sender not accepted\n", smtp_host);
			goto error2;
		}

		for (table = (char **) VectorBase(rcpts); *table != NULL; table++) {
			if (smtp2Rcpt(smtp, *table) != SMTP_OK)
				goto error2;
		}

		TimeStamp(&smtp->start, buffer, sizeof (buffer));
		(void) smtp2Printf(smtp, "Date: %s" CRLF, buffer);
		(void) smtp2Printf(smtp, "From: \"%s\" <%s>" CRLF, _NAME, smtp->sender);
		(void) smtp2Printf(smtp, "Message-ID: <%s@%s>" CRLF, smtp->id_string, smtp->local_ip);
		(void) smtp2Printf(smtp, "Subject: %s log limit exceeded %s" CRLF, _NAME, limit->token);
		(void) smtp2Print(smtp, CRLF, sizeof (CRLF)-1);
		(void) smtp2Printf(
			smtp, "/%s/ %s: limit exceeded (%d)" CRLF CRLF,
			rule->pattern, action, limit->reported + limit->counter
		);
	}

	if (sqlite3_bind_int64(ctx.select_limit_to_log, 1, limit->id_limit) != SQLITE_OK) {
		sql_error(__FILE__, __LINE__, ctx.increment_limit);
                goto error2;
	}
	if (sqlite3_bind_int64(ctx.select_limit_to_log, 2, limit->updated) != SQLITE_OK) {
		sql_error(__FILE__, __LINE__, ctx.increment_limit);
		goto error2;
	}

	while (sql_step(ctx.db, ctx.select_limit_to_log) == SQLITE_ROW) {
		if ((text = sqlite3_column_text(ctx.select_limit_to_log, 0)) == NULL)
			continue;
		if (0 < debug)
			(void) fprintf(stdout, "\t%s" CRLF, text);
		if (smtp != NULL)
			(void) smtp2Printf(smtp, "%s" CRLF, text);
	}

	if (rule->on_exceed != NULL)
		do_command(rule, rule->on_exceed, line, parens);

	(void) sqlite3_clear_bindings(ctx.select_limit_to_log);
	sqlite3_reset(ctx.select_limit_to_log);
	if (smtp != NULL)
		(void) smtp2Dot(smtp);
error2:
	smtp2Close(smtp);
error1:
	VectorDestroy(rcpts);
error0:
	;
}
Beispiel #16
0
/*
 * Pattern Rule Grammar
 * --------------------
 *
 * line := blank | "#" comment | rule
 *
 * rule := pattern *[ whitespace ] actions
 *
 * whitespace := SPACE | TAB
 *
 * pattern := "/" extended_regex "/"
 *
 * actions := action [ ";" actions ]
 *
 * action := thread | limit | report | on_exceed | on_expire
 *
 * quote := "'" | '"'
 *
 * index := number | quote text quote
 *
 * indices := index [ "," index ]
 *
 * thread := "t=" indices
 *
 * limit := "l=" indices "," max "/" period [ unit ]
 *
 * report := "r=" mail *[ "," mail ]
 *
 * on_exceed := "c=" quoted_shell_command
 *
 * on_expire := "C=" quoted_shell_command
 *
 * quoted_shell_command := quote text quote
 */
static int
init_rule(char *line)
{
	Vector fields;
	const char *next;
	struct pattern_rule *rule;
	int i, rc = -1, err;
	char *field, error[128];

	if (*line != '/')
		goto error0;
	if ((rule = calloc(1, sizeof (*rule))) == NULL)
		goto error0;
	if ((rule->pattern = TokenNext(line, &next, "/", TOKEN_KEEP_ASIS)) == NULL)
		goto error1;
	if ((fields = TextSplit(next, ";", TOKEN_KEEP_ASIS)) == NULL)
		goto error1;

	if ((err = regcomp(&rule->re, rule->pattern, REG_EXTENDED|REG_NEWLINE)) != 0) {
		(void) regerror(err, &rule->re, error, sizeof (error));
		(void) fprintf(stderr, "pattern /%s/: %s (%d)\n", rule->pattern, error, err);
		goto error2;
	}

	/* Assume new previously unknown pattern. */
	if ((err = sqlite3_bind_text(ctx.insert_pattern, 1, rule->pattern, -1, SQLITE_STATIC)) != SQLITE_OK) {
		sql_error(__FILE__, __LINE__, ctx.insert_pattern);
		goto error2;
	}
	if ((err = sql_step(ctx.db, ctx.insert_pattern)) != SQLITE_DONE) {
		sql_error(__FILE__, __LINE__, ctx.insert_pattern);
		goto error2;
	}

	rule->id_pattern = sqlite3_last_insert_rowid(ctx.db);
	(void) sqlite3_clear_bindings(ctx.insert_pattern);
	sqlite3_reset(ctx.insert_pattern);

	/* Last insert rowid is zero if no row was inserted, thus it
	 * already exists and we need to find the pattern number (OID).
	 */
	if (rule->id_pattern == 0) {
		if ((err = sqlite3_bind_text(ctx.find_pattern, 1, rule->pattern, -1, SQLITE_STATIC)) != SQLITE_OK) {
			sql_error(__FILE__, __LINE__, ctx.find_pattern);
			goto error2;
		}
		if ((err = sql_step(ctx.db, ctx.find_pattern)) != SQLITE_ROW) {
			sql_error(__FILE__, __LINE__, ctx.find_pattern);
			goto error2;
		}
		if ((rule->id_pattern = sqlite3_column_int64(ctx.find_pattern, 0)) == 0) {
			sql_error(__FILE__, __LINE__, ctx.find_pattern);
			goto error2;
		}
		(void) sqlite3_clear_bindings(ctx.find_pattern);
                sqlite3_reset(ctx.find_pattern);
	}

	for (i = 0; i < VectorLength(fields); i++) {
		field = VectorGet(fields, i);
		field += strspn(field, " \t");

		switch (*field) {
		case 'c':
			if (field[1] == '=') {
				free(rule->on_exceed);
				rule->on_exceed = VectorReplace(fields, i, NULL);
			}
			break;
		case 'C':
			if (field[1] == '=') {
				free(rule->on_expire);
				rule->on_expire = VectorReplace(fields, i, NULL);
			}
			break;
		case 'r':
			if (field[1] == '=') {
				free(rule->report);
				rule->report = VectorReplace(fields, i, NULL);
			}
			break;
		case 't':
			if (field[1] == '=')
				rule->thread = strtol(field+2, NULL, 10);
			break;
		case 'l':
			if (field[1] == '=')
				continue;
		}

		(void) VectorRemove(fields, i--);
	}

	field[strcspn(field, "\r\n")] = '\0';

	/* What remains of fields should be an empty vector or an array of l= actions. */
	rule->limits = (char **) VectorBase(fields);
	fields = NULL;

	rule->node.data = rule;
	rule->node.free = free_rule;
	listInsertAfter(&pattern_rules, NULL, &rule->node);

	rc = 0;
error2:
	VectorDestroy(fields);
error1:
	if (rc != 0)
		free_rule(rule);
error0:
	return rc;
}
Beispiel #17
0
void TimeCompare()
{
	Vector* vec;
	int i, functionsRun, sizes;
	clock_t start, end; 
	float seconds;
	ADTErr(*functions[9])(Vector* _vec) = {BubbleSort, ShakeSort, QuickSort, QuickSort_Iter, InsertionSort, ShellSort, MergeSort, MergeSort_Iter, SelectionSort};
	int vectorSizes[4] = {100, 100, 100, 100};/*TODO check for 10000*/
	
	printf("\nRunning time compare.\n");

	for(sizes = 0; sizes < 4; sizes++)
	{
		printf("\nFor vector of %d integers:\n\n", vectorSizes[sizes]);	
	
		for(functionsRun = 0; functionsRun < 11; functionsRun++)
		{
			vec = VectorCreate(100, 100);

			for(i = 0; i < vectorSizes[sizes]; i++)
			{
				VectorAdd(vec, rand() % 1000);
			}

			if(functionsRun == 9)
			{
				start = clock();
				CountingSort(vec, MAX_NUMBER);
				end = clock();
				seconds = (float)(end - start) / CLOCKS_PER_SEC;
			}
			else if(functionsRun == 10)
			{
				start = clock();
				CountingSort(vec, 3);
				end = clock();
				seconds = (float)(end - start) / CLOCKS_PER_SEC;
			}
			else 
			{
				start = clock();
				functions[functionsRun](vec);
				end = clock();
				seconds = (float)(end - start) / CLOCKS_PER_SEC;
			}

			switch(functionsRun)
			{
				case 0:
					printf("BubbleSort: %f\n", seconds);
					break;
				case 1:
					printf("ShakeSort: %f\n", seconds);
					break;
				case 2:
					printf("QuickSort: %f\n", seconds);
					break;
				case 3:
					printf("QuickSort_Iter: %f\n", seconds);
					break;
				case 4:
					printf("InsertionSort: %f\n", seconds);
					break;
				case 5:
					printf("ShellSort: %f\n", seconds);
					break;
				case 6:
					printf("MergeSort: %f\n", seconds);
					break;
				case 7:
					printf("MergeSort - Iterative: %f\n", seconds);
					break;
				case 8:
					printf("SelectionSort: %f\n", seconds);
					break;
				case 9:
					printf("Counting: %f\n", seconds);
					break;
				case 10:
					printf("Radix: %f\n", seconds);
					break;
			}

			VectorDestroy(vec);
		}
	}
}