1 /*-------------------------------------------------------------------------
2  * norm_test.c
3  *		Program to test Unicode normalization functions.
4  *
5  * Portions Copyright (c) 2017-2018, PostgreSQL Global Development Group
6  *
7  * IDENTIFICATION
8  *	  src/common/unicode_norm.c
9  *
10  *-------------------------------------------------------------------------
11  */
12 #include "postgres_fe.h"
13 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 
18 #include "common/unicode_norm.h"
19 
20 #include "norm_test_table.h"
21 
22 static char *
print_wchar_str(const pg_wchar * s)23 print_wchar_str(const pg_wchar *s)
24 {
25 #define BUF_DIGITS 50
26 	static char buf[BUF_DIGITS * 2 + 1];
27 	int			i;
28 
29 	i = 0;
30 	while (*s && i < BUF_DIGITS)
31 	{
32 		snprintf(&buf[i * 2], 3, "%04X", *s);
33 		i++;
34 		s++;
35 	}
36 	buf[i * 2] = '\0';
37 	return buf;
38 }
39 
40 static int
pg_wcscmp(const pg_wchar * s1,const pg_wchar * s2)41 pg_wcscmp(const pg_wchar *s1, const pg_wchar *s2)
42 {
43 	for (;;)
44 	{
45 		if (*s1 < *s2)
46 			return -1;
47 		if (*s1 > *s2)
48 			return 1;
49 		if (*s1 == 0)
50 			return 0;
51 		s1++;
52 		s2++;
53 	}
54 }
55 
56 int
main(int argc,char ** argv)57 main(int argc, char **argv)
58 {
59 	const		pg_unicode_test *test;
60 
61 	for (test = UnicodeNormalizationTests; test->input[0] != 0; test++)
62 	{
63 		pg_wchar   *result;
64 
65 		result = unicode_normalize_kc(test->input);
66 
67 		if (pg_wcscmp(test->output, result) != 0)
68 		{
69 			printf("FAILURE (Normalizationdata.txt line %d):\n", test->linenum);
70 			printf("input:\t%s\n", print_wchar_str(test->input));
71 			printf("expected:\t%s\n", print_wchar_str(test->output));
72 			printf("got\t%s\n", print_wchar_str(result));
73 			printf("\n");
74 			exit(1);
75 		}
76 	}
77 
78 	printf("All tests successful!\n");
79 	exit(0);
80 }
81