home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / tpd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  7.5 KB  |  297 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. ** File:        tpd.c
  21. ** Description: Exercising the thread private data bailywick.
  22. */
  23.  
  24. #include "prinit.h"
  25. #include "prlog.h"
  26. #include "prprf.h"
  27. #include "prthread.h"
  28. #include "prtypes.h"
  29.  
  30. #if defined(XP_MAC)
  31. #include "pprio.h"
  32. #else
  33. #include "private/pprio.h"
  34. #endif
  35.  
  36. #include "plgetopt.h"
  37.  
  38. static PRUintn key[128];
  39. static PRIntn debug = 0;
  40. static PRBool failed = PR_FALSE;
  41. static PRBool should = PR_TRUE;
  42. static PRBool did = PR_TRUE;
  43. static PRFileDesc *fout = NULL;
  44.  
  45. static void PrintProgress(void)
  46. {
  47.     if (debug > 0)
  48.     {
  49. #if defined(WIN16)
  50.         printf(
  51.             "Destructor should %s have been called and was%s\n",
  52.             ((should) ? "" : " NOT"), ((did) ? "" : " NOT"));
  53. #else    
  54.         PR_fprintf(
  55.             fout, "Destructor should %s have been called and was%s\n",
  56.             ((should) ? "" : " NOT"), ((did) ? "" : " NOT"));
  57. #endif
  58.     }
  59. }  /* PrintProgress */
  60.  
  61. static void MyAssert(const char *expr, const char *file, PRIntn line)
  62. {
  63.     if (debug > 0)
  64.         (void)PR_fprintf(fout, "'%s' in file: %s: %d\n", expr, file, line);
  65. }  /* MyAssert */
  66.  
  67. #define MY_ASSERT(_expr) \
  68.     ((_expr)?((void)0):MyAssert(# _expr,__FILE__,__LINE__))
  69.  
  70.  
  71. static void PR_CALLBACK Destructor(void *data)
  72. {
  73.     if (should) did = PR_TRUE;
  74.     else failed = PR_TRUE;
  75.     MY_ASSERT(NULL != data);
  76.     if (debug > 0) MyAssert((const char*)data, __FILE__, __LINE__);
  77. }  /* Destructor */
  78.  
  79. static void PR_CALLBACK Thread(void *null)
  80. {
  81.     void *pd;
  82.     PRStatus rv;
  83.     PRUintn keys;
  84.     char *key_string[] = {
  85.         "Key #0", "Key #1", "Key #2", "Key #3",
  86.         "Bogus #5", "Bogus #6", "Bogus #7", "Bogus #8"};
  87.     
  88.     did = should = PR_FALSE;
  89.     for (keys = 0; keys < 8; ++keys)
  90.     {
  91.         pd = PR_GetThreadPrivate(key[keys]);
  92.         MY_ASSERT(NULL == pd);
  93.     }
  94.     PrintProgress();
  95.  
  96.     did = should = PR_FALSE;
  97.     for (keys = 0; keys < 4; ++keys)
  98.     {
  99.         rv = PR_SetThreadPrivate(keys, key_string[keys]);
  100.         MY_ASSERT(PR_SUCCESS == rv);
  101.     }
  102.     PrintProgress();
  103.  
  104. #if !defined(DEBUG)
  105.     did = should = PR_FALSE;
  106.     for (keys = 4; keys < 8; ++keys)
  107.     {
  108.         rv = PR_SetThreadPrivate(keys, key_string[keys]);
  109.         MY_ASSERT(PR_FAILURE == rv);
  110.     }
  111.     PrintProgress();
  112. #endif
  113.     
  114.     did = PR_FALSE; should = PR_TRUE;
  115.     for (keys = 0; keys < 4; ++keys)
  116.     {
  117.         rv = PR_SetThreadPrivate(key[keys], key_string[keys]);
  118.         MY_ASSERT(PR_SUCCESS == rv);
  119.     }
  120.     PrintProgress();
  121.  
  122.     did = PR_FALSE; should = PR_TRUE;
  123.     for (keys = 0; keys < 4; ++keys)
  124.     {
  125.         rv = PR_SetThreadPrivate(key[keys], NULL);
  126.         MY_ASSERT(PR_SUCCESS == rv);
  127.     }
  128.     PrintProgress();
  129.  
  130.     did = should = PR_FALSE;
  131.     for (keys = 0; keys < 4; ++keys)
  132.     {
  133.         rv = PR_SetThreadPrivate(key[keys], NULL);
  134.         MY_ASSERT(PR_SUCCESS == rv);
  135.     }
  136.     PrintProgress();
  137.  
  138.     did = should = PR_FALSE;
  139.     for (keys = 8; keys < 127; ++keys)
  140.     {
  141.         rv = PR_SetThreadPrivate(key[keys], "EXTENSION");
  142.         MY_ASSERT(PR_SUCCESS == rv);
  143.     }
  144.     PrintProgress();
  145.  
  146.     did = PR_FALSE; should = PR_TRUE;
  147.     for (keys = 8; keys < 127; ++keys)
  148.     {
  149.         rv = PR_SetThreadPrivate(key[keys], NULL);
  150.         MY_ASSERT(PR_SUCCESS == rv);
  151.     }
  152.     PrintProgress();
  153.  
  154.     did = should = PR_FALSE;
  155.     for (keys = 8; keys < 127; ++keys)
  156.     {
  157.         rv = PR_SetThreadPrivate(key[keys], NULL);
  158.         MY_ASSERT(PR_SUCCESS == rv);
  159.     }
  160. }  /* Thread */
  161.  
  162. static PRIntn PR_CALLBACK Tpd(PRIntn argc, char **argv)
  163. {
  164.     void *pd;
  165.     PRStatus rv;
  166.     PRUintn keys;
  167.     PRThread *thread;
  168.     char *key_string[] = {
  169.         "Key #0", "Key #1", "Key #2", "Key #3",
  170.         "Bogus #5", "Bogus #6", "Bogus #7", "Bogus #8"};
  171.     
  172.     fout = PR_STDOUT;
  173.  
  174.     did = should = PR_FALSE;
  175.     for (keys = 0; keys < 4; ++keys)
  176.     {
  177.         rv = PR_NewThreadPrivateIndex(&key[keys], Destructor);
  178.         MY_ASSERT(PR_SUCCESS == rv);
  179.     }
  180.     PrintProgress();
  181.  
  182.     did = should = PR_FALSE;
  183.     for (keys = 0; keys < 8; ++keys)
  184.     {
  185.         pd = PR_GetThreadPrivate(key[keys]);
  186.         MY_ASSERT(NULL == pd);
  187.     }
  188.     PrintProgress();
  189.  
  190.     did = should = PR_FALSE;
  191.     for (keys = 0; keys < 4; ++keys)
  192.     {
  193.         rv = PR_SetThreadPrivate(keys, key_string[keys]);
  194.         MY_ASSERT(PR_SUCCESS == rv);
  195.     }
  196.     PrintProgress();
  197.  
  198. #if !defined(DEBUG)
  199.     did = should = PR_FALSE;
  200.     for (keys = 4; keys < 8; ++keys)
  201.     {
  202.         rv = PR_SetThreadPrivate(keys, key_string[keys]);
  203.         MY_ASSERT(PR_FAILURE == rv);
  204.     }
  205.     PrintProgress();
  206. #endif
  207.     
  208.     did = PR_FALSE; should = PR_TRUE;
  209.     for (keys = 0; keys < 4; ++keys)
  210.     {
  211.         rv = PR_SetThreadPrivate(key[keys], key_string[keys]);
  212.         MY_ASSERT(PR_SUCCESS == rv);
  213.     }
  214.     PrintProgress();
  215.  
  216.     did = PR_FALSE; should = PR_TRUE;
  217.     for (keys = 0; keys < 4; ++keys)
  218.     {
  219.         rv = PR_SetThreadPrivate(key[keys], NULL);
  220.         MY_ASSERT(PR_SUCCESS == rv);
  221.     }
  222.     PrintProgress();
  223.  
  224.     did = should = PR_FALSE;
  225.     for (keys = 0; keys < 4; ++keys)
  226.     {
  227.         rv = PR_SetThreadPrivate(key[keys], NULL);
  228.         MY_ASSERT(PR_SUCCESS == rv);
  229.     }
  230.     PrintProgress();
  231.  
  232.     did = should = PR_FALSE;
  233.     for (keys = 8; keys < 127; ++keys)
  234.     {
  235.         rv = PR_NewThreadPrivateIndex(&key[keys], Destructor);
  236.         MY_ASSERT(PR_SUCCESS == rv);
  237.         rv = PR_SetThreadPrivate(key[keys], "EXTENSION");
  238.         MY_ASSERT(PR_SUCCESS == rv);
  239.     }
  240.     PrintProgress();
  241.  
  242.     did = PR_FALSE; should = PR_TRUE;
  243.     for (keys = 8; keys < 127; ++keys)
  244.     {
  245.         rv = PR_SetThreadPrivate(key[keys], NULL);
  246.         MY_ASSERT(PR_SUCCESS == rv);
  247.     }
  248.     PrintProgress();
  249.  
  250.     did = should = PR_FALSE;
  251.     for (keys = 8; keys < 127; ++keys)
  252.     {
  253.         rv = PR_SetThreadPrivate(key[keys], NULL);
  254.         MY_ASSERT(PR_SUCCESS == rv);
  255.     }
  256.  
  257.     thread = PR_CreateThread(
  258.         PR_USER_THREAD, Thread, NULL, PR_PRIORITY_NORMAL,
  259.         PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
  260.  
  261.     (void)PR_JoinThread(thread);
  262.  
  263. #if defined(WIN16)
  264.     printf(
  265.         "%s\n",((PR_TRUE == failed) ? "FAILED" : "PASSED"));
  266. #else
  267.     (void)PR_fprintf(
  268.         fout, "%s\n",((PR_TRUE == failed) ? "FAILED" : "PASSED"));
  269. #endif    
  270.  
  271.     return 0;
  272.  
  273. }  /* Tpd */
  274.  
  275. PRIntn main(PRIntn argc, char *argv[])
  276. {
  277.     PLOptStatus os;
  278.     PLOptState *opt = PL_CreateOptState(argc, argv, "dl:r:");
  279.     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  280.     {
  281.         if (PL_OPT_BAD == os) continue;
  282.         switch (opt->option)
  283.         {
  284.         case 'd':  /* debug mode */
  285.             debug = PR_TRUE;
  286.             break;
  287.          default:
  288.             break;
  289.         }
  290.     }
  291.     PL_DestroyOptState(opt);
  292.     PR_STDIO_INIT();
  293.     return PR_Initialize(Tpd, argc, argv, 0);
  294. }  /* main */
  295.  
  296. /* tpd.c */
  297.