os/ossrv/genericopenlibs/cstdlib/TSTLIB/TMTHREAD.CPP
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/TSTLIB/TMTHREAD.CPP	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,174 @@
     1.4 +// Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// Test code for multi-threaded file descriptors etc.
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +#include <e32std.h>
    1.22 +#include <e32svr.h>	// for RDebug
    1.23 +#include <stdlib.h>
    1.24 +#include <unistd.h>
    1.25 +#include <stdio.h>
    1.26 +#include <string.h>
    1.27 +#include <sys/fcntl.h>
    1.28 +#include <sys/errno.h>
    1.29 +
    1.30 +extern "C" {
    1.31 +#include "CTEST.H"
    1.32 +}
    1.33 +
    1.34 +#include <estlib.h>	// for multi-threading control
    1.35 +
    1.36 +
    1.37 +#ifdef _DEBUG
    1.38 +	#define ttest_Next(testname)	\
    1.39 +	RDebug::Print(_L("t%d: %s"), (TInt)thread, testname); \
    1.40 +	printf("t%d: ", (TInt)thread); \
    1.41 +	test_Next(testname)
    1.42 +#else
    1.43 +	//don't use RDebug::Print when it's a release build
    1.44 +	#define ttest_Next(testname)   test_Next(testname)
    1.45 +#endif
    1.46 +
    1.47 +test_Data;
    1.48 +RSemaphore waiting[2];
    1.49 +
    1.50 +void over(TInt n)
    1.51 +	{
    1.52 +	// RDebug::Print(_L("Over(%d)"),n);
    1.53 +	waiting[1-n].Signal();
    1.54 +	waiting[n].Wait();
    1.55 +	}
    1.56 +
    1.57 +#define THREAD0		if (thread==0)
    1.58 +#define THREAD1		if (thread==(TAny*)1)
    1.59 +#define OVER		over((TInt)thread)
    1.60 +
    1.61 +// Shared variables
    1.62 +
    1.63 +int fd;
    1.64 +
    1.65 +/**
    1.66 +Interleaved test code
    1.67 +
    1.68 +@SYMTestCaseID          SYSLIB-STDLIB-CT-1071
    1.69 +@SYMTestCaseDesc	    Tests for multi-threaded file descriptors
    1.70 +@SYMTestPriority 	    High
    1.71 +@SYMTestActions  	    Run threads to open,read,close test files
    1.72 +@SYMTestExpectedResults Test must not fail
    1.73 +@SYMREQ                 REQ0000
    1.74 +*/		
    1.75 +TInt testfunction(TAny* thread)
    1.76 +	{
    1.77 +	int n;
    1.78 +	char *p;
    1.79 +	ttest_Next("Entering testfunction");
    1.80 +	//
    1.81 +	ttest_Next("Competing console reads - press 'A' then 'B'...");
    1.82 +	fflush(stdout);
    1.83 +	int c=getchar();
    1.84 +	fprintf(stderr, "t%d: read char %d\r\n", thread, c);
    1.85 +	//
    1.86 +	THREAD0
    1.87 +		{
    1.88 +		waiting[0].Wait();	// until Thread1 says OVER
    1.89 +		ttest_Next("Create test file");
    1.90 +		fd=open("c:\\testfile", O_RDWR+O_CREAT+O_TRUNC, 0777);
    1.91 +		test_ok(fd>=0);
    1.92 +		}
    1.93 +	//
    1.94 +	OVER;
    1.95 +	ttest_Next("Get the sequencing sorted out...");
    1.96 +	OVER;
    1.97 +	THREAD1
    1.98 +		{
    1.99 +		ttest_Next("Write to test file");
   1.100 +		p="Hello from thread 1\r\n";
   1.101 +		n=write(fd,p,strlen(p));
   1.102 +		test_ok(n==strlen(p));
   1.103 +		}
   1.104 +	THREAD0
   1.105 +		{
   1.106 +		ttest_Next("Close test file");
   1.107 +		close(fd);
   1.108 +		}
   1.109 +	OVER;
   1.110 +	THREAD1
   1.111 +		{
   1.112 +		ttest_Next("Reopen test file");
   1.113 +		fd=open("c:\\testfile",O_RDONLY,0);
   1.114 +		test_ok(fd>=0);
   1.115 +		}
   1.116 +	THREAD0
   1.117 +		{
   1.118 +		ttest_Next("Read from test file");
   1.119 +		char buf[80];
   1.120 +		buf[6]='\0';
   1.121 +		n=read(fd,buf,6);
   1.122 +		test_ok(n==6);
   1.123 +		test(strncmp(buf,"Hello ",6)==0);
   1.124 +		printf("Read >%s<... \r\n",buf);
   1.125 +		fflush(stdout);
   1.126 +		}
   1.127 +	OVER;
   1.128 +	THREAD1
   1.129 +		{
   1.130 +		ttest_Next("Read from test file");
   1.131 +		close(0);
   1.132 +		n=dup2(fd,0);
   1.133 +		test(n>=0);	// associate stdin with "testfile"
   1.134 +		char buf[80];
   1.135 +		p=fgets(buf,80,stdin);
   1.136 +		test_ok(p==buf);
   1.137 +		fprintf(stderr, "Read >%s<\r\n", buf);
   1.138 +		}
   1.139 +	THREAD0
   1.140 +		{
   1.141 +		ttest_Next("Close test file");
   1.142 +		close(fd);
   1.143 +		}
   1.144 +	OVER;
   1.145 +	ttest_Next("Completed testfunction");
   1.146 +	waiting[0].Signal();	// allow thread0 to continue
   1.147 +	waiting[1].Signal();	// allow thread1 to continue
   1.148 +	return 0;
   1.149 +	}
   1.150 +
   1.151 +// Thread management - main thread is thread0
   1.152 +
   1.153 +void init_threads()
   1.154 +	{
   1.155 +	waiting[0].CreateLocal(0);
   1.156 +	waiting[1].CreateLocal(0);
   1.157 +
   1.158 +	RThread thread1;
   1.159 +	TInt err=thread1.Create(_L("Thread1"),testfunction,0x10000,NULL,(TAny*)1);
   1.160 +	test(err==KErrNone);
   1.161 +	test_Next("Starting thread1");
   1.162 +	thread1.Resume();
   1.163 +	test_Next("entering main test...");
   1.164 +	}
   1.165 +
   1.166 +int main(int argc, char *argv[])
   1.167 +	{
   1.168 +	// SpawnPosixServerThread(); - provided by MCRT0.OBJ
   1.169 +
   1.170 +	start_redirection_server();
   1.171 +
   1.172 +	test_Title("TMTHREAD");
   1.173 +	init_threads();
   1.174 +	testfunction(0);
   1.175 +	test_Close();
   1.176 +	return KErrNone;
   1.177 +	}