| 1 /* |
|
| 2 * Purple |
|
| 3 * |
|
| 4 * Purple is the legal property of its developers, whose names are too |
|
| 5 * numerous to list here. Please refer to the COPYRIGHT file distributed |
|
| 6 * with this source distribution |
|
| 7 * |
|
| 8 * This program is free software; you can redistribute it and/or modify |
|
| 9 * it under the terms of the GNU General Public License as published by |
|
| 10 * the Free Software Foundation; either version 2 of the License, or (at |
|
| 11 * your option) any later version. |
|
| 12 * |
|
| 13 * This program is distributed in the hope that it will be useful, but |
|
| 14 * WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
| 16 * General Public License for more details. |
|
| 17 * |
|
| 18 * You should have received a copy of the GNU General Public License |
|
| 19 * along with this program; if not, write to the Free Software |
|
| 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA |
|
| 21 */ |
|
| 22 |
|
| 23 #include <glib.h> |
|
| 24 #include <string.h> |
|
| 25 |
|
| 26 #include <purple.h> |
|
| 27 |
|
| 28 /****************************************************************************** |
|
| 29 * Tests |
|
| 30 *****************************************************************************/ |
|
| 31 |
|
| 32 /* This test just verifies that purple_circular_buffer_new creates this |
|
| 33 * properly according to the docs. |
|
| 34 */ |
|
| 35 static void |
|
| 36 test_circular_buffer_new(void) { |
|
| 37 PurpleCircularBuffer *buffer = purple_circular_buffer_new(0); |
|
| 38 |
|
| 39 g_assert_true(PURPLE_IS_CIRCULAR_BUFFER(buffer)); |
|
| 40 |
|
| 41 g_assert_cmpuint(256, ==, purple_circular_buffer_get_grow_size(buffer)); |
|
| 42 g_assert_cmpuint(0, ==, purple_circular_buffer_get_used(buffer)); |
|
| 43 g_assert_cmpuint(0, ==, purple_circular_buffer_get_max_read(buffer)); |
|
| 44 |
|
| 45 g_object_unref(buffer); |
|
| 46 } |
|
| 47 |
|
| 48 /* This test make sure that purple_circular_buffer_reset works as described in |
|
| 49 * the documentation. |
|
| 50 */ |
|
| 51 static void |
|
| 52 test_circular_buffer_reset(void) { |
|
| 53 PurpleCircularBuffer *buffer = purple_circular_buffer_new(0); |
|
| 54 const gchar *data; |
|
| 55 |
|
| 56 purple_circular_buffer_append(buffer, "abc\0", 4); |
|
| 57 g_assert_cmpuint(4, ==, purple_circular_buffer_get_used(buffer)); |
|
| 58 g_assert_cmpuint(4, ==, purple_circular_buffer_get_max_read(buffer)); |
|
| 59 |
|
| 60 purple_circular_buffer_get_output(buffer); |
|
| 61 data = purple_circular_buffer_get_output(buffer); |
|
| 62 g_assert_cmpstr("abc", ==, data); |
|
| 63 |
|
| 64 purple_circular_buffer_reset(buffer); |
|
| 65 |
|
| 66 data = purple_circular_buffer_get_output(buffer); |
|
| 67 g_assert_cmpstr("abc", ==, data); |
|
| 68 |
|
| 69 g_object_unref(buffer); |
|
| 70 } |
|
| 71 |
|
| 72 /* This test verifies that purple_circular_buffer_mark_read works as described |
|
| 73 * in the documentation. |
|
| 74 */ |
|
| 75 static void |
|
| 76 test_circular_buffer_mark_read(void) { |
|
| 77 PurpleCircularBuffer *buffer = purple_circular_buffer_new(0); |
|
| 78 const gchar *data; |
|
| 79 |
|
| 80 purple_circular_buffer_append(buffer, "abc\0", 4); |
|
| 81 g_assert_cmpuint(4, ==, purple_circular_buffer_get_used(buffer)); |
|
| 82 g_assert_cmpuint(4, ==, purple_circular_buffer_get_max_read(buffer)); |
|
| 83 |
|
| 84 /* force a read to move the output */ |
|
| 85 purple_circular_buffer_get_output(buffer); |
|
| 86 data = purple_circular_buffer_get_output(buffer); |
|
| 87 g_assert_cmpstr("abc", ==, data); |
|
| 88 |
|
| 89 purple_circular_buffer_mark_read(buffer, 4); |
|
| 90 |
|
| 91 g_assert_cmpuint(0, ==, purple_circular_buffer_get_used(buffer)); |
|
| 92 g_assert_cmpuint(0, ==, purple_circular_buffer_get_max_read(buffer)); |
|
| 93 |
|
| 94 g_object_unref(buffer); |
|
| 95 } |
|
| 96 |
|
| 97 /* this test verifies that the buffer will grow 1 grow size to fit the data. */ |
|
| 98 static void |
|
| 99 test_circular_buffer_single_default_grow(void) { |
|
| 100 PurpleCircularBuffer *buffer = purple_circular_buffer_new(0); |
|
| 101 const gchar *data; |
|
| 102 |
|
| 103 purple_circular_buffer_append(buffer, "abc\0", 4); |
|
| 104 |
|
| 105 g_assert_cmpuint(4, ==, purple_circular_buffer_get_used(buffer)); |
|
| 106 g_assert_cmpuint(4, ==, purple_circular_buffer_get_max_read(buffer)); |
|
| 107 |
|
| 108 data = purple_circular_buffer_get_output(buffer); |
|
| 109 |
|
| 110 g_assert_cmpstr("abc", ==, data); |
|
| 111 |
|
| 112 g_object_unref(buffer); |
|
| 113 } |
|
| 114 |
|
| 115 /* this test create a circular buffer with a grow size of 1 to easily test |
|
| 116 * multiple grows. |
|
| 117 */ |
|
| 118 static void |
|
| 119 test_circular_buffer_multiple_grows(void) { |
|
| 120 PurpleCircularBuffer *buffer = purple_circular_buffer_new(1); |
|
| 121 |
|
| 122 purple_circular_buffer_append(buffer, "abcdefghijklmnopqrstuvwxyz\0", 27); |
|
| 123 |
|
| 124 g_assert_cmpuint(27, ==, purple_circular_buffer_get_used(buffer)); |
|
| 125 g_assert_cmpuint(27, ==, purple_circular_buffer_get_max_read(buffer)); |
|
| 126 |
|
| 127 g_assert_cmpstr("abcdefghijklmnopqrstuvwxyz", ==, purple_circular_buffer_get_output(buffer)); |
|
| 128 |
|
| 129 g_object_unref(buffer); |
|
| 130 } |
|
| 131 |
|
| 132 /****************************************************************************** |
|
| 133 * Main |
|
| 134 *****************************************************************************/ |
|
| 135 gint |
|
| 136 main(gint argc, gchar **argv) { |
|
| 137 g_test_init(&argc, &argv, NULL); |
|
| 138 |
|
| 139 g_test_set_nonfatal_assertions(); |
|
| 140 |
|
| 141 g_test_add_func("/circular_buffer/new", test_circular_buffer_new); |
|
| 142 g_test_add_func("/circular_buffer/reset", test_circular_buffer_reset); |
|
| 143 g_test_add_func("/circular_buffer/mark_read", test_circular_buffer_mark_read); |
|
| 144 g_test_add_func("/circular_buffer/single_default_grow", test_circular_buffer_single_default_grow); |
|
| 145 g_test_add_func("/circular_buffer/multiple_grows", test_circular_buffer_multiple_grows); |
|
| 146 |
|
| 147 return g_test_run(); |
|
| 148 } |
|