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
31 #include <stringprep.h>
34 static int error_count
= 0;
35 static int break_on_error
= 0;
38 fail (const char *format
, ...)
42 va_start (arg_ptr
, format
);
43 vfprintf (stderr
, format
, arg_ptr
);
51 escapeprint (char *str
, int len
)
56 for (i
= 0; i
< len
; i
++)
58 str
[i
] = str
[i
] & 0xFF;
59 if ((str
[i
] >= 'A' && str
[i
] <= 'Z') ||
60 (str
[i
] >= 'a' && str
[i
] <= 'z') ||
61 (str
[i
] >= '0' && str
[i
] <= '9') || str
[i
] == '.')
62 printf ("%c", str
[i
]);
64 printf ("\\x%02x", str
[i
]);
66 printf ("' (length %d bytes)\n", len
);
70 hexprint (char *str
, int len
)
75 for (i
= 0; i
< len
; i
++)
77 str
[i
] = str
[i
] & 0xFF;
78 printf ("%02x ", str
[i
]);
81 if ((i
+ 1) % 16 == 0 && i
+ 1 < len
)
87 binprint (char *str
, int len
)
92 for (i
= 0; i
< len
; i
++)
94 str
[i
] = str
[i
] & 0xFF;
95 printf ("%d%d%d%d%d%d%d%d ",
96 str
[i
] & 0x80 ? 1 : 0,
97 str
[i
] & 0x40 ? 1 : 0,
98 str
[i
] & 0x20 ? 1 : 0,
99 str
[i
] & 0x10 ? 1 : 0,
100 str
[i
] & 0x08 ? 1 : 0,
101 str
[i
] & 0x04 ? 1 : 0,
102 str
[i
] & 0x02 ? 1 : 0, str
[i
] & 0x01 ? 1 : 0);
103 if ((i
+ 1) % 3 == 0)
105 if ((i
+ 1) % 6 == 0 && i
+ 1 < len
)
116 const struct nfkc nfkc
[] = {
117 {"\xC2\xB5", "\xCE\xBC"},
122 main (int argc
, char *argv
[])
128 if (strcmp (argv
[argc
- 1], "-v") == 0 ||
129 strcmp (argv
[argc
- 1], "--verbose") == 0)
131 else if (strcmp (argv
[argc
- 1], "-b") == 0 ||
132 strcmp (argv
[argc
- 1], "--break-on-error") == 0)
134 else if (strcmp (argv
[argc
- 1], "-h") == 0 ||
135 strcmp (argv
[argc
- 1], "-?") == 0 ||
136 strcmp (argv
[argc
- 1], "--help") == 0)
138 printf ("Usage: %s [-vbh?] [--verbose] [--break-on-error] [--help]\n",
144 for (i
= 0; i
< sizeof (nfkc
) / sizeof (nfkc
[0]); i
++)
147 printf ("NFKC entry %d\n", i
);
149 out
= stringprep_utf8_nfkc_normalize (nfkc
[i
].in
, strlen (nfkc
[i
].in
));
152 fail ("NFKC entry %d failed fatally\n", i
);
159 escapeprint (nfkc
[i
].in
, strlen (nfkc
[i
].in
));
160 hexprint (nfkc
[i
].in
, strlen (nfkc
[i
].in
));
162 binprint (nfkc
[i
].in
, strlen (nfkc
[i
].in
));
166 escapeprint (out
, strlen (out
));
167 hexprint (out
, strlen (out
));
169 binprint (out
, strlen (out
));
172 printf ("expected out:\n");
173 escapeprint (nfkc
[i
].out
, strlen (nfkc
[i
].out
));
174 hexprint (nfkc
[i
].out
, strlen (nfkc
[i
].out
));
176 binprint (nfkc
[i
].out
, strlen (nfkc
[i
].out
));
180 if (strlen (nfkc
[i
].out
) != strlen (out
) ||
181 memcmp (nfkc
[i
].out
, out
, strlen (out
)) != 0)
183 fail ("NFKC entry %d failed\n", i
);
194 printf ("NFKC self tests done with %d errors\n", error_count
);
196 return error_count
? 1 : 0;