xed/tests/document-input-stream.c

159 lines
5.7 KiB
C
Raw Normal View History

2011-11-07 13:46:58 -06:00
/*
* document-input-stream.c
2011-11-07 16:52:18 -06:00
* This file is part of pluma
2011-11-07 13:46:58 -06:00
*
* Copyright (C) 2010 - Ignacio Casal Quinteiro
*
2011-11-07 16:52:18 -06:00
* pluma is free software; you can redistribute it and/or modify
2011-11-07 13:46:58 -06:00
* 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.
*
2011-11-07 16:52:18 -06:00
* pluma is distributed in the hope that it will be useful,
2011-11-07 13:46:58 -06:00
* 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
2011-11-07 16:52:18 -06:00
* along with pluma; if not, write to the Free Software
2011-11-07 13:46:58 -06:00
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
2011-11-07 16:52:18 -06:00
#include "pluma-document-input-stream.h"
2011-11-07 13:46:58 -06:00
#include <gio/gio.h>
#include <gtk/gtk.h>
#include <glib.h>
#include <string.h>
static void
test_consecutive_read (const gchar *inbuf,
const gchar *outbuf,
2011-11-07 16:52:18 -06:00
PlumaDocumentNewlineType type,
2011-11-07 13:46:58 -06:00
gsize read_chunk_len)
{
GtkTextBuffer *buf;
GInputStream *in;
gsize outlen;
gssize n, r;
GError *err = NULL;
gchar *b;
gboolean close;
buf = gtk_text_buffer_new (NULL);
gtk_text_buffer_set_text (buf, inbuf, -1);
b = g_malloc (200);
2011-11-07 16:52:18 -06:00
in = pluma_document_input_stream_new (buf, type);
2011-11-07 13:46:58 -06:00
outlen = strlen (outbuf);
n = 0;
do
{
r = g_input_stream_read (in, b + n, read_chunk_len, NULL, &err);
g_assert_cmpint (r, >=, 0);
g_assert_no_error (err);
n += r;
} while (r != 0);
g_assert_cmpint (n, ==, outlen);
b[n] = '\0';
g_assert_cmpstr (b, ==, outbuf);
close = g_input_stream_close (in, NULL, &err);
g_assert (close);
g_assert_no_error (err);
g_object_unref (buf);
g_object_unref (in);
g_free (b);
}
static void
test_empty ()
{
/* empty file should not have a trailing newline */
2011-11-07 16:52:18 -06:00
test_consecutive_read ("", "", PLUMA_DOCUMENT_NEWLINE_TYPE_CR_LF, 10);
2011-11-07 13:46:58 -06:00
}
static void
test_consecutive_cut_char ()
{
/* first \n is read then fo and then is added \r but not \n */
2011-11-07 16:52:18 -06:00
test_consecutive_read ("\nfo\nbar\n\nblah\n", "\r\nfo\r\nbar\r\n\r\nblah\r\n\r\n", PLUMA_DOCUMENT_NEWLINE_TYPE_CR_LF, 8);
test_consecutive_read ("\nfo\nbar\n\nblah", "\r\nfo\r\nbar\r\n\r\nblah\r\n", PLUMA_DOCUMENT_NEWLINE_TYPE_CR_LF, 8);
2011-11-07 13:46:58 -06:00
}
static void
test_consecutive_big_read ()
{
2011-11-07 16:52:18 -06:00
test_consecutive_read ("\nfo\nbar\n\nblah\n", "\rfo\rbar\r\rblah\r\r", PLUMA_DOCUMENT_NEWLINE_TYPE_CR, 200);
test_consecutive_read ("\nfo\nbar\n\nblah", "\rfo\rbar\r\rblah\r", PLUMA_DOCUMENT_NEWLINE_TYPE_CR, 200);
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
test_consecutive_read ("\rfo\rbar\r\rblah\r", "\nfo\nbar\n\nblah\n\n", PLUMA_DOCUMENT_NEWLINE_TYPE_LF, 200);
test_consecutive_read ("\rfo\rbar\r\rblah", "\nfo\nbar\n\nblah\n", PLUMA_DOCUMENT_NEWLINE_TYPE_LF, 200);
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
test_consecutive_read ("\r\nfo\r\nbar\r\n\r\nblah\r\n", "\nfo\nbar\n\nblah\n\n", PLUMA_DOCUMENT_NEWLINE_TYPE_LF, 200);
test_consecutive_read ("\r\nfo\r\nbar\r\n\r\nblah", "\nfo\nbar\n\nblah\n", PLUMA_DOCUMENT_NEWLINE_TYPE_LF, 200);
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
test_consecutive_read ("\nfo\nbar\n\nblah\n", "\r\nfo\r\nbar\r\n\r\nblah\r\n\r\n", PLUMA_DOCUMENT_NEWLINE_TYPE_CR_LF, 200);
test_consecutive_read ("\nfo\nbar\n\nblah", "\r\nfo\r\nbar\r\n\r\nblah\r\n", PLUMA_DOCUMENT_NEWLINE_TYPE_CR_LF, 200);
2011-11-07 13:46:58 -06:00
}
static void
test_consecutive_middle_read ()
{
2011-11-07 16:52:18 -06:00
test_consecutive_read ("\nfo\nbar\n\nblah\n", "\rfo\rbar\r\rblah\r\r", PLUMA_DOCUMENT_NEWLINE_TYPE_CR, 6);
test_consecutive_read ("\nfo\nbar\n\nblah", "\rfo\rbar\r\rblah\r", PLUMA_DOCUMENT_NEWLINE_TYPE_CR, 6);
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
test_consecutive_read ("\rfo\rbar\r\rblah\r", "\nfo\nbar\n\nblah\n\n", PLUMA_DOCUMENT_NEWLINE_TYPE_LF, 6);
test_consecutive_read ("\rfo\rbar\r\rblah", "\nfo\nbar\n\nblah\n", PLUMA_DOCUMENT_NEWLINE_TYPE_LF, 6);
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
test_consecutive_read ("\r\nfo\r\nbar\r\n\r\nblah\r\n", "\nfo\nbar\n\nblah\n\n", PLUMA_DOCUMENT_NEWLINE_TYPE_LF, 6);
test_consecutive_read ("\r\nfo\r\nbar\r\n\r\nblah", "\nfo\nbar\n\nblah\n", PLUMA_DOCUMENT_NEWLINE_TYPE_LF, 6);
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
test_consecutive_read ("\nfo\nbar\n\nblah\n", "\r\nfo\r\nbar\r\n\r\nblah\r\n\r\n", PLUMA_DOCUMENT_NEWLINE_TYPE_CR_LF, 6);
test_consecutive_read ("\nfo\nbar\n\nblah", "\r\nfo\r\nbar\r\n\r\nblah\r\n", PLUMA_DOCUMENT_NEWLINE_TYPE_CR_LF, 6);
2011-11-07 13:46:58 -06:00
}
static void
test_consecutive_multibyte_cut ()
{
2011-11-07 16:52:18 -06:00
test_consecutive_read ("hello\nhello\xe6\x96\x87\nworld\n", "hello\rhello\xe6\x96\x87\rworld\r\r", PLUMA_DOCUMENT_NEWLINE_TYPE_CR, 6);
test_consecutive_read ("hello\rhello\xe6\x96\x87\rworld\r", "hello\rhello\xe6\x96\x87\rworld\r\r", PLUMA_DOCUMENT_NEWLINE_TYPE_CR, 6);
test_consecutive_read ("hello\nhello\xe6\x96\x87\nworld\n", "hello\nhello\xe6\x96\x87\nworld\n\n", PLUMA_DOCUMENT_NEWLINE_TYPE_LF, 6);
2011-11-07 13:46:58 -06:00
}
static void
test_consecutive_multibyte_big_read ()
{
2011-11-07 16:52:18 -06:00
test_consecutive_read ("hello\nhello\xe6\x96\x87\nworld\n", "hello\rhello\xe6\x96\x87\rworld\r\r", PLUMA_DOCUMENT_NEWLINE_TYPE_CR, 200);
test_consecutive_read ("hello\rhello\xe6\x96\x87\rworld\r", "hello\rhello\xe6\x96\x87\rworld\r\r", PLUMA_DOCUMENT_NEWLINE_TYPE_CR, 200);
test_consecutive_read ("hello\nhello\xe6\x96\x87\nworld\n", "hello\nhello\xe6\x96\x87\nworld\n\n", PLUMA_DOCUMENT_NEWLINE_TYPE_LF, 200);
2011-11-07 13:46:58 -06:00
}
int main (int argc,
char *argv[])
{
2013-11-04 05:46:45 -06:00
#if !GLIB_CHECK_VERSION (2, 36, 0)
2011-11-07 13:46:58 -06:00
g_type_init ();
2013-11-04 05:46:45 -06:00
#endif
2011-11-07 13:46:58 -06:00
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/document-input-stream/empty", test_empty);
g_test_add_func ("/document-input-stream/consecutive_cut_char", test_consecutive_cut_char);
g_test_add_func ("/document-input-stream/consecutive_big_read", test_consecutive_big_read);
g_test_add_func ("/document-input-stream/consecutive_middle_read", test_consecutive_middle_read);
g_test_add_func ("/document-input-stream/consecutive_multibyte_cut", test_consecutive_multibyte_cut);
g_test_add_func ("/document-input-stream/consecutive_multibyte_big_read", test_consecutive_multibyte_big_read);
return g_test_run ();
}