<commit_before>/*
 * Copyright (C) 2015-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 plibraryloader_test

#include "plib.h"

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

BOOST_AUTO_TEST_SUITE (BOOST_TEST_MODULE)

BOOST_AUTO_TEST_CASE (plibraryloader_general_test)
{
	PLibraryLoader	*loader;
	pchar		*err_msg;
	void		(*shutdown_func) (void);

	p_lib_init ();

	/* We assume that 2nd argument is a PLib library path */
	BOOST_REQUIRE (boost::unit_test::framework::master_test_suite().argc == 2);

	/* Invalid usage */
	BOOST_CHECK (p_library_loader_new (NULL) == NULL);
	BOOST_CHECK (p_library_loader_new ("./unexistent_file.nofile") == NULL);
	BOOST_CHECK (p_library_loader_get_symbol (NULL, NULL) == NULL);
	BOOST_CHECK (p_library_loader_get_symbol (NULL, "unexistent_symbol") == NULL);

	p_library_loader_free (NULL);

	/* General tests */
	err_msg = p_library_loader_get_last_error ();
	p_free (err_msg);

	loader = p_library_loader_new (boost::unit_test::framework::master_test_suite().argv[1]);
	BOOST_REQUIRE (loader != NULL);

	BOOST_CHECK (p_library_loader_get_symbol (loader, "there_is_no_such_a_symbol") == (PFuncAddr) NULL);

	err_msg = p_library_loader_get_last_error ();
	BOOST_CHECK (err_msg != NULL);<commit_msg>tests: Add out of memory checks from PLibraryLoader<commit_after>/*
 * Copyright (C) 2015-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 plibraryloader_test

#include "plib.h"

#include <stdio.h>

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

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);
}

BOOST_AUTO_TEST_SUITE (BOOST_TEST_MODULE)

BOOST_AUTO_TEST_CASE (plibraryloader_nomem_test)
{
	p_lib_init ();

	/* We assume that 2nd argument is a PLib library path */
	BOOST_REQUIRE (boost::unit_test::framework::master_test_suite().argc == 2);

	/* Cleanup from previous run */
	p_file_remove ("." P_DIR_SEPARATOR "p_empty_file.txt", NULL);

	FILE *file = fopen ("." P_DIR_SEPARATOR "p_empty_file.txt", "w");
	BOOST_CHECK (file != NULL);
	BOOST_CHECK (fclose (file) == 0);

	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_library_loader_new ("." P_DIR_SEPARATOR "p_empty_file.txt") == NULL);
	BOOST_CHECK (p_library_loader_new (boost::unit_test::framework::master_test_suite().argv[1]) == 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);

	BOOST_CHECK (p_file_remove ("." P_DIR_SEPARATOR "p_empty_file.txt", NULL) == TRUE);

	p_lib_shutdown ();
}

BOOST_AUTO_TEST_CASE (plibraryloader_general_test)
{
	PLibraryLoader	*loader;
	pchar		*err_msg;
	void		(*shutdown_func) (void);

	p_lib_init ();

	/* We assume that 2nd argument is a PLib library path */
	BOOST_REQUIRE (boost::unit_test::framework::master_test_suite().argc == 2);

	/* Invalid usage */
	BOOST_CHECK (p_library_loader_new (NULL) == NULL);
	BOOST_CHECK (p_library_loader_new ("./unexistent_file.nofile") == NULL);
	BOOST_CHECK (p_library_loader_get_symbol (NULL, NULL) == NULL);
	BOOST_CHECK (p_library_loader_get_symbol (NULL, "unexistent_symbol") == NULL);

	p_library_loader_free (NULL);

	/* General tests */
	err_msg = p_library_loader_get_last_error ();
	p_free (err_msg);

	loader = p_library_loader_new (boost::unit_test::framework::master_test_suite().argv[1]);
	BOOST_REQUIRE (loader != NULL);

	BOOST_CHECK (p_library_loader_get_symbol (loader, "there_is_no_such_a_symbol") == (PFuncAddr) NULL);

	err_msg = p_library_loader_get_last_error ();
	BOOST_CHECK (err_msg != NULL);
{'fraction_non_alphanumeric': 0.1112488928255093, 'fraction_numerical': 0.008857395925597875, 'mean_word_length': 5.365276211950395, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, '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': '2736645', 'n_tokens_mistral': 2024, 'n_tokens_neox': 1922, 'n_words': 538}