<commit_before>/*
 * Copyright (C) 2013-2016 <NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
 */

#ifndef PLIB_TESTS_STATIC
#  define BOOST_TEST_DYN_LINK
#endif

#define BOOST_TEST_MODULE plist_test

#include "plib.h"

#include <string.h>

#ifdef PLIB_TESTS_STATIC
#  include <boost/test/included/unit_test.hpp>
#else
#  include <boost/test/unit_test.hpp>
#endif

typedef struct _TestData {
	pint test_array[3];
	pint index;
} TestData;

static void foreach_test_func (ppointer data, ppointer user_data)
{
	if (user_data == NULL)
		return;

	TestData *test_data = (TestData *) user_data;

	if (test_data->index < 0 || test_data->index > 2)
		return;

	test_data->test_array[test_data->index] = P_POINTER_TO_INT (data);
	++test_data->index;
}

BOOST_AUTO_TEST_SUITE (BOOST_TEST_MODULE)

BOOST_AUTO_TEST_CASE (plist_invalid_test)
{
	p_lib_init ();

	BOOST_CHECK (p_list_remove (NULL, NULL) == NULL);
	BOOST_CHECK (p_list_last (NULL) == NULL);
	BOOST_CHECK (p_list_length (NULL) == 0);
	BOOST_CHECK (p_list_reverse (NULL) == NULL);

	p_list_free (NULL);
	p_list_foreach (NULL, NULL, NULL);

	p_lib_shutdown ();
}

BOOST_AUTO_TEST_CASE (plist_general_test)
{
	PList		*list = NULL;
	TestData	test_data;

	p_lib_init ();

	/* Testing append */<commit_msg>tests: Add out of memory checks for PList<commit_after>/*
 * Copyright (C) 2013-2016 <NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
 */

#ifndef PLIB_TESTS_STATIC
#  define BOOST_TEST_DYN_LINK
#endif

#define BOOST_TEST_MODULE plist_test

#include "plib.h"

#include <string.h>

#ifdef PLIB_TESTS_STATIC
#  include <boost/test/included/unit_test.hpp>
#else
#  include <boost/test/unit_test.hpp>
#endif

typedef struct _TestData {
	pint test_array[3];
	pint index;
} TestData;

extern "C" ppointer pmem_alloc (psize nbytes)
{
	P_UNUSED (nbytes);
	return (ppointer) NULL;
}

extern "C" ppointer pmem_realloc (ppointer block, psize nbytes)
{
	P_UNUSED (block);
	P_UNUSED (nbytes);
	return (ppointer) NULL;
}

extern "C" void pmem_free (ppointer block)
{
	P_UNUSED (block);
}

static void foreach_test_func (ppointer data, ppointer user_data)
{
	if (user_data == NULL)
		return;

	TestData *test_data = (TestData *) user_data;

	if (test_data->index < 0 || test_data->index > 2)
		return;

	test_data->test_array[test_data->index] = P_POINTER_TO_INT (data);
	++test_data->index;
}

BOOST_AUTO_TEST_SUITE (BOOST_TEST_MODULE)

BOOST_AUTO_TEST_CASE (plist_nomem_test)
{
	p_lib_init ();

	PMemVTable vtable;

	vtable.free	= pmem_free;
	vtable.malloc	= pmem_alloc;
	vtable.realloc	= pmem_realloc;

	BOOST_CHECK (p_mem_set_vtable (&vtable) == TRUE);

	BOOST_CHECK (p_list_append (NULL, PINT_TO_POINTER (10)) == NULL);
	BOOST_CHECK (p_list_prepend (NULL, PINT_TO_POINTER (10)) == NULL);

	vtable.malloc	= (ppointer (*)(psize)) malloc;
	vtable.realloc	= (ppointer (*)(ppointer, psize)) realloc;
	vtable.free	= (void (*)(ppointer)) free;

	BOOST_CHECK (p_mem_set_vtable (&vtable) == TRUE);

	p_lib_shutdown ();
}

BOOST_AUTO_TEST_CASE (plist_invalid_test)
{
	p_lib_init ();

	BOOST_CHECK (p_list_remove (NULL, NULL) == NULL);
	BOOST_CHECK (p_list_last (NULL) == NULL);
	BOOST_CHECK (p_list_length (NULL) == 0);
	BOOST_CHECK (p_list_reverse (NULL) == NULL);

	p_list_free (NULL);
	p_list_foreach (NULL, NULL, NULL);

	p_lib_shutdown ();
}

BOOST_AUTO_TEST_CASE (plist_general_test)
{
	PList		*list = NULL;
	TestData	test_data;

	p_lib_init ();

	/* Testing append */
{'fraction_non_alphanumeric': 0.10644081974069428, 'fraction_numerical': 0.010874111250522794, 'mean_word_length': 4.7835550181378474, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 27, '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': '5590879', 'n_tokens_mistral': 1779, 'n_tokens_neox': 1674, 'n_words': 508}