<commit_before>/*
 * Copyright 2012 <NAME>.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "p_testsuite.h"

static int testlen(const char *restrict file, const char *name, URI *restrict uri, struct urimatch *restrict test, size_t (*fn)(URI *restrict, char *restrict, size_t), size_t expected);

static int teststr(const char *restrict file, const char *name, URI *restrict uri, char *restrict buf, size_t buflen, struct urimatch *restrict test, size_t (*fn)(URI *restrict, char *restrict, size_t), const char *restrict expected);

int
test_urimatch(const char *file, struct urimatch *tests)
{
	URI *uri, *base;
	int c;
	int failed, didfail;
	char *buffer, *p;
	size_t bufsize, bl;
	int r;

	buffer = NULL;
	bufsize = 0;
	failed = 0;
	for(c = 0; tests[c].uri; c++)
	{
		base = NULL;
		bl = 0;
		if(tests[c].base)
		{
			base = uri_create_str(tests[c].base, NULL);
			bl = strlen(tests[c].base);
			if(!base)
			{
				fprintf(stderr, "%s: failed to construct base URI '%s': %s\n", file, tests[c].base, strerror(errno));
				failed++;
				continue;
			}			
		}
		bl += strlen(tests[c].uri);
		uri = uri_create_str(tests[c].uri, base);
		if(!uri)
		{
			fprintf(stderr, "%s: failed to construct URI '%s': %s\n", file, tests[c].uri, strerror(errno));
			uri_destroy(base);
			failed++;
			continue;
		}
		bl = (bl * 3) + 1;
		if(bl > bufsize)
		{
			p = (char *) realloc(buffer, bl);
			if(!p)
			{
				fprintf(stderr, "%s: failed to resize buffer from %u to %u bytes: %s\n", file, (unsigned) bufsize, (unsigned) bl, strerror(errno));
				exit(2);
			}
			buffer = p;
			bufsize = bl;
		}
		didfail = 0;
#define TESTLEN(name, mask) \
		if(tests[c].testmask & mask) \
		{ \
			didfail += testlen(file, #name, uri, &(tests[c]), uri_##name, tests[c].name##len); \
		}
		TESTLEN(scheme, UM_SCHEME_LEN);
		TESTLEN(auth, UM_AUTH_LEN);
		TESTLEN(host, UM_HOST_LEN);
		TESTLEN(port, UM_PORT_LEN);
		TESTLEN(path, UM_PATH_LEN);
		TESTLEN(query, UM_QUERY_LEN);
		TESTLEN(fragment, UM_FRAGMENT_LEN);
#undef TESTLEN
		if(tests[c].testmask & UM_RECOMPOSED_LEN)
		{
			didfail += testlen(file, "recomposed string", uri, &(tests[c]), uri_str, tests[c].recomposedlen);
		}
		if(tests[c].testmask && UM_ABSOLUTE)
		{
			if((r = uri_absolute(uri)) != tests[c].absolute)
			{
				didfail++;
				if(tests[c].absolute)
				{
					fprintf(stderr, "%s: URI expected to be absolute, but return value was %d for %s\n", file, r, tests[c].uri);
				}
				else
				{
					fprintf(stderr, "%s: URI expected to not be absolute, but return value was %d for %s\n", file, r, tests[c].uri);
				}
			}
		}
		if(tests[c].testmask & UM_PORT)
		{
			if((r = uri_portnum(uri)) != tests[c].portnum)
			{
				didfail++;
				fprintf(stderr, "%s: parsed port number (%d) does not match expected port number (%d) for URI %s\n", file, r, tests[c].portnum, tests[c].uri);
			}
		}
#define TESTSTR(name, mask) \
		if(tests[c].testmask & mask) \
		{ \
			didfail += teststr(file, #name, uri, buffer, bufsize, &(tests[c]), uri_##name, tests[c].name); \
		}
		TESTSTR(scheme, UM_SCHEME);
		TESTSTR(auth, UM_AUTH);
		TESTSTR(host, UM_HOST);<commit_msg>Fix incorrect use of logical and<commit_after>/*
 * Copyright 2012 <NAME>.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "p_testsuite.h"

static int testlen(const char *restrict file, const char *name, URI *restrict uri, struct urimatch *restrict test, size_t (*fn)(URI *restrict, char *restrict, size_t), size_t expected);

static int teststr(const char *restrict file, const char *name, URI *restrict uri, char *restrict buf, size_t buflen, struct urimatch *restrict test, size_t (*fn)(URI *restrict, char *restrict, size_t), const char *restrict expected);

int
test_urimatch(const char *file, struct urimatch *tests)
{
	URI *uri, *base;
	int c;
	int failed, didfail;
	char *buffer, *p;
	size_t bufsize, bl;
	int r;

	buffer = NULL;
	bufsize = 0;
	failed = 0;
	for(c = 0; tests[c].uri; c++)
	{
		base = NULL;
		bl = 0;
		if(tests[c].base)
		{
			base = uri_create_str(tests[c].base, NULL);
			bl = strlen(tests[c].base);
			if(!base)
			{
				fprintf(stderr, "%s: failed to construct base URI '%s': %s\n", file, tests[c].base, strerror(errno));
				failed++;
				continue;
			}			
		}
		bl += strlen(tests[c].uri);
		uri = uri_create_str(tests[c].uri, base);
		if(!uri)
		{
			fprintf(stderr, "%s: failed to construct URI '%s': %s\n", file, tests[c].uri, strerror(errno));
			uri_destroy(base);
			failed++;
			continue;
		}
		bl = (bl * 3) + 1;
		if(bl > bufsize)
		{
			p = (char *) realloc(buffer, bl);
			if(!p)
			{
				fprintf(stderr, "%s: failed to resize buffer from %u to %u bytes: %s\n", file, (unsigned) bufsize, (unsigned) bl, strerror(errno));
				exit(2);
			}
			buffer = p;
			bufsize = bl;
		}
		didfail = 0;
#define TESTLEN(name, mask) \
		if(tests[c].testmask & mask) \
		{ \
			didfail += testlen(file, #name, uri, &(tests[c]), uri_##name, tests[c].name##len); \
		}
		TESTLEN(scheme, UM_SCHEME_LEN);
		TESTLEN(auth, UM_AUTH_LEN);
		TESTLEN(host, UM_HOST_LEN);
		TESTLEN(port, UM_PORT_LEN);
		TESTLEN(path, UM_PATH_LEN);
		TESTLEN(query, UM_QUERY_LEN);
		TESTLEN(fragment, UM_FRAGMENT_LEN);
#undef TESTLEN
		if(tests[c].testmask & UM_RECOMPOSED_LEN)
		{
			didfail += testlen(file, "recomposed string", uri, &(tests[c]), uri_str, tests[c].recomposedlen);
		}
		if(tests[c].testmask & UM_ABSOLUTE)
		{
			if((r = uri_absolute(uri)) != tests[c].absolute)
			{
				didfail++;
				if(tests[c].absolute)
				{
					fprintf(stderr, "%s: URI expected to be absolute, but return value was %d for %s\n", file, r, tests[c].uri);
				}
				else
				{
					fprintf(stderr, "%s: URI expected to not be absolute, but return value was %d for %s\n", file, r, tests[c].uri);
				}
			}
		}
		if(tests[c].testmask & UM_PORT)
		{
			if((r = uri_portnum(uri)) != tests[c].portnum)
			{
				didfail++;
				fprintf(stderr, "%s: parsed port number (%d) does not match expected port number (%d) for URI %s\n", file, r, tests[c].portnum, tests[c].uri);
			}
		}
#define TESTSTR(name, mask) \
		if(tests[c].testmask & mask) \
		{ \
			didfail += teststr(file, #name, uri, buffer, bufsize, &(tests[c]), uri_##name, tests[c].name); \
		}
		TESTSTR(scheme, UM_SCHEME);
		TESTSTR(auth, UM_AUTH);
		TESTSTR(host, UM_HOST);
{'fraction_non_alphanumeric': 0.1548780487804878, 'fraction_numerical': 0.004336043360433604, 'mean_word_length': 3.671518987341772, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5465080', 'n_tokens_mistral': 3034, 'n_tokens_neox': 2721, 'n_words': 774}