• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

test/TestNumeric.cpp

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #include <spl/Debug.h>
00018 #include <spl/Log.h>
00019 #include <spl/Double.h>
00020 #include <spl/Numeric.h>
00021 #include <spl/Int32.h>
00022 #include <spl/Int64.h>
00023 #include <spl/UInt32.h>
00024 #include <spl/UInt64.h>
00025 
00026 #include <math.h>
00027 
00028 #ifdef DEBUG
00029 
00030 static void _TestIsNumeric()
00031 {
00032         UNIT_ASSERT("", Numeric::IsNumeric("") == false);
00033         UNIT_ASSERT(".", Numeric::IsNumeric(".") == false);
00034         UNIT_ASSERT("a", Numeric::IsNumeric("a") == false);
00035         UNIT_ASSERT("--", Numeric::IsNumeric("--") == false);
00036         UNIT_ASSERT("+1+", Numeric::IsNumeric("+1+") == false);
00037         UNIT_ASSERT("1+", Numeric::IsNumeric("1+") == false);
00038         UNIT_ASSERT("999.9.", Numeric::IsNumeric("999.9.") == false);
00039         UNIT_ASSERT(".9.", Numeric::IsNumeric(".9.") == false);
00040 
00041         UNIT_ASSERT("1", Numeric::IsNumeric("1") == true);
00042         UNIT_ASSERT("999", Numeric::IsNumeric("999") == true);
00043         UNIT_ASSERT("999.9", Numeric::IsNumeric("999.9") == true);
00044         UNIT_ASSERT(".9", Numeric::IsNumeric(".9") == true);
00045 
00046         UNIT_ASSERT("-1", Numeric::IsNumeric("-1") == true);
00047         UNIT_ASSERT("+1", Numeric::IsNumeric("+1") == true);
00048         UNIT_ASSERT("-10", Numeric::IsNumeric("-10") == true);
00049 
00050         Log::SWriteOkFail( "IsNumeric test" );
00051 }
00052 
00053 static void _TestInt32Parse()
00054 {
00055         UNIT_ASSERT("42", Int32::Parse("42") == 42);
00056         UNIT_ASSERT("-42", Int32::Parse("-42") == -42);
00057         UNIT_ASSERT("+42", Int32::Parse("+42") == 42);
00058         UNIT_ASSERT("0x42", Int32::Parse("0x42", 4, 16) == 0x42);
00059         UNIT_ASSERT("42 base 16", Int32::Parse("42", 2, 16) == 0x42);
00060 
00061         Log::SWriteOkFail( "Int32::Parse test" );
00062 }
00063 
00064 static void _TestInt32IsInt()
00065 {
00066         UNIT_ASSERT("42", Int32::IsInt("42"));
00067         UNIT_ASSERT("-42", Int32::IsInt("-42"));
00068         UNIT_ASSERT("+42", Int32::IsInt("+42"));
00069         UNIT_ASSERT("0x42", Int32::IsInt("0x42", 4, 16));
00070 
00071         UNIT_ASSERT("42.0", !Int32::IsInt("42.0"));
00072         UNIT_ASSERT("0x42 base 10", !Int32::IsInt("0x42", 4, 10));
00073         UNIT_ASSERT("42 base 16", Int32::IsInt("42", 2, 16));
00074         UNIT_ASSERT("a42", !Int32::IsInt("a42"));
00075         UNIT_ASSERT("42a", !Int32::IsInt("42a"));
00076         UNIT_ASSERT("a", !Int32::IsInt("a"));
00077 
00078         Log::SWriteOkFail( "Int32::IsInt test" );
00079 }
00080 
00081 static void _TestInt32ToString()
00082 {
00083         UNIT_ASSERT("42", *Int32::ToString(42) == "42");
00084         UNIT_ASSERT("-42", *Int32::ToString(-42) == "-42");
00085         UNIT_ASSERT("0x42", *Int32::ToString(0x42, 16) == "42");
00086 
00087         Log::SWriteOkFail( "Int32::ToString test" );
00088 }
00089 
00090 static void _TestUInt32Parse()
00091 {
00092         UNIT_ASSERT("42", UInt32::Parse("42") == 42);
00093         UNIT_ASSERT("+42", UInt32::Parse("+42") == 42);
00094         UNIT_ASSERT("0x42", UInt32::Parse("0x42", 4, 16) == 0x42);
00095 
00096         Log::SWriteOkFail( "UInt32::Parse test" );
00097 }
00098 
00099 static void _TestUInt32IsInt()
00100 {
00101         UNIT_ASSERT("42", UInt32::IsInt("42"));
00102         UNIT_ASSERT("-42", !UInt32::IsInt("-42"));
00103         UNIT_ASSERT("+42", !UInt32::IsInt("+42"));
00104         UNIT_ASSERT("0x42", UInt32::IsInt("0x42", 4, 16));
00105 
00106         UNIT_ASSERT("42.0", !UInt32::IsInt("42.0"));
00107         UNIT_ASSERT("0x42 base 10", !UInt32::IsInt("0x42", 4, 10));
00108         UNIT_ASSERT("42 base 16", UInt32::IsInt("42", 4, 16));
00109         UNIT_ASSERT("a42", !UInt32::IsInt("a42"));
00110         UNIT_ASSERT("42a", !UInt32::IsInt("42a"));
00111         UNIT_ASSERT("a", !UInt32::IsInt("a"));
00112 
00113         Log::SWriteOkFail( "UInt32::IsInt test" );
00114 }
00115 
00116 static void _TestUInt32ToString()
00117 {
00118         UNIT_ASSERT("42", *UInt32::ToString(42) == "42");
00119         UNIT_ASSERT("0x42", *UInt32::ToString(0x42, 16) == "42");
00120 
00121         Log::SWriteOkFail( "UInt32::ToString test" );
00122 }
00123 
00124 static void _TestUInt64Parse()
00125 {
00126         UNIT_ASSERT("42", UInt64::Parse("42") == 42);
00127         UNIT_ASSERT("+42", UInt64::Parse("+42") == 42);
00128         UNIT_ASSERT("0x42", UInt64::Parse("0x42", 4, 16) == 0x42);
00129 
00130         Log::SWriteOkFail( "UInt64::Parse test" );
00131 }
00132 
00133 static void _TestUInt64IsInt()
00134 {
00135         UNIT_ASSERT("42", UInt64::IsInt("42"));
00136         UNIT_ASSERT("-42", !UInt64::IsInt("-42"));
00137         UNIT_ASSERT("+42", !UInt64::IsInt("+42"));
00138         //UNIT_ASSERT("0x42", UInt64::IsInt("0x42", 4, 16));
00139 
00140         UNIT_ASSERT("42.0", !UInt64::IsInt("42.0"));
00141         //UNIT_ASSERT("0x42 base 10", !UInt64::IsInt("0x42", 4, 10));
00142         //UNIT_ASSERT("42 base 16", UInt64::IsInt("42", 16));
00143         UNIT_ASSERT("a42", !UInt64::IsInt("a42"));
00144         UNIT_ASSERT("42a", !UInt64::IsInt("42a"));
00145         UNIT_ASSERT("a", !UInt64::IsInt("a"));
00146 
00147         Log::SWriteOkFail( "UInt64::IsInt test" );
00148 }
00149 
00150 static void _TestUInt64ToString()
00151 {
00152         UNIT_ASSERT("42", *UInt64::ToString(42) == "42");
00153         UNIT_ASSERT("0x42", *UInt64::ToString(0x42, 16) == "42");
00154 
00155         Log::SWriteOkFail( "UInt64::ToString test" );
00156 }
00157 
00158 static void _TestInt64Parse()
00159 {
00160         UNIT_ASSERT("42", Int64::Parse("42") == 42);
00161         UNIT_ASSERT("+42", Int64::Parse("+42") == 42);
00162         UNIT_ASSERT("-42", Int64::Parse("-42") == -42);
00163         //UNIT_ASSERT("0x42", Int64::Parse("0x42", 16) == 0x42);
00164 
00165         Log::SWriteOkFail( "Int64::Parse test" );
00166 }
00167 
00168 static void _TestInt64IsInt()
00169 {
00170         UNIT_ASSERT("42", Int64::IsInt("42"));
00171         UNIT_ASSERT("-42", Int64::IsInt("-42"));
00172         UNIT_ASSERT("+42", Int64::IsInt("+42"));
00173         //UNIT_ASSERT("0x42", Int64::IsInt("0x42", 16));
00174 
00175         UNIT_ASSERT("42.0", !Int64::IsInt("42.0"));
00176         //UNIT_ASSERT("0x42 base 10", !Int64::IsInt("0x42", 10));
00177         //UNIT_ASSERT("42 base 16", Int64::IsInt("42", 16));
00178         UNIT_ASSERT("a42", !Int64::IsInt("a42"));
00179         UNIT_ASSERT("42a", !Int64::IsInt("42a"));
00180         UNIT_ASSERT("a", !Int64::IsInt("a"));
00181 
00182         Log::SWriteOkFail( "Int64::IsInt test" );
00183 }
00184 
00185 static void _TestInt64ToString()
00186 {
00187         UNIT_ASSERT("42", *Int64::ToString(42) == "42");
00188         //UNIT_ASSERT("0x42", Int64::ToString(0x42, 16) == "0x42");
00189 
00190         Log::SWriteOkFail( "Int64::ToString test" );
00191 }
00192 
00193 #include <stdio.h>
00194 
00195 static void _TestDoubleParse()
00196 {
00197         UNIT_ASSERT("42", Double::Parse("42") == 42);
00198         UNIT_ASSERT("42.42", floor(Double::Parse("42.42")*100) == 4242);
00199         UNIT_ASSERT("-42", Double::Parse("-42") == -42);
00200         UNIT_ASSERT("-42.42", floor(Double::Parse("-42.42")*100) == -4242);
00201         UNIT_ASSERT("42e2", Double::Parse("42e2") == 42e2);
00202         UNIT_ASSERT("42e-2", floor(Double::Parse("42e-2")*100) == floor(42e-2 * 100));
00203 
00204         Log::SWriteOkFail( "Double::Parse test" );
00205 }
00206 
00207 static void _TestDoubleIsDouble()
00208 {
00209         UNIT_ASSERT("42", Double::IsDouble("42"));
00210         UNIT_ASSERT(".42", Double::IsDouble(".42"));
00211         UNIT_ASSERT("-42", Double::IsDouble("-42"));
00212         UNIT_ASSERT("+42", Double::IsDouble("+42"));
00213 
00214         UNIT_ASSERT("a42", !Double::IsDouble("a42.0"));
00215         UNIT_ASSERT("42a", !Double::IsDouble("42a"));
00216         UNIT_ASSERT("a", !Double::IsDouble("a"));
00217 
00218         Log::SWriteOkFail( "Double::IsDouble test" );
00219 }
00220 
00221 static void _TestDoubleToString()
00222 {
00223         UNIT_ASSERT("-42.1", *Double::ToString(-42.1) == "-42.1");
00224         UNIT_ASSERT("42.0", *Double::ToString(42.0) == "42");
00225         UNIT_ASSERT("42e2", *Double::ToString(42e2) == "4200");
00226         UNIT_ASSERT("42e-2", *Double::ToString(42e-2) == "0.42");
00227 
00228         Log::SWriteOkFail( "Double::ToString test" );
00229 }
00230 
00231 void _TestNumeric()
00232 {
00233         _TestIsNumeric();
00234         DEBUG_CLEAR_MEM_CHECK_POINTS();
00235         DEBUG_DUMP_MEM_LEAKS();
00236 
00237         _TestInt32Parse();
00238         DEBUG_CLEAR_MEM_CHECK_POINTS();
00239         DEBUG_DUMP_MEM_LEAKS();
00240 
00241         _TestInt32IsInt();
00242         DEBUG_CLEAR_MEM_CHECK_POINTS();
00243         DEBUG_DUMP_MEM_LEAKS();
00244 
00245         _TestInt32ToString();
00246         DEBUG_CLEAR_MEM_CHECK_POINTS();
00247         DEBUG_DUMP_MEM_LEAKS();
00248 
00249         UNIT_ASSERT("Int32::Min/Max", Int32::MinValue() < Int32::MaxValue());
00250 
00251         _TestUInt32Parse();
00252         DEBUG_CLEAR_MEM_CHECK_POINTS();
00253         DEBUG_DUMP_MEM_LEAKS();
00254 
00255         _TestUInt32IsInt();
00256         DEBUG_CLEAR_MEM_CHECK_POINTS();
00257         DEBUG_DUMP_MEM_LEAKS();
00258 
00259         _TestUInt32ToString();
00260         DEBUG_CLEAR_MEM_CHECK_POINTS();
00261         DEBUG_DUMP_MEM_LEAKS();
00262 
00263         UNIT_ASSERT("UInt32::Min/Max", UInt32::MinValue() < UInt32::MaxValue());
00264 
00265         _TestUInt64Parse();
00266         DEBUG_CLEAR_MEM_CHECK_POINTS();
00267         DEBUG_DUMP_MEM_LEAKS();
00268 
00269         _TestUInt64IsInt();
00270         DEBUG_CLEAR_MEM_CHECK_POINTS();
00271         DEBUG_DUMP_MEM_LEAKS();
00272 
00273         _TestUInt64ToString();
00274         DEBUG_CLEAR_MEM_CHECK_POINTS();
00275         DEBUG_DUMP_MEM_LEAKS();
00276 
00277         UNIT_ASSERT("UInt32::Min/Max", UInt64::MinValue() < UInt64::MaxValue());
00278 
00279         _TestInt64Parse();
00280         DEBUG_CLEAR_MEM_CHECK_POINTS();
00281         DEBUG_DUMP_MEM_LEAKS();
00282 
00283         _TestInt64IsInt();
00284         DEBUG_CLEAR_MEM_CHECK_POINTS();
00285         DEBUG_DUMP_MEM_LEAKS();
00286 
00287         _TestInt64ToString();
00288         DEBUG_CLEAR_MEM_CHECK_POINTS();
00289         DEBUG_DUMP_MEM_LEAKS();
00290 
00291         UNIT_ASSERT("Int64::Min/Max", Int64::MinValue() < Int64::MaxValue());
00292 
00293         _TestDoubleParse();
00294         DEBUG_CLEAR_MEM_CHECK_POINTS();
00295         DEBUG_DUMP_MEM_LEAKS();
00296 
00297         _TestDoubleIsDouble();
00298         DEBUG_CLEAR_MEM_CHECK_POINTS();
00299         DEBUG_DUMP_MEM_LEAKS();
00300 
00301         _TestDoubleToString();
00302         DEBUG_CLEAR_MEM_CHECK_POINTS();
00303         DEBUG_DUMP_MEM_LEAKS();
00304 }
00305 
00306 #endif