annotate src/libsndfile-1.0.27/tests/locale_test.c @ 83:ae30d91d2ffe

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