Skip to content
This repository
Browse code

added test for writing/reading multiple chunks of the same ID

  • Loading branch information...
commit 8c062feddd0e0cebd1307f0574d43dc28ecc6b62 1 parent 45160f0
umläute authored March 16, 2012

Showing 1 changed file with 101 additions and 0 deletions. Show diff stats Hide diff stats

  1. 101  tests/chunk_test.c
101  tests/chunk_test.c
@@ -164,6 +164,105 @@ chunk_test_helper (const char *filename, int typemajor, const char * testdata)
164 164
 	sf_close (file) ;
165 165
 	unlink (filename) ;
166 166
 } /* chunk_test_helper */
  167
+static void
  168
+multichunk_test_helper (const char *filename, int typemajor, const char * testdata [], size_t testdata_len)
  169
+{	SNDFILE			*file ;
  170
+	SF_INFO			sfinfo ;
  171
+	SF_CHUNK_INFO	chunk_info ;
  172
+	SF_CHUNK_ITERATOR * iterator ;
  173
+	uint32_t		* length_before ;
  174
+	int				err ;
  175
+	size_t		i ;
  176
+
  177
+	length_before = calloc (testdata_len, sizeof (*length_before)) ;
  178
+
  179
+	sfinfo.samplerate	= 44100 ;
  180
+	sfinfo.channels		= 1 ;
  181
+	sfinfo.frames		= 0 ;
  182
+
  183
+	switch (typemajor)
  184
+	{	case SF_FORMAT_OGG :
  185
+			sfinfo.format = typemajor | SF_FORMAT_VORBIS ;
  186
+			break ;
  187
+
  188
+		default :
  189
+			sfinfo.format = typemajor | SF_FORMAT_PCM_16 ;
  190
+			break ;
  191
+		} ;
  192
+
  193
+	file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
  194
+
  195
+	/* Set up the chunk to write. */
  196
+	for (i = 0 ; i < testdata_len ; i++)
  197
+	{	memset (&chunk_info, 0, sizeof (chunk_info)) ;
  198
+		snprintf (chunk_info.id, sizeof (chunk_info.id), "Test") ;
  199
+		chunk_info.id_size = 4 ;
  200
+
  201
+		chunk_info.data = strdup (testdata [i]) ;
  202
+		chunk_info.datalen = strlen (chunk_info.data) ;
  203
+
  204
+		length_before [i] = chunk_info.datalen ;
  205
+
  206
+		err = sf_set_chunk (file, &chunk_info) ;
  207
+		exit_if_true (
  208
+			err != SF_ERR_NO_ERROR,
  209
+			"\n\nLine %d : sf_set_chunk returned for testdata[%d] '%s' : %s\n\n", __LINE__, (int) i, testdata [i], sf_error_number (err)
  210
+			) ;
  211
+
  212
+		memset (chunk_info.data, 0, chunk_info.datalen) ;
  213
+		free (chunk_info.data) ;
  214
+	}
  215
+
  216
+	sf_close (file) ;
  217
+
  218
+	file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
  219
+
  220
+	memset (&chunk_info, 0, sizeof (chunk_info)) ;
  221
+	snprintf (chunk_info.id, sizeof (chunk_info.id), "Test") ;
  222
+	chunk_info.id_size = 4 ;
  223
+
  224
+	iterator = sf_create_chunk_iterator (file, &chunk_info) ;
  225
+
  226
+	i = 0 ;
  227
+	while (iterator)
  228
+	{	err = sf_get_chunk_size (iterator, &chunk_info) ;
  229
+		exit_if_true (
  230
+			i > testdata_len,
  231
+			"\n\nLine %d : iterated to chunk #%d, but only %d chunks have been written\n\n", __LINE__, (int) i, (int) testdata_len
  232
+			) ;
  233
+
  234
+		exit_if_true (
  235
+			err != SF_ERR_NO_ERROR,
  236
+			"\n\nLine %d : sf_get_chunk_size returned for testdata[%d] '%s' : %s\n\n", __LINE__, (int) i, testdata [i], sf_error_number (err)
  237
+			) ;
  238
+
  239
+		exit_if_true (
  240
+			length_before [i] > chunk_info.datalen || chunk_info.datalen - length_before [i] > 4,
  241
+			"\n\nLine %d : testdata[%d] '%s' : Bad chunk length %u (previous length %u)\n\n", __LINE__, (int) i, testdata [i], chunk_info.datalen, length_before [i]
  242
+			) ;
  243
+
  244
+		chunk_info.data = malloc (chunk_info.datalen) ;
  245
+		err = sf_get_chunk_data (iterator, &chunk_info) ;
  246
+		exit_if_true (
  247
+			err != SF_ERR_NO_ERROR,
  248
+			"\n\nLine %d : sf_get_chunk_size returned for testdata[%d] '%s' : %s\n\n", __LINE__, (int) i, testdata [i], sf_error_number (err)
  249
+			) ;
  250
+
  251
+		exit_if_true (
  252
+			memcmp (testdata [i], chunk_info.data, length_before [i]),
  253
+			"\n\nLine %d : Data compare failed at %d.\n    %s\n    %s\n\n", __LINE__, (int) i, testdata [i], (char*) chunk_info.data
  254
+			) ;
  255
+
  256
+		free (chunk_info.data) ;
  257
+		iterator = sf_next_chunk_iterator (iterator) ;
  258
+		i++ ;
  259
+	}
  260
+
  261
+	sf_close (file) ;
  262
+	free (length_before) ;
  263
+	unlink (filename) ;
  264
+} /* multichunk_test_helper */
  265
+
167 266
 
168 267
 static void
169 268
 chunk_test (const char *filename, int typemajor)
@@ -176,5 +275,7 @@ chunk_test (const char *filename, int typemajor)
176 275
 	for (k = 0 ; k < ARRAY_LEN (testdata) ; k++)
177 276
 		chunk_test_helper (filename, typemajor, testdata [k]) ;
178 277
 
  278
+	multichunk_test_helper (filename, typemajor, testdata, ARRAY_LEN (testdata)) ;
  279
+
179 280
 	puts ("ok") ;
180 281
 } /* chunk_test */

0 notes on commit 8c062fe

Please sign in to comment.
Something went wrong with that request. Please try again.