|
1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 #undef G_DISABLE_ASSERT |
|
18 #undef G_LOG_DOMAIN |
|
19 |
|
20 #include <glib.h> |
|
21 #include <stdio.h> |
|
22 #include <stdlib.h> |
|
23 #include <string.h> |
|
24 |
|
25 #ifdef SYMBIAN |
|
26 #include "mrt2_glib2_test.h" |
|
27 #endif /*SYMBIAN*/ |
|
28 |
|
29 typedef struct { |
|
30 const char *key; |
|
31 const char *str; |
|
32 } Line; |
|
33 |
|
34 |
|
35 int |
|
36 compare_collate (const void *a, const void *b) |
|
37 { |
|
38 const Line *line_a = a; |
|
39 const Line *line_b = b; |
|
40 |
|
41 return g_utf8_collate (line_a->str, line_b->str); |
|
42 } |
|
43 |
|
44 int |
|
45 compare_key (const void *a, const void *b) |
|
46 { |
|
47 const Line *line_a = a; |
|
48 const Line *line_b = b; |
|
49 |
|
50 return strcmp (line_a->key, line_b->key); |
|
51 } |
|
52 |
|
53 gchar* sorted_res_arr[15] = |
|
54 { |
|
55 "ABC", |
|
56 "Bart", |
|
57 "BART", |
|
58 "Bartlett", |
|
59 "CA", |
|
60 "can", |
|
61 "Canada", |
|
62 "CBS", |
|
63 "NBC", |
|
64 "next", |
|
65 "NeXT", |
|
66 "west", |
|
67 "West", |
|
68 "western", |
|
69 NULL |
|
70 }; |
|
71 |
|
72 int main (int argc, char **argv) |
|
73 { |
|
74 GIOChannel *in; |
|
75 GError *error = NULL; |
|
76 gchar *srcdir = getenv ("srcdir"); |
|
77 gchar *testfile; |
|
78 GArray *line_array; |
|
79 guint i; |
|
80 |
|
81 #ifdef SYMBIAN |
|
82 g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL); |
|
83 g_set_print_handler(mrtPrintHandler); |
|
84 #endif /*SYMBIAN*/ |
|
85 |
|
86 line_array = g_array_new (FALSE, FALSE, sizeof(Line)); |
|
87 |
|
88 if (!srcdir) |
|
89 srcdir = "c:"; |
|
90 |
|
91 testfile = g_strconcat (srcdir, G_DIR_SEPARATOR_S "casecollate.txt", NULL); |
|
92 |
|
93 in = g_io_channel_new_file (testfile, "r", &error); |
|
94 if (!in) |
|
95 { |
|
96 g_print("Cannot open %s: %s\n", testfile, error->message); |
|
97 |
|
98 g_assert(FALSE && "unicode-collate failed"); |
|
99 |
|
100 #ifdef SYMBIAN |
|
101 testResultXml("unicode-collate"); |
|
102 #endif /* EMULATOR */ |
|
103 |
|
104 return 1; |
|
105 } |
|
106 |
|
107 while (TRUE) |
|
108 { |
|
109 gsize term_pos; |
|
110 gchar *str; |
|
111 Line line; |
|
112 gint keylen; |
|
113 gint strlen; |
|
114 |
|
115 if (g_io_channel_read_line (in, &str, NULL, &term_pos, &error) != G_IO_STATUS_NORMAL) |
|
116 break; |
|
117 |
|
118 str[term_pos] = '\0'; |
|
119 |
|
120 line.key = g_utf8_collate_key (str, -1); |
|
121 line.str = str; |
|
122 |
|
123 g_array_append_val (line_array, line); |
|
124 } |
|
125 |
|
126 if (error) |
|
127 { |
|
128 g_print("Error reading test file, %s\n", error->message); |
|
129 |
|
130 g_assert(FALSE && "unicode-collate failed"); |
|
131 |
|
132 #ifdef SYMBIAN |
|
133 testResultXml("unicode-collate"); |
|
134 #endif /* EMULATOR */ |
|
135 |
|
136 return 1; |
|
137 } |
|
138 |
|
139 qsort (line_array->data, line_array->len, sizeof (Line), compare_collate); |
|
140 for (i = 0; i < line_array->len; i++) |
|
141 { |
|
142 if(strcmp(sorted_res_arr[i], g_array_index (line_array, Line, i).str)) |
|
143 { |
|
144 g_assert(FALSE && "compare_collate failed\n"); |
|
145 |
|
146 } |
|
147 } |
|
148 |
|
149 qsort (line_array->data, line_array->len, sizeof (Line), compare_key); |
|
150 for (i = 0; i < line_array->len; i++) |
|
151 { |
|
152 if(strcmp(sorted_res_arr[i], g_array_index (line_array, Line, i).str)) |
|
153 { |
|
154 g_assert(FALSE && "compare_key failed\n"); |
|
155 } |
|
156 } |
|
157 |
|
158 g_io_channel_unref (in); |
|
159 |
|
160 #ifdef SYMBIAN |
|
161 testResultXml("unicode-collate"); |
|
162 #endif /* EMULATOR */ |
|
163 |
|
164 return 0; |
|
165 } |