<commit_before>/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2012  Intel Corporation. All rights reserved.
 *
 *
 *  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 St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib.h>

#include "src/shared/util.h"
#include "src/shared/queue.h"

static void test_basic(void)
{
	struct queue *queue;
	unsigned int n, i;

	queue = queue_new();
	g_assert(queue != NULL);

	for (n = 0; n < 1024; n++) {
		for (i = 1; i < n + 2; i++)
			queue_push_tail(queue, UINT_TO_PTR(i));

		g_assert(queue_length(queue) == n + 1);

		for (i = 1; i < n + 2; i++) {
			void *ptr;

			ptr = queue_pop_head(queue);
			g_assert(ptr != NULL);
			g_assert(i == PTR_TO_UINT(ptr));
		}

		g_assert(queue_isempty(queue) == true);
	}

	queue_destroy(queue, NULL);
}

static void foreach_destroy(void *data, void *user_data)
{
	struct queue *queue = user_data;

	queue_destroy(queue, NULL);
}

static void test_foreach_destroy(void)
{
	struct queue *queue;

	queue = queue_new();
	g_assert(queue != NULL);

	queue_push_tail(queue, UINT_TO_PTR(1));
	queue_push_tail(queue, UINT_TO_PTR(2));

	queue_foreach(queue, foreach_destroy, queue);
}

static void foreach_remove_all(void *data, void *user_data)
{
	struct queue *queue = user_data;

	queue_remove_all(queue, NULL, NULL, NULL);
}

static void test_foreach_remove_all(void)
{
	struct queue *queue;

	queue = queue_new();
	g_assert(queue != NULL);

	queue_push_tail(queue, UINT_TO_PTR(1));
	queue_push_tail(queue, UINT_TO_PTR(2));

	queue_foreach(queue, foreach_remove_all, queue);
	queue_destroy(queue, NULL);
}

static struct queue *static_queue;

static void destroy_remove(void *user_data)
{
	queue_remove(static_queue, user_data);
}

static void test_destroy_remove(void)
{
	static_queue = queue_new();

	g_assert(static_queue != NULL);

	queue_push_tail(static_queue, UINT_TO_PTR(1));
	queue_push_tail(static_queue, UINT_TO_PTR(2));

	queue_destroy(static_queue, destroy_remove);
}

static void test_push_after(void)
{
	struct queue *queue;
	unsigned int len, i;

	queue = queue_new();
	g_assert(queue != NULL);

	/*
	 * Pre-populate queue. Initial elements are:
	 *   [ NULL, 2, 5 ]
	 */
	g_assert(queue_push_tail(queue, NULL));
	g_assert(queue_push_tail(queue, UINT_TO_PTR(2)));
	g_assert(queue_push_tail(queue, UINT_TO_PTR(5)));
	g_assert(queue_length(queue) == 3);

	/* Invalid insertion */
	g_assert(!queue_push_after(queue, UINT_TO_PTR(6), UINT_TO_PTR(1)));

	/* Valid insertions */
	g_assert(queue_push_after(queue, NULL, UINT_TO_PTR(1)));
	g_assert(queue_push_after(queue, UINT_TO_PTR(2), UINT_TO_PTR(3)));
	g_assert(queue_push_after(queue, UINT_TO_PTR(3), UINT_TO_PTR(4)));
	g_assert(queue_push_after(queue, UINT_TO_PTR(5), UINT_TO_PTR(6)));

	g_assert(queue_peek_head(queue) == NULL);
	g_assert(queue_peek_tail(queue) == UINT_TO_PTR(6));

	/*
	 * Queue should contain 7 elements:
	 *   [ NULL, 1, 2, 3, 4, 5, 6 ]
	 */
	len = queue_length(queue);
	g_assert(len == 7);

	for (i = 0; i < 7; i++)
		g_assert(queue_pop_head(queue) == UINT_TO_PTR(i));

	/* Test with identical elements */
	g_assert(queue_push_head(queue, UINT_TO_PTR(1)));
	g_assert(queue_push_head(queue, UINT_TO_PTR(1)));
	g_assert(queue_push_head(queue, UINT_TO_PTR(1)));
	g_assert(queue_push_after(queue, UINT_TO_PTR(1), UINT_TO_PTR(0)));

	g_assert(queue_pop_head(queue) == UINT_TO_PTR(1));
	g_assert(queue_pop_head(queue) == UINT_TO_PTR(0));
	g_assert(queue_pop_head(queue) == UINT_TO_PTR(1));
	g_assert(queue_pop_head(queue) == UINT_TO_PTR(1));
}

int main(int argc, char *argv[])
{
	g_test_init(&argc, &argv, NULL);

	g_test_add_func("/queue/basic", test_basic);
	g_test_add_func("/queue/foreach_destroy", test_foreach_destroy);
	g_test_add_func("/queue/foreach_remove_all", test_foreach_remove_all);
	g_test_add_func("/queue/destroy_remove", test_destroy_remove);
	g_test_add_func("/queue/push_after", test_push_after);

	return g_test_run();
}
<commit_msg>unit/test-queue: Fix memory leak<commit_after>/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2012  Intel Corporation. All rights reserved.
 *
 *
 *  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 St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib.h>

#include "src/shared/util.h"
#include "src/shared/queue.h"

static void test_basic(void)
{
	struct queue *queue;
	unsigned int n, i;

	queue = queue_new();
	g_assert(queue != NULL);

	for (n = 0; n < 1024; n++) {
		for (i = 1; i < n + 2; i++)
			queue_push_tail(queue, UINT_TO_PTR(i));

		g_assert(queue_length(queue) == n + 1);

		for (i = 1; i < n + 2; i++) {
			void *ptr;

			ptr = queue_pop_head(queue);
			g_assert(ptr != NULL);
			g_assert(i == PTR_TO_UINT(ptr));
		}

		g_assert(queue_isempty(queue) == true);
	}

	queue_destroy(queue, NULL);
}

static void foreach_destroy(void *data, void *user_data)
{
	struct queue *queue = user_data;

	queue_destroy(queue, NULL);
}

static void test_foreach_destroy(void)
{
	struct queue *queue;

	queue = queue_new();
	g_assert(queue != NULL);

	queue_push_tail(queue, UINT_TO_PTR(1));
	queue_push_tail(queue, UINT_TO_PTR(2));

	queue_foreach(queue, foreach_destroy, queue);
}

static void foreach_remove_all(void *data, void *user_data)
{
	struct queue *queue = user_data;

	queue_remove_all(queue, NULL, NULL, NULL);
}

static void test_foreach_remove_all(void)
{
	struct queue *queue;

	queue = queue_new();
	g_assert(queue != NULL);

	queue_push_tail(queue, UINT_TO_PTR(1));
	queue_push_tail(queue, UINT_TO_PTR(2));

	queue_foreach(queue, foreach_remove_all, queue);
	queue_destroy(queue, NULL);
}

static struct queue *static_queue;

static void destroy_remove(void *user_data)
{
	queue_remove(static_queue, user_data);
}

static void test_destroy_remove(void)
{
	static_queue = queue_new();

	g_assert(static_queue != NULL);

	queue_push_tail(static_queue, UINT_TO_PTR(1));
	queue_push_tail(static_queue, UINT_TO_PTR(2));

	queue_destroy(static_queue, destroy_remove);
}

static void test_push_after(void)
{
	struct queue *queue;
	unsigned int len, i;

	queue = queue_new();
	g_assert(queue != NULL);

	/*
	 * Pre-populate queue. Initial elements are:
	 *   [ NULL, 2, 5 ]
	 */
	g_assert(queue_push_tail(queue, NULL));
	g_assert(queue_push_tail(queue, UINT_TO_PTR(2)));
	g_assert(queue_push_tail(queue, UINT_TO_PTR(5)));
	g_assert(queue_length(queue) == 3);

	/* Invalid insertion */
	g_assert(!queue_push_after(queue, UINT_TO_PTR(6), UINT_TO_PTR(1)));

	/* Valid insertions */
	g_assert(queue_push_after(queue, NULL, UINT_TO_PTR(1)));
	g_assert(queue_push_after(queue, UINT_TO_PTR(2), UINT_TO_PTR(3)));
	g_assert(queue_push_after(queue, UINT_TO_PTR(3), UINT_TO_PTR(4)));
	g_assert(queue_push_after(queue, UINT_TO_PTR(5), UINT_TO_PTR(6)));

	g_assert(queue_peek_head(queue) == NULL);
	g_assert(queue_peek_tail(queue) == UINT_TO_PTR(6));

	/*
	 * Queue should contain 7 elements:
	 *   [ NULL, 1, 2, 3, 4, 5, 6 ]
	 */
	len = queue_length(queue);
	g_assert(len == 7);

	for (i = 0; i < 7; i++)
		g_assert(queue_pop_head(queue) == UINT_TO_PTR(i));

	/* Test with identical elements */
	g_assert(queue_push_head(queue, UINT_TO_PTR(1)));
	g_assert(queue_push_head(queue, UINT_TO_PTR(1)));
	g_assert(queue_push_head(queue, UINT_TO_PTR(1)));
	g_assert(queue_push_after(queue, UINT_TO_PTR(1), UINT_TO_PTR(0)));

	g_assert(queue_pop_head(queue) == UINT_TO_PTR(1));
	g_assert(queue_pop_head(queue) == UINT_TO_PTR(0));
	g_assert(queue_pop_head(queue) == UINT_TO_PTR(1));
	g_assert(queue_pop_head(queue) == UINT_TO_PTR(1));

	queue_destroy(queue, NULL);
}

int main(int argc, char *argv[])
{
	g_test_init(&argc, &argv, NULL);

	g_test_add_func("/queue/basic", test_basic);
	g_test_add_func("/queue/foreach_destroy", test_foreach_destroy);
	g_test_add_func("/queue/foreach_remove_all", test_foreach_remove_all);
	g_test_add_func("/queue/destroy_remove", test_destroy_remove);
	g_test_add_func("/queue/push_after", test_push_after);

	return g_test_run();
}

{'fraction_non_alphanumeric': 0.13268051221349403, 'fraction_numerical': 0.014204239750349726, 'mean_word_length': 5.365753424657535, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 7, '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': '4394794', 'n_tokens_mistral': 3588, 'n_tokens_neox': 3412, 'n_words': 791}