Skip to content

Commit

Permalink
Add tests for smart converter
Browse files Browse the repository at this point in the history
  • Loading branch information
nacho committed Jan 17, 2010
1 parent 41eb183 commit 181ff5b
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Process this file with automake to produce Makefile.in
ACLOCAL_AMFLAGS = -I m4

SUBDIRS = gedit pixmaps po data plugin-loaders plugins docs win32 osx
SUBDIRS = gedit pixmaps po data plugin-loaders plugins docs tests win32 osx

if !OS_OSX
SUBDIRS += help
Expand Down
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ plugins/spell/Makefile
plugins/taglist/Makefile
plugins/time/Makefile
po/Makefile.in
tests/Makefile
win32/gedit.iss
win32/Makefile
osx/Info.plist
Expand Down
10 changes: 10 additions & 0 deletions tests/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
INCLUDES = -g -I$(top_srcdir) -I$(top_srcdir)/gedit $(GEDIT_DEBUG_FLAGS) $(GEDIT_CFLAGS)

noinst_PROGRAMS = $(TEST_PROGS)
progs_ldadd = $(top_builddir)/gedit/libgedit.la


TEST_PROGS = smart-converter
smart_converter_SOURCES = smart-converter.c
smart_converter_LDADD = $(progs_ldadd)

216 changes: 216 additions & 0 deletions tests/smart-converter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
/*
* smart-converter.c
* This file is part of gedit
*
* Copyright (C) 2009 - Ignacio Casal Quinteiro
*
* gedit 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.
*
* gedit 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 gedit; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/


#include "gedit-smart-charset-converter.h"
#include "gedit-encodings.h"
#include <gio/gio.h>
#include <glib.h>
#include <string.h>

#define TEXT_TO_CONVERT "this is some text to make the tests"

static gchar *
get_text_with_encoding (const gchar *text,
const GeditEncoding *encoding)
{
GCharsetConverter *converter;
GConverterResult res;
gchar *conv_text;
gsize read, written;
GError *err = NULL;

converter = g_charset_converter_new (gedit_encoding_get_charset (encoding),
"UTF-8",
NULL);

conv_text = g_malloc (200);

res = g_converter_convert (G_CONVERTER (converter),
text,
strlen (text),
conv_text,
200,
0,
&read,
&written,
&err);

g_assert (err == NULL);
conv_text[written] = '\0';

return conv_text;
}

static GSList *
get_all_encodings ()
{
GSList *encs = NULL;
gint i = 0;

while (TRUE)
{
const GeditEncoding *enc;

enc = gedit_encoding_get_from_index (i);

if (enc == NULL)
break;

encs = g_slist_prepend (encs, (gpointer)enc);
i++;
}

return encs;
}

static void
do_test (const gchar *test_in,
GSList *encodings)
{
GeditSmartCharsetConverter *converter;
gchar *out;
gsize len;
gsize bytes_read;
gsize bytes_written;
GError *err;

converter = gedit_smart_charset_converter_new (encodings);

out = g_malloc (200);
len = strlen (test_in);
err = NULL;

g_converter_convert (G_CONVERTER (converter),
test_in,
len,
out,
200,
0,
&bytes_read,
&bytes_written,
&err);

g_assert (err == NULL);
out[bytes_written] = '\0';
g_assert_cmpstr (out, ==, TEXT_TO_CONVERT);
}
#if 0
static void
do_test_few_memory (const gchar *test_in,
const gchar *test_out,
GeditNewlineConverterNewlineType type,
gint memory) /* less than 200 */
{
GeditConverter *converter;
GConverterResult res;
gchar *out;
gchar *out_aux;
gsize len;
gsize bytes_read;
gsize bytes_read_aux;
gsize bytes_written;
gsize bytes_written_aux;
GError *err;
gint i;

converter = gedit_newline_converter_new ();
gedit_newline_converter_set_newline_type (converter, type);

out = g_malloc (200);
out_aux = g_malloc (memory);
len = strlen (test_in);
err = NULL;
i = 0;
bytes_read_aux = 0;
bytes_written_aux = 0;

do
{
res = g_converter_convert (G_CONVERTER (converter),
test_in + bytes_read_aux,
len,
out_aux,
memory,
G_CONVERTER_INPUT_AT_END,
&bytes_read,
&bytes_written,
&err);
memcpy (out + bytes_written_aux, out_aux, bytes_written);
bytes_read_aux += bytes_read;
bytes_written_aux += bytes_written;
len -= bytes_read;
} while (res != G_CONVERTER_FINISHED && res != G_CONVERTER_ERROR);

g_assert (err == NULL);
out[bytes_written_aux] = '\0';
g_assert_cmpstr (out, ==, test_out);
}
#endif

static void
test_utf8_utf8 ()
{
GSList *encs = NULL;

encs = g_slist_prepend (encs, (gpointer)gedit_encoding_get_utf8 ());

do_test (TEXT_TO_CONVERT, encs);
/* Missing malformed utf8 string and string with last char cut */

g_slist_free (encs);
}

static void
test_xxx_xxx ()
{
GSList *encs, *l;

encs = get_all_encodings ();

/* Here we just test all encodings it is just to know that the conversions
are done ok */
for (l = encs; l != NULL; l = g_slist_next (l))
{
gchar *text;
GSList *test_enc = NULL;

text = get_text_with_encoding (TEXT_TO_CONVERT, (const GeditEncoding *)l->data);
test_enc = g_slist_prepend (test_enc, l->data);

do_test (text, test_enc);
g_slist_free (test_enc);
g_free (text);
}
}

int main (int argc,
char *argv[])
{
g_type_init ();
g_test_init (&argc, &argv, NULL);

g_test_add_func ("/smart-converter/utf8-utf8", test_utf8_utf8);
g_test_add_func ("/smart-converter/xxx-xxx", test_xxx_xxx);

return g_test_run ();
}

0 comments on commit 181ff5b

Please sign in to comment.