Tizen 2.1 base
[external/enchant.git] / tests / test-enchant.c
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* enchant
3  * Copyright (C) 2003 Dom Lachowicz
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02110-1301, USA.
19  *
20  * In addition, as a special exception, Dom Lachowicz
21  * gives permission to link the code of this program with
22  * the non-LGPL Spelling Provider libraries (eg: a MSFT Office
23  * spell checker backend) and distribute linked combinations including
24  * the two.  You must obey the GNU Lesser General Public License in all
25  * respects for all of the code used other than said providers. If you modify
26  * this file, you may extend this exception to your version of the
27  * file, but you are not obligated to do so. If you do not wish to
28  * do so, delete this exception statement from your version.
29  */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include "enchant.h"
35
36 static void
37 enumerate_providers_fn (const char * const name,
38                         const char * const desc,
39                         const char * const file,
40                         void * ud)
41 {
42         printf ("%s: '%s' (%s)\n", name, desc, file);
43 }
44
45 static void
46 describe_dict_fn (const char * const lang,
47                   const char * const name,
48                   const char * const desc,
49                   const char * const file,
50                   void * ud)
51 {
52         printf ("%s: %s '%s' (%s)\n", lang, name, desc, file);
53 }
54
55 static const char * print_found(EnchantDict *dict, const char * word)
56 {
57         if (enchant_dict_check (dict, word, -1) == 0)
58                 return "found";
59         return "not found";
60 }
61
62 static void
63 run_dict_tests (EnchantDict * dict)
64 {
65         char **suggs;
66         size_t n_suggs;
67         size_t i, j;
68         
69         const char *check_checks[] = { "hello", "helllo" };
70         const char *sugg_checks[] = { "helllo", "taag" };
71         
72         for (i = 0; i < (sizeof (check_checks) / sizeof (check_checks[0])); i++)
73                 {
74                         printf ("enchant_dict_check (%s): %s\n", check_checks[i],
75                                 print_found(dict, check_checks[i]));
76                 }
77         
78         for (i = 0; i < (sizeof (sugg_checks) / sizeof (sugg_checks[0])); i++)
79                 {
80                         suggs =
81                                 enchant_dict_suggest (dict, sugg_checks[i], strlen (sugg_checks[i]),
82                                                       &n_suggs);
83                         
84                         printf ("enchant_dict_suggest(%s): %d\n", sugg_checks[i], n_suggs);
85                         for (j = 0; j < n_suggs; j++)
86                                 {
87                                         printf ("\t=>%s\n", suggs[j]);
88                                 }
89
90                         if (suggs && n_suggs)
91                                 enchant_dict_free_string_list (dict, suggs);
92                 }
93
94         printf ("Adding 'helllo' to session\n");
95         enchant_dict_add_to_session (dict, "helllo", 6);
96         for (i = 0; i < (sizeof (check_checks) / sizeof (check_checks[0])); i++)
97                 {
98                         printf ("enchant_dict_check (%s): %s\n", check_checks[i],
99                                 print_found(dict, check_checks[i]));
100                 }
101
102         printf ("Adding 'helllo' to personal\n");
103         enchant_dict_add (dict, "helllo", 6);
104         for (i = 0; i < (sizeof (check_checks) / sizeof (check_checks[0])); i++)
105                 {
106                         printf ("enchant_dict_check (%s): %s\n", check_checks[i],
107                                 print_found(dict, check_checks[i]));
108                 }
109 }
110
111 int
112 main (int argc, char **argv)
113 {
114         EnchantBroker *broker;
115         EnchantDict *dict;
116         const char * err;
117         
118         broker = enchant_broker_init ();
119         
120         dict = enchant_broker_request_dict (broker, "en_US");
121         
122         if (!dict) 
123                 {
124                         err = enchant_broker_get_error (broker);
125                         if (err)
126                                 fprintf (stderr, "Couldn't create dictionary for en_US: %s\n", err);
127                         else
128                                 fprintf (stderr, "Couldn't create dictionary for en_US\n");
129                 } 
130         else 
131                 {
132                         enchant_dict_describe (dict, describe_dict_fn, NULL);
133                         run_dict_tests (dict);                  
134                         enchant_broker_free_dict (broker, dict);
135                 }
136         
137         dict = enchant_broker_request_pwl_dict (broker, "test.pwl");
138         if (!dict) 
139                 {
140                         err = enchant_broker_get_error (broker);
141                         if (err)
142                                 fprintf (stderr, "Couldn't create personal wordlist dictionary: %s\n", err);
143                         else
144                                 fprintf (stderr, "Couldn't create personal wordlist dictionary\n");
145                 } 
146         else 
147                 {
148                         enchant_dict_describe (dict, describe_dict_fn, NULL);
149                         run_dict_tests (dict);
150                         enchant_broker_free_dict (broker, dict);
151                 }
152
153         enchant_broker_describe (broker, enumerate_providers_fn, NULL);
154         enchant_broker_free (broker);
155         
156         return 0;
157 }