backout 29799f914cab, Bug 917642 - [Helix] Please update the helix blobs
[gecko.git] / nsprpub / pr / tests / semaerr1.c
blobfd1463dbcf6d68c8fee3bb45e4ffefcda8c97d10
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nspr.h"
7 #include "plgetopt.h"
9 #include <stdio.h>
11 #ifdef SYMBIAN
12 #define SEM_NAME1 "c:\\data\\foo.sem"
13 #define SEM_NAME2 "c:\\data\\bar.sem"
14 #else
15 #define SEM_NAME1 "/tmp/foo.sem"
16 #define SEM_NAME2 "/tmp/bar.sem"
17 #endif
18 #define SEM_MODE 0666
20 static PRBool debug_mode = PR_FALSE;
22 static void Help(void)
24 fprintf(stderr, "semaerr1 test program usage:\n");
25 fprintf(stderr, "\t-d debug mode (FALSE)\n");
26 fprintf(stderr, "\t-h this message\n");
27 } /* Help */
29 int main(int argc, char **argv)
31 PLOptStatus os;
32 PLOptState *opt = PL_CreateOptState(argc, argv, "dh");
33 PRSem *sem;
35 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
36 if (PL_OPT_BAD == os) continue;
37 switch (opt->option) {
38 case 'd': /* debug mode */
39 debug_mode = PR_TRUE;
40 break;
41 case 'h':
42 default:
43 Help();
44 return 2;
47 PL_DestroyOptState(opt);
50 * PR_SEM_CREATE|PR_SEM_EXCL should be able to
51 * create a nonexistent semaphore.
53 (void) PR_DeleteSemaphore(SEM_NAME2);
54 sem = PR_OpenSemaphore(SEM_NAME2, PR_SEM_CREATE|PR_SEM_EXCL, SEM_MODE, 0);
55 if (sem == NULL) {
56 fprintf(stderr, "PR_OpenSemaphore failed\n");
57 exit(1);
59 if (PR_CloseSemaphore(sem) == PR_FAILURE) {
60 fprintf(stderr, "PR_CloseSemaphore failed\n");
61 exit(1);
63 if (PR_DeleteSemaphore(SEM_NAME2) == PR_FAILURE) {
64 fprintf(stderr, "PR_DeleteSemaphore failed\n");
65 exit(1);
69 * Opening an existing semaphore with PR_SEM_CREATE|PR_SEM_EXCL.
70 * should fail with PR_FILE_EXISTS_ERROR.
72 sem = PR_OpenSemaphore(SEM_NAME1, PR_SEM_CREATE|PR_SEM_EXCL, SEM_MODE, 0);
73 if (sem != NULL) {
74 fprintf(stderr, "PR_OpenSemaphore should fail but succeeded\n");
75 exit(1);
77 if (PR_GetError() != PR_FILE_EXISTS_ERROR) {
78 fprintf(stderr, "Expect %d but got %d\n", PR_FILE_EXISTS_ERROR,
79 PR_GetError());
80 exit(1);
84 * Try again, with just PR_SEM_CREATE. This should succeed.
86 sem = PR_OpenSemaphore(SEM_NAME1, PR_SEM_CREATE, SEM_MODE, 0);
87 if (sem == NULL) {
88 fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n",
89 PR_GetError(), PR_GetOSError());
90 exit(1);
92 if (PR_CloseSemaphore(sem) == PR_FAILURE) {
93 fprintf(stderr, "PR_CloseSemaphore failed\n");
94 exit(1);
97 sem = PR_OpenSemaphore(SEM_NAME2, PR_SEM_CREATE|PR_SEM_EXCL, SEM_MODE, 0);
98 if (sem == NULL) {
99 fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n",
100 PR_GetError(), PR_GetOSError());
101 exit(1);
103 if (PR_CloseSemaphore(sem) == PR_FAILURE) {
104 fprintf(stderr, "PR_CloseSemaphore failed\n");
105 exit(1);
108 printf("PASS\n");
109 return 0;