1 /* tst_nfkc.c Self tests for stringprep_utf8_nfkc_normalize().
2 * Copyright (C) 2002, 2003 Simon Josefsson
4 * This file is part of GNU Libidn.
6 * GNU Libidn 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 2 of the License, or
9 * (at your option) any later version.
11 * GNU Libidn 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.
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Libidn; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 static int error_count
= 0;
26 static int break_on_error
= 0;
29 fail (const char *format
, ...)
33 va_start (arg_ptr
, format
);
34 vfprintf (stderr
, format
, arg_ptr
);
42 escapeprint (char *str
, int len
)
47 for (i
= 0; i
< len
; i
++)
49 str
[i
] = str
[i
] & 0xFF;
50 if ((str
[i
] >= 'A' && str
[i
] <= 'Z') ||
51 (str
[i
] >= 'a' && str
[i
] <= 'z') ||
52 (str
[i
] >= '0' && str
[i
] <= '9') || str
[i
] == '.')
53 printf ("%c", str
[i
]);
55 printf ("\\x%02x", str
[i
]);
57 printf ("' (length %d bytes)\n", len
);
61 hexprint (char *str
, int len
)
66 for (i
= 0; i
< len
; i
++)
68 str
[i
] = str
[i
] & 0xFF;
69 printf ("%02x ", str
[i
]);
72 if ((i
+ 1) % 16 == 0 && i
+ 1 < len
)
78 binprint (char *str
, int len
)
83 for (i
= 0; i
< len
; i
++)
85 str
[i
] = str
[i
] & 0xFF;
86 printf ("%d%d%d%d%d%d%d%d ",
87 str
[i
] & 0x80 ? 1 : 0,
88 str
[i
] & 0x40 ? 1 : 0,
89 str
[i
] & 0x20 ? 1 : 0,
90 str
[i
] & 0x10 ? 1 : 0,
91 str
[i
] & 0x08 ? 1 : 0,
92 str
[i
] & 0x04 ? 1 : 0,
93 str
[i
] & 0x02 ? 1 : 0, str
[i
] & 0x01 ? 1 : 0);
96 if ((i
+ 1) % 6 == 0 && i
+ 1 < len
)
109 "\xC2\xB5", "\xCE\xBC"}
116 main (int argc
, char *argv
[])
122 if (strcmp (argv
[argc
- 1], "-v") == 0 ||
123 strcmp (argv
[argc
- 1], "--verbose") == 0)
125 else if (strcmp (argv
[argc
- 1], "-b") == 0 ||
126 strcmp (argv
[argc
- 1], "--break-on-error") == 0)
128 else if (strcmp (argv
[argc
- 1], "-h") == 0 ||
129 strcmp (argv
[argc
- 1], "-?") == 0 ||
130 strcmp (argv
[argc
- 1], "--help") == 0)
132 printf ("Usage: %s [-vbh?] [--verbose] [--break-on-error] [--help]\n",
138 for (i
= 0; i
< sizeof (nfkc
) / sizeof (nfkc
[0]); i
++)
141 printf ("NFKC entry %d\n", i
);
143 out
= stringprep_utf8_nfkc_normalize (nfkc
[i
].in
, strlen (nfkc
[i
].in
));
146 fail ("NFKC entry %d failed fatally\n", i
);
153 escapeprint (nfkc
[i
].in
, strlen (nfkc
[i
].in
));
154 hexprint (nfkc
[i
].in
, strlen (nfkc
[i
].in
));
156 binprint (nfkc
[i
].in
, strlen (nfkc
[i
].in
));
160 escapeprint (out
, strlen (out
));
161 hexprint (out
, strlen (out
));
163 binprint (out
, strlen (out
));
166 printf ("expected out:\n");
167 escapeprint (nfkc
[i
].out
, strlen (nfkc
[i
].out
));
168 hexprint (nfkc
[i
].out
, strlen (nfkc
[i
].out
));
170 binprint (nfkc
[i
].out
, strlen (nfkc
[i
].out
));
174 if (strlen (nfkc
[i
].out
) != strlen (out
) ||
175 memcmp (nfkc
[i
].out
, out
, strlen (out
)) != 0)
177 fail ("NFKC entry %d failed\n", i
);
188 printf ("NFKC self tests done with %d errors\n", error_count
);
190 return error_count
? 1 : 0;