~ubuntu-branches/ubuntu/natty/diffutils/natty

« back to all changes in this revision

Viewing changes to gnulib-tests/test-striconv.c

  • Committer: Bazaar Package Importer
  • Author(s): Santiago Vila
  • Date: 2010-05-04 20:38:00 UTC
  • mfrom: (2.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20100504203800-f67xd9rsa9xl9qqj
Tags: 1:3.0-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- buffer-read-only: t -*- vi: set ro: */
 
2
/* DO NOT EDIT! GENERATED AUTOMATICALLY! */
 
3
/* Test of character set conversion.
 
4
   Copyright (C) 2007-2010 Free Software Foundation, Inc.
 
5
 
 
6
   This program is free software: you can redistribute it and/or modify
 
7
   it under the terms of the GNU General Public License as published by
 
8
   the Free Software Foundation; either version 3 of the License, or
 
9
   (at your option) any later version.
 
10
 
 
11
   This program is distributed in the hope that it will be useful,
 
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
   GNU General Public License for more details.
 
15
 
 
16
   You should have received a copy of the GNU General Public License
 
17
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
18
 
 
19
/* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
 
20
 
 
21
#include <config.h>
 
22
 
 
23
#include "striconv.h"
 
24
 
 
25
#if HAVE_ICONV
 
26
# include <iconv.h>
 
27
#endif
 
28
 
 
29
#include <errno.h>
 
30
#include <stdlib.h>
 
31
#include <string.h>
 
32
 
 
33
#include "macros.h"
 
34
 
 
35
int
 
36
main ()
 
37
{
 
38
#if HAVE_ICONV
 
39
  /* Assume that iconv() supports at least the encodings ASCII, ISO-8859-1,
 
40
     and UTF-8.  */
 
41
  iconv_t cd_88591_to_utf8 = iconv_open ("UTF-8", "ISO-8859-1");
 
42
  iconv_t cd_utf8_to_88591 = iconv_open ("ISO-8859-1", "UTF-8");
 
43
 
 
44
  ASSERT (cd_88591_to_utf8 != (iconv_t)(-1));
 
45
  ASSERT (cd_utf8_to_88591 != (iconv_t)(-1));
 
46
 
 
47
  /* ------------------------- Test mem_cd_iconv() ------------------------- */
 
48
 
 
49
  /* Test conversion from ISO-8859-1 to UTF-8 with no errors.  */
 
50
  {
 
51
    static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
 
52
    static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
 
53
    char *result = NULL;
 
54
    size_t length = 0;
 
55
    int retval = mem_cd_iconv (input, strlen (input), cd_88591_to_utf8,
 
56
                               &result, &length);
 
57
    ASSERT (retval == 0);
 
58
    ASSERT (length == strlen (expected));
 
59
    ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
 
60
    free (result);
 
61
  }
 
62
 
 
63
  /* Test conversion from UTF-8 to ISO-8859-1 with no errors.  */
 
64
  {
 
65
    static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
 
66
    static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
 
67
    char *result = NULL;
 
68
    size_t length = 0;
 
69
    int retval = mem_cd_iconv (input, strlen (input), cd_utf8_to_88591,
 
70
                               &result, &length);
 
71
    ASSERT (retval == 0);
 
72
    ASSERT (length == strlen (expected));
 
73
    ASSERT (result != NULL && memcmp (result, expected, strlen (expected)) == 0);
 
74
    free (result);
 
75
  }
 
76
 
 
77
  /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ.  */
 
78
  {
 
79
    static const char input[] = "\342\202\254"; /* EURO SIGN */
 
80
    char *result = NULL;
 
81
    size_t length = 0;
 
82
    int retval = mem_cd_iconv (input, strlen (input), cd_utf8_to_88591,
 
83
                               &result, &length);
 
84
    ASSERT (retval == -1 && errno == EILSEQ);
 
85
    ASSERT (result == NULL);
 
86
  }
 
87
 
 
88
  /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL.  */
 
89
  {
 
90
    static const char input[] = "\342";
 
91
    char *result = NULL;
 
92
    size_t length = 0;
 
93
    int retval = mem_cd_iconv (input, strlen (input), cd_utf8_to_88591,
 
94
                               &result, &length);
 
95
    ASSERT (retval == 0);
 
96
    ASSERT (length == 0);
 
97
    free (result);
 
98
  }
 
99
 
 
100
  /* ------------------------- Test str_cd_iconv() ------------------------- */
 
101
 
 
102
  /* Test conversion from ISO-8859-1 to UTF-8 with no errors.  */
 
103
  {
 
104
    static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
 
105
    static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
 
106
    char *result = str_cd_iconv (input, cd_88591_to_utf8);
 
107
    ASSERT (result != NULL);
 
108
    ASSERT (strcmp (result, expected) == 0);
 
109
    free (result);
 
110
  }
 
111
 
 
112
  /* Test conversion from UTF-8 to ISO-8859-1 with no errors.  */
 
113
  {
 
114
    static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
 
115
    static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
 
116
    char *result = str_cd_iconv (input, cd_utf8_to_88591);
 
117
    ASSERT (result != NULL);
 
118
    ASSERT (strcmp (result, expected) == 0);
 
119
    free (result);
 
120
  }
 
121
 
 
122
  /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ.  */
 
123
  {
 
124
    static const char input[] = "Costs: 27 \342\202\254"; /* EURO SIGN */
 
125
    char *result = str_cd_iconv (input, cd_utf8_to_88591);
 
126
    ASSERT (result == NULL && errno == EILSEQ);
 
127
  }
 
128
 
 
129
  /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL.  */
 
130
  {
 
131
    static const char input[] = "\342";
 
132
    char *result = str_cd_iconv (input, cd_utf8_to_88591);
 
133
    ASSERT (result != NULL);
 
134
    ASSERT (strcmp (result, "") == 0);
 
135
    free (result);
 
136
  }
 
137
 
 
138
  iconv_close (cd_88591_to_utf8);
 
139
  iconv_close (cd_utf8_to_88591);
 
140
 
 
141
  /* -------------------------- Test str_iconv() -------------------------- */
 
142
 
 
143
  /* Test conversion from ISO-8859-1 to UTF-8 with no errors.  */
 
144
  {
 
145
    static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
 
146
    static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
 
147
    char *result = str_iconv (input, "ISO-8859-1", "UTF-8");
 
148
    ASSERT (result != NULL);
 
149
    ASSERT (strcmp (result, expected) == 0);
 
150
    free (result);
 
151
  }
 
152
 
 
153
  /* Test conversion from UTF-8 to ISO-8859-1 with no errors.  */
 
154
  {
 
155
    static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
 
156
    static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
 
157
    char *result = str_iconv (input, "UTF-8", "ISO-8859-1");
 
158
    ASSERT (result != NULL);
 
159
    ASSERT (strcmp (result, expected) == 0);
 
160
    free (result);
 
161
  }
 
162
 
 
163
  /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ.  */
 
164
  {
 
165
    static const char input[] = "Costs: 27 \342\202\254"; /* EURO SIGN */
 
166
    char *result = str_iconv (input, "UTF-8", "ISO-8859-1");
 
167
    ASSERT (result == NULL && errno == EILSEQ);
 
168
  }
 
169
 
 
170
  /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL.  */
 
171
  {
 
172
    static const char input[] = "\342";
 
173
    char *result = str_iconv (input, "UTF-8", "ISO-8859-1");
 
174
    ASSERT (result != NULL);
 
175
    ASSERT (strcmp (result, "") == 0);
 
176
    free (result);
 
177
  }
 
178
 
 
179
#endif
 
180
 
 
181
  return 0;
 
182
}