Bug 449371 Firefox/Thunderbird crashes at exit [@ gdk_display_x11_finalize], p=Brian...
[wine-gecko.git] / nsprpub / pr / tests / fdcach.c
blob33382fa8a772016cdde6aefed2fc5c4c6cf5cb69
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: fdcach.c
40 * Description:
41 * This test verifies that the fd cache and stack are working
42 * correctly.
45 #include "nspr.h"
47 #include <stdio.h>
48 #include <stdlib.h>
51 * Define ORDER_PRESERVED if the implementation of PR_SetFDCacheSize
52 * preserves the ordering of the fd's when moving them between the
53 * cache and the stack.
55 #define ORDER_PRESERVED 1
58 * NUM_FDS must be <= FD_CACHE_SIZE.
60 #define FD_CACHE_SIZE 1024
61 #define NUM_FDS 20
63 int main(int argc, char **argv)
65 int i;
66 PRFileDesc *fds[NUM_FDS];
67 PRFileDesc *savefds[NUM_FDS];
68 int numfds = sizeof(fds)/sizeof(fds[0]);
71 * Switch between cache and stack when they are empty.
72 * Then start with the fd cache.
74 PR_SetFDCacheSize(0, FD_CACHE_SIZE);
75 PR_SetFDCacheSize(0, 0);
76 PR_SetFDCacheSize(0, FD_CACHE_SIZE);
78 /* Add some fd's to the fd cache. */
79 for (i = 0; i < numfds; i++) {
80 savefds[i] = PR_NewTCPSocket();
81 if (NULL == savefds[i]) {
82 fprintf(stderr, "PR_NewTCPSocket failed\n");
83 exit(1);
86 for (i = 0; i < numfds; i++) {
87 if (PR_Close(savefds[i]) == PR_FAILURE) {
88 fprintf(stderr, "PR_Close failed\n");
89 exit(1);
94 * Create some fd's. These fd's should come from
95 * the fd cache. Verify the FIFO ordering of the fd
96 * cache.
98 for (i = 0; i < numfds; i++) {
99 fds[i] = PR_NewTCPSocket();
100 if (NULL == fds[i]) {
101 fprintf(stderr, "PR_NewTCPSocket failed\n");
102 exit(1);
104 if (fds[i] != savefds[i]) {
105 fprintf(stderr, "fd cache malfunctioned\n");
106 exit(1);
109 /* Put the fd's back to the fd cache. */
110 for (i = 0; i < numfds; i++) {
111 if (PR_Close(savefds[i]) == PR_FAILURE) {
112 fprintf(stderr, "PR_Close failed\n");
113 exit(1);
117 /* Switch to the fd stack. */
118 PR_SetFDCacheSize(0, 0);
121 * Create some fd's. These fd's should come from
122 * the fd stack.
124 for (i = 0; i < numfds; i++) {
125 fds[i] = PR_NewTCPSocket();
126 if (NULL == fds[i]) {
127 fprintf(stderr, "PR_NewTCPSocket failed\n");
128 exit(1);
130 #ifdef ORDER_PRESERVED
131 if (fds[i] != savefds[numfds-1-i]) {
132 fprintf(stderr, "fd stack malfunctioned\n");
133 exit(1);
135 #else
136 savefds[numfds-1-i] = fds[i];
137 #endif
139 /* Put the fd's back to the fd stack. */
140 for (i = 0; i < numfds; i++) {
141 if (PR_Close(savefds[i]) == PR_FAILURE) {
142 fprintf(stderr, "PR_Close failed\n");
143 exit(1);
148 * Now create some fd's and verify the LIFO ordering of
149 * the fd stack.
151 for (i = 0; i < numfds; i++) {
152 fds[i] = PR_NewTCPSocket();
153 if (NULL == fds[i]) {
154 fprintf(stderr, "PR_NewTCPSocket failed\n");
155 exit(1);
157 if (fds[i] != savefds[numfds-1-i]) {
158 fprintf(stderr, "fd stack malfunctioned\n");
159 exit(1);
162 /* Put the fd's back to the fd stack. */
163 for (i = 0; i < numfds; i++) {
164 if (PR_Close(savefds[i]) == PR_FAILURE) {
165 fprintf(stderr, "PR_Close failed\n");
166 exit(1);
170 /* Switch to the fd cache. */
171 PR_SetFDCacheSize(0, FD_CACHE_SIZE);
173 for (i = 0; i < numfds; i++) {
174 fds[i] = PR_NewTCPSocket();
175 if (NULL == fds[i]) {
176 fprintf(stderr, "PR_NewTCPSocket failed\n");
177 exit(1);
179 #ifdef ORDER_PRESERVED
180 if (fds[i] != savefds[i]) {
181 fprintf(stderr, "fd cache malfunctioned\n");
182 exit(1);
184 #else
185 savefds[i] = fds[i];
186 #endif
188 for (i = 0; i < numfds; i++) {
189 if (PR_Close(savefds[i]) == PR_FAILURE) {
190 fprintf(stderr, "PR_Close failed\n");
191 exit(1);
195 for (i = 0; i < numfds; i++) {
196 fds[i] = PR_NewTCPSocket();
197 if (NULL == fds[i]) {
198 fprintf(stderr, "PR_NewTCPSocket failed\n");
199 exit(1);
201 if (fds[i] != savefds[i]) {
202 fprintf(stderr, "fd cache malfunctioned\n");
203 exit(1);
206 for (i = 0; i < numfds; i++) {
207 if (PR_Close(savefds[i]) == PR_FAILURE) {
208 fprintf(stderr, "PR_Close failed\n");
209 exit(1);
213 /* Switch to the fd stack. */
214 PR_SetFDCacheSize(0, 0);
216 for (i = 0; i < numfds; i++) {
217 fds[i] = PR_NewTCPSocket();
218 if (NULL == fds[i]) {
219 fprintf(stderr, "PR_NewTCPSocket failed\n");
220 exit(1);
222 #ifdef ORDER_PRESERVED
223 if (fds[i] != savefds[numfds-1-i]) {
224 fprintf(stderr, "fd stack malfunctioned\n");
225 exit(1);
227 #else
228 savefds[numfds-1-i];
229 #endif
231 for (i = 0; i < numfds; i++) {
232 if (PR_Close(savefds[i]) == PR_FAILURE) {
233 fprintf(stderr, "PR_Close failed\n");
234 exit(1);
238 for (i = 0; i < numfds; i++) {
239 fds[i] = PR_NewTCPSocket();
240 if (NULL == fds[i]) {
241 fprintf(stderr, "PR_NewTCPSocket failed\n");
242 exit(1);
244 if (fds[i] != savefds[numfds-1-i]) {
245 fprintf(stderr, "fd stack malfunctioned\n");
246 exit(1);
249 for (i = 0; i < numfds; i++) {
250 if (PR_Close(savefds[i]) == PR_FAILURE) {
251 fprintf(stderr, "PR_Close failed\n");
252 exit(1);
256 PR_Cleanup();
257 printf("PASS\n");
258 return 0;