Back to index

lightning-sunbird  0.9+nobinonly
fdcach.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1999-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039  * File: fdcach.c
00040  * Description:
00041  *   This test verifies that the fd cache and stack are working
00042  *   correctly.
00043  */
00044 
00045 #include "nspr.h"
00046 
00047 #include <stdio.h>
00048 #include <stdlib.h>
00049 
00050 /*
00051  * Define ORDER_PRESERVED if the implementation of PR_SetFDCacheSize
00052  * preserves the ordering of the fd's when moving them between the
00053  * cache and the stack.
00054  */
00055 #define ORDER_PRESERVED 1
00056 
00057 /*
00058  * NUM_FDS must be <= FD_CACHE_SIZE.
00059  */
00060 #define FD_CACHE_SIZE 1024
00061 #define NUM_FDS 20
00062 
00063 int main(int argc, char **argv)
00064 {
00065     int i;
00066     PRFileDesc *fds[NUM_FDS];
00067     PRFileDesc *savefds[NUM_FDS];
00068     int numfds = sizeof(fds)/sizeof(fds[0]);
00069 
00070     /*
00071      * Switch between cache and stack when they are empty.
00072      * Then start with the fd cache.
00073      */
00074     PR_SetFDCacheSize(0, FD_CACHE_SIZE);
00075     PR_SetFDCacheSize(0, 0);
00076     PR_SetFDCacheSize(0, FD_CACHE_SIZE);
00077 
00078     /* Add some fd's to the fd cache. */
00079     for (i = 0; i < numfds; i++) {
00080         savefds[i] = PR_NewTCPSocket();
00081         if (NULL == savefds[i]) {
00082             fprintf(stderr, "PR_NewTCPSocket failed\n");
00083             exit(1);
00084         }
00085     }
00086     for (i = 0; i < numfds; i++) {
00087         if (PR_Close(savefds[i]) == PR_FAILURE) {
00088             fprintf(stderr, "PR_Close failed\n");
00089             exit(1);
00090         } 
00091     }
00092 
00093     /*
00094      * Create some fd's.  These fd's should come from
00095      * the fd cache.  Verify the FIFO ordering of the fd
00096      * cache.
00097      */
00098     for (i = 0; i < numfds; i++) {
00099         fds[i] = PR_NewTCPSocket();
00100         if (NULL == fds[i]) {
00101             fprintf(stderr, "PR_NewTCPSocket failed\n");
00102             exit(1);
00103         }
00104         if (fds[i] != savefds[i]) {
00105             fprintf(stderr, "fd cache malfunctioned\n");
00106             exit(1);
00107         }
00108     }
00109     /* Put the fd's back to the fd cache. */
00110     for (i = 0; i < numfds; i++) {
00111         if (PR_Close(savefds[i]) == PR_FAILURE) {
00112             fprintf(stderr, "PR_Close failed\n");
00113             exit(1);
00114         } 
00115     }
00116 
00117     /* Switch to the fd stack. */
00118     PR_SetFDCacheSize(0, 0);
00119 
00120     /*
00121      * Create some fd's.  These fd's should come from
00122      * the fd stack.
00123      */
00124     for (i = 0; i < numfds; i++) {
00125         fds[i] = PR_NewTCPSocket();
00126         if (NULL == fds[i]) {
00127             fprintf(stderr, "PR_NewTCPSocket failed\n");
00128             exit(1);
00129         }
00130 #ifdef ORDER_PRESERVED
00131         if (fds[i] != savefds[numfds-1-i]) {
00132             fprintf(stderr, "fd stack malfunctioned\n");
00133             exit(1);
00134         }
00135 #else
00136         savefds[numfds-1-i] = fds[i];
00137 #endif
00138     }
00139     /* Put the fd's back to the fd stack. */
00140     for (i = 0; i < numfds; i++) {
00141         if (PR_Close(savefds[i]) == PR_FAILURE) {
00142             fprintf(stderr, "PR_Close failed\n");
00143             exit(1);
00144         } 
00145     }
00146 
00147     /*
00148      * Now create some fd's and verify the LIFO ordering of
00149      * the fd stack.
00150      */
00151     for (i = 0; i < numfds; i++) {
00152         fds[i] = PR_NewTCPSocket();
00153         if (NULL == fds[i]) {
00154             fprintf(stderr, "PR_NewTCPSocket failed\n");
00155             exit(1);
00156         }
00157         if (fds[i] != savefds[numfds-1-i]) {
00158             fprintf(stderr, "fd stack malfunctioned\n");
00159             exit(1);
00160         }
00161     }
00162     /* Put the fd's back to the fd stack. */
00163     for (i = 0; i < numfds; i++) {
00164         if (PR_Close(savefds[i]) == PR_FAILURE) {
00165             fprintf(stderr, "PR_Close failed\n");
00166             exit(1);
00167         } 
00168     }
00169 
00170     /* Switch to the fd cache. */
00171     PR_SetFDCacheSize(0, FD_CACHE_SIZE);
00172 
00173     for (i = 0; i < numfds; i++) {
00174         fds[i] = PR_NewTCPSocket();
00175         if (NULL == fds[i]) {
00176             fprintf(stderr, "PR_NewTCPSocket failed\n");
00177             exit(1);
00178         }
00179 #ifdef ORDER_PRESERVED
00180         if (fds[i] != savefds[i]) {
00181             fprintf(stderr, "fd cache malfunctioned\n");
00182             exit(1);
00183         }
00184 #else
00185         savefds[i] = fds[i];
00186 #endif
00187     }
00188     for (i = 0; i < numfds; i++) {
00189         if (PR_Close(savefds[i]) == PR_FAILURE) {
00190             fprintf(stderr, "PR_Close failed\n");
00191             exit(1);
00192         } 
00193     }
00194 
00195     for (i = 0; i < numfds; i++) {
00196         fds[i] = PR_NewTCPSocket();
00197         if (NULL == fds[i]) {
00198             fprintf(stderr, "PR_NewTCPSocket failed\n");
00199             exit(1);
00200         }
00201         if (fds[i] != savefds[i]) {
00202             fprintf(stderr, "fd cache malfunctioned\n");
00203             exit(1);
00204         }
00205     }
00206     for (i = 0; i < numfds; i++) {
00207         if (PR_Close(savefds[i]) == PR_FAILURE) {
00208             fprintf(stderr, "PR_Close failed\n");
00209             exit(1);
00210         } 
00211     }
00212 
00213     /* Switch to the fd stack. */
00214     PR_SetFDCacheSize(0, 0);
00215 
00216     for (i = 0; i < numfds; i++) {
00217         fds[i] = PR_NewTCPSocket();
00218         if (NULL == fds[i]) {
00219             fprintf(stderr, "PR_NewTCPSocket failed\n");
00220             exit(1);
00221         }
00222 #ifdef ORDER_PRESERVED
00223         if (fds[i] != savefds[numfds-1-i]) {
00224             fprintf(stderr, "fd stack malfunctioned\n");
00225             exit(1);
00226         }
00227 #else
00228         savefds[numfds-1-i];
00229 #endif
00230     }
00231     for (i = 0; i < numfds; i++) {
00232         if (PR_Close(savefds[i]) == PR_FAILURE) {
00233             fprintf(stderr, "PR_Close failed\n");
00234             exit(1);
00235         } 
00236     }
00237 
00238     for (i = 0; i < numfds; i++) {
00239         fds[i] = PR_NewTCPSocket();
00240         if (NULL == fds[i]) {
00241             fprintf(stderr, "PR_NewTCPSocket failed\n");
00242             exit(1);
00243         }
00244         if (fds[i] != savefds[numfds-1-i]) {
00245             fprintf(stderr, "fd stack malfunctioned\n");
00246             exit(1);
00247         }
00248     }
00249     for (i = 0; i < numfds; i++) {
00250         if (PR_Close(savefds[i]) == PR_FAILURE) {
00251             fprintf(stderr, "PR_Close failed\n");
00252             exit(1);
00253         } 
00254     }
00255 
00256     PR_Cleanup();
00257     printf("PASS\n");
00258     return 0;
00259 }