summaryrefslogtreecommitdiff
path: root/tests/format_check_test.c
blob: a371c71ff764c25485a95a6f77c43936be5f4825 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
** Copyright (C) 2011 Erik de Castro Lopo <erikd@mega-nerd.com>
**
** 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include "sfconfig.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if HAVE_UNISTD_H
#include <unistd.h>
#endif

#include "sndfile.h"
#include "utils.h"

static void format_error_test (void) ;
static void format_combo_test (void) ;

int
main (void)
{
	format_error_test () ;
	format_combo_test () ;

	return 0 ;
} /* main */

/*==============================================================================
*/

static void
format_error_test (void)
{	const char *filename = "format-error.wav" ;
	SNDFILE *file ;
	SF_INFO info ;

	print_test_name (__func__, NULL) ;

	memset (&info, 0, sizeof (info)) ;
	info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ;
	info.channels = 1 ;
	info.samplerate = 44100 ;

	info.format = SF_FORMAT_WAV ;
	file = sf_open (filename, SFM_WRITE, &info) ;
	exit_if_true (file != NULL, "\n\nLine %d : Format should not be valid.\n\n", __LINE__) ;
	exit_if_true (
		strstr (sf_strerror (NULL), "minor format") == NULL,
		"\n\nLine %d : Error string should reference bad 'minor format'.\n\n", __LINE__
		) ;

	info.format = SF_FORMAT_PCM_16 ;
	file = sf_open (filename, SFM_WRITE, &info) ;
	exit_if_true (file != NULL, "\n\nLine %d : Format should not be valid.\n\n", __LINE__) ;
	exit_if_true (
		strstr (sf_strerror (NULL), "major format") == NULL,
		"\n\nLine %d : Error string should reference bad 'major format'.\n\n", __LINE__
		) ;

	unlink (filename) ;
	puts ("ok") ;
} /* format_error_test */

static void
format_combo_test (void)
{	int container_max, codec_max, cont, codec ;

	print_test_name (__func__, NULL) ;

	sf_command (NULL, SFC_GET_FORMAT_MAJOR_COUNT, &container_max, sizeof (container_max)) ;
	sf_command (NULL, SFC_GET_FORMAT_SUBTYPE_COUNT, &codec_max, sizeof (codec_max)) ;

	for (cont = 0 ; cont < container_max + 10 ; cont ++)
	{	SF_FORMAT_INFO major_fmt_info ;

		memset (&major_fmt_info, 0, sizeof (major_fmt_info)) ;
		major_fmt_info.format = cont ;
		(void) sf_command (NULL, SFC_GET_FORMAT_MAJOR, &major_fmt_info, sizeof (major_fmt_info)) ;

		for (codec = 0 ; codec < codec_max + 10 ; codec ++)
		{	SF_FORMAT_INFO subtype_fmt_info ;
			SNDFILE * sndfile ;
			SF_INFO info ;
			char filename [128] ;
			int subtype_is_valid, check_is_valid ;

			memset (&subtype_fmt_info, 0, sizeof (subtype_fmt_info)) ;
			subtype_fmt_info.format = codec ;
			subtype_is_valid = sf_command (NULL, SFC_GET_FORMAT_SUBTYPE, &subtype_fmt_info, sizeof (subtype_fmt_info)) == 0 ;

			sf_info_setup (&info, major_fmt_info.format | subtype_fmt_info.format, 22050, 1) ;

			check_is_valid = sf_format_check (&info) ;

			exit_if_true (
				NOT (subtype_is_valid) && check_is_valid,
				"\n\nLine %d : Subtype is not valid but checks ok.\n",
				__LINE__
				) ;

			snprintf (filename, sizeof (filename), "format-check.%s", major_fmt_info.extension) ;

			sndfile = sf_open (filename, SFM_WRITE, &info) ;

			sf_close (sndfile) ;
			unlink (filename) ;

			if (major_fmt_info.extension != NULL && strcmp (major_fmt_info.extension, "sd2") == 0)
			{	snprintf (filename, sizeof (filename), "._format-check.%s", major_fmt_info.extension) ;
				unlink (filename) ;
				} ;

			exit_if_true (
				sndfile && NOT (check_is_valid),
				"\n\nError : Format was not valid but file opened correctly.\n"
				"    Container : %s\n"
				"    Codec     : %s\n\n",
				major_fmt_info.name, subtype_fmt_info.name
				) ;

			exit_if_true (
				NOT (sndfile) && check_is_valid,
				"\n\nError : Format was valid but file failed to open.\n"
				"    Container : %s\n"
				"    Codec     : %s\n\n",
				major_fmt_info.name, subtype_fmt_info.name
				) ;
			} ;
		} ;

	puts ("ok") ;
} /* format_combo_test */