genericopenlibs/cstdlib/TSTLIB/TWFILES.C
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /*
       
     2 * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 * Some file manipulation via STDIO
       
    16 * 
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 
       
    22 #include <stdlib.h>	/* definition of exit() */
       
    23 #include <stdio.h>
       
    24 #include <errno.h>
       
    25 #include <string.h>
       
    26 
       
    27 #include <unistd.h>	/* for getcwd */
       
    28 #include <sys/fcntl.h>	/* for O_RDONLY */
       
    29 #include "CTEST.H"
       
    30 
       
    31 test_Data;
       
    32 
       
    33 char *poem[] = {
       
    34     "I wandered lonely as a cloud\n",
       
    35     "That floats on high o'er hill and vale\n",
       
    36     0
       
    37 };
       
    38 
       
    39 char *extraline = "Something something daffodils";
       
    40 
       
    41 char *finalpoem = 
       
    42     "I wandered lonely as a cloud\n"
       
    43     "That floats on high o'er hill and vale\n"
       
    44     "Something something daffodils";
       
    45 
       
    46 wchar_t* filename = L"c:\\tfiles\x20AC.txt";
       
    47 
       
    48 /**
       
    49 @SYMTestCaseID          SYSLIB-STDLIB-CT-1090
       
    50 @SYMTestCaseDesc	    Tests for POSIX 1003.1 file manipulation routines
       
    51 @SYMTestPriority 	    High
       
    52 @SYMTestActions  	    Create a new file and write some text into it,check for errors
       
    53                         Open a file for appending,close and open to read back the file.Check for errors
       
    54 @SYMTestExpectedResults Test must not fail
       
    55 @SYMREQ                 REQ0000
       
    56 */		
       
    57 void daffodils()
       
    58 	{
       
    59 	FILE *in, *out;
       
    60 	char *cp, buf[256];
       
    61 	int n, err;
       
    62 	
       
    63 
       
    64 	test_Next("Writing simple text file");
       
    65 
       
    66 	out = wfopen(filename, L"w");
       
    67 	test(out != 0); 
       
    68 
       
    69 	for (n=0; poem[n]; n++)
       
    70 	    {
       
    71 	    err=fputs(poem[n], out);
       
    72 		test(err>=0);
       
    73 	    }
       
    74 	fclose(out);
       
    75 
       
    76 	/* 2. Open a file for appending */
       
    77 
       
    78 	test_Next("Open file for appending");
       
    79 
       
    80 	out = wfopen(filename, L"a");
       
    81 	test(out != 0);
       
    82 
       
    83 	n = fwrite(extraline, 1, strlen(extraline), out);
       
    84 	test(n == (int)strlen(extraline));
       
    85 	fclose(out);
       
    86 
       
    87 	/* 3. Read back the file */
       
    88 
       
    89 	test_Next("Read back the file using fread");
       
    90 
       
    91 	in = wfopen(filename, L"r");
       
    92 	test(in != 0);
       
    93 	cp = finalpoem;
       
    94 
       
    95 	do
       
    96 	    {
       
    97 	    n = fread(buf, 1, 17, in);
       
    98 	    test(n >= 0);
       
    99 		if (n>0)
       
   100 			test(strncmp(buf,cp,n)==0);
       
   101 		cp += n;
       
   102 	    }
       
   103 	while (!feof(in));
       
   104 
       
   105 	fclose(in);
       
   106 
       
   107 	/* 4. Read back the file a line at a time */
       
   108 
       
   109 	test_Next("Read back the file using fgets");
       
   110 
       
   111 	in = wfopen(filename, L"r");
       
   112 	test(in != 0);
       
   113 
       
   114 	n = 0;
       
   115 	do
       
   116 	    {
       
   117 	    cp = fgets(buf, sizeof(buf), in);
       
   118 	    test(cp != NULL);
       
   119 		if (poem[n])
       
   120 			{
       
   121 			test(strcmp(cp,poem[n])==0);
       
   122 			n++;
       
   123 			}
       
   124 		else
       
   125 			test(strcmp(cp,extraline)==0);
       
   126 	    }
       
   127 	while (!feof(in));
       
   128 
       
   129 	fclose(in);
       
   130 	}
       
   131 
       
   132 /**
       
   133 @SYMTestCaseID          SYSLIB-STDLIB-CT-1091
       
   134 @SYMTestCaseDesc	    Tests for POSIX 1003.1 file manipulation routines
       
   135 @SYMTestPriority 	    High
       
   136 @SYMTestActions  	    Open a file 100 times and close them in different orders.Test for the error code. 
       
   137 @SYMTestExpectedResults Test must not fail
       
   138 @SYMREQ                 REQ0000
       
   139 */		
       
   140 #define NFILES 100
       
   141 void maxfiles()
       
   142 	{
       
   143 	int fids[NFILES];
       
   144 	int i,n;
       
   145 
       
   146 	test_Next("Open file 100 times, close in reverse order");
       
   147 	for (i=0; i<NFILES; i++)
       
   148 		{
       
   149 		fids[i] = wopen(filename, O_RDONLY);
       
   150 		test(fids[i]>=0);
       
   151 		}
       
   152 	for (i=NFILES-1; i>=0; i--)
       
   153 		{
       
   154 		close(fids[i]);
       
   155 		}
       
   156 
       
   157 	test_Next("Open file 100 times, close in same order");
       
   158 	for (i=0; i<NFILES; i++)
       
   159 		{
       
   160 		fids[i] = wopen(filename, O_RDONLY);
       
   161 		test(fids[i]>=0);
       
   162 		}
       
   163 	for (i=0; i<NFILES; i++)
       
   164 		{
       
   165 		close(fids[i]);
       
   166 		}
       
   167 
       
   168 	test_Next("Open file 100 times, close in mod 7 order");
       
   169 	for (i=0; i<NFILES; i++)
       
   170 		{
       
   171 		fids[i] = wopen(filename, O_RDONLY);
       
   172 		test(fids[i]>=0);
       
   173 		}
       
   174 	for (i=0,n=0; i<NFILES; i++)
       
   175 		{
       
   176 		close(fids[n]);
       
   177 		n += 7;
       
   178 		if (n >= NFILES)
       
   179 			n -= NFILES;
       
   180 		}
       
   181 	}
       
   182 
       
   183 int close_console=0;
       
   184 void allTests()
       
   185 	{
       
   186 	daffodils();
       
   187 	maxfiles();
       
   188 
       
   189 	if (close_console)
       
   190 		{
       
   191 		test_Close();
       
   192 		close(0);
       
   193 		close(1);
       
   194 		close(2);
       
   195 		}
       
   196 	}
       
   197 
       
   198 int main()
       
   199 	{
       
   200 	void* client;
       
   201 	int err;
       
   202 
       
   203 	test_Title("TWFILES");
       
   204 
       
   205 	allTests();
       
   206 
       
   207 	test_Next("Do it again using the CPosixServer (for them, not me)");
       
   208 	close_console=1;
       
   209 
       
   210 	start_posix_server();	/* calls SpawnPosixServer from C++ code */
       
   211 
       
   212 
       
   213 	client=create_thread(allTests, "TWFILES tests");
       
   214 	test(client!=0);
       
   215 	start_thread(client);
       
   216 	err=wait_for_thread(client);
       
   217 	test(err==0);
       
   218 
       
   219 	test_Close();
       
   220 
       
   221 	return 0;
       
   222 	}