i18n: New functions for truncating strings in an arbitrary encoding.
[pspp-builds.git] / tests / libpspp / i18n-test.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2010, 2011 Free Software Foundation, Inc.
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 3 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, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include "libpspp/i18n.h"
24
25 #undef NDEBUG
26 #include <assert.h>
27
28 int
29 main (int argc, char *argv[])
30 {
31   i18n_init ();
32
33   if (argc == 5 && !strcmp (argv[1], "recode"))
34     {
35       const char *from = argv[2];
36       const char *to = argv[3];
37       const char *string = argv[4];
38       char *result = recode_string (to, from, string, -1);
39       puts (result);
40       assert (strlen (result) == recode_string_len (to, from, string, -1));
41       free (result);
42     }
43   else if (argc == 6 && !strcmp (argv[1], "concat"))
44     {
45       const char *head = argv[2];
46       const char *tail = argv[3];
47       const char *encoding = argv[4];
48       int max_len = atoi (argv[5]);
49       char *result;
50
51       result = utf8_encoding_concat (head, tail, encoding, max_len);
52       puts (result);
53
54       assert (strlen (result)
55               == utf8_encoding_concat_len (head, tail, encoding, max_len));
56
57       if (tail[0] == '\0')
58         {
59           char *result2 = utf8_encoding_trunc (head, encoding, max_len);
60           assert (!strcmp (result, result2));
61           assert (strlen (result2)
62                   == utf8_encoding_trunc_len (head, encoding, max_len));
63           free (result2);
64         }
65
66       free (result);
67     }
68   else
69     {
70       fprintf (stderr, "\
71 usage: %s recode FROM TO STRING\n\
72 where FROM is the source encoding,\n\
73       TO is the target encoding,\n\
74       and STRING is the text to recode.\n\
75 \n\
76 usage: %s concat HEAD TAIL ENCODING MAX_LEN\n\
77 where HEAD is the first string to concatenate\n\
78       TAIL is the second string to concatenate\n\
79       ENCODING is the encoding in which to measure the result's length\n\
80       MAX_LEN is the maximum length of the result in ENCODING.\n",
81                argv[0], argv[0]);
82       return EXIT_FAILURE;
83     }
84
85   i18n_done ();
86
87   return 0;
88 }