Back to index

lightning-sunbird  0.9+nobinonly
dtoa.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) 1998-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  *
00040  * This file contains a test program for the function conversion functions
00041  * for double precision code:
00042  * PR_strtod
00043  * PR_dtoa
00044  * PR_cnvtf
00045  *
00046  * This file was ns/nspr/tests/dtoa.c, created by rrj on 1996/06/22.
00047  *
00048  *****************************************************************************/
00049 #include <stdio.h>
00050 #include <sys/types.h>
00051 #include <string.h>
00052 #include <locale.h>
00053 #include "prprf.h"
00054 #include "prdtoa.h"
00055 
00056 static int failed_already = 0;
00057 
00058 int main( int argc, char* argv[] )
00059 {
00060     double num;
00061     double num1;
00062     double zero = 0.0;
00063     char   cnvt[50];
00064     
00065     num = 1e24;
00066     num1 = PR_strtod("1e24",NULL);
00067     if(num1 != num){
00068        fprintf(stderr,"Failed to convert numeric value %s\n","1e24");
00069         failed_already = 1;
00070     }
00071 
00072     PR_cnvtf(cnvt,sizeof(cnvt),20,num);
00073     if(strcmp("1e+24",cnvt) != 0){
00074        fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
00075         failed_already = 1;
00076     }
00077 
00078     num = 0.001e7;
00079     num1 = PR_strtod("0.001e7",NULL);
00080     if(num1 != num){
00081        fprintf(stderr,"Failed to convert numeric value %s\n","0.001e7");
00082         failed_already = 1;
00083     }
00084     PR_cnvtf(cnvt,sizeof(cnvt),20,num);
00085     if(strcmp("10000",cnvt) != 0){
00086        fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
00087         failed_already = 1;
00088     }
00089 
00090     num = 0.0000000000000753;
00091     num1 = PR_strtod("0.0000000000000753",NULL);
00092     if(num1 != num){
00093        fprintf(stderr,"Failed to convert numeric value %s\n",
00094               "0.0000000000000753");
00095         failed_already = 1;
00096     }
00097     PR_cnvtf(cnvt,sizeof(cnvt),20,num);
00098     if(strcmp("7.53e-14",cnvt) != 0){
00099        fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
00100         failed_already = 1;
00101     }
00102 
00103     num = 1.867e73;
00104     num1 = PR_strtod("1.867e73",NULL);
00105     if(num1 != num){
00106        fprintf(stderr,"Failed to convert numeric value %s\n","1.867e73");
00107         failed_already = 1;
00108     }
00109     PR_cnvtf(cnvt,sizeof(cnvt),20,num);
00110     if(strcmp("1.867e+73",cnvt) != 0){
00111        fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
00112         failed_already = 1;
00113     }
00114 
00115 
00116     num = -1.867e73;
00117     num1 = PR_strtod("-1.867e73",NULL);
00118     if(num1 != num){
00119        fprintf(stderr,"Failed to convert numeric value %s\n","-1.867e73");
00120         failed_already = 1;
00121     }
00122     PR_cnvtf(cnvt,sizeof(cnvt),20,num);
00123     if(strcmp("-1.867e+73",cnvt) != 0){
00124        fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
00125         failed_already = 1;
00126     }
00127 
00128     num = -1.867e-73;
00129     num1 = PR_strtod("-1.867e-73",NULL);
00130     if(num1 != num){
00131        fprintf(stderr,"Failed to convert numeric value %s\n","-1.867e-73");
00132         failed_already = 1;
00133     }
00134 
00135     PR_cnvtf(cnvt,sizeof(cnvt),20,num);
00136     if(strcmp("-1.867e-73",cnvt) != 0){
00137        fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
00138         failed_already = 1;
00139     }
00140 
00141     /* Testing for infinity */
00142     num = 1.0 / zero;
00143     num1 = PR_strtod("1.867e765",NULL);
00144     if(num1 != num){
00145        fprintf(stderr,"Failed to convert numeric value %s\n","1.867e765");
00146         failed_already = 1;
00147     }
00148 
00149     PR_cnvtf(cnvt,sizeof(cnvt),20,num);
00150     if(strcmp("Infinity",cnvt) != 0){
00151        fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
00152         failed_already = 1;
00153     }
00154 
00155     num = -1.0 / zero;
00156     num1 = PR_strtod("-1.867e765",NULL);
00157     if(num1 != num){
00158        fprintf(stderr,"Failed to convert numeric value %s\n","-1.867e765");
00159         failed_already = 1;
00160     }
00161 
00162     PR_cnvtf(cnvt,sizeof(cnvt),20,num);
00163     if(strcmp("-Infinity",cnvt) != 0){
00164        fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
00165         failed_already = 1;
00166     }
00167 
00168     /* Testing for NaN. PR_strtod can't parse "NaN" and "Infinity" */
00169     num = zero / zero;
00170 
00171     PR_cnvtf(cnvt,sizeof(cnvt),20,num);
00172     if(strcmp("NaN",cnvt) != 0){
00173        fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
00174         failed_already = 1;
00175     }
00176 
00177     num = - zero / zero;
00178     PR_cnvtf(cnvt,sizeof(cnvt),20,num);
00179     if(strcmp("NaN",cnvt) != 0){
00180        fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
00181         failed_already = 1;
00182     }
00183 
00184     num = 1.0000000001e21;
00185     num1 = PR_strtod("1.0000000001e21",NULL);
00186     if(num1 != num){
00187        fprintf(stderr,"Failed to convert numeric value %s\n",
00188               "1.0000000001e21");
00189         failed_already = 1;
00190     }
00191 
00192     PR_cnvtf(cnvt,sizeof(cnvt),20,num);
00193     if(strcmp("1.0000000001e+21",cnvt) != 0){
00194        fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
00195         failed_already = 1;
00196     }
00197 
00198 
00199     num = -1.0000000001e-21;
00200     num1 = PR_strtod("-1.0000000001e-21",NULL);
00201     if(num1 != num){
00202        fprintf(stderr,"Failed to convert numeric value %s\n",
00203               "-1.0000000001e-21");
00204         failed_already = 1;
00205     }
00206     PR_cnvtf(cnvt,sizeof(cnvt),20,num);
00207     if(strcmp("-1.0000000001e-21",cnvt) != 0){
00208        fprintf(stderr,"Failed to convert numeric value %lf %s\n",num,cnvt);
00209         failed_already = 1;
00210     }
00211     if (failed_already) {
00212         printf("FAILED\n");
00213     } else {
00214         printf("PASSED\n");
00215     }
00216     return failed_already;
00217 }