isnumber.c (2819B)
1 // $Id$ --*- c -*-- 2 3 // Copyright (C) 2006 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de> 4 // 5 // This program is free software; you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation; version 2 of the License. 8 // 9 // This program is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with this program; if not, write to the Free Software 16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 18 #define ENSC_TESTSUITE 19 20 #ifdef HAVE_CONFIG_H 21 # include <config.h> 22 #endif 23 24 #include "lib_internal/util.h" 25 #include "lib_internal/coreassert.h" 26 27 #define TEST_TMPL(FUNC, TYPE, STR, VAL, STRICT) \ 28 do { \ 29 TYPE exp = (TYPE)(VAL)!=(TYPE)BAD ? (VAL) : 0; \ 30 bool val = (TYPE)(VAL)==(TYPE)BAD ? false : true; \ 31 TYPE tmp; \ 32 bool rc = FUNC((STR), &tmp, (STRICT)); \ 33 char const * const UNUSED STR_FUNC = #FUNC; \ 34 char const * const UNUSED STR_VAL = #VAL; \ 35 char const * const UNUSED STR_STRICT = #STRICT; \ 36 assert(rc == val); \ 37 rc = FUNC((STR), 0, (STRICT)); \ 38 assert(val == rc); \ 39 if (val) assert(tmp == exp); \ 40 } while (0) 41 42 #define TESTS(STR, VAL, STRICT) \ 43 TEST_TMPL(isNumber, signed long, STR, VAL, STRICT) 44 45 #define TESTU(STR, VAL, STRICT) \ 46 TEST_TMPL(isNumberUnsigned, unsigned long, STR, VAL, STRICT) 47 48 #define TEST(STR, VALS0, VALS1, VALU0, VALU1) \ 49 TESTS(STR, VALS0, true); \ 50 TESTS(STR, VALS1, false); \ 51 TESTU(STR, VALU0, true); \ 52 TESTU(STR, VALU1, false); 53 54 55 #define BAD 0xdeadbeaf 56 57 int main() 58 { 59 TEST( "0", 0, 0, 0, 0); 60 TEST( "1", 1, 1, 1, 1); 61 TEST("-1", -1, -1, BAD, BAD); 62 TEST( "1k", BAD, 1000, BAD, 1000); 63 //TEST("-1k", BAD, -1000, BAD, BAD); 64 TEST( "1K", BAD, 1024, BAD, 1024); 65 //TEST("-1K", BAD, -1024, BAD, BAD); 66 TEST( "1m", BAD, 1000000, BAD, 1000000); 67 //TEST("-1m", BAD, -1000000, BAD, BAD); 68 TEST( "1M", BAD, 1048576, BAD, 1048576); 69 //TEST("-1M", BAD, -1048576, BAD, BAD); 70 71 TEST( "010", 8, 8, 8, 8); 72 TEST( "010k", BAD, 8000, BAD, 8000); 73 TEST("-010", -8, -8, BAD, BAD); 74 //TEST("-010k", BAD, 8000, BAD, BAD); 75 76 TEST( "0x10", 16, 16, 16, 16); 77 TEST( "0x10k", BAD, 16000, BAD, 16000); 78 TEST("-0x10", -16, -16, BAD, BAD); 79 //TEST("-0x10k", BAD, -16000, BAD, BAD); 80 }