home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / join.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.7 KB  |  200 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /***********************************************************************
  20. **
  21. ** Name: dbmalloc1.c
  22. **
  23. ** Description: Tests PR_SetMallocCountdown PR_ClearMallocCountdown functions.
  24. **
  25. ** Modification History:
  26. ** 
  27. ** 19-May-97 AGarcia - separate the four join tests into different unit test modules.
  28. **            AGarcia- Converted the test to accomodate the debug_mode flag.
  29. **          The debug mode will print all of the printfs associated with this test.
  30. **            The regress mode will be the default mode. Since the regress tool limits
  31. **          the output to a one line status:PASS or FAIL,all of the printf statements
  32. **            have been handled with an if (debug_mode) statement. 
  33. ***********************************************************************/
  34.  
  35. /***********************************************************************
  36. ** Includes
  37. ***********************************************************************/
  38. /* Used to get the command line option */
  39. #include "plgetopt.h"
  40. #include "prttools.h"
  41.  
  42. #include "nspr.h"
  43.  
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47.  
  48. #ifdef XP_MAC
  49. #include "prlog.h"
  50. #define printf PR_LogPrint
  51. extern void SetupMacPrintfLog(char *logFile);
  52. #endif
  53. /***********************************************************************
  54. ** PRIVATE FUNCTION:    Test_Result
  55. ** DESCRIPTION: Used in conjunction with the regress tool, prints out the
  56. **                status of the test case.
  57. ** INPUTS:      PASS/FAIL
  58. ** OUTPUTS:     None
  59. ** RETURN:      None
  60. ** SIDE EFFECTS:
  61. **      
  62. ** RESTRICTIONS:
  63. **      None
  64. ** MEMORY:      NA
  65. ** ALGORITHM:   Determine what the status is and print accordingly.
  66. **      
  67. ***********************************************************************/
  68.  
  69.  
  70. static Test_Result (int result)
  71. {
  72.     if (result == PASS)
  73.         printf ("PASS\n");
  74.     else
  75.         printf ("FAIL\n");
  76.     exit (1);
  77. }
  78.  
  79.  
  80. /*
  81.     Program to test joining of threads.  Two threads are created.  One
  82.     to be waited upon until it has started.  The other to join after it has
  83.     completed.
  84. */
  85.  
  86.  
  87. static void PR_CALLBACK lowPriority(void *arg)
  88. {
  89. }
  90.  
  91. static void PR_CALLBACK highPriority(void *arg)
  92. {
  93. }
  94.  
  95. void runTest(PRThreadScope scope1, PRThreadScope scope2)
  96. {
  97.     PRThread *low,*high;
  98.  
  99.     /* create the low and high priority threads */
  100.     
  101.     low = PR_CreateThread(PR_USER_THREAD,
  102.                      lowPriority, 0, 
  103.                      PR_PRIORITY_LOW,
  104.                      scope1,
  105.                      PR_JOINABLE_THREAD,
  106.                      0);
  107.     if (!low) {
  108.         if (debug_mode) printf("\tcannot create low priority thread\n");
  109.         else Test_Result(FAIL);
  110.         return;
  111.     }
  112.  
  113.     high = PR_CreateThread(PR_USER_THREAD,
  114.                      highPriority, 0, 
  115.                      PR_PRIORITY_HIGH,
  116.                      scope2,
  117.                      PR_JOINABLE_THREAD,
  118.                      0);
  119.     if (!high) {
  120.         if (debug_mode) printf("\tcannot create high priority thread\n");
  121.         else Test_Result(FAIL);
  122.         return;
  123.     }
  124.  
  125.     /* Do the joining for both threads */
  126.     if (PR_JoinThread(low) == PR_FAILURE) {
  127.         if (debug_mode) printf("\tcannot join low priority thread\n");
  128.         else Test_Result (FAIL);
  129.         return;
  130.     } else {
  131.         if (debug_mode) printf("\tjoined low priority thread\n");
  132.     }
  133.     if (PR_JoinThread(high) == PR_FAILURE) {
  134.         if (debug_mode) printf("\tcannot join high priority thread\n");
  135.         else Test_Result(FAIL);
  136.         return;
  137.     } else {
  138.         if (debug_mode) printf("\tjoined high priority thread\n");
  139.     }
  140. }
  141.  
  142. static PRIntn PR_CALLBACK RealMain(int argc, char **argv)
  143. {
  144.     /* The command line argument: -d is used to determine if the test is being run
  145.     in debug mode. The regress tool requires only one line output:PASS or FAIL.
  146.     All of the printfs associated with this test has been handled with a if (debug_mode)
  147.     test.
  148.     Usage: test_name -d
  149.     */
  150.     
  151.     PLOptStatus os;
  152.     PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
  153.     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  154.     {
  155.         if (PL_OPT_BAD == os) continue;
  156.         switch (opt->option)
  157.         {
  158.         case 'd':  /* debug mode */
  159.             debug_mode = 1;
  160.             break;
  161.          default:
  162.             break;
  163.         }
  164.     }
  165.     PL_DestroyOptState(opt);
  166.  
  167. #ifdef XP_MAC
  168.     SetupMacPrintfLog("join.log");
  169.     debug_mode = 1;
  170. #endif
  171.  
  172.     
  173.     
  174.  /* main test */
  175.     printf("User-User test\n");
  176.     runTest(PR_LOCAL_THREAD, PR_LOCAL_THREAD);
  177.     printf("User-Kernel test\n");
  178.     runTest(PR_LOCAL_THREAD, PR_GLOBAL_THREAD);
  179.     printf("Kernel-User test\n");
  180.     runTest(PR_GLOBAL_THREAD, PR_LOCAL_THREAD);
  181.     printf("Kernel-Kernel test\n");
  182.     runTest(PR_GLOBAL_THREAD, PR_GLOBAL_THREAD);
  183.  
  184.     printf("PASSED\n");
  185.  
  186.     return 0;
  187. }
  188.  
  189.  
  190.  
  191.  
  192. PRIntn main(PRIntn argc, char *argv[])
  193. {
  194.     PRIntn rv;
  195.     
  196.     PR_STDIO_INIT();
  197.     rv = PR_Initialize(RealMain, argc, argv, 0);
  198.     return rv;
  199. }  /* main */
  200.