~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/nsprpub/pr/tests/tpd.c

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 Mozilla Public
 
4
 * License Version 1.1 (the "License"); you may not use this file
 
5
 * except in compliance with the License. You may obtain a copy of
 
6
 * the License at http://www.mozilla.org/MPL/
 
7
 * 
 
8
 * Software distributed under the License is distributed on an "AS
 
9
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
10
 * implied. See the License for the specific language governing
 
11
 * rights and limitations under the License.
 
12
 * 
 
13
 * The Original Code is the Netscape Portable Runtime (NSPR).
 
14
 * 
 
15
 * The Initial Developer of the Original Code is Netscape
 
16
 * Communications Corporation.  Portions created by Netscape are 
 
17
 * Copyright (C) 1998-2000 Netscape Communications Corporation.  All
 
18
 * Rights Reserved.
 
19
 * 
 
20
 * Contributor(s):
 
21
 * 
 
22
 * Alternatively, the contents of this file may be used under the
 
23
 * terms of the GNU General Public License Version 2 or later (the
 
24
 * "GPL"), in which case the provisions of the GPL are applicable 
 
25
 * instead of those above.  If you wish to allow use of your 
 
26
 * version of this file only under the terms of the GPL and not to
 
27
 * allow others to use your version of this file under the MPL,
 
28
 * indicate your decision by deleting the provisions above and
 
29
 * replace them with the notice and other provisions required by
 
30
 * the GPL.  If you do not delete the provisions above, a recipient
 
31
 * may use your version of this file under either the MPL or the
 
32
 * GPL.
 
33
 */
 
34
 
 
35
/*
 
36
** File:        tpd.c
 
37
** Description: Exercising the thread private data bailywick.
 
38
*/
 
39
 
 
40
#include "prmem.h"
 
41
#include "prinit.h"
 
42
#include "prlog.h"
 
43
#include "prprf.h"
 
44
#include "prthread.h"
 
45
#include "prtypes.h"
 
46
 
 
47
#if defined(XP_MAC)
 
48
#include "pprio.h"
 
49
#else
 
50
#include "private/pprio.h"
 
51
#endif
 
52
 
 
53
#include "plgetopt.h"
 
54
 
 
55
static PRUintn key[128];
 
56
static PRIntn debug = 0;
 
57
static PRBool failed = PR_FALSE;
 
58
static PRBool should = PR_TRUE;
 
59
static PRBool did = PR_TRUE;
 
60
static PRFileDesc *fout = NULL;
 
61
 
 
62
static void PrintProgress(PRIntn line)
 
63
{
 
64
    failed = failed || (should && !did);
 
65
    failed = failed || (!should && did);
 
66
    if (debug > 0)
 
67
    {
 
68
#if defined(WIN16)
 
69
        printf(
 
70
            "@ line %d destructor should%s have been called and was%s\n",
 
71
            line, ((should) ? "" : " NOT"), ((did) ? "" : " NOT"));
 
72
#else    
 
73
        PR_fprintf(
 
74
            fout, "@ line %d destructor should%s have been called and was%s\n",
 
75
            line, ((should) ? "" : " NOT"), ((did) ? "" : " NOT"));
 
76
#endif
 
77
    }
 
78
}  /* PrintProgress */
 
79
 
 
80
static void MyAssert(const char *expr, const char *file, PRIntn line)
 
81
{
 
82
    if (debug > 0)
 
83
        (void)PR_fprintf(fout, "'%s' in file: %s: %d\n", expr, file, line);
 
84
}  /* MyAssert */
 
85
 
 
86
#define MY_ASSERT(_expr) \
 
87
    ((_expr)?((void)0):MyAssert(# _expr,__FILE__,__LINE__))
 
88
 
 
89
 
 
90
static void PR_CALLBACK Destructor(void *data)
 
91
{
 
92
    MY_ASSERT(NULL != data);
 
93
    if (should) did = PR_TRUE;
 
94
    else failed = PR_TRUE;
 
95
    /*
 
96
     * We don't actually free the storage since it's actually allocated
 
97
     * on the stack. Normally, this would not be the case and this is
 
98
     * the opportunity to free whatever.
 
99
    PR_Free(data);
 
100
     */
 
101
}  /* Destructor */
 
102
 
 
103
static void PR_CALLBACK Thread(void *null)
 
104
{
 
105
    void *pd;
 
106
    PRStatus rv;
 
107
    PRUintn keys;
 
108
    char *key_string[] = {
 
109
        "Key #0", "Key #1", "Key #2", "Key #3",
 
110
        "Bogus #5", "Bogus #6", "Bogus #7", "Bogus #8"};
 
111
    
 
112
    did = should = PR_FALSE;
 
113
    for (keys = 0; keys < 8; ++keys)
 
114
    {
 
115
        pd = PR_GetThreadPrivate(key[keys]);
 
116
        MY_ASSERT(NULL == pd);
 
117
    }
 
118
    PrintProgress(__LINE__);
 
119
 
 
120
    did = should = PR_FALSE;
 
121
    for (keys = 0; keys < 4; ++keys)
 
122
    {
 
123
        rv = PR_SetThreadPrivate(key[keys], key_string[keys]);
 
124
        MY_ASSERT(PR_SUCCESS == rv);
 
125
    }
 
126
    PrintProgress(__LINE__);
 
127
 
 
128
    did = should = PR_FALSE;
 
129
    for (keys = 4; keys < 8; ++keys)
 
130
    {
 
131
        rv = PR_SetThreadPrivate(key[keys], key_string[keys]);
 
132
        MY_ASSERT(PR_FAILURE == rv);
 
133
    }
 
134
    PrintProgress(__LINE__);
 
135
    
 
136
    did = PR_FALSE; should = PR_TRUE;
 
137
    for (keys = 0; keys < 4; ++keys)
 
138
    {
 
139
        rv = PR_SetThreadPrivate(key[keys], key_string[keys]);
 
140
        MY_ASSERT(PR_SUCCESS == rv);
 
141
    }
 
142
    PrintProgress(__LINE__);
 
143
 
 
144
    did = PR_FALSE; should = PR_TRUE;
 
145
    for (keys = 0; keys < 4; ++keys)
 
146
    {
 
147
        rv = PR_SetThreadPrivate(key[keys], NULL);
 
148
        MY_ASSERT(PR_SUCCESS == rv);
 
149
    }
 
150
    PrintProgress(__LINE__);
 
151
 
 
152
    did = should = PR_FALSE;
 
153
    for (keys = 0; keys < 4; ++keys)
 
154
    {
 
155
        rv = PR_SetThreadPrivate(key[keys], NULL);
 
156
        MY_ASSERT(PR_SUCCESS == rv);
 
157
    }
 
158
    PrintProgress(__LINE__);
 
159
 
 
160
    did = should = PR_FALSE;
 
161
    for (keys = 8; keys < 127; ++keys)
 
162
    {
 
163
        rv = PR_SetThreadPrivate(key[keys], "EXTENSION");
 
164
        MY_ASSERT(PR_SUCCESS == rv);
 
165
    }
 
166
    PrintProgress(__LINE__);
 
167
 
 
168
    did = PR_FALSE; should = PR_TRUE;
 
169
    for (keys = 8; keys < 127; ++keys)
 
170
    {
 
171
        rv = PR_SetThreadPrivate(key[keys], NULL);
 
172
        MY_ASSERT(PR_SUCCESS == rv);
 
173
    }
 
174
    PrintProgress(__LINE__);
 
175
 
 
176
    did = should = PR_FALSE;
 
177
    for (keys = 8; keys < 127; ++keys)
 
178
    {
 
179
        rv = PR_SetThreadPrivate(key[keys], NULL);
 
180
        MY_ASSERT(PR_SUCCESS == rv);
 
181
    }
 
182
 
 
183
    /* put in keys and leave them there for thread exit */
 
184
    did = should = PR_FALSE;
 
185
    for (keys = 0; keys < 4; ++keys)
 
186
    {
 
187
        rv = PR_SetThreadPrivate(key[keys], key_string[keys]);
 
188
        MY_ASSERT(PR_SUCCESS == rv);
 
189
    }
 
190
    PrintProgress(__LINE__);
 
191
    did = PR_FALSE; should = PR_TRUE;
 
192
 
 
193
}  /* Thread */
 
194
 
 
195
static PRIntn PR_CALLBACK Tpd(PRIntn argc, char **argv)
 
196
{
 
197
    void *pd;
 
198
    PRStatus rv;
 
199
    PRUintn keys;
 
200
    PRThread *thread;
 
201
    char *key_string[] = {
 
202
        "Key #0", "Key #1", "Key #2", "Key #3",
 
203
        "Bogus #5", "Bogus #6", "Bogus #7", "Bogus #8"};
 
204
    
 
205
    fout = PR_STDOUT;
 
206
 
 
207
    did = should = PR_FALSE;
 
208
    for (keys = 0; keys < 4; ++keys)
 
209
    {
 
210
        rv = PR_NewThreadPrivateIndex(&key[keys], Destructor);
 
211
        MY_ASSERT(PR_SUCCESS == rv);
 
212
    }
 
213
    PrintProgress(__LINE__);
 
214
 
 
215
    did = should = PR_FALSE;
 
216
    for (keys = 0; keys < 8; ++keys)
 
217
    {
 
218
        pd = PR_GetThreadPrivate(key[keys]);
 
219
        MY_ASSERT(NULL == pd);
 
220
    }
 
221
    PrintProgress(__LINE__);
 
222
 
 
223
    did = should = PR_FALSE;
 
224
    for (keys = 0; keys < 4; ++keys)
 
225
    {
 
226
        rv = PR_SetThreadPrivate(key[keys], key_string[keys]);
 
227
        MY_ASSERT(PR_SUCCESS == rv);
 
228
    }
 
229
    PrintProgress(__LINE__);
 
230
 
 
231
    for (keys = 4; keys < 8; ++keys)
 
232
                key[keys] = 4096;               /* set to invalid value */
 
233
    did = should = PR_FALSE;
 
234
    for (keys = 4; keys < 8; ++keys)
 
235
    {
 
236
        rv = PR_SetThreadPrivate(key[keys], key_string[keys]);
 
237
        MY_ASSERT(PR_FAILURE == rv);
 
238
    }
 
239
    PrintProgress(__LINE__);
 
240
    
 
241
    did = PR_FALSE; should = PR_TRUE;
 
242
    for (keys = 0; keys < 4; ++keys)
 
243
    {
 
244
        rv = PR_SetThreadPrivate(key[keys], key_string[keys]);
 
245
        MY_ASSERT(PR_SUCCESS == rv);
 
246
    }
 
247
    PrintProgress(__LINE__);
 
248
 
 
249
    did = PR_FALSE; should = PR_TRUE;
 
250
    for (keys = 0; keys < 4; ++keys)
 
251
    {
 
252
        rv = PR_SetThreadPrivate(key[keys], NULL);
 
253
        MY_ASSERT(PR_SUCCESS == rv);
 
254
    }
 
255
    PrintProgress(__LINE__);
 
256
 
 
257
    did = should = PR_FALSE;
 
258
    for (keys = 0; keys < 4; ++keys)
 
259
    {
 
260
        rv = PR_SetThreadPrivate(key[keys], NULL);
 
261
        MY_ASSERT(PR_SUCCESS == rv);
 
262
    }
 
263
    PrintProgress(__LINE__);
 
264
 
 
265
    did = should = PR_FALSE;
 
266
    for (keys = 8; keys < 127; ++keys)
 
267
    {
 
268
        rv = PR_NewThreadPrivateIndex(&key[keys], Destructor);
 
269
        MY_ASSERT(PR_SUCCESS == rv);
 
270
        rv = PR_SetThreadPrivate(key[keys], "EXTENSION");
 
271
        MY_ASSERT(PR_SUCCESS == rv);
 
272
    }
 
273
    PrintProgress(__LINE__);
 
274
 
 
275
    did = PR_FALSE; should = PR_TRUE;
 
276
    for (keys = 8; keys < 127; ++keys)
 
277
    {
 
278
        rv = PR_SetThreadPrivate(key[keys], NULL);
 
279
        MY_ASSERT(PR_SUCCESS == rv);
 
280
    }
 
281
    PrintProgress(__LINE__);
 
282
 
 
283
    did = should = PR_FALSE;
 
284
    for (keys = 8; keys < 127; ++keys)
 
285
    {
 
286
        rv = PR_SetThreadPrivate(key[keys], NULL);
 
287
        MY_ASSERT(PR_SUCCESS == rv);
 
288
    }
 
289
 
 
290
    thread = PR_CreateThread(
 
291
        PR_USER_THREAD, Thread, NULL, PR_PRIORITY_NORMAL,
 
292
        PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
 
293
 
 
294
    (void)PR_JoinThread(thread);
 
295
 
 
296
    PrintProgress(__LINE__);
 
297
 
 
298
#if defined(WIN16)
 
299
    printf(
 
300
        "%s\n",((PR_TRUE == failed) ? "FAILED" : "PASSED"));
 
301
#else
 
302
    (void)PR_fprintf(
 
303
        fout, "%s\n",((PR_TRUE == failed) ? "FAILED" : "PASSED"));
 
304
#endif    
 
305
 
 
306
    return 0;
 
307
 
 
308
}  /* Tpd */
 
309
 
 
310
PRIntn main(PRIntn argc, char *argv[])
 
311
{
 
312
        PLOptStatus os;
 
313
        PLOptState *opt = PL_CreateOptState(argc, argv, "dl:r:");
 
314
        while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
 
315
    {
 
316
                if (PL_OPT_BAD == os) continue;
 
317
        switch (opt->option)
 
318
        {
 
319
        case 'd':  /* debug mode */
 
320
                        debug = PR_TRUE;
 
321
            break;
 
322
         default:
 
323
            break;
 
324
        }
 
325
    }
 
326
        PL_DestroyOptState(opt);
 
327
    PR_STDIO_INIT();
 
328
    return PR_Initialize(Tpd, argc, argv, 0);
 
329
}  /* main */
 
330
 
 
331
/* tpd.c */