1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/glib/tsrc/BC/src/iochannel_test.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,232 @@
1.4 +/*
1.5 +* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
1.6 +*
1.7 +* This library is free software; you can redistribute it and/or
1.8 +* modify it under the terms of the GNU Lesser General Public
1.9 +* License as published by the Free Software Foundation; either
1.10 +* version 2 of the License, or (at your option) any later version.
1.11 +*
1.12 +* This library is distributed in the hope that it will be useful,
1.13 +* but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1.15 +* Lesser General Public License for more details.
1.16 +*
1.17 +* You should have received a copy of the GNU Lesser General Public
1.18 +* License along with this library; if not, write to the
1.19 +* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
1.20 +* Boston, MA 02111-1307, USA.
1.21 +*
1.22 +* Description:
1.23 +*
1.24 +*/
1.25 +
1.26 +
1.27 +#undef G_DISABLE_ASSERT
1.28 +#undef G_LOG_DOMAIN
1.29 +
1.30 +#include <stdio.h>
1.31 +#include <string.h>
1.32 +#include <errno.h>
1.33 +#include <stdlib.h>
1.34 +#include "glib.h"
1.35 +
1.36 +#define BUFFER_SIZE 1024
1.37 +
1.38 +#ifdef SYMBIAN
1.39 +#include "mrt2_glib2_test.h"
1.40 +#endif /*SYMBIAN*/
1.41 +
1.42 +int iochannel_test()
1.43 +{
1.44 + GIOChannel *gio_r, *gio_w ;
1.45 + GError *gerr = NULL;
1.46 + GString *buffer;
1.47 + char *filename;
1.48 + char *srcdir = getenv ("srcdir");
1.49 + const gchar encoding[] = "ISO-8859-5";
1.50 + GIOStatus status;
1.51 + GIOFlags flags;
1.52 + GIOChannelError gio_error;
1.53 + GIOCondition gio_condition;
1.54 + const gchar *line_term;
1.55 + GIOChannel *temp;
1.56 + char *buf;
1.57 + gsize size_read,size_written;
1.58 + GIOError io_error;
1.59 + gunichar thechar;
1.60 +
1.61 + // This call should set gio_error to G_IO_CHANNEL_ERROR_INVAL
1.62 + gio_error = g_io_channel_error_from_errno(EINVAL);
1.63 +
1.64 + g_assert(gio_error == G_IO_CHANNEL_ERROR_INVAL);
1.65 +
1.66 + if (!srcdir)
1.67 + srcdir = "c:";
1.68 + filename = g_strconcat (srcdir, G_DIR_SEPARATOR_S, "iochannel-test-infile", NULL);
1.69 +
1.70 + gio_r = g_io_channel_new_file (filename, "r", &gerr);
1.71 + if (gerr)
1.72 + {
1.73 + g_warning ("Unable to open file %s: %s", filename, gerr->message);
1.74 + g_error_free (gerr);
1.75 + return 1;
1.76 + }
1.77 + gio_w = g_io_channel_new_file ("c:\\iochannel-test-outfile", "w", &gerr);
1.78 + if (gerr)
1.79 + {
1.80 + g_warning ("Unable to open file %s: %s", "iochannel-test-outfile", gerr->message);
1.81 + g_error_free (gerr);
1.82 + return 1;
1.83 + }
1.84 +
1.85 + g_io_channel_set_encoding (gio_r, NULL, &gerr);
1.86 +
1.87 + if (gerr)
1.88 + {
1.89 + g_warning (gerr->message);
1.90 + g_error_free (gerr);
1.91 + return 1;
1.92 + }
1.93 +
1.94 + g_io_channel_set_buffered(gio_r,TRUE);
1.95 +
1.96 + g_assert(gio_r->use_buffer == TRUE);
1.97 +
1.98 + g_io_channel_set_encoding (gio_r, encoding, &gerr);
1.99 + if (gerr)
1.100 + {
1.101 + g_warning (gerr->message);
1.102 + g_error_free (gerr);
1.103 + return 1;
1.104 + }
1.105 +
1.106 + g_io_channel_set_buffer_size (gio_r, BUFFER_SIZE);
1.107 +
1.108 + // check g_io_channel_get_buffer_size
1.109 + g_assert(g_io_channel_get_buffer_size(gio_r) == BUFFER_SIZE);
1.110 +
1.111 + // check g_io_channel_get_buffered
1.112 + g_assert(g_io_channel_get_buffered(gio_r) == TRUE);
1.113 +
1.114 + //g_io_channel_get_close_on_unref
1.115 + g_assert(g_io_channel_get_close_on_unref(gio_r) == TRUE);
1.116 +
1.117 + //check g_io_channel_get_encoding
1.118 + g_assert(!strcmp(g_io_channel_get_encoding(gio_r),encoding));
1.119 +
1.120 + line_term = g_io_channel_get_line_term(gio_r,NULL);
1.121 +
1.122 + //check g_io_channel_get_line_term
1.123 + g_assert(line_term == NULL);
1.124 +
1.125 + temp = g_io_channel_ref(gio_r);
1.126 +
1.127 + //check g_io_channel_ref
1.128 + g_assert(temp == gio_r && temp->ref_count == 2);
1.129 +
1.130 + g_io_channel_unref(gio_r);
1.131 +
1.132 + temp = NULL;
1.133 +
1.134 + status = g_io_channel_set_flags (gio_r, G_IO_FLAG_NONBLOCK, &gerr);
1.135 + if (status == G_IO_STATUS_ERROR)
1.136 + {
1.137 + g_warning (gerr->message);
1.138 + g_error_free (gerr);
1.139 + gerr = NULL;
1.140 + }
1.141 + flags = g_io_channel_get_flags (gio_r);
1.142 + buffer = g_string_sized_new (BUFFER_SIZE);
1.143 +
1.144 + status = g_io_channel_read_to_end(gio_r,&buf,&size_read,&gerr);
1.145 +
1.146 + // checks g_io_channel_read_to_end
1.147 + g_assert(status == G_IO_STATUS_NORMAL && gerr == NULL);
1.148 +
1.149 + status = g_io_channel_write_chars(gio_w,buf,size_read,&size_written,&gerr);
1.150 +
1.151 + // checks g_io_channel_write_chars
1.152 + g_assert(status == G_IO_STATUS_NORMAL && size_read == size_written && gerr == NULL);
1.153 +
1.154 + switch (status)
1.155 + {
1.156 + case G_IO_STATUS_NORMAL:
1.157 + break;
1.158 + case G_IO_STATUS_ERROR:
1.159 + g_warning (gerr->message);
1.160 + g_error_free (gerr);
1.161 + gerr = NULL;
1.162 + break;
1.163 + default:
1.164 + g_warning ("Abnormal exit from write loop.");
1.165 + break;
1.166 + }
1.167 +
1.168 + gio_condition = g_io_channel_get_buffer_condition(gio_w);
1.169 +
1.170 + //checks g_io_channel_get_buffer_condition
1.171 + g_assert(gio_condition == G_IO_OUT);
1.172 +
1.173 + do
1.174 + status = g_io_channel_flush (gio_w, &gerr);
1.175 + while (status == G_IO_STATUS_AGAIN);
1.176 +
1.177 + if (status == G_IO_STATUS_ERROR)
1.178 + {
1.179 + g_warning (gerr->message);
1.180 + g_error_free (gerr);
1.181 + gerr = NULL;
1.182 + }
1.183 +
1.184 +
1.185 + io_error = g_io_channel_seek(gio_r,0,G_SEEK_SET);
1.186 +
1.187 + // check g_io_channel_seek
1.188 + g_assert(io_error == G_IO_ERROR_NONE);
1.189 +
1.190 + status = g_io_channel_read_unichar(gio_r,&thechar,&gerr);
1.191 +
1.192 + //check g_io_channel_read_unichar
1.193 + g_assert(status == G_IO_STATUS_NORMAL && gerr == NULL);
1.194 +
1.195 + status = g_io_channel_write_unichar(gio_w,thechar,&gerr);
1.196 +
1.197 + //check g_io_channel_write_unichar
1.198 + g_assert(status == G_IO_STATUS_NORMAL && gerr == NULL);
1.199 +
1.200 + g_io_channel_set_line_term(gio_r,"L",-1);
1.201 +
1.202 + status = g_io_channel_read_line_string (gio_r, buffer, NULL, &gerr);
1.203 +
1.204 + //checks g_io_channel_set_line_term
1.205 + g_assert(buffer->str[strlen(buffer->str)-1] == 'L');
1.206 +
1.207 + do
1.208 + status = g_io_channel_flush (gio_w, &gerr);
1.209 + while (status == G_IO_STATUS_AGAIN);
1.210 +
1.211 +
1.212 + g_io_channel_unref(gio_r);
1.213 + g_io_channel_unref(gio_w);
1.214 +
1.215 + return 0;
1.216 +
1.217 +}
1.218 +
1.219 +int main()
1.220 +{
1.221 + int retval;
1.222 + #ifdef SYMBIAN
1.223 +
1.224 +
1.225 + g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
1.226 + #endif /*SYMBIAN*/
1.227 +
1.228 + retval = iochannel_test();
1.229 +
1.230 + #ifdef SYMBIAN
1.231 + testResultXml("iochannel_test");
1.232 + #endif /* EMULATOR */
1.233 +
1.234 + return retval;
1.235 +}
1.236 \ No newline at end of file