Update contrib.
1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // Test code for multi-threaded file descriptors etc.
19 #include <e32svr.h> // for RDebug
24 #include <sys/fcntl.h>
25 #include <sys/errno.h>
31 #include <estlib.h> // for multi-threading control
35 #define ttest_Next(testname) \
36 RDebug::Print(_L("t%d: %s"), (TInt)thread, testname); \
37 printf("t%d: ", (TInt)thread); \
40 //don't use RDebug::Print when it's a release build
41 #define ttest_Next(testname) test_Next(testname)
45 RSemaphore waiting[2];
49 // RDebug::Print(_L("Over(%d)"),n);
50 waiting[1-n].Signal();
54 #define THREAD0 if (thread==0)
55 #define THREAD1 if (thread==(TAny*)1)
56 #define OVER over((TInt)thread)
65 @SYMTestCaseID SYSLIB-STDLIB-CT-1071
66 @SYMTestCaseDesc Tests for multi-threaded file descriptors
68 @SYMTestActions Run threads to open,read,close test files
69 @SYMTestExpectedResults Test must not fail
72 TInt testfunction(TAny* thread)
76 ttest_Next("Entering testfunction");
78 ttest_Next("Competing console reads - press 'A' then 'B'...");
81 fprintf(stderr, "t%d: read char %d\r\n", thread, c);
85 waiting[0].Wait(); // until Thread1 says OVER
86 ttest_Next("Create test file");
87 fd=open("c:\\testfile", O_RDWR+O_CREAT+O_TRUNC, 0777);
92 ttest_Next("Get the sequencing sorted out...");
96 ttest_Next("Write to test file");
97 p="Hello from thread 1\r\n";
98 n=write(fd,p,strlen(p));
99 test_ok(n==strlen(p));
103 ttest_Next("Close test file");
109 ttest_Next("Reopen test file");
110 fd=open("c:\\testfile",O_RDONLY,0);
115 ttest_Next("Read from test file");
120 test(strncmp(buf,"Hello ",6)==0);
121 printf("Read >%s<... \r\n",buf);
127 ttest_Next("Read from test file");
130 test(n>=0); // associate stdin with "testfile"
132 p=fgets(buf,80,stdin);
134 fprintf(stderr, "Read >%s<\r\n", buf);
138 ttest_Next("Close test file");
142 ttest_Next("Completed testfunction");
143 waiting[0].Signal(); // allow thread0 to continue
144 waiting[1].Signal(); // allow thread1 to continue
148 // Thread management - main thread is thread0
152 waiting[0].CreateLocal(0);
153 waiting[1].CreateLocal(0);
156 TInt err=thread1.Create(_L("Thread1"),testfunction,0x10000,NULL,(TAny*)1);
158 test_Next("Starting thread1");
160 test_Next("entering main test...");
163 int main(int argc, char *argv[])
165 // SpawnPosixServerThread(); - provided by MCRT0.OBJ
167 start_redirection_server();
169 test_Title("TMTHREAD");