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