libgda r3343 - in trunk: . libgda tests



Author: vivien
Date: Thu Mar  5 20:47:11 2009
New Revision: 3343
URL: http://svn.gnome.org/viewvc/libgda?rev=3343&view=rev

Log:
2009-03-05  Vivien Malerba <malerba gnome-db org>

	* libgda/gda-value.c: fix for bug #574193
	* tests/Makefile.am:
	* tests/test-ddl-creator.c: remove created DB before running test, and run
	as a test
	* tests/Makefile.am:
	* tests/tests/test-bin-converter.c: new test for gda_binary_to_string()
	and gda_string_to_binary()


Added:
   trunk/tests/test-bin-converter.c
Modified:
   trunk/ChangeLog
   trunk/libgda/gda-value.c
   trunk/tests/   (props changed)
   trunk/tests/Makefile.am
   trunk/tests/test-ddl-creator.c

Modified: trunk/libgda/gda-value.c
==============================================================================
--- trunk/libgda/gda-value.c	(original)
+++ trunk/libgda/gda-value.c	Thu Mar  5 20:47:11 2009
@@ -2485,8 +2485,8 @@
  * @bin: a correctly filled @GdaBinary structure
  * @maxlen: a maximum len used to truncate, or 0 for no maximum length
  *
- * Converts all the non printable characters of bin->data into the \xxx representation
- * where xxx is the octal representation of the byte, and the '\' (backslash) character
+ * Converts all the non printable characters of bin->data into the "\xyz" representation
+ * where "xyz" is the octal representation of the byte, and the '\' (backslash) character
  * is converted to "\\".
  *
  * Returns: a new string from @bin
@@ -2548,10 +2548,10 @@
 				guchar val = *ptr;
 				g_memmove (ptr+4, ptr+1, realsize - offset);
 				*ptr = '\\';
-				*(ptr+1) = val / 49 + '0';
-				val = val % 49;
-				*(ptr+2) = val / 7 + '0';
-				val = val % 7;
+				*(ptr+1) = val / 64 + '0';
+				val = val % 64;
+				*(ptr+2) = val / 8 + '0';
+				val = val % 8;
 				*(ptr+3) = val + '0';
 				
 				ptr += 4;
@@ -2567,7 +2567,9 @@
  * gda_string_to_binary
  * @str: a string to convert
  *
- * Performs the reverse of gda_binary_to_string().
+ * Performs the reverse of gda_binary_to_string() (note that for any "\xyz" succession
+ * of 4 characters where "xyz" represents a valid octal value, the resulting read value will
+ * be modulo 256)
  *
  * Returns: a new #GdaBinary if no error were found in @str, or NULL otherwise
  */
@@ -2588,22 +2590,22 @@
 	}
 
 	total = strlen (str);
-	retval = g_memdup (str, total);
+	retval = g_memdup (str, total + 1);
 	ptr = (gchar *) retval;
 	while (offset < total) {
 		if (*ptr == '\\') {
 			if (*(ptr+1) == '\\') {
-				g_memmove (ptr+1, ptr+2, total - offset);
 				offset += 2;
+				g_memmove (ptr+1, ptr+2, total - offset);
 			}
 			else {
-				if ((*(ptr+1) >= '0') && (*(ptr+1) <= '9') &&
-				    (*(ptr+2) >= '0') && (*(ptr+2) <= '9') &&
-				    (*(ptr+3) >= '0') && (*(ptr+3) <= '9')) {
-					*ptr = (*(ptr+1) - '0') * 49 +
-						(*(ptr+2) - '0') * 7 +
-						*(ptr+3) - '0';
-					g_memmove (ptr+1, ptr+4, total - offset);
+				if ((*(ptr+1) >= '0') && (*(ptr+1) <= '7') &&
+				    (*(ptr+2) >= '0') && (*(ptr+2) <= '7') &&
+				    (*(ptr+3) >= '0') && (*(ptr+3) <= '7')) {
+					*ptr = (*(ptr+1) - '0') * 64 +
+						(*(ptr+2) - '0') * 8 +
+						(*(ptr+3) - '0');
+					g_memmove (ptr+1, ptr+4, total - offset - 3);
 					offset += 4;
 				}
 				else {

Modified: trunk/tests/Makefile.am
==============================================================================
--- trunk/tests/Makefile.am	(original)
+++ trunk/tests/Makefile.am	Thu Mar  5 20:47:11 2009
@@ -1,5 +1,7 @@
 noinst_LTLIBRARIES = libgda-test-4.0.la
-noinst_PROGRAMS = test-ddl-creator
+TESTS = test-ddl-creator test-bin-converter
+check_PROGRAMS = test-ddl-creator test-bin-converter
+
 
 SUBDIRS = providers parser value-holders meta-store data-models multi-threading
 
@@ -7,7 +9,9 @@
         -I$(top_builddir) \
         -I$(top_srcdir) \
         -I$(top_srcdir)/libgda \
-        $(LIBGDA_CFLAGS)
+        $(LIBGDA_CFLAGS) \
+	-DCHECK_FILES=\""$(top_srcdir)"\" \
+	-DTOP_BUILD_DIR=\""$(top_builddir)"\"
 
 test_headers = \
         gda-ddl-creator.h
@@ -19,6 +23,7 @@
 	test-cnc-utils.c
 
 libgda_test_4_0_la_LDFLAGS = -version-info $(GDA_CURRENT):$(GDA_REVISION):$(GDA_AGE) $(NO_UNDEFINED)
+
 libgda_test_4_0_la_LIBADD = \
 	$(LIBGDA_LIBS) \
 	$(top_builddir)/libgda/libgda-4.0.la 
@@ -30,3 +35,14 @@
         $(top_builddir)/libgda/libgda-4.0.la \
 	libgda-test-4.0.la \
         $(LIBGDA_LIBS)
+
+
+test_bin_converter_SOURCES = \
+        test-bin-converter.c
+
+test_bin_converter_LDADD = \
+        $(top_builddir)/libgda/libgda-4.0.la \
+	libgda-test-4.0.la \
+        $(LIBGDA_LIBS)
+
+EXTRA_DIST = dbstruct.xml
\ No newline at end of file

Added: trunk/tests/test-bin-converter.c
==============================================================================
--- (empty file)
+++ trunk/tests/test-bin-converter.c	Thu Mar  5 20:47:11 2009
@@ -0,0 +1,72 @@
+/* 
+ * Copyright (C) 2009 The GNOME Foundation.
+ *
+ * AUTHORS:
+ *      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., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <libgda/libgda.h>
+
+int
+main (int argc, char** argv)
+{
+	gchar *file;
+	GdaBinary bin;
+	gchar *bin_data;
+	gsize bin_length;
+	GError *error = NULL;
+
+	/* load binary data */
+	file = g_build_filename (CHECK_FILES, "data", "sales_test.db", NULL);
+	if (! g_file_get_contents (file, &bin_data, &bin_length, &error)) {
+		g_print ("Error reading binary file: %s\n", error->message);
+		return EXIT_FAILURE;
+	}		
+	g_free (file);
+	bin.data = (guchar*) bin_data;
+	bin.binary_length = bin_length;
+
+	/* convert to string */
+	gchar *conv1;
+	conv1 = gda_binary_to_string (&bin, 0);
+	
+	/* convert back to binary */
+	GdaBinary *bin2;
+	bin2 = gda_string_to_binary (conv1);
+	
+	/* compare bin */
+	if (bin.binary_length != bin2->binary_length) {
+		g_print ("Error: binary length differ: from %ld to %ld\n",
+			 bin.binary_length, bin2->binary_length);
+		return EXIT_FAILURE;
+	}
+	gint i;
+	for (i = 0; i < bin.binary_length; i++) {
+		if (bin.data[i] != bin2->data[i]) {
+			g_print ("Error: binary differ orig[%d]=%d and copy[%d]=%d\n",
+				 i, bin.data[i], i, bin2->data[i]);
+			return EXIT_FAILURE;
+		}
+	}
+	
+	g_free (conv1);
+	gda_binary_free (bin2);
+	g_free (bin_data);
+
+	g_print ("Ok (file size: %ld)\n", bin_length);
+	return EXIT_SUCCESS;
+}

Modified: trunk/tests/test-ddl-creator.c
==============================================================================
--- trunk/tests/test-ddl-creator.c	(original)
+++ trunk/tests/test-ddl-creator.c	Thu Mar  5 20:47:11 2009
@@ -1,4 +1,4 @@
-/* GDA - Information schema documentation extractor
+/* 
  * Copyright (C) 2008 The GNOME Foundation.
  *
  * AUTHORS:
@@ -29,17 +29,23 @@
 	GError *error = NULL;
 	GdaConnection *cnc;
 	gchar *str;
+	gchar *file;
 
+	/* set up test environment */
+        g_setenv ("GDA_TOP_BUILD_DIR", TOP_BUILD_DIR, 0);
 	gda_init ();
 	
 	ddl = gda_ddl_creator_new ();
-	if (!gda_ddl_creator_set_dest_from_file (ddl, "dbstruct.xml", &error)) {
+	file = g_build_filename (CHECK_FILES, "tests", "dbstruct.xml", NULL);
+	if (!gda_ddl_creator_set_dest_from_file (ddl, file, &error)) {
 		g_print ("Error creating GdaDDLCreator: %s\n", error && error->message ? error->message : "No detail");
 		g_error_free (error);
 		return EXIT_FAILURE;
 	}
+	g_free (file);
 	
 	/* open a connection */
+	g_unlink ("creator.db");
 	cnc = gda_connection_open_from_string ("SQLite", "DB_DIR=.;DB_NAME=creator", NULL, GDA_CONNECTION_OPTIONS_NONE, &error);
 	if (!cnc) {
 		g_print ("Error opening connection: %s\n", error && error->message ? error->message : "No detail");
@@ -67,6 +73,7 @@
 	}
 
 	g_object_unref (ddl);
+	g_unlink ("creator.db");
 
 	return EXIT_SUCCESS;
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]