nspr: import 3.0 RC1 cutoff from CVS
[mozilla-nspr.git] / nsprpub / pr / tests / pipeself.c
blob5bb9791acc087b701c36b5fb747c6862196ce9bf
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is the Netscape Portable Runtime (NSPR).
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1999-2000
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 * File: pipeself.c
41 * Description:
42 * This test has two threads communicating with each other using
43 * two unidirectional pipes. The primordial thread is the ping
44 * thread and the other thread is the pong thread. The ping
45 * thread writes "ping" to the pong thread and the pong thread
46 * writes "pong" back.
49 #include "prio.h"
50 #include "prerror.h"
51 #include "prthread.h"
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
57 #define NUM_ITERATIONS 10
59 static PRFileDesc *ping_in, *ping_out;
60 static PRFileDesc *pong_in, *pong_out;
62 static void PongThreadFunc(void *arg)
64 char buf[1024];
65 int idx;
66 PRInt32 nBytes;
67 PRStatus status;
69 for (idx = 0; idx < NUM_ITERATIONS; idx++) {
70 memset(buf, 0, sizeof(buf));
71 nBytes = PR_Read(pong_in, buf, sizeof(buf));
72 if (nBytes == -1) {
73 fprintf(stderr, "PR_Read failed\n");
74 exit(1);
76 printf("pong thread: received \"%s\"\n", buf);
77 if (nBytes != 5) {
78 fprintf(stderr, "pong thread: expected 5 bytes but got %d bytes\n",
79 nBytes);
80 exit(1);
82 if (strcmp(buf, "ping") != 0) {
83 fprintf(stderr, "pong thread: expected \"ping\" but got \"%s\"\n",
84 buf);
85 exit(1);
87 strcpy(buf, "pong");
88 printf("pong thread: sending \"%s\"\n", buf);
89 nBytes = PR_Write(pong_out, buf, 5);
90 if (nBytes == -1) {
91 fprintf(stderr, "PR_Write failed: (%d, %d)\n", PR_GetError(),
92 PR_GetOSError());
93 exit(1);
97 status = PR_Close(pong_in);
98 if (status == PR_FAILURE) {
99 fprintf(stderr, "PR_Close failed\n");
100 exit(1);
102 status = PR_Close(pong_out);
103 if (status == PR_FAILURE) {
104 fprintf(stderr, "PR_Close failed\n");
105 exit(1);
109 int main()
111 PRStatus status;
112 PRThread *pongThread;
113 char buf[1024];
114 PRInt32 nBytes;
115 int idx;
117 status = PR_CreatePipe(&ping_in, &pong_out);
118 if (status == PR_FAILURE) {
119 fprintf(stderr, "PR_CreatePipe failed\n");
120 exit(1);
122 status = PR_CreatePipe(&pong_in, &ping_out);
123 if (status == PR_FAILURE) {
124 fprintf(stderr, "PR_CreatePipe failed\n");
125 exit(1);
128 pongThread = PR_CreateThread(PR_USER_THREAD, PongThreadFunc, NULL,
129 PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
130 if (pongThread == NULL) {
131 fprintf(stderr, "PR_CreateThread failed\n");
132 exit(1);
135 for (idx = 0; idx < NUM_ITERATIONS; idx++) {
136 strcpy(buf, "ping");
137 printf("ping thread: sending \"%s\"\n", buf);
138 nBytes = PR_Write(ping_out, buf, 5);
139 if (nBytes == -1) {
140 fprintf(stderr, "PR_Write failed: (%d, %d)\n", PR_GetError(),
141 PR_GetOSError());
142 exit(1);
144 memset(buf, 0, sizeof(buf));
145 nBytes = PR_Read(ping_in, buf, sizeof(buf));
146 if (nBytes == -1) {
147 fprintf(stderr, "PR_Read failed\n");
148 exit(1);
150 printf("ping thread: received \"%s\"\n", buf);
151 if (nBytes != 5) {
152 fprintf(stderr, "ping thread: expected 5 bytes but got %d bytes\n",
153 nBytes);
154 exit(1);
156 if (strcmp(buf, "pong") != 0) {
157 fprintf(stderr, "ping thread: expected \"pong\" but got \"%s\"\n",
158 buf);
159 exit(1);
163 status = PR_Close(ping_in);
164 if (status == PR_FAILURE) {
165 fprintf(stderr, "PR_Close failed\n");
166 exit(1);
168 status = PR_Close(ping_out);
169 if (status == PR_FAILURE) {
170 fprintf(stderr, "PR_Close failed\n");
171 exit(1);
173 status = PR_JoinThread(pongThread);
174 if (status == PR_FAILURE) {
175 fprintf(stderr, "PR_JoinThread failed\n");
176 exit(1);
179 #ifdef XP_UNIX
181 * Test PR_Available for pipes
183 status = PR_CreatePipe(&ping_in, &ping_out);
184 if (status == PR_FAILURE) {
185 fprintf(stderr, "PR_CreatePipe failed\n");
186 exit(1);
188 nBytes = PR_Write(ping_out, buf, 250);
189 if (nBytes == -1) {
190 fprintf(stderr, "PR_Write failed: (%d, %d)\n", PR_GetError(),
191 PR_GetOSError());
192 exit(1);
194 nBytes = PR_Available(ping_in);
195 if (nBytes < 0) {
196 fprintf(stderr, "PR_Available failed: (%d, %d)\n", PR_GetError(),
197 PR_GetOSError());
198 exit(1);
199 } else if (nBytes != 250) {
200 fprintf(stderr, "PR_Available: expected 250 bytes but got %d bytes\n",
201 nBytes);
202 exit(1);
204 printf("PR_Available: expected %d, got %d bytes\n",250, nBytes);
205 /* read some data */
206 nBytes = PR_Read(ping_in, buf, 7);
207 if (nBytes == -1) {
208 fprintf(stderr, "PR_Read failed\n");
209 exit(1);
211 /* check available data */
212 nBytes = PR_Available(ping_in);
213 if (nBytes < 0) {
214 fprintf(stderr, "PR_Available failed: (%d, %d)\n", PR_GetError(),
215 PR_GetOSError());
216 exit(1);
217 } else if (nBytes != (250 - 7)) {
218 fprintf(stderr, "PR_Available: expected 243 bytes but got %d bytes\n",
219 nBytes);
220 exit(1);
222 printf("PR_Available: expected %d, got %d bytes\n",243, nBytes);
223 /* read all data */
224 nBytes = PR_Read(ping_in, buf, sizeof(buf));
225 if (nBytes == -1) {
226 fprintf(stderr, "PR_Read failed\n");
227 exit(1);
228 } else if (nBytes != 243) {
229 fprintf(stderr, "PR_Read failed: expected %d, got %d bytes\n",
230 243, nBytes);
231 exit(1);
233 /* check available data */
234 nBytes = PR_Available(ping_in);
235 if (nBytes < 0) {
236 fprintf(stderr, "PR_Available failed: (%d, %d)\n", PR_GetError(),
237 PR_GetOSError());
238 exit(1);
239 } else if (nBytes != 0) {
240 fprintf(stderr, "PR_Available: expected 0 bytes but got %d bytes\n",
241 nBytes);
242 exit(1);
244 printf("PR_Available: expected %d, got %d bytes\n", 0, nBytes);
246 status = PR_Close(ping_in);
247 if (status == PR_FAILURE) {
248 fprintf(stderr, "PR_Close failed\n");
249 exit(1);
251 status = PR_Close(ping_out);
252 if (status == PR_FAILURE) {
253 fprintf(stderr, "PR_Close failed\n");
254 exit(1);
256 #endif /* XP_UNIX */
258 printf("PASS\n");
259 return 0;