<commit_before>
#include "camel-test.h"

#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>

#ifdef ENABLE_THREADS
#include <pthread.h>
#endif

#include <camel/camel.h>

#ifdef ENABLE_THREADS
/* well i dunno, doesn't seem to be in the headers but hte manpage mentions it */
/* a nonportable checking mutex for glibc, not really needed, just validates
   the test harness really */
# ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
static pthread_mutex_t lock = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
# else
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
# endif
#define CAMEL_TEST_LOCK pthread_mutex_lock(&lock)
#define CAMEL_TEST_UNLOCK pthread_mutex_unlock(&lock)
#define CAMEL_TEST_ID (pthread_self())
#else
#define CAMEL_TEST_LOCK
#define CAMEL_TEST_UNLOCK
#define CAMEL_TEST_ID (0)
#endif

static int setup;
static int ok;

struct _stack {
	struct _stack *next;
	int fatal;
	char *what;
};

/* per-thread state */
struct _state {
	char *test;
	int nonfatal;
	struct _stack *state;
};

static GHashTable *info_table;

int camel_test_verbose;

static void
dump_action(int id, struct _state *s, void *d)
{
	struct _stack *node;

#ifdef ENABLE_THREADS
	printf("\nThread %d:\n", id);
#endif
	node = s->state;
	if (node) {
		printf("Current action:\n");
		while (node) {
			printf("\t%s%s\n", node->fatal?"":"[nonfatal]", node->what);
			node = node->next;
		}
	}
	printf("\tTest: %s\n", s->test);
}

static void die(int sig)
{
	static int indie = 0;

	if (!indie) {
		indie = 1;
		printf("\n\nReceived fatal signal %d\n", sig);
		g_hash_table_foreach(info_table, (GHFunc)dump_action, 0);

#ifdef ENABLE_THREADS
		if (camel_test_verbose > 2) {
			printf("Attach debugger to pid %d to debug\n", getpid());
			sleep(1000);
		}
#endif
	}

	_exit(1);
}

static struct _state *
current_state(void)
{
	struct _state *info;

	if (info_table == NULL)
		info_table = g_hash_table_new(0, 0);

	info = g_hash_table_lookup(info_table, (void *)CAMEL_TEST_ID);
	if (info == NULL) {
		info = g_malloc0(sizeof(*info));
		g_hash_table_insert(info_table, (void *)CAMEL_TEST_ID, info);
	}
	return info;
}
	

void camel_test_init(int argc, char **argv)<commit_msg>removed #ifdef ENABLE_THREADS<commit_after>
#include "camel-test.h"

#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <pthread.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>

#include <camel/camel.h>

/* well i dunno, doesn't seem to be in the headers but hte manpage mentions it */
/* a nonportable checking mutex for glibc, not really needed, just validates
   the test harness really */
# ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
static pthread_mutex_t lock = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
# else
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
# endif
#define CAMEL_TEST_LOCK pthread_mutex_lock(&lock)
#define CAMEL_TEST_UNLOCK pthread_mutex_unlock(&lock)
#define CAMEL_TEST_ID (pthread_self())

static int setup;
static int ok;

struct _stack {
	struct _stack *next;
	int fatal;
	char *what;
};

/* per-thread state */
struct _state {
	char *test;
	int nonfatal;
	struct _stack *state;
};

static GHashTable *info_table;

int camel_test_verbose;

static void
dump_action(int id, struct _state *s, void *d)
{
	struct _stack *node;
	
	printf("\nThread %d:\n", id);
	
	node = s->state;
	if (node) {
		printf("Current action:\n");
		while (node) {
			printf("\t%s%s\n", node->fatal?"":"[nonfatal]", node->what);
			node = node->next;
		}
	}
	printf("\tTest: %s\n", s->test);
}

static void die(int sig)
{
	static int indie = 0;

	if (!indie) {
		indie = 1;
		printf("\n\nReceived fatal signal %d\n", sig);
		g_hash_table_foreach(info_table, (GHFunc)dump_action, 0);
		
		if (camel_test_verbose > 2) {
			printf("Attach debugger to pid %d to debug\n", getpid());
			sleep(1000);
		}
	}

	_exit(1);
}

static struct _state *
current_state(void)
{
	struct _state *info;

	if (info_table == NULL)
		info_table = g_hash_table_new(0, 0);

	info = g_hash_table_lookup(info_table, (void *)CAMEL_TEST_ID);
	if (info == NULL) {
		info = g_malloc0(sizeof(*info));
		g_hash_table_insert(info_table, (void *)CAMEL_TEST_ID, info);
	}
	return info;
}
	

void camel_test_init(int argc, char **argv)
{'fraction_non_alphanumeric': 0.13566790352504637, 'fraction_numerical': 0.005797773654916512, 'mean_word_length': 4.804845222072679, 'pattern_counts': {'":': 2, '<': 21, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4772049', 'n_tokens_mistral': 1781, 'n_tokens_neox': 1675, 'n_words': 421}