sl@0: /*
sl@0: * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0: * All rights reserved.
sl@0: * This component and the accompanying materials are made available
sl@0: * under the terms of "Eclipse Public License v1.0"
sl@0: * which accompanies this distribution, and is available
sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0: *
sl@0: * Initial Contributors:
sl@0: * Nokia Corporation - initial contribution.
sl@0: *
sl@0: * Contributors:
sl@0: *
sl@0: * Description:
sl@0: * Some file manipulation via STDIO
sl@0: * 
sl@0: *
sl@0: */
sl@0: 
sl@0: 
sl@0: 
sl@0: #include <stdlib.h>	/* definition of exit() */
sl@0: #include <stdio.h>
sl@0: #include <errno.h>
sl@0: #include <string.h>
sl@0: 
sl@0: #include <unistd.h>	/* for getcwd */
sl@0: #include <sys/fcntl.h>	/* for O_RDONLY */
sl@0: #include "CTEST.H"
sl@0: 
sl@0: test_Data;
sl@0: 
sl@0: char *poem[] = {
sl@0:     "I wandered lonely as a cloud\n",
sl@0:     "That floats on high o'er hill and vale\n",
sl@0:     0
sl@0: };
sl@0: 
sl@0: char *extraline = "Something something daffodils";
sl@0: 
sl@0: char *finalpoem = 
sl@0:     "I wandered lonely as a cloud\n"
sl@0:     "That floats on high o'er hill and vale\n"
sl@0:     "Something something daffodils";
sl@0: 
sl@0: wchar_t* filename = L"c:\\tfiles\x20AC.txt";
sl@0: 
sl@0: /**
sl@0: @SYMTestCaseID          SYSLIB-STDLIB-CT-1090
sl@0: @SYMTestCaseDesc	    Tests for POSIX 1003.1 file manipulation routines
sl@0: @SYMTestPriority 	    High
sl@0: @SYMTestActions  	    Create a new file and write some text into it,check for errors
sl@0:                         Open a file for appending,close and open to read back the file.Check for errors
sl@0: @SYMTestExpectedResults Test must not fail
sl@0: @SYMREQ                 REQ0000
sl@0: */		
sl@0: void daffodils()
sl@0: 	{
sl@0: 	FILE *in, *out;
sl@0: 	char *cp, buf[256];
sl@0: 	int n, err;
sl@0: 	
sl@0: 
sl@0: 	test_Next("Writing simple text file");
sl@0: 
sl@0: 	out = wfopen(filename, L"w");
sl@0: 	test(out != 0); 
sl@0: 
sl@0: 	for (n=0; poem[n]; n++)
sl@0: 	    {
sl@0: 	    err=fputs(poem[n], out);
sl@0: 		test(err>=0);
sl@0: 	    }
sl@0: 	fclose(out);
sl@0: 
sl@0: 	/* 2. Open a file for appending */
sl@0: 
sl@0: 	test_Next("Open file for appending");
sl@0: 
sl@0: 	out = wfopen(filename, L"a");
sl@0: 	test(out != 0);
sl@0: 
sl@0: 	n = fwrite(extraline, 1, strlen(extraline), out);
sl@0: 	test(n == (int)strlen(extraline));
sl@0: 	fclose(out);
sl@0: 
sl@0: 	/* 3. Read back the file */
sl@0: 
sl@0: 	test_Next("Read back the file using fread");
sl@0: 
sl@0: 	in = wfopen(filename, L"r");
sl@0: 	test(in != 0);
sl@0: 	cp = finalpoem;
sl@0: 
sl@0: 	do
sl@0: 	    {
sl@0: 	    n = fread(buf, 1, 17, in);
sl@0: 	    test(n >= 0);
sl@0: 		if (n>0)
sl@0: 			test(strncmp(buf,cp,n)==0);
sl@0: 		cp += n;
sl@0: 	    }
sl@0: 	while (!feof(in));
sl@0: 
sl@0: 	fclose(in);
sl@0: 
sl@0: 	/* 4. Read back the file a line at a time */
sl@0: 
sl@0: 	test_Next("Read back the file using fgets");
sl@0: 
sl@0: 	in = wfopen(filename, L"r");
sl@0: 	test(in != 0);
sl@0: 
sl@0: 	n = 0;
sl@0: 	do
sl@0: 	    {
sl@0: 	    cp = fgets(buf, sizeof(buf), in);
sl@0: 	    test(cp != NULL);
sl@0: 		if (poem[n])
sl@0: 			{
sl@0: 			test(strcmp(cp,poem[n])==0);
sl@0: 			n++;
sl@0: 			}
sl@0: 		else
sl@0: 			test(strcmp(cp,extraline)==0);
sl@0: 	    }
sl@0: 	while (!feof(in));
sl@0: 
sl@0: 	fclose(in);
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: @SYMTestCaseID          SYSLIB-STDLIB-CT-1091
sl@0: @SYMTestCaseDesc	    Tests for POSIX 1003.1 file manipulation routines
sl@0: @SYMTestPriority 	    High
sl@0: @SYMTestActions  	    Open a file 100 times and close them in different orders.Test for the error code. 
sl@0: @SYMTestExpectedResults Test must not fail
sl@0: @SYMREQ                 REQ0000
sl@0: */		
sl@0: #define NFILES 100
sl@0: void maxfiles()
sl@0: 	{
sl@0: 	int fids[NFILES];
sl@0: 	int i,n;
sl@0: 
sl@0: 	test_Next("Open file 100 times, close in reverse order");
sl@0: 	for (i=0; i<NFILES; i++)
sl@0: 		{
sl@0: 		fids[i] = wopen(filename, O_RDONLY);
sl@0: 		test(fids[i]>=0);
sl@0: 		}
sl@0: 	for (i=NFILES-1; i>=0; i--)
sl@0: 		{
sl@0: 		close(fids[i]);
sl@0: 		}
sl@0: 
sl@0: 	test_Next("Open file 100 times, close in same order");
sl@0: 	for (i=0; i<NFILES; i++)
sl@0: 		{
sl@0: 		fids[i] = wopen(filename, O_RDONLY);
sl@0: 		test(fids[i]>=0);
sl@0: 		}
sl@0: 	for (i=0; i<NFILES; i++)
sl@0: 		{
sl@0: 		close(fids[i]);
sl@0: 		}
sl@0: 
sl@0: 	test_Next("Open file 100 times, close in mod 7 order");
sl@0: 	for (i=0; i<NFILES; i++)
sl@0: 		{
sl@0: 		fids[i] = wopen(filename, O_RDONLY);
sl@0: 		test(fids[i]>=0);
sl@0: 		}
sl@0: 	for (i=0,n=0; i<NFILES; i++)
sl@0: 		{
sl@0: 		close(fids[n]);
sl@0: 		n += 7;
sl@0: 		if (n >= NFILES)
sl@0: 			n -= NFILES;
sl@0: 		}
sl@0: 	}
sl@0: 
sl@0: int close_console=0;
sl@0: void allTests()
sl@0: 	{
sl@0: 	daffodils();
sl@0: 	maxfiles();
sl@0: 
sl@0: 	if (close_console)
sl@0: 		{
sl@0: 		test_Close();
sl@0: 		close(0);
sl@0: 		close(1);
sl@0: 		close(2);
sl@0: 		}
sl@0: 	}
sl@0: 
sl@0: int main()
sl@0: 	{
sl@0: 	void* client;
sl@0: 	int err;
sl@0: 
sl@0: 	test_Title("TWFILES");
sl@0: 
sl@0: 	allTests();
sl@0: 
sl@0: 	test_Next("Do it again using the CPosixServer (for them, not me)");
sl@0: 	close_console=1;
sl@0: 
sl@0: 	start_posix_server();	/* calls SpawnPosixServer from C++ code */
sl@0: 
sl@0: 
sl@0: 	client=create_thread(allTests, "TWFILES tests");
sl@0: 	test(client!=0);
sl@0: 	start_thread(client);
sl@0: 	err=wait_for_thread(client);
sl@0: 	test(err==0);
sl@0: 
sl@0: 	test_Close();
sl@0: 
sl@0: 	return 0;
sl@0: 	}