i18n: New functions for truncating strings in an arbitrary encoding.
[pspp-builds.git] / tests / libpspp / i18n.at
1 AT_BANNER([i18n recoding])
2
3 m4_divert_push([PREPARE_TESTS])
4 supports_encodings () {
5   case "$host" in
6     *-*-linux* | *-*-*-gnu*)
7       dnl GNU/Linux always has the encodings we want.  We can't ask
8       dnl config.charset about them because it has a special case here
9       dnl too and won't tell us.
10       return 0
11       ;;
12     *)
13       for encoding in "$@"; do
14         $SHELL $top_srcdir/gl/config.charset "$host" | grep '$2' || return 77
15       done
16       ;;
17   esac
18 }
19 m4_divert_pop([PREPARE_TESTS])
20
21 # CHECK_I18N_RECODE([TITLE], [FROM-CODING], [TO-CODING],
22 #                   [FROM-TEXT], [TO-TEXT])
23 #
24 # Converts FROM-TEXT from FROM-CODING to TO-CODING and checks that the result
25 # is TO-TEXT.  The "printf" program is applied to both FROM-TEXT and TO-TEXT to
26 # allow for backslash-escapes.  (Hex escapes are not portable; use octal
27 # escapes instead.)
28 m4_define([CHECK_I18N_RECODE],
29   [AT_SETUP([convert $1])
30    AT_KEYWORDS([i18n])
31
32    dnl Skip the test if this host doesn't know the source and target encodings.
33    AT_CHECK([supports_encodings '$2' '$3'])
34    AT_CHECK_UNQUOTED([i18n-test recode '$2' '$3' `printf '$4'`], [0], [`printf '$5'`
35 ])
36    AT_CLEANUP])
37      
38 CHECK_I18N_RECODE([reflexively], [ASCII], [ASCII], [abc], [abc])
39 CHECK_I18N_RECODE([without any change], [ASCII], [UTF-8], [abc], [abc])
40
41 CHECK_I18N_RECODE([from ISO-8859-1 to UTF-8], [ISO-8859-1], [UTF-8],
42                   [\242], [\302\242])
43 CHECK_I18N_RECODE([from UTF-8 to ISO-8859-1], [UTF-8], [ISO-8859-1],
44                   [\302\242], [\242])
45
46 # 0xc0 == 0300 is invalid in UTF-8
47 CHECK_I18N_RECODE([invalid UTF-8 to ISO-8859-1], [UTF-8], [ISO-8859-1],
48                   [xy\300z], [xy?z])
49 # 0xc2 == 0302 is the first byte of a 2-byte UTF-8 sequence
50 CHECK_I18N_RECODE([truncated UTF-8 to ISO-8559-1], [UTF-8], [ISO-8859-1],
51                   [xy\302], [xy?])
52
53 dnl The input to this test is 7 bytes long and the expected output is 9 bytes.
54 dnl So it should exercise the E2BIG case 
55 CHECK_I18N_RECODE([from ISO-8859-1 to UTF-8 with overflow], 
56                   [ISO-8859-1], [UTF-8],
57                   [Tsch\374\337!], [Tsch\303\274\303\237!])
58 \f
59 AT_BANNER([i18n concatenation])
60
61 # CHECK_I18N_CONCAT([HEAD], [TAIL], [ENCODING], [MAX-LEN], [ANSWER])
62 #
63 # Concatenates HEAD and TAIL, omitting as many characters from HEAD as needed
64 # to make the result come out to no more than MAX-LEN bytes if it was expressed
65 # in ENCODING, and checks that the answer matches ANSWER.  HEAD, TAIL, and
66 # ANSWER are all in UTF-8.  The "printf" program is applied to HEAD, TAIL, and
67 # ANSWER to allow for backslash-escapes.  (Hex escapes are not portable; use
68 # octal escapes instead.)
69 m4_define([CHECK_I18N_CONCAT],
70   [AT_SETUP([m4_if([$2], [], [truncate "$1" to $4 bytes in $3],
71                              [truncate "$1" + "$2" to $4 bytes in $3])])
72    AT_KEYWORDS([i18n])
73
74    dnl Skip the test if this host doesn't know the encoding.
75    AT_CHECK([supports_encodings '$3'])
76    AT_CHECK_UNQUOTED(
77      [i18n-test concat "`printf '$1'`" "`printf '$2'`" '$3' '$4'], [0],
78      [`printf '$5'`
79 ])
80    AT_CLEANUP])
81
82 CHECK_I18N_CONCAT([abc], [], [UTF-8], [6], [abc])
83 CHECK_I18N_CONCAT([], [xyz], [UTF-8], [6], [xyz])
84 CHECK_I18N_CONCAT([], [], [UTF-8], [6], [])
85 CHECK_I18N_CONCAT([abcdefghij], [], [UTF-8], [6], [abcdef])
86 CHECK_I18N_CONCAT([], [tuvwxyz], [UTF-8], [6], [tuvwxyz])
87
88 CHECK_I18N_CONCAT([abc], [xyz], [UTF-8], [6], [abcxyz])
89 CHECK_I18N_CONCAT([abcd], [xyz], [UTF-8], [6], [abcxyz])
90 CHECK_I18N_CONCAT([abc], [uvwxyz], [UTF-8], [6], [uvwxyz])
91
92 # x in a box ( x⃞ ) is U+0078, U+20DE, 4 bytes in UTF-8, and one grapheme
93 # cluster.
94 CHECK_I18N_CONCAT([x\342\203\236], [y], [UTF-8], [0], [y])
95 CHECK_I18N_CONCAT([x\342\203\236], [y], [UTF-8], [1], [y])
96 CHECK_I18N_CONCAT([x\342\203\236], [y], [UTF-8], [2], [y])
97 CHECK_I18N_CONCAT([x\342\203\236], [y], [UTF-8], [3], [y])
98 CHECK_I18N_CONCAT([x\342\203\236], [y], [UTF-8], [4], [y])
99 CHECK_I18N_CONCAT([x\342\203\236], [y], [UTF-8], [5], [x\342\203\236y])
100
101 # éèä is only 3 bytes in ISO-8859-1.
102 CHECK_I18N_CONCAT([\303\251\303\250\303\244], [xyz], [ISO-8859-1], [0], [xyz])
103 CHECK_I18N_CONCAT([\303\251\303\250\303\244], [xyz], [ISO-8859-1], [1], [xyz])
104 CHECK_I18N_CONCAT([\303\251\303\250\303\244], [xyz], [ISO-8859-1], [2], [xyz])
105 CHECK_I18N_CONCAT([\303\251\303\250\303\244], [xyz], [ISO-8859-1], [3], [xyz])
106 CHECK_I18N_CONCAT([\303\251\303\250\303\244], [xyz], [ISO-8859-1], [4], 
107                   [\303\251xyz])
108 CHECK_I18N_CONCAT([\303\251\303\250\303\244], [xyz], [ISO-8859-1], [5],
109                   [\303\251\303\250xyz])
110 CHECK_I18N_CONCAT([\303\251\303\250\303\244], [xyz], [ISO-8859-1], [6],
111                   [\303\251\303\250\303\244xyz])