kerneltest/e32test/misc/t_destruct.cpp
changeset 43 96e5fb8b040d
child 33 0173bcd7697c
equal deleted inserted replaced
-1:000000000000 43:96e5fb8b040d
       
     1 // Copyright (c) 2007-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 the License "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".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // e32test\misc\t_destruct.cpp
       
    15 // Test that global objects are destroyed correctly
       
    16 // 
       
    17 //
       
    18 
       
    19 #define __E32TEST_EXTENSION__
       
    20 
       
    21 #include <e32std.h>
       
    22 #include <e32std_private.h>
       
    23 #include <f32file.h>
       
    24 #include <e32test.h>
       
    25 #include <e32msgqueue.h>
       
    26 
       
    27 #include "t_destruct.h"
       
    28 
       
    29 _LIT(KDestructSlave, "t_destruct_slave");
       
    30 
       
    31 const char* KMessageNames[] =
       
    32 	{
       
    33 	"Construct",
       
    34 	"ConstructStatic",
       
    35 	"ConstructDynamic",
       
    36 	"ConstructStatic3",
       
    37 	"PreDestruct",
       
    38 	"Destruct",
       
    39 	"DestructStatic",
       
    40 	"DestructDynamic",
       
    41 	"DestructStatic3"
       
    42 	};
       
    43 
       
    44 RTest test(_L("t_destruct"));
       
    45 RMsgQueue<TMessage> MessageQueue;
       
    46 
       
    47 void TestNextMessage(TMessage aExpected, TBool& ok)
       
    48 	{
       
    49 	TMessage message;
       
    50 	TInt r = MessageQueue.Receive(message);
       
    51 	if (r == KErrUnderflow)
       
    52 		{
       
    53 		RDebug::Printf(" * expected message %s but got underflow", KMessageNames[aExpected]);
       
    54 		ok = EFalse;
       
    55 		return;
       
    56 		}
       
    57 	test_KErrNone(r);
       
    58 	test(message < ENumMessges);
       
    59 	test(aExpected < ENumMessges);
       
    60 	if (message == aExpected)
       
    61 		RDebug::Printf(" - received message %s", KMessageNames[message]);
       
    62 	else
       
    63 		{
       
    64 		RDebug::Printf(" * expected message %s but got %s", KMessageNames[aExpected], KMessageNames[message]);
       
    65 		ok = EFalse;
       
    66 		}
       
    67 	}
       
    68 
       
    69 void TestDestruction(TTestType aTestType)
       
    70 	{
       
    71 	TBuf<4> cmd;
       
    72 	cmd.AppendFormat(_L("%d"), aTestType);
       
    73 
       
    74 	RProcess p;
       
    75 	test_KErrNone(p.Create(KDestructSlave, cmd));
       
    76 	
       
    77 	TRequestStatus status;
       
    78 	p.Logon(status);
       
    79 	p.Resume();
       
    80 	User::WaitForRequest(status);
       
    81 
       
    82 	TExitType expectedExit = aTestType != ETestLastThreadPanic ? EExitKill : EExitPanic;
       
    83 	test_Equal(expectedExit, p.ExitType());
       
    84 	test_Equal(aTestType, p.ExitReason());
       
    85 
       
    86 	CLOSE_AND_WAIT(p);
       
    87 	
       
    88 	TMessage message;
       
    89 	TBool ok = ETrue;
       
    90 
       
    91 	TestNextMessage(EMessageConstructStatic3, ok);
       
    92 	TestNextMessage(EMessageConstructStatic, ok);
       
    93 	TestNextMessage(EMessageConstruct, ok);
       
    94 	TestNextMessage(EMessageConstructDynamic, ok);
       
    95 	TestNextMessage(EMessagePreDestruct, ok);
       
    96 	
       
    97 	if (aTestType != ETestLastThreadPanic && aTestType != ETestDestructorExits)
       
    98 		{
       
    99 		TestNextMessage(EMessageDestruct, ok);
       
   100 		TestNextMessage(EMessageDestructStatic, ok);
       
   101 		TestNextMessage(EMessageDestructStatic3, ok);
       
   102 		}
       
   103 	
       
   104 	if (aTestType != ETestLastThreadPanic)
       
   105 		TestNextMessage(EMessageDestructDynamic, ok);
       
   106 	
       
   107 	test(ok);
       
   108 	test_Equal(KErrUnderflow, MessageQueue.Receive(message));
       
   109 	}
       
   110 
       
   111 TInt E32Main()
       
   112 	{
       
   113     test.Title();
       
   114     test.Start(_L("t_destruct"));
       
   115 
       
   116 	// Turn off evil lazy dll unloading
       
   117 	RLoader l;
       
   118 	test_KErrNone(l.Connect());
       
   119 	test_KErrNone(l.CancelLazyDllUnload());
       
   120 	l.Close();
       
   121 
       
   122 	test_KErrNone(MessageQueue.CreateGlobal(KMessageQueueName, 10));
       
   123 	
       
   124 	test.Next(_L("Test global object destruction when main thread returns"));
       
   125 	TestDestruction(ETestMainThreadReturn);
       
   126 	
       
   127 	test.Next(_L("Test global object destruction when main thread exits"));
       
   128 	TestDestruction(ETestMainThreadExit);
       
   129 	
       
   130 	test.Next(_L("Test global object destruction when child thread exits"));
       
   131 	TestDestruction(ETestChildThreadReturn);
       
   132 
       
   133 	test.Next(_L("Test global object destruction when other thread has exited"));
       
   134 	TestDestruction(ETestOtherThreadExit);
       
   135 
       
   136 	test.Next(_L("Test global object destruction when other thread has panicked"));
       
   137 	TestDestruction(ETestOtherThreadPanic);
       
   138 
       
   139 	test.Next(_L("Test global object destruction when other thread killed by critial thread exit"));
       
   140 	TestDestruction(ETestOtherThreadRunning);
       
   141 
       
   142 	test.Next(_L("Test global object destruction when permanent thread exits"));
       
   143 	TestDestruction(ETestPermanentThreadExit);
       
   144 
       
   145 	test.Next(_L("Test global object destruction only happens once when destrctor creates new thread"));
       
   146 	TestDestruction(ETestRecursive);
       
   147 
       
   148 	test.Next(_L("Test global object destruction only happens once when destrctor calls User::Exit"));
       
   149 	TestDestruction(ETestDestructorExits);
       
   150 
       
   151 	test.Next(_L("Test NO global object destruction when last thread panics"));
       
   152 	TestDestruction(ETestLastThreadPanic);
       
   153 	
       
   154     test.End();
       
   155 	return KErrNone;
       
   156 	}