3 * unit test for data protocol
5 * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 #include "../gstcheck.h"
25 #include <gst/dataprotocol/dataprotocol.h>
26 #include "libs/gst/dataprotocol/dp-private.h" /* private header */
28 /* test our method of reading and writing headers using TO/FROM_BE */
29 GST_START_TEST (test_conversion)
32 guint8 write_array[9];
33 guint16 read_two, expect_two;
34 guint32 read_four, expect_four;
35 guint64 read_eight, expect_eight;
38 for (i = 0; i < 9; ++i) {
43 for (i = 0; i < 8; ++i) {
44 read_two = GST_READ_UINT16_BE (array + i);
45 expect_two = array[i] * (1 << 8) + array[i + 1];
46 fail_unless (read_two == expect_two,
47 "GST_READ_UINT16_BE %d: read %d != %d\n", i, read_two, expect_two);
51 for (i = 0; i < 8; ++i) {
52 GST_WRITE_UINT16_BE (&write_array[i], read_two);
53 fail_unless (memcmp (array + 7, write_array + i, 2) == 0,
54 "GST_WRITE_UINT16_BE %d: memcmp failed", i);
58 for (i = 0; i < 5; ++i) {
59 read_four = GST_READ_UINT32_BE (array + i);
60 expect_four = array[i] * (1 << 24) + array[i + 1] * (1 << 16)
61 + array[i + 2] * (1 << 8) + array[i + 3];
62 fail_unless (read_four == expect_four,
63 "GST_READ_UINT32_BE %d: read %d != %d\n", i, read_four, expect_four);
67 for (i = 0; i < 2; ++i) {
68 read_eight = GST_READ_UINT64_BE (array + i);
69 expect_eight = array[i] * (1LL << 56) + array[i + 1] * (1LL << 48)
70 + array[i + 2] * (1LL << 40) + array[i + 3] * (1LL << 32)
71 + array[i + 4] * (1 << 24) + array[i + 5] * (1 << 16)
72 + array[i + 6] * (1 << 8) + array[i + 7];
73 fail_unless (read_eight == expect_eight,
74 "GST_READ_UINT64_BE %d: read %" G_GUINT64_FORMAT
75 " != %" G_GUINT64_FORMAT "\n", i, read_eight, expect_eight);
79 GST_WRITE_UINT64_BE (&write_array[0], read_eight);
80 fail_unless (memcmp (array + 1, write_array, 8) == 0,
81 "GST_WRITE_UINT64_BE: memcmp failed");
85 /* test creation of header from buffer and back again */
86 GST_START_TEST (test_buffer)
95 g_message ("Creating a new 8-byte buffer with ts 0.5 sec, dur 1 sec\n");
96 buffer = gst_buffer_new_and_alloc (8);
97 GST_BUFFER_TIMESTAMP (buffer) = (GstClockTime) (GST_SECOND * 0.5);
98 GST_BUFFER_DURATION (buffer) = (GstClockTime) GST_SECOND;
99 GST_BUFFER_OFFSET (buffer) = (guint64) 10;
100 GST_BUFFER_OFFSET_END (buffer) = (guint64) 19;
101 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_IN_CAPS);
102 memmove (GST_BUFFER_DATA (buffer), "a buffer", 8);
104 /* create a buffer with CRC checking */
105 fail_unless (gst_dp_header_from_buffer (buffer, GST_DP_HEADER_FLAG_CRC,
106 &header_length, &header), "Could not create header from buffer.");
108 /* validate the header */
109 fail_unless (gst_dp_validate_header (header_length, header),
110 "Could not validate header");
111 /* create a new, empty buffer with the right size */
112 newbuffer = gst_dp_buffer_from_header (header_length, header);
113 fail_unless (newbuffer != NULL, "Could not create a new buffer from header");
114 fail_unless (GST_IS_BUFFER (newbuffer), "Created buffer is not a GstBuffer");
115 /* read/copy the data */
116 memmove (GST_BUFFER_DATA (newbuffer), GST_BUFFER_DATA (buffer),
117 GST_BUFFER_SIZE (buffer));
118 /* validate the buffer */
119 fail_unless (gst_dp_validate_payload (header_length, header,
120 GST_BUFFER_DATA (newbuffer)), "Could not validate payload");
122 g_message ("new buffer timestamp: %" GST_TIME_FORMAT "\n",
123 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (newbuffer)));
124 g_message ("new buffer duration: %" GST_TIME_FORMAT "\n",
125 GST_TIME_ARGS (GST_BUFFER_DURATION (newbuffer)));
126 g_message ("new buffer offset: %" G_GUINT64_FORMAT "\n",
127 GST_BUFFER_OFFSET (newbuffer));
128 g_message ("new buffer offset_end: %" G_GUINT64_FORMAT "\n",
129 GST_BUFFER_OFFSET_END (newbuffer));
130 fail_unless (GST_BUFFER_TIMESTAMP (newbuffer) ==
131 GST_BUFFER_TIMESTAMP (buffer), "Timestamps don't match !");
132 fail_unless (GST_BUFFER_DURATION (newbuffer) == GST_BUFFER_DURATION (buffer),
133 "Durations don't match !");
134 fail_unless (GST_BUFFER_OFFSET (newbuffer) == GST_BUFFER_OFFSET (buffer),
135 "Offsets don't match !");
136 fail_unless (GST_BUFFER_OFFSET_END (newbuffer) ==
137 GST_BUFFER_OFFSET_END (buffer), "Offset ends don't match !");
138 fail_unless (GST_BUFFER_FLAG_IS_SET (newbuffer, GST_BUFFER_FLAG_IN_CAPS),
139 "GST_BUFFER_IN_CAPS flag should have been copied !");
145 GST_START_TEST (test_caps)
147 gchar *string, *newstring;
148 GstCaps *caps, *newcaps;
151 guint8 *header, *payload;
153 caps = gst_caps_from_string ("audio/x-raw-float, "
154 "rate = (int) [ 11025, 48000 ], "
155 "channels = (int) [ 1, 2 ], " "endianness = (int) BYTE_ORDER, "
156 "width = (int) 32, " "buffer-frames = (int) 0");
157 string = gst_caps_to_string (caps);
158 g_message ("Created caps: %s\n", string);
159 fail_unless (gst_dp_packet_from_caps (caps, 0, &header_length, &header,
160 &payload), "Could not create packet from caps.");
162 /* validate the packet */
163 fail_unless (gst_dp_validate_packet (header_length, header, payload),
164 "Could not validate packet");
165 newcaps = gst_dp_caps_from_packet (header_length, header, payload);
166 fail_unless (newcaps != NULL, "Could not create caps from packet");
167 //g_return_val_if_fail (GST_IS_CAPS (newcaps), -1);
168 newstring = gst_caps_to_string (newcaps);
169 g_message ("Received caps: %s\n", newstring);
170 fail_unless (strcmp (string, newstring) == 0,
171 "Created caps do not match original caps");
177 GST_START_TEST (test_event)
182 guint8 *header, *payload;
184 g_message ("Testing EOS event at 1s\n");
185 send = gst_event_new (GST_EVENT_EOS);
186 GST_EVENT_TIMESTAMP (send) = GST_SECOND;
187 fail_unless (gst_dp_packet_from_event (send, GST_DP_HEADER_FLAG_CRC,
188 &header_length, &header, &payload),
189 "Could not create packet from eos event");
191 receive = gst_dp_event_from_packet (header_length, header, payload);
193 g_message ("EOS, timestamp %" GST_TIME_FORMAT "\n",
194 GST_TIME_ARGS (GST_EVENT_TIMESTAMP (receive)));
195 fail_unless (GST_EVENT_TYPE (receive) == GST_EVENT_EOS,
196 "Received event is not EOS");
197 fail_unless (GST_EVENT_TIMESTAMP (receive) == GST_SECOND,
198 "EOS timestamp is not 1.0 sec");
199 gst_event_unref (send);
200 gst_event_unref (receive);
202 g_message ("Testing FLUSH event at 2s\n");
203 send = gst_event_new (GST_EVENT_FLUSH);
204 GST_EVENT_TIMESTAMP (send) = GST_SECOND * 2;
205 fail_unless (gst_dp_packet_from_event (send, GST_DP_HEADER_FLAG_CRC,
206 &header_length, &header, &payload),
207 "Could not create packet from flush event");
209 receive = gst_dp_event_from_packet (header_length, header, payload);
211 g_message ("Flush, timestamp %" GST_TIME_FORMAT "\n",
212 GST_TIME_ARGS (GST_EVENT_TIMESTAMP (receive)));
213 fail_unless (GST_EVENT_TYPE (receive) == GST_EVENT_FLUSH,
214 "Received event is not flush");
215 fail_unless (GST_EVENT_TIMESTAMP (receive) == GST_SECOND * 2,
216 "Flush timestamp is not 2.0 sec");
217 gst_event_unref (send);
218 gst_event_unref (receive);
220 g_message ("Testing SEEK event with 1 second at 3 seconds\n");
221 send = gst_event_new_seek (GST_FORMAT_TIME, GST_SECOND);
222 GST_EVENT_TIMESTAMP (send) = GST_SECOND * 3;
223 fail_unless (gst_dp_packet_from_event (send, GST_DP_HEADER_FLAG_CRC,
224 &header_length, &header, &payload),
225 "Could not create packet from seek event");
227 receive = gst_dp_event_from_packet (header_length, header, payload);
229 g_message ("Seek, timestamp %" GST_TIME_FORMAT ", to %" GST_TIME_FORMAT "\n",
230 GST_TIME_ARGS (GST_EVENT_TIMESTAMP (receive)),
231 GST_TIME_ARGS (GST_EVENT_SEEK_OFFSET (receive)));
232 fail_unless (GST_EVENT_TYPE (receive) == GST_EVENT_SEEK,
233 "Returned event is not seek");
234 fail_unless (GST_EVENT_TIMESTAMP (receive) == GST_SECOND * 3,
235 "Seek timestamp is not 3.0 sec");
236 fail_unless (GST_EVENT_SEEK_FORMAT (receive) == GST_FORMAT_TIME,
237 "Seek format is not time");
238 fail_unless (GST_EVENT_SEEK_OFFSET (receive) == GST_SECOND,
239 "Seek offset is not 1.0 sec");
240 gst_event_unref (send);
241 gst_event_unref (receive);
244 gst_object_suite (void)
246 Suite *s = suite_create ("data protocol");
247 TCase *tc_chain = tcase_create ("general");
249 suite_add_tcase (s, tc_chain);
250 tcase_add_test (tc_chain, test_conversion);
251 tcase_add_test (tc_chain, test_buffer);
252 tcase_add_test (tc_chain, test_caps);
253 tcase_add_test (tc_chain, test_event);
259 main (int argc, char **argv)
263 Suite *s = gst_object_suite ();
264 SRunner *sr = srunner_create (s);
266 gst_check_init (&argc, &argv);
269 srunner_run_all (sr, CK_NORMAL);
270 nf = srunner_ntests_failed (sr);