Merge branch 'master' of https://scm.cri.ensmp.fr/git/Faustine
[Faustine.git] / interpretor / lib / src / libsndfile-1.0.25 / tests / locale_test.c
1 /*
2 ** Copyright (C) 2005-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
3 **
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU General Public License as published by
6 ** the Free Software Foundation; either version 2 of the License, or
7 ** (at your option) any later version.
8 **
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ** GNU General Public License for more details.
13 **
14 ** You should have received a copy of the GNU General Public License
15 ** along with this program; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #include "sfconfig.h"
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #if HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28
29 #if HAVE_LOCALE_H
30 #include <locale.h>
31 #endif
32
33 #if OS_IS_WIN32
34 #include <windows.h>
35 #define ENABLE_SNDFILE_WINDOWS_PROTOTYPES 1
36 #endif
37
38 #include "sndfile.h"
39 #include "utils.h"
40
41 static void utf8_test (void) ;
42 static void wchar_test (void) ;
43
44 int
45 main (void)
46 {
47 utf8_test () ;
48 wchar_test () ;
49
50 return 0 ;
51 } /* main */
52
53 /*==============================================================================
54 */
55
56 static void
57 wchar_test (void)
58 {
59 #if OS_IS_WIN32
60 SNDFILE * file ;
61 SF_INFO info ;
62 LPCWSTR filename = L"test.wav" ;
63
64 print_test_name (__func__, "test.wav") ;
65
66 info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ;
67 info.channels = 1 ;
68 info.samplerate = 44100 ;
69
70 file = sf_wchar_open (filename, SFM_WRITE, &info) ;
71 exit_if_true (file == NULL, "\n\nLine %d : sf_wchar_open failed : %s\n\n", __LINE__, sf_strerror (NULL)) ;
72 sf_close (file) ;
73
74 /* This should check that the file did in fact get created with a
75 ** wchar_t * filename.
76 */
77 exit_if_true (
78 GetFileAttributesW (filename) == INVALID_FILE_ATTRIBUTES,
79 "\n\nLine %d : GetFileAttributes failed.\n\n", __LINE__
80 ) ;
81
82 /* Use this because the file was created with CreateFileW. */
83 DeleteFileW (filename) ;
84
85 puts ("ok") ;
86 #endif
87 } /* wchar_test */
88
89 /*==============================================================================
90 */
91
92 typedef struct
93 { const char *locale ;
94 int utf8 ;
95 const char *filename ;
96 int width ;
97 } LOCALE_DATA ;
98
99 static void locale_test (const LOCALE_DATA * locdata) ;
100
101 static void
102 utf8_test (void)
103 { LOCALE_DATA ldata [] =
104 { { "de_DE", 1, "F\303\274\303\237e.au", 7 },
105 { "en_AU", 1, "kangaroo.au", 11 },
106 { "POSIX", 0, "posix.au", 8 },
107 { "pt_PT", 1, "concei\303\247\303\243o.au", 12 },
108
109 #if OS_IS_WIN32 == 0
110 { "ja_JP", 1, "\343\201\212\343\201\257\343\202\210\343\201\206\343\201\224\343\201\226\343\201\204\343\201\276\343\201\231.au", 21 },
111 #endif
112
113 { "vi_VN", 1, "qu\341\273\221c ng\341\273\257.au", 11 },
114 { NULL, 0, NULL, 0 }
115 } ;
116 int k ;
117
118 for (k = 0 ; ldata [k].locale != NULL ; k++)
119 locale_test (ldata + k) ;
120 } /* utf8_test */
121
122
123 static void
124 locale_test (const LOCALE_DATA * ldata)
125 {
126 #if (HAVE_LOCALE_H == 0 || HAVE_SETLOCALE == 0)
127 locname = filename = NULL ;
128 width = 0 ;
129 return ;
130 #else
131 const short wdata [] = { 1, 2, 3, 4, 5, 6, 7, 8 } ;
132 short rdata [ARRAY_LEN (wdata)] ;
133 const char *old_locale ;
134 char utf8_locname [32] ;
135 SNDFILE *file ;
136 SF_INFO sfinfo ;
137
138 snprintf (utf8_locname, sizeof (utf8_locname), "%s%s", ldata->locale, ldata->utf8 ? ".UTF-8" : "") ;
139
140 /* Change the locale saving the old one. */
141 if ((old_locale = setlocale (LC_CTYPE, utf8_locname)) == NULL)
142 return ;
143
144 printf (" locale_test %-8s : %s %*c ", ldata->locale, ldata->filename, 24 - ldata->width, ' ') ;
145 fflush (stdout) ;
146
147 sfinfo.format = SF_FORMAT_AU | SF_FORMAT_PCM_16 ;
148 sfinfo.channels = 1 ;
149 sfinfo.samplerate = 44100 ;
150
151 file = test_open_file_or_die (ldata->filename, SFM_WRITE, &sfinfo, 0, __LINE__) ;
152 test_write_short_or_die (file, 0, wdata, ARRAY_LEN (wdata), __LINE__) ;
153 sf_close (file) ;
154
155 file = test_open_file_or_die (ldata->filename, SFM_READ, &sfinfo, 0, __LINE__) ;
156 test_read_short_or_die (file, 0, rdata, ARRAY_LEN (rdata), __LINE__) ;
157 sf_close (file) ;
158
159 unlink (ldata->filename) ;
160
161 /* Restore old locale. */
162 setlocale (LC_CTYPE, old_locale) ;
163
164 puts ("ok") ;
165 #endif
166 } /* locale_test */
167