~ubuntu-branches/ubuntu/precise/liboggz/precise

« back to all changes in this revision

Viewing changes to src/tests/io-read-single.c

  • Committer: Bazaar Package Importer
  • Author(s): Jamie Wilkinson
  • Date: 2005-04-16 01:19:44 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 hoary)
  • Revision ID: james.westby@ubuntu.com-20050416011944-5ipwrrc260ihkpp8
Tags: 0.9.1-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Copyright (C) 2003 Commonwealth Scientific and Industrial Research
 
3
   Organisation (CSIRO) Australia
 
4
 
 
5
   Redistribution and use in source and binary forms, with or without
 
6
   modification, are permitted provided that the following conditions
 
7
   are met:
 
8
 
 
9
   - Redistributions of source code must retain the above copyright
 
10
   notice, this list of conditions and the following disclaimer.
 
11
 
 
12
   - Redistributions in binary form must reproduce the above copyright
 
13
   notice, this list of conditions and the following disclaimer in the
 
14
   documentation and/or other materials provided with the distribution.
 
15
 
 
16
   - Neither the name of CSIRO Australia nor the names of its
 
17
   contributors may be used to endorse or promote products derived from
 
18
   this software without specific prior written permission.
 
19
 
 
20
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
21
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
22
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 
23
   PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ORGANISATION OR
 
24
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
25
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
26
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 
27
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 
28
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 
29
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
30
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
31
*/
 
32
 
 
33
#include "config.h"
 
34
 
 
35
#include <string.h>
 
36
 
 
37
#include <oggz/oggz.h>
 
38
 
 
39
#include "oggz_tests.h"
 
40
 
 
41
/* #define DEBUG */
 
42
 
 
43
#define DATA_BUF_LEN 1024
 
44
 
 
45
static long serialno;
 
46
static long bytes_generated = 0;
 
47
static int read_called = 0;
 
48
static int eof_reached = 0;
 
49
 
 
50
static int
 
51
hungry (OGGZ * oggz, int empty, void * user_data)
 
52
{
 
53
  unsigned char buf[1];
 
54
  ogg_packet op;
 
55
  static int iter = 0;
 
56
  static long b_o_s = 1;
 
57
  static long e_o_s = 0;
 
58
 
 
59
  if (iter > 10) return 1;
 
60
 
 
61
  buf[0] = 'a' + iter;
 
62
 
 
63
  op.packet = buf;
 
64
  op.bytes = 1;
 
65
  op.b_o_s = b_o_s;
 
66
  op.e_o_s = e_o_s;
 
67
  op.granulepos = iter;
 
68
  op.packetno = iter;
 
69
 
 
70
  /* Main check */
 
71
   if (oggz_write_feed (oggz, &op, serialno, 0, NULL) != 0)
 
72
    FAIL ("Oggz write failed");
 
73
 
 
74
  iter++;
 
75
  b_o_s = 0;
 
76
  if (iter == 10) e_o_s = 1;
 
77
  
 
78
  return 0;
 
79
}
 
80
 
 
81
static int
 
82
read_packet (OGGZ * oggz, ogg_packet * op, long serialno, void * user_data)
 
83
{
 
84
  static int iter = 0;
 
85
  static long b_o_s = 1;
 
86
  static long e_o_s = 0;
 
87
 
 
88
#ifdef DEBUG
 
89
  printf ("%08" PRI_off_t "x: serialno %010ld, "
 
90
          "granulepos %" PRId64 ", packetno %" PRId64,
 
91
          oggz_tell (oggz), serialno, op->granulepos, op->packetno);
 
92
 
 
93
  if (op->b_o_s) {
 
94
    printf (" *** bos");
 
95
  }
 
96
 
 
97
  if (op->e_o_s) {
 
98
    printf (" *** eos");
 
99
  }
 
100
 
 
101
  printf ("\n");
 
102
#endif
 
103
 
 
104
  if (op->bytes != 1)
 
105
    FAIL ("Packet too long");
 
106
 
 
107
  if (op->packet[0] != 'a' + iter)
 
108
    FAIL ("Packet contains incorrect data");
 
109
 
 
110
  if ((op->b_o_s == 0) != (b_o_s == 0))
 
111
    FAIL ("Packet has incorrect b_o_s");
 
112
 
 
113
  if ((op->e_o_s == 0) != (e_o_s == 0))
 
114
    FAIL ("Packet has incorrect e_o_s");
 
115
 
 
116
  if (op->granulepos != -1 && op->granulepos != iter)
 
117
    FAIL ("Packet has incorrect granulepos");
 
118
 
 
119
  if (op->packetno != iter)
 
120
    FAIL ("Packet has incorrect packetno");
 
121
 
 
122
  iter++;
 
123
  b_o_s = 0;
 
124
  if (iter == 10) e_o_s = 1;
 
125
 
 
126
  return OGGZ_STOP_OK;
 
127
}
 
128
 
 
129
static size_t
 
130
my_io_read (void * user_handle, void * buf, size_t n)
 
131
{
 
132
  unsigned char * data_buf = (unsigned char *)user_handle;
 
133
  static int offset = 0;
 
134
  int len;
 
135
 
 
136
  /* Mark that the read IO method was actually used */
 
137
  read_called++;
 
138
 
 
139
  len = MIN (n, bytes_generated - offset);
 
140
 
 
141
  memcpy (buf, &data_buf[offset], len);
 
142
 
 
143
  offset += len;
 
144
 
 
145
  if (len == 0) eof_reached = 1;
 
146
 
 
147
  return len;
 
148
}
 
149
 
 
150
int
 
151
main (int argc, char * argv[])
 
152
{
 
153
  OGGZ * reader, * writer;
 
154
  unsigned char data_buf[DATA_BUF_LEN];
 
155
  long n, ret;
 
156
 
 
157
  INFO ("Testing override of IO reading");
 
158
 
 
159
  writer = oggz_new (OGGZ_WRITE);
 
160
  if (writer == NULL)
 
161
    FAIL("newly created OGGZ writer == NULL");
 
162
 
 
163
  serialno = oggz_serialno_new (writer);
 
164
 
 
165
  if (oggz_write_set_hungry_callback (writer, hungry, 1, NULL) == -1)
 
166
    FAIL("Could not set hungry callback");
 
167
 
 
168
  reader = oggz_new (OGGZ_READ);
 
169
  if (reader == NULL)
 
170
    FAIL("newly created OGGZ reader == NULL");
 
171
 
 
172
  oggz_io_set_read (reader, my_io_read, data_buf);
 
173
 
 
174
  oggz_set_read_callback (reader, -1, read_packet, NULL);
 
175
 
 
176
  n = oggz_write_output (writer, data_buf, DATA_BUF_LEN);
 
177
 
 
178
  if (n >= DATA_BUF_LEN)
 
179
    FAIL("Too much data generated by writer");
 
180
 
 
181
  bytes_generated = n;
 
182
 
 
183
  do {
 
184
    ret = oggz_read (reader, 4);
 
185
  } while (ret > 0 || ret == OGGZ_ERR_READ_STOP_OK);
 
186
 
 
187
  if (ret != 0) FAIL ("oggz_read didn't reach EOF");
 
188
 
 
189
  if (!eof_reached) FAIL ("oggz_read returned 0 before EOF");
 
190
 
 
191
  if (read_called == 0)
 
192
    FAIL("Read method ignored");
 
193
 
 
194
  if (oggz_close (reader) != 0)
 
195
    FAIL("Could not close OGGZ reader");
 
196
 
 
197
  if (oggz_close (writer) != 0)
 
198
    FAIL("Could not close OGGZ writer");
 
199
 
 
200
  exit (0);
 
201
}