Blame testsuite/test-buffer-input-stream.c

Packit a7d494
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*- */
Packit a7d494
/* test-buffer-input-stream.c
Packit a7d494
 * This file is part of GtkSourceView
Packit a7d494
 *
Packit a7d494
 * Copyright (C) 2010 - Ignacio Casal Quinteiro
Packit a7d494
 *
Packit a7d494
 * GtkSourceView is free software; you can redistribute it and/or
Packit a7d494
 * modify it under the terms of the GNU Lesser General Public
Packit a7d494
 * License as published by the Free Software Foundation; either
Packit a7d494
 * version 2.1 of the License, or (at your option) any later version.
Packit a7d494
 *
Packit a7d494
 * GtkSourceView is distributed in the hope that it will be useful,
Packit a7d494
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit a7d494
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit a7d494
 * Lesser General Public License for more details.
Packit a7d494
 *
Packit a7d494
 * You should have received a copy of the GNU Lesser General Public
Packit a7d494
 * License along with this library; if not, write to the Free Software
Packit a7d494
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
Packit a7d494
 */
Packit a7d494
Packit a7d494
#include <gio/gio.h>
Packit a7d494
#include <gtk/gtk.h>
Packit a7d494
#include <string.h>
Packit a7d494
#include <gtksourceview/gtksource.h>
Packit a7d494
#include "gtksourceview/gtksourcebufferinputstream.h"
Packit a7d494
Packit a7d494
static void
Packit a7d494
test_consecutive_read (const gchar          *inbuf,
Packit a7d494
		       const gchar          *outbuf,
Packit a7d494
		       GtkSourceNewlineType  type,
Packit a7d494
		       gsize                 read_chunk_len)
Packit a7d494
{
Packit a7d494
	GtkTextBuffer *buf;
Packit a7d494
	GtkSourceBufferInputStream *in;
Packit a7d494
	gsize outlen;
Packit a7d494
	gssize n, r;
Packit a7d494
	GError *err = NULL;
Packit a7d494
	gchar *b;
Packit a7d494
	gboolean close;
Packit a7d494
Packit a7d494
	buf = gtk_text_buffer_new (NULL);
Packit a7d494
	gtk_text_buffer_set_text (buf, inbuf, -1);
Packit a7d494
Packit a7d494
	b = g_malloc (200);
Packit a7d494
	in = _gtk_source_buffer_input_stream_new (buf, type, TRUE);
Packit a7d494
Packit a7d494
	outlen = strlen (outbuf);
Packit a7d494
	n = 0;
Packit a7d494
Packit a7d494
	do
Packit a7d494
	{
Packit a7d494
		r = g_input_stream_read (G_INPUT_STREAM (in), b + n, read_chunk_len, NULL, &err;;
Packit a7d494
		g_assert_cmpint (r, >=, 0);
Packit a7d494
		g_assert_no_error (err);
Packit a7d494
Packit a7d494
		n += r;
Packit a7d494
	} while (r != 0);
Packit a7d494
Packit a7d494
	g_assert_cmpint (n, ==, outlen);
Packit a7d494
Packit a7d494
	b[n] = '\0';
Packit a7d494
Packit a7d494
	g_assert_cmpstr (b, ==, outbuf);
Packit a7d494
Packit a7d494
	close = g_input_stream_close (G_INPUT_STREAM (in), NULL, &err;;
Packit a7d494
	g_assert (close);
Packit a7d494
	g_assert_no_error (err);
Packit a7d494
Packit a7d494
	g_object_unref (buf);
Packit a7d494
	g_object_unref (in);
Packit a7d494
	g_free (b);
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
test_empty (void)
Packit a7d494
{
Packit a7d494
	/* empty file should not have a trailing newline */
Packit a7d494
	test_consecutive_read ("", "", GTK_SOURCE_NEWLINE_TYPE_CR_LF, 10);
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
test_consecutive_cut_char (void)
Packit a7d494
{
Packit a7d494
	/* first \n is read then fo and then is added \r but not \n */
Packit a7d494
	test_consecutive_read ("\nfo\nbar\n\nblah\n", "\r\nfo\r\nbar\r\n\r\nblah\r\n\r\n", GTK_SOURCE_NEWLINE_TYPE_CR_LF, 8);
Packit a7d494
	test_consecutive_read ("\nfo\nbar\n\nblah", "\r\nfo\r\nbar\r\n\r\nblah\r\n", GTK_SOURCE_NEWLINE_TYPE_CR_LF, 8);
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
test_consecutive_big_read (void)
Packit a7d494
{
Packit a7d494
	test_consecutive_read ("\nfo\nbar\n\nblah\n", "\rfo\rbar\r\rblah\r\r", GTK_SOURCE_NEWLINE_TYPE_CR, 200);
Packit a7d494
	test_consecutive_read ("\nfo\nbar\n\nblah", "\rfo\rbar\r\rblah\r", GTK_SOURCE_NEWLINE_TYPE_CR, 200);
Packit a7d494
Packit a7d494
	test_consecutive_read ("\rfo\rbar\r\rblah\r", "\nfo\nbar\n\nblah\n\n", GTK_SOURCE_NEWLINE_TYPE_LF, 200);
Packit a7d494
	test_consecutive_read ("\rfo\rbar\r\rblah", "\nfo\nbar\n\nblah\n", GTK_SOURCE_NEWLINE_TYPE_LF, 200);
Packit a7d494
Packit a7d494
	test_consecutive_read ("\r\nfo\r\nbar\r\n\r\nblah\r\n", "\nfo\nbar\n\nblah\n\n", GTK_SOURCE_NEWLINE_TYPE_LF, 200);
Packit a7d494
	test_consecutive_read ("\r\nfo\r\nbar\r\n\r\nblah", "\nfo\nbar\n\nblah\n", GTK_SOURCE_NEWLINE_TYPE_LF, 200);
Packit a7d494
Packit a7d494
	test_consecutive_read ("\nfo\nbar\n\nblah\n", "\r\nfo\r\nbar\r\n\r\nblah\r\n\r\n", GTK_SOURCE_NEWLINE_TYPE_CR_LF, 200);
Packit a7d494
	test_consecutive_read ("\nfo\nbar\n\nblah", "\r\nfo\r\nbar\r\n\r\nblah\r\n", GTK_SOURCE_NEWLINE_TYPE_CR_LF, 200);
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
test_consecutive_middle_read (void)
Packit a7d494
{
Packit a7d494
	test_consecutive_read ("\nfo\nbar\n\nblah\n", "\rfo\rbar\r\rblah\r\r", GTK_SOURCE_NEWLINE_TYPE_CR, 6);
Packit a7d494
	test_consecutive_read ("\nfo\nbar\n\nblah", "\rfo\rbar\r\rblah\r", GTK_SOURCE_NEWLINE_TYPE_CR, 6);
Packit a7d494
Packit a7d494
	test_consecutive_read ("\rfo\rbar\r\rblah\r", "\nfo\nbar\n\nblah\n\n", GTK_SOURCE_NEWLINE_TYPE_LF, 6);
Packit a7d494
	test_consecutive_read ("\rfo\rbar\r\rblah", "\nfo\nbar\n\nblah\n", GTK_SOURCE_NEWLINE_TYPE_LF, 6);
Packit a7d494
Packit a7d494
	test_consecutive_read ("\r\nfo\r\nbar\r\n\r\nblah\r\n", "\nfo\nbar\n\nblah\n\n", GTK_SOURCE_NEWLINE_TYPE_LF, 6);
Packit a7d494
	test_consecutive_read ("\r\nfo\r\nbar\r\n\r\nblah", "\nfo\nbar\n\nblah\n", GTK_SOURCE_NEWLINE_TYPE_LF, 6);
Packit a7d494
Packit a7d494
	test_consecutive_read ("\nfo\nbar\n\nblah\n", "\r\nfo\r\nbar\r\n\r\nblah\r\n\r\n", GTK_SOURCE_NEWLINE_TYPE_CR_LF, 6);
Packit a7d494
	test_consecutive_read ("\nfo\nbar\n\nblah", "\r\nfo\r\nbar\r\n\r\nblah\r\n", GTK_SOURCE_NEWLINE_TYPE_CR_LF, 6);
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
test_consecutive_multibyte_cut (void)
Packit a7d494
{
Packit a7d494
	test_consecutive_read ("hello\nhello\xe6\x96\x87\nworld\n", "hello\rhello\xe6\x96\x87\rworld\r\r", GTK_SOURCE_NEWLINE_TYPE_CR, 6);
Packit a7d494
	test_consecutive_read ("hello\rhello\xe6\x96\x87\rworld\r", "hello\rhello\xe6\x96\x87\rworld\r\r", GTK_SOURCE_NEWLINE_TYPE_CR, 6);
Packit a7d494
	test_consecutive_read ("hello\nhello\xe6\x96\x87\nworld\n", "hello\nhello\xe6\x96\x87\nworld\n\n", GTK_SOURCE_NEWLINE_TYPE_LF, 6);
Packit a7d494
}
Packit a7d494
Packit a7d494
static void
Packit a7d494
test_consecutive_multibyte_big_read (void)
Packit a7d494
{
Packit a7d494
	test_consecutive_read ("hello\nhello\xe6\x96\x87\nworld\n", "hello\rhello\xe6\x96\x87\rworld\r\r", GTK_SOURCE_NEWLINE_TYPE_CR, 200);
Packit a7d494
	test_consecutive_read ("hello\rhello\xe6\x96\x87\rworld\r", "hello\rhello\xe6\x96\x87\rworld\r\r", GTK_SOURCE_NEWLINE_TYPE_CR, 200);
Packit a7d494
	test_consecutive_read ("hello\nhello\xe6\x96\x87\nworld\n", "hello\nhello\xe6\x96\x87\nworld\n\n", GTK_SOURCE_NEWLINE_TYPE_LF, 200);
Packit a7d494
}
Packit a7d494
Packit a7d494
gint
Packit a7d494
main (gint   argc,
Packit a7d494
      gchar *argv[])
Packit a7d494
{
Packit a7d494
	g_test_init (&argc, &argv, NULL);
Packit a7d494
Packit a7d494
	g_test_add_func ("/buffer-input-stream/empty", test_empty);
Packit a7d494
Packit a7d494
	g_test_add_func ("/buffer-input-stream/consecutive_cut_char", test_consecutive_cut_char);
Packit a7d494
	g_test_add_func ("/buffer-input-stream/consecutive_big_read", test_consecutive_big_read);
Packit a7d494
	g_test_add_func ("/buffer-input-stream/consecutive_middle_read", test_consecutive_middle_read);
Packit a7d494
Packit a7d494
	g_test_add_func ("/buffer-input-stream/consecutive_multibyte_cut", test_consecutive_multibyte_cut);
Packit a7d494
	g_test_add_func ("/buffer-input-stream/consecutive_multibyte_big_read", test_consecutive_multibyte_big_read);
Packit a7d494
Packit a7d494
	return g_test_run ();
Packit a7d494
}