Thu, 31 Jul 2025 20:57:19 -0500
Remove Purple.CircularBuffer
This was primarily used for file transfer, which should be using GIOStreams
instead.
Testing Done:
Called in the turtles.
Reviewed at https://reviews.imfreedom.org/r/4083/
| libpurple/circularbuffer.c | file | annotate | diff | comparison | revisions | |
| libpurple/circularbuffer.h | file | annotate | diff | comparison | revisions | |
| libpurple/meson.build | file | annotate | diff | comparison | revisions | |
| libpurple/tests/meson.build | file | annotate | diff | comparison | revisions | |
| libpurple/tests/test_circular_buffer.c | file | annotate | diff | comparison | revisions | |
| po/POTFILES.in | file | annotate | diff | comparison | revisions |
--- a/libpurple/circularbuffer.c Thu Jul 31 20:55:42 2025 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,480 +0,0 @@ -/* - * Purple - Internet Messaging Library - * Copyright (C) Pidgin Developers <devel@pidgin.im> - * - * Purple is the legal property of its developers, whose names are too numerous - * to list here. Please refer to the COPYRIGHT file distributed with this - * source distribution. - * - * This library 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 library 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 library; if not, see <https://www.gnu.org/licenses/>. - */ - -#include "circularbuffer.h" - -#define DEFAULT_BUF_SIZE 256 - -/****************************************************************************** - * Structs - *****************************************************************************/ -typedef struct { - /* A pointer to the starting address of our chunk of memory. */ - gchar *buffer; - - /* The incremental amount to increase this buffer by when the buffer is not - * big enough to hold incoming data, in bytes. */ - gsize growsize; - - /* The length of this buffer, in bytes. */ - gsize buflen; - - /* The number of bytes of this buffer that contain unread data. */ - gsize bufused; - - /* A pointer to the next byte where new incoming data is buffered to. */ - gchar *input; - - /* A pointer to the next byte of buffered data that should be read by the - * consumer. */ - gchar *output; -} PurpleCircularBufferPrivate; - -/****************************************************************************** - * Enums - *****************************************************************************/ -enum { - PROP_0, - PROP_GROW_SIZE, - PROP_BUFFER_USED, - PROP_INPUT, - PROP_OUTPUT, - N_PROPERTIES, -}; - -/****************************************************************************** - * Globals - *****************************************************************************/ -static GParamSpec *properties[N_PROPERTIES] = {NULL, }; - -G_DEFINE_TYPE_WITH_PRIVATE(PurpleCircularBuffer, purple_circular_buffer, - G_TYPE_OBJECT); - -/****************************************************************************** - * Circular Buffer Implementation - *****************************************************************************/ -static void -purple_circular_buffer_real_grow(PurpleCircularBuffer *buffer, gsize len) { - PurpleCircularBufferPrivate *priv = NULL; - gsize in_offset = 0, out_offset = 0; - gsize start_buflen; - GObject *obj; - - priv = purple_circular_buffer_get_instance_private(buffer); - - start_buflen = priv->buflen; - - while((priv->buflen - priv->bufused) < len) - priv->buflen += priv->growsize; - - if(priv->input != NULL) { - in_offset = priv->input - priv->buffer; - out_offset = priv->output - priv->buffer; - } - - priv->buffer = g_realloc(priv->buffer, priv->buflen); - - /* adjust the fill and remove pointer locations */ - if(priv->input == NULL) { - priv->input = priv->output = priv->buffer; - } else { - priv->input = priv->buffer + in_offset; - priv->output = priv->buffer + out_offset; - } - - /* If the fill pointer is wrapped to before the remove - * pointer, we need to shift the data */ - if (in_offset < out_offset - || (in_offset == out_offset && priv->bufused > 0)) - { - gsize shift_n = MIN(priv->buflen - start_buflen, in_offset); - memcpy(priv->buffer + start_buflen, priv->buffer, shift_n); - - /* If we couldn't fit the wrapped read buffer at the end */ - if (shift_n < in_offset) { - memmove(priv->buffer, priv->buffer + shift_n, in_offset - shift_n); - priv->input = priv->buffer + (in_offset - shift_n); - } else { - priv->input = priv->buffer + start_buflen + in_offset; - } - } - - obj = G_OBJECT(buffer); - g_object_freeze_notify(obj); - g_object_notify_by_pspec(obj, properties[PROP_INPUT]); - g_object_notify_by_pspec(obj, properties[PROP_OUTPUT]); - g_object_thaw_notify(obj); -} - -static void -purple_circular_buffer_real_append(PurpleCircularBuffer *buffer, - gconstpointer src, gsize len) -{ - PurpleCircularBufferPrivate *priv = NULL; - gsize len_stored; - GObject *obj; - - priv = purple_circular_buffer_get_instance_private(buffer); - - /* Grow the buffer, if necessary */ - if((priv->buflen - priv->bufused) < len) - purple_circular_buffer_grow(buffer, len); - - /* If there is not enough room to copy all of src before hitting - * the end of the buffer then we will need to do two copies. - * One copy from input to the end of the buffer, and the - * second copy from the start of the buffer to the end of src. */ - if(priv->input >= priv->output) - len_stored = MIN(len, priv->buflen - (priv->input - priv->buffer)); - else - len_stored = len; - - if(len_stored > 0) - memcpy(priv->input, src, len_stored); - - if(len_stored < len) { - memcpy(priv->buffer, (char*)src + len_stored, len - len_stored); - priv->input = priv->buffer + (len - len_stored); - } else { - priv->input += len_stored; - } - - priv->bufused += len; - - obj = G_OBJECT(buffer); - g_object_freeze_notify(obj); - g_object_notify_by_pspec(obj, properties[PROP_BUFFER_USED]); - g_object_notify_by_pspec(obj, properties[PROP_INPUT]); - g_object_thaw_notify(obj); -} - -static gsize -purple_circular_buffer_real_max_read_size(PurpleCircularBuffer *buffer) { - PurpleCircularBufferPrivate *priv = NULL; - gsize max_read; - - priv = purple_circular_buffer_get_instance_private(buffer); - - if(priv->bufused == 0) - max_read = 0; - else if((priv->output - priv->input) >= 0) - max_read = priv->buflen - (priv->output - priv->buffer); - else - max_read = priv->input - priv->output; - - return max_read; -} - -static gboolean -purple_circular_buffer_real_mark_read(PurpleCircularBuffer *buffer, - gsize len) -{ - PurpleCircularBufferPrivate *priv = NULL; - GObject *obj; - - g_return_val_if_fail(purple_circular_buffer_get_max_read(buffer) >= len, FALSE); - - priv = purple_circular_buffer_get_instance_private(buffer); - - priv->output += len; - priv->bufused -= len; - - /* wrap to the start if we're at the end */ - if ((gsize)(priv->output - priv->buffer) == priv->buflen) - priv->output = priv->buffer; - - obj = G_OBJECT(buffer); - g_object_freeze_notify(obj); - g_object_notify_by_pspec(obj, properties[PROP_BUFFER_USED]); - g_object_notify_by_pspec(obj, properties[PROP_OUTPUT]); - g_object_thaw_notify(obj); - - return TRUE; -} - -/****************************************************************************** - * Private API - *****************************************************************************/ -static void -purple_circular_buffer_set_grow_size(PurpleCircularBuffer *buffer, - gsize grow_size) -{ - PurpleCircularBufferPrivate *priv = - purple_circular_buffer_get_instance_private(buffer); - - priv->growsize = (grow_size != 0) ? grow_size : DEFAULT_BUF_SIZE; - - g_object_notify_by_pspec(G_OBJECT(buffer), properties[PROP_GROW_SIZE]); -} - -static const gchar * -purple_circular_buffer_get_input(PurpleCircularBuffer *buffer) { - PurpleCircularBufferPrivate *priv = NULL; - - g_return_val_if_fail(PURPLE_IS_CIRCULAR_BUFFER(buffer), NULL); - - priv = purple_circular_buffer_get_instance_private(buffer); - - return priv->input; -} - -/****************************************************************************** - * Object Stuff - *****************************************************************************/ -static void -purple_circular_buffer_init(G_GNUC_UNUSED PurpleCircularBuffer *buffer) -{ -} - -static void -purple_circular_buffer_finalize(GObject *obj) { - PurpleCircularBufferPrivate *priv = - purple_circular_buffer_get_instance_private( - PURPLE_CIRCULAR_BUFFER(obj)); - - g_free(priv->buffer); - - G_OBJECT_CLASS(purple_circular_buffer_parent_class)->finalize(obj); -} - -static void -purple_circular_buffer_get_property(GObject *obj, guint param_id, - GValue *value, GParamSpec *pspec) -{ - PurpleCircularBuffer *buffer = PURPLE_CIRCULAR_BUFFER(obj); - - switch(param_id) { - case PROP_GROW_SIZE: - g_value_set_uint64(value, - purple_circular_buffer_get_grow_size(buffer)); - break; - case PROP_BUFFER_USED: - g_value_set_uint64(value, purple_circular_buffer_get_used(buffer)); - break; - case PROP_INPUT: - g_value_set_pointer(value, - (void*) purple_circular_buffer_get_input(buffer)); - break; - case PROP_OUTPUT: - g_value_set_pointer(value, - (void*) purple_circular_buffer_get_output(buffer)); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec); - break; - } -} - -static void -purple_circular_buffer_set_property(GObject *obj, guint param_id, - const GValue *value, GParamSpec *pspec) -{ - PurpleCircularBuffer *buffer = PURPLE_CIRCULAR_BUFFER(obj); - - switch(param_id) { - case PROP_GROW_SIZE: - purple_circular_buffer_set_grow_size(buffer, - g_value_get_uint64(value)); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec); - break; - } -} - -static void -purple_circular_buffer_class_init(PurpleCircularBufferClass *klass) { - GObjectClass *obj_class = G_OBJECT_CLASS(klass); - PurpleCircularBufferClass *buffer_class = PURPLE_CIRCULAR_BUFFER_CLASS(klass); - - obj_class->finalize = purple_circular_buffer_finalize; - obj_class->get_property = purple_circular_buffer_get_property; - obj_class->set_property = purple_circular_buffer_set_property; - - buffer_class->grow = purple_circular_buffer_real_grow; - buffer_class->append = purple_circular_buffer_real_append; - buffer_class->max_read_size = purple_circular_buffer_real_max_read_size; - buffer_class->mark_read = purple_circular_buffer_real_mark_read; - - /** - * PurpleCircularBuffer:grow-size: - * - * The grow size of the buffer. - * - * Since: 3.0 - */ - properties[PROP_GROW_SIZE] = g_param_spec_uint64( - "grow-size", NULL, NULL, - 0, G_MAXSIZE, 0, - G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS); - - /** - * PurpleCircularBuffer:buffer-used: - * - * How much of the buffer that has been used. - * - * Since: 3.0 - */ - properties[PROP_BUFFER_USED] = g_param_spec_uint64( - "buffer-used", NULL, NULL, - 0, G_MAXSIZE, 0, - G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); - - /** - * PurpleCircularBuffer:input: - * - * The input pointer of the buffer. - * - * Since: 3.0 - */ - properties[PROP_INPUT] = g_param_spec_pointer( - "input", NULL, NULL, - G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); - - /** - * PurpleCircularBuffer:output: - * - * The output pointer of the buffer. - * - * Since: 3.0 - */ - properties[PROP_OUTPUT] = g_param_spec_pointer( - "output", NULL, NULL, - G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); - - g_object_class_install_properties(obj_class, N_PROPERTIES, properties); -} - -/****************************************************************************** - * API - *****************************************************************************/ -PurpleCircularBuffer * -purple_circular_buffer_new(gsize growsize) { - return g_object_new(PURPLE_TYPE_CIRCULAR_BUFFER, - "grow-size", (guint64)(growsize ? growsize : DEFAULT_BUF_SIZE), - NULL); -} - -void -purple_circular_buffer_grow(PurpleCircularBuffer *buffer, gsize len) { - PurpleCircularBufferClass *klass = NULL; - - g_return_if_fail(PURPLE_IS_CIRCULAR_BUFFER(buffer)); - - klass = PURPLE_CIRCULAR_BUFFER_GET_CLASS(buffer); - if(klass && klass->grow) - klass->grow(buffer, len); -} - -void -purple_circular_buffer_append(PurpleCircularBuffer *buffer, gconstpointer src, - gsize len) -{ - PurpleCircularBufferClass *klass = NULL; - - g_return_if_fail(PURPLE_IS_CIRCULAR_BUFFER(buffer)); - g_return_if_fail(src != NULL); - - klass = PURPLE_CIRCULAR_BUFFER_GET_CLASS(buffer); - if(klass && klass->append) - klass->append(buffer, src, len); -} - -gsize -purple_circular_buffer_get_max_read(PurpleCircularBuffer *buffer) { - PurpleCircularBufferClass *klass = NULL; - - g_return_val_if_fail(PURPLE_IS_CIRCULAR_BUFFER(buffer), 0); - - klass = PURPLE_CIRCULAR_BUFFER_GET_CLASS(buffer); - if(klass && klass->max_read_size) - return klass->max_read_size(buffer); - - return 0; -} - -gboolean -purple_circular_buffer_mark_read(PurpleCircularBuffer *buffer, gsize len) { - PurpleCircularBufferClass *klass = NULL; - - g_return_val_if_fail(PURPLE_IS_CIRCULAR_BUFFER(buffer), FALSE); - - klass = PURPLE_CIRCULAR_BUFFER_GET_CLASS(buffer); - if(klass && klass->mark_read) - return klass->mark_read(buffer, len); - - return FALSE; -} - -gsize -purple_circular_buffer_get_grow_size(PurpleCircularBuffer *buffer) { - - PurpleCircularBufferPrivate *priv = NULL; - - g_return_val_if_fail(PURPLE_IS_CIRCULAR_BUFFER(buffer), 0); - - priv = purple_circular_buffer_get_instance_private(buffer); - - return priv->growsize; -} - -gsize -purple_circular_buffer_get_used(PurpleCircularBuffer *buffer) { - PurpleCircularBufferPrivate *priv = NULL; - - g_return_val_if_fail(PURPLE_IS_CIRCULAR_BUFFER(buffer), 0); - - priv = purple_circular_buffer_get_instance_private(buffer); - - return priv->bufused; -} - -const gchar * -purple_circular_buffer_get_output(PurpleCircularBuffer *buffer) { - PurpleCircularBufferPrivate *priv = NULL; - - g_return_val_if_fail(PURPLE_IS_CIRCULAR_BUFFER(buffer), NULL); - - priv = purple_circular_buffer_get_instance_private(buffer); - - return priv->output; -} - -void -purple_circular_buffer_reset(PurpleCircularBuffer *buffer) { - PurpleCircularBufferPrivate *priv = NULL; - GObject *obj; - - g_return_if_fail(PURPLE_IS_CIRCULAR_BUFFER(buffer)); - - priv = purple_circular_buffer_get_instance_private(buffer); - - priv->input = priv->buffer; - priv->output = priv->buffer; - - obj = G_OBJECT(buffer); - g_object_freeze_notify(obj); - g_object_notify_by_pspec(obj, properties[PROP_INPUT]); - g_object_notify_by_pspec(obj, properties[PROP_OUTPUT]); - g_object_thaw_notify(obj); -} -
--- a/libpurple/circularbuffer.h Thu Jul 31 20:55:42 2025 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,198 +0,0 @@ -/* - * Purple - Internet Messaging Library - * Copyright (C) Pidgin Developers <devel@pidgin.im> - * - * Purple is the legal property of its developers, whose names are too numerous - * to list here. Please refer to the COPYRIGHT file distributed with this - * source distribution. - * - * This library 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 library 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 library; if not, see <https://www.gnu.org/licenses/>. - */ - -#if !defined(PURPLE_GLOBAL_HEADER_INSIDE) && !defined(PURPLE_COMPILATION) -# error "only <purple.h> may be included directly" -#endif - -#ifndef PURPLE_CIRCULAR_BUFFER_H -#define PURPLE_CIRCULAR_BUFFER_H - -#include <glib.h> -#include <glib-object.h> - -#include "purpleversion.h" - -G_BEGIN_DECLS - -/** - * PurpleCircularBuffer: - * - * A circular buffer implementation. - * - * Since: 3.0 - */ -#define PURPLE_TYPE_CIRCULAR_BUFFER (purple_circular_buffer_get_type()) - -PURPLE_AVAILABLE_IN_3_0 -G_DECLARE_DERIVABLE_TYPE(PurpleCircularBuffer, purple_circular_buffer, PURPLE, - CIRCULAR_BUFFER, GObject) - -struct _PurpleCircularBufferClass { - /*< private >*/ - GObjectClass parent; - - void (*grow)(PurpleCircularBuffer *buffer, gsize len); - void (*append)(PurpleCircularBuffer *buffer, gconstpointer src, gsize len); - gsize (*max_read_size)(PurpleCircularBuffer *buffer); - gboolean (*mark_read)(PurpleCircularBuffer *buffer, gsize len); - - void (*purple_reserved1)(void); - void (*purple_reserved2)(void); - void (*purple_reserved3)(void); - void (*purple_reserved4)(void); -}; - -/** - * purple_circular_buffer_new: - * @growsize: The amount that the buffer should grow the first time data - * is appended and every time more space is needed. Pass in - * "0" to use the default of 256 bytes. - * - * Creates a new circular buffer. This will not allocate any memory for the - * actual buffer until data is appended to it. - * - * Returns: The new PurpleCircularBuffer. - * - * Since: 3.0 - */ -PURPLE_AVAILABLE_IN_3_0 -PurpleCircularBuffer *purple_circular_buffer_new(gsize growsize); - -/** - * purple_circular_buffer_append: - * @buffer: The PurpleCircularBuffer to which to append the data - * @src: pointer to the data to copy into the buffer - * @len: number of bytes to copy into the buffer - * - * Append data to the PurpleCircularBuffer. This will grow the internal - * buffer to fit the added data, if needed. - * - * Since: 3.0 - */ -PURPLE_AVAILABLE_IN_3_0 -void purple_circular_buffer_append(PurpleCircularBuffer *buffer, gconstpointer src, gsize len); - -/** - * purple_circular_buffer_get_max_read: - * @buffer: the PurpleCircularBuffer for which to determine the maximum - * contiguous bytes that can be read. - * - * Determine the maximum number of contiguous bytes that can be read from the - * PurpleCircularBuffer. - * Note: This may not be the total number of bytes that are buffered - a - * subsequent call after calling purple_circular_buffer_mark_read() may indicate - * more data is available to read. - * - * Returns: the number of bytes that can be read from the PurpleCircularBuffer - * - * Since: 3.0 - */ -PURPLE_AVAILABLE_IN_3_0 -gsize purple_circular_buffer_get_max_read(PurpleCircularBuffer *buffer); - -/** - * purple_circular_buffer_mark_read: - * @buffer: The PurpleCircularBuffer to mark bytes read from - * @len: The number of bytes to mark as read - * - * Mark the number of bytes that have been read from the buffer. - * - * Returns: TRUE if we successfully marked the bytes as having been read, FALSE - * otherwise. - * - * Since: 3.0 - */ -PURPLE_AVAILABLE_IN_3_0 -gboolean purple_circular_buffer_mark_read(PurpleCircularBuffer *buffer, gsize len); - -/** - * purple_circular_buffer_grow: - * @buffer: The PurpleCircularBuffer to grow. - * @len: The number of bytes the buffer should be able to hold. - * - * Increases the buffer size by a multiple of grow size, so that it can hold at - * least 'len' bytes. - * - * Since: 3.0 - */ -PURPLE_AVAILABLE_IN_3_0 -void purple_circular_buffer_grow(PurpleCircularBuffer *buffer, gsize len); - -/** - * purple_circular_buffer_get_grow_size: - * @buffer: The PurpleCircularBuffer from which to get grow size. - * - * Returns the number of bytes by which the buffer grows when more space is - * needed. - * - * Returns: The grow size of the buffer. - * - * Since: 3.0 - */ -PURPLE_AVAILABLE_IN_3_0 -gsize purple_circular_buffer_get_grow_size(PurpleCircularBuffer *buffer); - -/** - * purple_circular_buffer_get_used: - * @buffer: The PurpleCircularBuffer from which to get used count. - * - * Returns the number of bytes of this buffer that contain unread data. - * - * Returns: The number of bytes that contain unread data. - * - * Since: 3.0 - */ -PURPLE_AVAILABLE_IN_3_0 -gsize purple_circular_buffer_get_used(PurpleCircularBuffer *buffer); - -/** - * purple_circular_buffer_get_output: - * @buffer: The PurpleCircularBuffer from which to get the output pointer. - * - * Returns the output pointer of the buffer, where unread data is available. - * Use purple_circular_buffer_get_max_read() to determine the number of - * contiguous bytes that can be read from this output. After reading the data, - * call purple_circular_buffer_mark_read() to mark the retrieved data as read. - * - * Returns: The output pointer for the buffer. - * - * Since: 3.0 - */ -PURPLE_AVAILABLE_IN_3_0 -const gchar *purple_circular_buffer_get_output(PurpleCircularBuffer *buffer); - -/** - * purple_circular_buffer_reset: - * @buffer: The PurpleCircularBuffer to reset. - * - * Resets the buffer input and output pointers to the start of the buffer. - * - * Since: 3.0 - */ -PURPLE_AVAILABLE_IN_3_0 -void purple_circular_buffer_reset(PurpleCircularBuffer *buffer); - -G_END_DECLS - -#endif /* PURPLE_CIRCULAR_BUFFER_H */ -
--- a/libpurple/meson.build Thu Jul 31 20:55:42 2025 -0500 +++ b/libpurple/meson.build Thu Jul 31 20:57:19 2025 -0500 @@ -1,6 +1,5 @@ purple_coresources = [ 'accounts.c', - 'circularbuffer.c', 'core.c', 'debug.c', 'image.c', @@ -96,7 +95,6 @@ purple_coreheaders = [ 'accounts.h', - 'circularbuffer.h', 'core.h', 'debug.h', 'image.h',
--- a/libpurple/tests/meson.build Thu Jul 31 20:55:42 2025 -0500 +++ b/libpurple/tests/meson.build Thu Jul 31 20:57:19 2025 -0500 @@ -9,7 +9,6 @@ 'badge_manager', 'badges', 'channel_join_details', - 'circular_buffer', 'create_conversation_details', 'command', 'commands',
--- a/libpurple/tests/test_circular_buffer.c Thu Jul 31 20:55:42 2025 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,148 +0,0 @@ -/* - * Purple - * - * Purple is the legal property of its developers, whose names are too - * numerous to list here. Please refer to the COPYRIGHT file distributed - * with this source distribution - * - * 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 02111-1301 USA - */ - -#include <glib.h> -#include <string.h> - -#include <purple.h> - -/****************************************************************************** - * Tests - *****************************************************************************/ - -/* This test just verifies that purple_circular_buffer_new creates this - * properly according to the docs. - */ -static void -test_circular_buffer_new(void) { - PurpleCircularBuffer *buffer = purple_circular_buffer_new(0); - - g_assert_true(PURPLE_IS_CIRCULAR_BUFFER(buffer)); - - g_assert_cmpuint(256, ==, purple_circular_buffer_get_grow_size(buffer)); - g_assert_cmpuint(0, ==, purple_circular_buffer_get_used(buffer)); - g_assert_cmpuint(0, ==, purple_circular_buffer_get_max_read(buffer)); - - g_object_unref(buffer); -} - -/* This test make sure that purple_circular_buffer_reset works as described in - * the documentation. - */ -static void -test_circular_buffer_reset(void) { - PurpleCircularBuffer *buffer = purple_circular_buffer_new(0); - const gchar *data; - - purple_circular_buffer_append(buffer, "abc\0", 4); - g_assert_cmpuint(4, ==, purple_circular_buffer_get_used(buffer)); - g_assert_cmpuint(4, ==, purple_circular_buffer_get_max_read(buffer)); - - purple_circular_buffer_get_output(buffer); - data = purple_circular_buffer_get_output(buffer); - g_assert_cmpstr("abc", ==, data); - - purple_circular_buffer_reset(buffer); - - data = purple_circular_buffer_get_output(buffer); - g_assert_cmpstr("abc", ==, data); - - g_object_unref(buffer); -} - -/* This test verifies that purple_circular_buffer_mark_read works as described - * in the documentation. - */ -static void -test_circular_buffer_mark_read(void) { - PurpleCircularBuffer *buffer = purple_circular_buffer_new(0); - const gchar *data; - - purple_circular_buffer_append(buffer, "abc\0", 4); - g_assert_cmpuint(4, ==, purple_circular_buffer_get_used(buffer)); - g_assert_cmpuint(4, ==, purple_circular_buffer_get_max_read(buffer)); - - /* force a read to move the output */ - purple_circular_buffer_get_output(buffer); - data = purple_circular_buffer_get_output(buffer); - g_assert_cmpstr("abc", ==, data); - - purple_circular_buffer_mark_read(buffer, 4); - - g_assert_cmpuint(0, ==, purple_circular_buffer_get_used(buffer)); - g_assert_cmpuint(0, ==, purple_circular_buffer_get_max_read(buffer)); - - g_object_unref(buffer); -} - -/* this test verifies that the buffer will grow 1 grow size to fit the data. */ -static void -test_circular_buffer_single_default_grow(void) { - PurpleCircularBuffer *buffer = purple_circular_buffer_new(0); - const gchar *data; - - purple_circular_buffer_append(buffer, "abc\0", 4); - - g_assert_cmpuint(4, ==, purple_circular_buffer_get_used(buffer)); - g_assert_cmpuint(4, ==, purple_circular_buffer_get_max_read(buffer)); - - data = purple_circular_buffer_get_output(buffer); - - g_assert_cmpstr("abc", ==, data); - - g_object_unref(buffer); -} - -/* this test create a circular buffer with a grow size of 1 to easily test - * multiple grows. - */ -static void -test_circular_buffer_multiple_grows(void) { - PurpleCircularBuffer *buffer = purple_circular_buffer_new(1); - - purple_circular_buffer_append(buffer, "abcdefghijklmnopqrstuvwxyz\0", 27); - - g_assert_cmpuint(27, ==, purple_circular_buffer_get_used(buffer)); - g_assert_cmpuint(27, ==, purple_circular_buffer_get_max_read(buffer)); - - g_assert_cmpstr("abcdefghijklmnopqrstuvwxyz", ==, purple_circular_buffer_get_output(buffer)); - - g_object_unref(buffer); -} - -/****************************************************************************** - * Main - *****************************************************************************/ -gint -main(gint argc, gchar **argv) { - g_test_init(&argc, &argv, NULL); - - g_test_set_nonfatal_assertions(); - - g_test_add_func("/circular_buffer/new", test_circular_buffer_new); - g_test_add_func("/circular_buffer/reset", test_circular_buffer_reset); - g_test_add_func("/circular_buffer/mark_read", test_circular_buffer_mark_read); - g_test_add_func("/circular_buffer/single_default_grow", test_circular_buffer_single_default_grow); - g_test_add_func("/circular_buffer/multiple_grows", test_circular_buffer_multiple_grows); - - return g_test_run(); -}