1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
/*
* Copyright (C) 2008 - 2011 Vivien Malerba <malerba@gnome-db.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <glib.h>
#include <glib-object.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <gmodule.h>
#include <libgda/libgda.h>
#include <sql-parser/gda-sql-parser.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#define NTHREADS 3
typedef struct {
GMutex *mutex;
GPrivate *thread_priv;
xmlDocPtr doc;
xmlNodePtr node;
gboolean all_done;
GHashTable *parsers_hash;
GdaSqlParserMode current_tested_mode;
} ThData;
typedef struct {
gint ntests;
gint nfailures;
} ThPrivate;
static GdaSqlParser *create_parser_for_provider (const gchar *prov_name);
static gint do_test (GdaSqlParser *parser, const xmlChar *id, const xmlChar *sql, const xmlChar *expected,
const xmlChar *error_line, const xmlChar *error_col);
static void set_mode_foreach (gpointer key, gpointer value, gpointer pmode);
static gpointer start_thread (ThData *data);
int
main (int argc, char** argv)
{
xmlDocPtr doc;
GdaSqlParser *parser;
gint failures = 0;
gint ntests = 0;
gchar *fname;
GHashTable *parsers_hash;
GdaDataModel *providers_model;
gint i;
gda_init ();
/* load file */
fname = g_build_filename (ROOT_DIR, "tests", "parser", "testdata.xml", NULL);
if (! g_file_test (fname, G_FILE_TEST_EXISTS)) {
g_print ("File '%s' does not exist\n", fname);
exit (1);
}
/* create parsers */
parsers_hash = g_hash_table_new (g_str_hash, g_str_equal);
providers_model = gda_config_list_providers ();
for (i = 0; i < gda_data_model_get_n_rows (providers_model); i++) {
const GValue *pname;
GError *lerror = NULL;
pname = gda_data_model_get_value_at (providers_model, 0, i, &lerror);
if (!pname) {
g_print ("Can't get data model's value: %s",
lerror && lerror->message ? lerror->message : "No detail");
exit (1);
}
parser = create_parser_for_provider (g_value_get_string (pname));
g_hash_table_insert (parsers_hash, g_strdup (g_value_get_string (pname)), parser);
g_print ("Created parser for provider %s\n", g_value_get_string (pname));
}
g_object_unref (providers_model);
g_hash_table_insert (parsers_hash, "", gda_sql_parser_new ());
/* use test data */
ThData data;
doc = xmlParseFile (fname);
g_free (fname);
g_assert (doc);
GdaSqlParserMode pmode;
for (pmode = GDA_SQL_PARSER_MODE_PARSE; pmode <= GDA_SQL_PARSER_MODE_DELIMIT; pmode++) {
gint j;
g_hash_table_foreach (parsers_hash, (GHFunc) set_mode_foreach, GINT_TO_POINTER (pmode));
for (j = 0; j < 20; j++) {
gint i;
GThread *threads[NTHREADS];
data.mutex = g_mutex_new ();
data.thread_priv = g_private_new (g_free);
data.doc = doc;
data.node = NULL;
data.all_done = FALSE;
data.parsers_hash = parsers_hash;
data.current_tested_mode = pmode;
for (i = 0; i < NTHREADS; i++) {
//g_print ("Starting thread %d\n", i);
threads [i] = g_thread_create ((GThreadFunc) start_thread, &data, TRUE, NULL);
}
for (i = 0; i < NTHREADS; i++) {
ThPrivate *priv;
//g_print ("Joining thread %d... ", i);
priv = g_thread_join (threads [i]);
ntests += priv->ntests;
failures += priv->nfailures;
//g_print ("%d tests and %d failures\n", priv->ntests, priv->nfailures);
g_free (priv);
}
g_mutex_free (data.mutex);
}
}
xmlFreeDoc (doc);
g_print ("TESTS COUNT: %d\n", ntests);
g_print ("FAILURES: %d\n", failures);
return failures != 0 ? 1 : 0;
}
static void
set_mode_foreach (gpointer key, gpointer value, gpointer pmode)
{
g_object_set (G_OBJECT (value), "mode", GPOINTER_TO_INT (pmode), NULL);
}
static gpointer
start_thread (ThData *data)
{
while (1) {
xmlNodePtr node; /* node to test */
/* thread's private data */
ThPrivate *thpriv = g_private_get (data->thread_priv);
if (!thpriv) {
thpriv = g_new (ThPrivate, 1);
thpriv->ntests = 0;
thpriv->nfailures = 0;
g_private_set (data->thread_priv, thpriv);
}
/* grep next test to perform, and parser to use */
xmlChar *prov_name;
GdaSqlParser *parser;
g_mutex_lock (data->mutex);
if (data->all_done) {
ThPrivate *copy = g_new (ThPrivate, 1);
g_mutex_unlock (data->mutex);
*copy = *thpriv;
return copy;
}
if (data->node)
data->node = data->node->next;
else {
xmlNodePtr root = xmlDocGetRootElement (data->doc);
g_assert (!strcmp ((gchar*) root->name, "testdata"));
data->node = root->children;
}
if (!data->node) {
ThPrivate *copy = g_new (ThPrivate, 1);
data->all_done = TRUE;
g_mutex_unlock (data->mutex);
*copy = *thpriv;
return copy;
}
node = data->node;
prov_name = xmlGetProp (node, BAD_CAST "provider");
if (prov_name) {
parser = g_hash_table_lookup (data->parsers_hash, (gchar *) prov_name);
xmlFree (prov_name);
}
else
parser = g_hash_table_lookup (data->parsers_hash, "");
g_mutex_unlock (data->mutex);
g_thread_yield ();
/* test elected node */
if (strcmp ((gchar*) node->name, "test"))
continue;
if (!parser)
continue;
xmlNodePtr snode;
xmlChar *sql = NULL;
xmlChar *id;
id = xmlGetProp (node, BAD_CAST "id");
for (snode = node->children; snode; snode = snode->next) {
if (!strcmp ((gchar*) snode->name, "sql"))
sql = xmlNodeGetContent (snode);
else if (!strcmp ((gchar*) snode->name, "expected")) {
xmlChar *expected;
xmlChar *mode;
expected = xmlNodeGetContent (snode);
mode = xmlGetProp (snode, BAD_CAST "mode");
if ((mode && !strcmp (mode, "delim") &&
(data->current_tested_mode == GDA_SQL_PARSER_MODE_DELIMIT)) ||
((!mode || strcmp (mode, "delim")) &&
(data->current_tested_mode == GDA_SQL_PARSER_MODE_PARSE))) {
if (sql) {
thpriv->nfailures += do_test (parser, id, sql, expected, NULL, NULL);
thpriv->ntests +=1;
}
else
g_print ("===== Test '%s' doe not have any <sql> tag!\n", id);
}
if (expected) xmlFree (expected);
if (mode) xmlFree (mode);
}
else if (!strcmp ((gchar*) snode->name, "error")) {
xmlChar *error_line, *error_col;
xmlChar *mode;
mode = xmlGetProp (snode, BAD_CAST "mode");
error_line = xmlGetProp (snode, BAD_CAST "line");
error_col = xmlGetProp (snode, BAD_CAST "col");
if ((mode && !strcmp (mode, "delim") &&
(data->current_tested_mode == GDA_SQL_PARSER_MODE_DELIMIT)) ||
((!mode || strcmp (mode, "delim")) &&
(data->current_tested_mode == GDA_SQL_PARSER_MODE_PARSE))) {
if (sql) {
thpriv->nfailures += do_test (parser, id, sql, NULL, error_line, error_col);
thpriv->ntests += 1;
}
else
g_print ("===== Test '%s' doe not have any <sql> tag!\n", id);
}
if (mode) xmlFree (mode);
if (error_line) xmlFree (error_line);
if (error_col) xmlFree (error_col);
}
}
/* mem free */
if (sql) xmlFree (sql);
if (id) xmlFree (id);
}
}
/*
* Returns: the number of failures. This function is called from several threads at the same time
*/
static gint
do_test (GdaSqlParser *parser, const xmlChar *id, const xmlChar *sql, const xmlChar *expected,
const xmlChar *error_line, const xmlChar *error_col)
{
gboolean failures = 0;
#ifdef GDA_DEBUG_NO
GdaSqlParserMode mode;
g_object_get (G_OBJECT (parser), "mode", &mode, NULL);
g_print ("===== TEST %s (%s mode) SQL: @%s@\n", id,
mode == GDA_SQL_PARSER_MODE_PARSE ? "PARSE" : "DELIMIT", sql);
#endif
if (expected) {
GdaBatch *batch;
GError *error = NULL;
batch = gda_sql_parser_parse_string_as_batch (parser, (gchar *) sql, NULL, &error);
if (batch) {
gchar *ser;
ser = gda_batch_serialize (batch);
if (strcmp (ser, (gchar *) expected)) {
g_print ("ERROR for test '%s':\n *exp: %s\n *got: %s\n",
id, expected, ser);
failures ++;
}
g_free (ser);
g_object_unref (batch);
}
else {
if (error && (error->domain == GDA_SQL_PARSER_ERROR) &&
(error->code == GDA_SQL_PARSER_EMPTY_SQL_ERROR) &&
(*expected == 0))
/* OK */;
else {
g_print ("ERROR for test '%s':\n *got error: %s\n", id,
error && error->message ? error->message: "No detail");
if (error)
g_error_free (error);
failures ++;
}
}
}
else if (error_line && error_col) {
GdaBatch *batch;
GError *error = NULL;
/* rem: we need to use a mutex here because we actually perform 2 calls to the parser
* and we don't want them interrupted by a thread context change */
gda_lockable_lock ((GdaLockable*) parser);
batch = gda_sql_parser_parse_string_as_batch (parser, (gchar *) sql, NULL, &error);
if (batch) {
gchar *ser;
ser = gda_batch_serialize (batch);
g_print ("ERROR for test '%s':\n *got: %s\n",
id, ser);
g_free (ser);
g_object_unref (batch);
failures ++;
}
else {
/* FIXME: test error line and col */
gint line, col;
g_object_get (parser, "line-error", &line, "column-error", &col, NULL);
if ((atoi ((gchar*) error_line) != line) || (atoi ((gchar *) error_col) != col)) {
g_print ("ERROR for test '%s':\n *exp line=%s, col=%s\n *got line=%d, col=%d\n",
id, error_line, error_col, line, col);
failures ++;
}
if (error)
g_error_free (error);
}
gda_lockable_unlock ((GdaLockable*) parser);
}
else
g_print ("Missing or incomplete expected result for test '%s'!\n", id);
return failures;
}
static GdaSqlParser *
create_parser_for_provider (const gchar *prov_name)
{
GdaServerProvider *prov;
GdaSqlParser *parser;
GError *error = NULL;
prov = gda_config_get_provider (prov_name, &error);
if (!prov)
g_error ("Could not create provider for '%s': %s\n", prov_name,
error && error->message ? error->message : "No detail");
parser = gda_server_provider_create_parser (prov, NULL);
if (!parser)
parser = gda_sql_parser_new ();
return parser;
}
|