Initial release, version 0.0.0.
[gsasl.git] / tests / unicode.c
blobd452381f2a5c76b6f225ddeb1a00d90a894dd39d
1 /* unicode.c libgsasl self tests for unicode related functions
2 * Copyright (C) 2002 Simon Josefsson
4 * This file is part of libgsasl.
6 * Libgsasl 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 * Libgsasl 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 libgsasl; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <stdio.h>
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #ifdef HAVE_STDLIB_H
27 #include <stdlib.h>
28 #endif
29 #include <gsasl.h>
31 #include <stdarg.h>
33 static int debug = 0;
34 static int error_count = 0;
35 static int break_on_error = 0;
37 static void
38 fail ( const char *format, ... )
40 va_list arg_ptr ;
42 va_start( arg_ptr, format ) ;
43 vfprintf (stderr, format, arg_ptr );
44 va_end(arg_ptr);
45 error_count++;
46 if (break_on_error)
47 exit(1);
50 static void
51 escapeprint (char *str,
52 int len)
54 int i;
56 printf("\t ;; `");
57 for (i = 0; i < len; i++)
58 if ((str[i] >= 'A' && str[i] <= 'Z') ||
59 (str[i] >= 'a' && str[i] <= 'z') ||
60 (str[i] >= '0' && str[i] <= '9') ||
61 str[i] == '.')
62 printf("%c", str[i]);
63 else
64 printf("\\x%02x", str[i]);
65 printf("' (length %d bytes)\n", len);
68 static void
69 hexprint ( char *str,
70 int len)
72 int i;
74 printf("\t ;; ");
75 for (i = 0; i < len; i++)
77 printf("%02x ", str[i]);
78 if ((i+1)%8 == 0) printf(" ");
79 if ((i+1)%16 == 0 && i+1 < len) printf("\n\t ;; ");
83 static void
84 binprint ( char *str,
85 int len)
87 int i;
89 printf("\t ;; ");
90 for (i = 0; i < len; i++)
92 printf("%d%d%d%d%d%d%d%d ",
93 str[i] & 0x80 ? 1 : 0,
94 str[i] & 0x40 ? 1 : 0,
95 str[i] & 0x20 ? 1 : 0,
96 str[i] & 0x10 ? 1 : 0,
97 str[i] & 0x08 ? 1 : 0,
98 str[i] & 0x04 ? 1 : 0,
99 str[i] & 0x02 ? 1 : 0,
100 str[i] & 0x01 ? 1 : 0);
101 if ((i+1)%3 == 0) printf(" ");
102 if ((i+1)%6 == 0 && i+1 < len) printf("\n\t ;; ");
106 struct nfkc {
107 char *in;
108 char *out;
109 } nfkc[] = {
110 { "\xC2\xB5", "\xCE\xBC" },
111 { "\xC2\xAA", "\x61" }
115 main (int argc, char *argv[])
117 char *out;
118 int i;
121 if (strcmp (argv[argc-1], "-v") == 0 ||
122 strcmp (argv[argc-1], "--verbose") == 0)
123 debug = 1;
124 else if (strcmp (argv[argc-1], "-b") == 0 ||
125 strcmp (argv[argc-1], "--break-on-error") == 0)
126 break_on_error = 1;
127 else if (strcmp (argv[argc-1], "-h") == 0 ||
128 strcmp (argv[argc-1], "-?") == 0 ||
129 strcmp (argv[argc-1], "--help") == 0)
131 printf("Usage: %s [-vbh?] [--verbose] [--break-on-error] [--help]\n",
132 argv[0]);
133 return 1;
135 while (argc-- > 1);
137 for (i = 0; i < sizeof(nfkc) / sizeof(nfkc[0]); i++)
139 if (debug)
140 printf("NFKC entry %d\n", i);
142 out = gsasl_utf8_nfkc_normalize (nfkc[i].in, strlen(nfkc[i].in));
143 if (out == NULL)
145 fail("gsasl_utf8_nfkc_normalize() entry %d failed fatally\n", i);
146 continue;
149 if (debug)
151 printf("in:\n");
152 escapeprint(nfkc[i].in, strlen(nfkc[i].in));
153 hexprint(nfkc[i].in, strlen(nfkc[i].in)); puts("");
154 binprint(nfkc[i].in, strlen(nfkc[i].in)); puts("");
156 printf("out:\n");
157 escapeprint(out, strlen(out));
158 hexprint(out, strlen(out)); puts("");
159 binprint(out, strlen(out)); puts("");
161 printf("expected out:\n");
162 escapeprint(nfkc[i].out, strlen(nfkc[i].out));
163 hexprint(nfkc[i].out, strlen(nfkc[i].out)); puts("");
164 binprint(nfkc[i].out, strlen(nfkc[i].out)); puts("");
167 if (strlen(nfkc[i].out) != strlen(out) ||
168 memcmp (nfkc[i].out, out, strlen(out)) != 0)
170 fail("gsasl_utf8_nfkc_normalize() entry %d failed\n", i);
171 if (debug)
172 printf("ERROR\n");
174 else if (debug)
175 printf("OK\n");
178 if (debug)
179 printf("Libgsasl unicode self tests done with %d errors\n", error_count);
181 return error_count ? 1 : 0;