1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/TSTLIB/TFILES.C Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,222 @@
1.4 +/*
1.5 +* Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +* Some file manipulation via STDIO
1.19 +*
1.20 +*
1.21 +*/
1.22 +
1.23 +
1.24 +
1.25 +#include <stdlib.h> /* definition of exit() */
1.26 +#include <stdio.h>
1.27 +#include <errno.h>
1.28 +#include <string.h>
1.29 +
1.30 +#include <unistd.h> /* for getcwd */
1.31 +#include <sys/fcntl.h> /* for O_RDONLY */
1.32 +#include "CTEST.H"
1.33 +
1.34 +test_Data;
1.35 +
1.36 +char *poem[] = {
1.37 + "I wandered lonely as a cloud\n",
1.38 + "That floats on high o'er hill and vale\n",
1.39 + 0
1.40 +};
1.41 +
1.42 +char *extraline = "Something something daffodils";
1.43 +
1.44 +char *finalpoem =
1.45 + "I wandered lonely as a cloud\n"
1.46 + "That floats on high o'er hill and vale\n"
1.47 + "Something something daffodils";
1.48 +
1.49 +char* filename = "c:\\tfiles.txt";
1.50 +
1.51 +/**
1.52 +@SYMTestCaseID SYSLIB-STDLIB-CT-1055
1.53 +@SYMTestCaseDesc Tests for file manipulation operations
1.54 +@SYMTestPriority High
1.55 +@SYMTestActions Create a new file and write some text into it,check for errors
1.56 + Open a file for appending,close and open to read back the file.Check for errors
1.57 +@SYMTestExpectedResults Test must not fail
1.58 +@SYMREQ REQ0000
1.59 +*/
1.60 +void daffodils()
1.61 + {
1.62 + FILE *in, *out;
1.63 + char *cp, buf[256];
1.64 + int n, err;
1.65 +
1.66 +
1.67 + test_Next("Writing simple text file");
1.68 +
1.69 + out = fopen(filename, "w");
1.70 + test(out != 0);
1.71 +
1.72 + for (n=0; poem[n]; n++)
1.73 + {
1.74 + err=fputs(poem[n], out);
1.75 + test(err>=0);
1.76 + }
1.77 + fclose(out);
1.78 +
1.79 + /* 2. Open a file for appending */
1.80 +
1.81 + test_Next("Open file for appending");
1.82 +
1.83 + out = fopen(filename, "a");
1.84 + test(out != 0);
1.85 +
1.86 + n = fwrite(extraline, 1, strlen(extraline), out);
1.87 + test(n == (int)strlen(extraline));
1.88 + fclose(out);
1.89 +
1.90 + /* 3. Read back the file */
1.91 +
1.92 + test_Next("Read back the file using fread");
1.93 +
1.94 + in = fopen(filename, "r");
1.95 + test(in != 0);
1.96 + cp = finalpoem;
1.97 +
1.98 + do
1.99 + {
1.100 + n = fread(buf, 1, 17, in);
1.101 + test(n >= 0);
1.102 + if (n>0)
1.103 + test(strncmp(buf,cp,n)==0);
1.104 + cp += n;
1.105 + }
1.106 + while (!feof(in));
1.107 +
1.108 + fclose(in);
1.109 +
1.110 + /* 4. Read back the file a line at a time */
1.111 +
1.112 + test_Next("Read back the file using fgets");
1.113 +
1.114 + in = fopen(filename, "r");
1.115 + test(in != 0);
1.116 +
1.117 + n = 0;
1.118 + do
1.119 + {
1.120 + cp = fgets(buf, sizeof(buf), in);
1.121 + test(cp != NULL);
1.122 + if (poem[n])
1.123 + {
1.124 + test(strcmp(cp,poem[n])==0);
1.125 + n++;
1.126 + }
1.127 + else
1.128 + test(strcmp(cp,extraline)==0);
1.129 + }
1.130 + while (!feof(in));
1.131 +
1.132 + fclose(in);
1.133 + }
1.134 +
1.135 +/**
1.136 +@SYMTestCaseID SYSLIB-STDLIB-CT-1056
1.137 +@SYMTestCaseDesc Tests for maximum files open and close operations
1.138 +@SYMTestPriority High
1.139 +@SYMTestActions Open a file 100 times and close them in different orders.Tests for the error code.
1.140 +@SYMTestExpectedResults Test must not fail
1.141 +@SYMREQ REQ0000
1.142 +*/
1.143 +#define NFILES 100
1.144 +void maxfiles()
1.145 + {
1.146 + int fids[NFILES];
1.147 + int i,n;
1.148 +
1.149 + test_Next("Open file 100 times, close in reverse order");
1.150 + for (i=0; i<NFILES; i++)
1.151 + {
1.152 + fids[i] = open(filename, O_RDONLY, 0666);
1.153 + test(fids[i]>=0);
1.154 + }
1.155 + for (i=NFILES-1; i>=0; i--)
1.156 + {
1.157 + close(fids[i]);
1.158 + }
1.159 +
1.160 + test_Next("Open file 100 times, close in same order");
1.161 + for (i=0; i<NFILES; i++)
1.162 + {
1.163 + fids[i] = open(filename, O_RDONLY, 0666);
1.164 + test(fids[i]>=0);
1.165 + }
1.166 + for (i=0; i<NFILES; i++)
1.167 + {
1.168 + close(fids[i]);
1.169 + }
1.170 +
1.171 + test_Next("Open file 100 times, close in mod 7 order");
1.172 + for (i=0; i<NFILES; i++)
1.173 + {
1.174 + fids[i] = open(filename, O_RDONLY, 0666);
1.175 + test(fids[i]>=0);
1.176 + }
1.177 + for (i=0,n=0; i<NFILES; i++)
1.178 + {
1.179 + close(fids[n]);
1.180 + n += 7;
1.181 + if (n >= NFILES)
1.182 + n -= NFILES;
1.183 + }
1.184 + }
1.185 +
1.186 +int close_console=0;
1.187 +void allTests()
1.188 + {
1.189 + daffodils();
1.190 + maxfiles();
1.191 +
1.192 + if (close_console)
1.193 + {
1.194 + test_Close();
1.195 + close(0);
1.196 + close(1);
1.197 + close(2);
1.198 + }
1.199 + }
1.200 +
1.201 +int main()
1.202 + {
1.203 + void* client;
1.204 + int err;
1.205 +
1.206 + test_Title("TFILES");
1.207 +
1.208 + allTests();
1.209 +
1.210 + test_Next("Do it again using the CPosixServer (for them, not me)");
1.211 + close_console=1;
1.212 +
1.213 + start_posix_server(); /* calls SpawnPosixServer from C++ code */
1.214 +
1.215 +
1.216 + client=create_thread(allTests, "TFILES tests");
1.217 + test(client!=0);
1.218 + start_thread(client);
1.219 + err=wait_for_thread(client);
1.220 + test(err==0);
1.221 +
1.222 + test_Close();
1.223 +
1.224 + return 0;
1.225 + }