From: Erik van der Kouwe Date: Mon, 25 Jan 2010 18:16:25 +0000 (+0000) Subject: Additions to inttypes.h format strings X-Git-Tag: v3.1.6~35 X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/zpipe.c?a=commitdiff_plain;h=f8804c0240967f603007733466b51b1ae505bfda;p=minix.git Additions to inttypes.h format strings --- diff --git a/include/inttypes.h b/include/inttypes.h index 0a3c0c7e8..31f4ec005 100644 --- a/include/inttypes.h +++ b/include/inttypes.h @@ -24,19 +24,33 @@ #define PRI16 "" #define PRILEAST16 "" #define PRIFAST16 "" -#if _WORD_SIZE == 2 -#define PRI32 "l" -#define PRILEAST32 "l" -#define PRIFAST32 "l" -#else + +#if __SIZEOF_LONG__ > 4 #define PRI32 "" -#define PRILEAST32 "" -#define PRIFAST32 "" +#else +#define PRI32 "l" #endif -#if _WORD_SIZE > 2 && __L64 +#define PRILEAST32 PRI32 +#define PRIFAST32 PRI32 + +#if defined(__LONG_LONG_SUPPORTED) +#define PRI64 "ll" +#elif _WORD_SIZE > 2 && __L64 #define PRI64 "l" -#define PRILEAST64 "l" -#define PRIFAST64 "l" +#endif + +#ifdef PRI64 +#define PRILEAST64 PRI64 +#define PRIFAST64 PRI64 +#define PRIMAX PRI64 +#else +#define PRIMAX PRI32 +#endif + +#if _PTR_SIZE == _WORD_SIZE +#define PRIPTR "" +#elif _PTR_SIZE == 2*_WORD_SIZE +#define PRIPTR "l" #endif /* Macros for fprintf, the ones defined by the standard. */ @@ -49,11 +63,13 @@ #define PRId32 PRI32"d" #define PRIdLEAST32 PRILEAST32"d" #define PRIdFAST32 PRIFAST32"d" -#if _WORD_SIZE > 2 && __L64 +#ifdef PRI64 #define PRId64 PRI64"d" #define PRIdLEAST64 PRILEAST64"d" #define PRIdFAST64 PRIFAST64"d" #endif +#define PRIdMAX PRIMAX"d" +#define PRIdPTR PRIPTR"d" #define PRIi8 PRI8"i" #define PRIiLEAST8 PRILEAST8"i" @@ -64,11 +80,13 @@ #define PRIi32 PRI32"i" #define PRIiLEAST32 PRILEAST32"i" #define PRIiFAST32 PRIFAST32"i" -#if _WORD_SIZE > 2 && __L64 +#ifdef PRI64 #define PRIi64 PRI64"i" #define PRIiLEAST64 PRILEAST64"i" #define PRIiFAST64 PRIFAST64"i" #endif +#define PRIiMAX PRIMAX"i" +#define PRIiPTR PRIPTR"i" #define PRIo8 PRI8"o" #define PRIoLEAST8 PRILEAST8"o" @@ -79,11 +97,13 @@ #define PRIo32 PRI32"o" #define PRIoLEAST32 PRILEAST32"o" #define PRIoFAST32 PRIFAST32"o" -#if _WORD_SIZE > 2 && __L64 +#ifdef PRI64 #define PRIo64 PRI64"o" #define PRIoLEAST64 PRILEAST64"o" #define PRIoFAST64 PRIFAST64"o" #endif +#define PRIoMAX PRIMAX"o" +#define PRIoPTR PRIPTR"o" #define PRIu8 PRI8"u" #define PRIuLEAST8 PRILEAST8"u" @@ -94,11 +114,13 @@ #define PRIu32 PRI32"u" #define PRIuLEAST32 PRILEAST32"u" #define PRIuFAST32 PRIFAST32"u" -#if _WORD_SIZE > 2 && __L64 +#ifdef PRI64 #define PRIu64 PRI64"u" #define PRIuLEAST64 PRILEAST64"u" #define PRIuFAST64 PRIFAST64"u" #endif +#define PRIuMAX PRIMAX"u" +#define PRIuPTR PRIPTR"u" #define PRIx8 PRI8"x" #define PRIxLEAST8 PRILEAST8"x" @@ -109,11 +131,13 @@ #define PRIx32 PRI32"x" #define PRIxLEAST32 PRILEAST32"x" #define PRIxFAST32 PRIFAST32"x" -#if _WORD_SIZE > 2 && __L64 +#ifdef PRI64 #define PRIx64 PRI64"x" #define PRIxLEAST64 PRILEAST64"x" #define PRIxFAST64 PRIFAST64"x" #endif +#define PRIxMAX PRIMAX"x" +#define PRIxPTR PRIPTR"x" #define PRIX8 PRI8"X" #define PRIXLEAST8 PRILEAST8"X" @@ -124,11 +148,13 @@ #define PRIX32 PRI32"X" #define PRIXLEAST32 PRILEAST32"X" #define PRIXFAST32 PRIFAST32"X" -#if _WORD_SIZE > 2 && __L64 +#ifdef PRI64 #define PRIX64 PRI64"X" #define PRIXLEAST64 PRILEAST64"X" #define PRIXFAST64 PRIFAST64"X" #endif +#define PRIXMAX PRIMAX"X" +#define PRIXPTR PRIPTR"X" /* Macros to scan integers with fscanf(), nonstandard first group. */ #define SCN8 "hh" @@ -230,7 +256,12 @@ #endif /* !__cplusplus || __STDC_FORMAT_MACROS */ /* Integer conversion functions for [u]intmax_t. */ +#ifdef __LONG_LONG_SUPPORTED +#define stroimax(nptr, endptr, base) strtoll(nptr, endptr, base) +#define stroumax(nptr, endptr, base) strtoull(nptr, endptr, base) +#else #define stroimax(nptr, endptr, base) strtol(nptr, endptr, base) #define stroumax(nptr, endptr, base) strtoul(nptr, endptr, base) +#endif #endif /* _INTTYPES_H */ diff --git a/include/stdint.h b/include/stdint.h index d696c494e..601dacd66 100644 --- a/include/stdint.h +++ b/include/stdint.h @@ -87,8 +87,14 @@ typedef unsigned uintptr_t; typedef long intptr_t; typedef unsigned long uintptr_t; #endif + +#if defined(__LONG_LONG_SUPPORTED) || (_WORD_SIZE > 2 && __L64) +typedef int64_t intmax_t; +typedef uint64_t uintmax_t; +#else typedef long intmax_t; typedef unsigned long uintmax_t; +#endif #if !__cplusplus || defined(__STDC_LIMIT_MACROS) #ifndef _LIMITS_H @@ -178,9 +184,16 @@ typedef unsigned long uintmax_t; #define INTPTR_MAX LONG_MAX #define UINTPTR_MAX ULONG_MAX #endif + +#if defined(__LONG_LONG_SUPPORTED) || (_WORD_SIZE > 2 && __L64) +#define INTMAX_MIN INT64_MIN +#define INTMAX_MAX INT64_MAX +#define UINTMAX_MAX UINT64_MAX +#else #define INTMAX_MIN LONG_MIN #define INTMAX_MAX LONG_MAX #define UINTMAX_MAX ULONG_MAX +#endif #endif /* !__cplusplus || __STDC_LIMIT_MACROS */ diff --git a/test/Makefile b/test/Makefile index 5b0bf2f7b..50097cf6a 100644 --- a/test/Makefile +++ b/test/Makefile @@ -10,11 +10,11 @@ OBJ= test1 test2 test3 test4 test5 test6 test7 test8 test9 \ test21 test22 test23 test25 test26 test27 test28 test29 \ test30 test31 test32 test34 test35 test36 test37 test38 \ test39 t10a t11a t11b test40 t40a t40b t40c t40d t40e t40f test41 \ - test42 test44 test45 test47 test48 + test42 test44 test45 test47 test48 test49 BIGOBJ= test20 test24 ROOTOBJ= test11 test33 test43 test46 -GCCOBJ= test45-gcc +GCCOBJ= test45-gcc test49-gcc all: $(OBJ) $(BIGOBJ) $(GCCOBJ) $(ROOTOBJ) Benchmarks chmod 755 *.sh run @@ -100,4 +100,6 @@ test45-gcc: test45.c test45.h test46: test46.c test47: test47.c test48: test48.c +test49: test49.c +test49-gcc: test49.c diff --git a/test/run b/test/run index e6d97bbf1..5267264f1 100755 --- a/test/run +++ b/test/run @@ -13,13 +13,13 @@ badones= # list of tests that failed # Print test welcome message clr -echo "Running POSIX compliance test suite. There are 51 tests in total." +echo "Running POSIX compliance test suite. There are 53 tests in total." echo " " # Run all the tests, keeping track of who failed. for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 \ 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 \ - 41 42 43 44 45 45-gcc 46 47 48 sh1.sh sh2.sh + 41 42 43 44 45 45-gcc 46 47 48 49 49-gcc sh1.sh sh2.sh do if [ -x ./test$i ] then diff --git a/test/test49.c b/test/test49.c new file mode 100755 index 000000000..290e5033e --- /dev/null +++ b/test/test49.c @@ -0,0 +1,212 @@ +#include +#include +#include +#include +#include +#include +#include + +#if defined(__LONG_LONG_SUPPORTED) || (_WORD_SIZE > 2 && __L64) +#define TEST_64_BIT +#endif + +#define ERR err(__LINE__) + +#define MAX_ERROR 4 +static int errct; + +static void quit(void) +{ + if (errct == 0) + { + printf("ok\n"); + exit(0); + } + else + { + printf("%d errors\n", errct); + exit(1); + } +} + +static void err(int line) +{ + printf("error at line %d\n", line); + if (++errct >= MAX_ERROR) + quit(); +} + +#define TEST_PRINTF(type, macro, value, result) \ +{ \ + char buffer[256]; \ + snprintf(buffer, sizeof(buffer), "%" macro, (type) value); \ + if (strcmp(buffer, result) != 0) err(__LINE__); \ +} + +int main(void) +{ +#ifdef __LONG_LONG_SUPPORTED + printf("Test 49 (GCC) "); +#else + printf("Test 49 (ACK) "); +#endif + fflush(stdout); + + /* test integer sizes */ + if (sizeof(int8_t) != 1) ERR; + if (sizeof(uint8_t) != 1) ERR; + if (sizeof(int_fast8_t) < 1) ERR; + if (sizeof(uint_fast8_t) < 1) ERR; + if (sizeof(int_least8_t) < 1) ERR; + if (sizeof(uint_least8_t) < 1) ERR; + if (sizeof(int16_t) != 2) ERR; + if (sizeof(uint16_t) != 2) ERR; + if (sizeof(int_fast16_t) < 2) ERR; + if (sizeof(uint_fast16_t) < 2) ERR; + if (sizeof(int_least16_t) < 2) ERR; + if (sizeof(uint_least16_t) < 2) ERR; + if (sizeof(int32_t) != 4) ERR; + if (sizeof(uint32_t) != 4) ERR; + if (sizeof(int_fast32_t) < 4) ERR; + if (sizeof(uint_fast32_t) < 4) ERR; + if (sizeof(int_least32_t) < 4) ERR; + if (sizeof(uint_least32_t) < 4) ERR; +#ifdef TEST_64_BIT + if (sizeof(int64_t) != 8) ERR; + if (sizeof(uint64_t) != 8) ERR; + if (sizeof(int_fast64_t) < 8) ERR; + if (sizeof(uint_fast64_t) < 8) ERR; + if (sizeof(int_least64_t) < 8) ERR; + if (sizeof(uint_least64_t) < 8) ERR; +#endif + if (sizeof(intptr_t) != sizeof(void *)) ERR; + if (sizeof(uintptr_t) != sizeof(void *)) ERR; +#ifdef TEST_64_BIT + if (sizeof(intmax_t) < 8) ERR; + if (sizeof(uintmax_t) < 8) ERR; +#else + if (sizeof(intmax_t) < 4) ERR; + if (sizeof(uintmax_t) < 4) ERR; +#endif + + /* test integer signedness */ + if ((int8_t) (-1) >= 0) ERR; + if ((uint8_t) (-1) <= 0) ERR; + if ((int_fast8_t) (-1) >= 0) ERR; + if ((uint_fast8_t) (-1) <= 0) ERR; + if ((int_least8_t) (-1) >= 0) ERR; + if ((uint_least8_t) (-1) <= 0) ERR; + if ((int16_t) (-1) >= 0) ERR; + if ((uint16_t) (-1) <= 0) ERR; + if ((int_fast16_t) (-1) >= 0) ERR; + if ((uint_fast16_t) (-1) <= 0) ERR; + if ((int_least16_t) (-1) >= 0) ERR; + if ((uint_least16_t) (-1) <= 0) ERR; + if ((int32_t) (-1) >= 0) ERR; + if ((uint32_t) (-1) <= 0) ERR; + if ((int_fast32_t) (-1) >= 0) ERR; + if ((uint_fast32_t) (-1) <= 0) ERR; + if ((int_least32_t) (-1) >= 0) ERR; + if ((uint_least32_t) (-1) <= 0) ERR; +#ifdef TEST_64_BIT + if ((int64_t) (-1) >= 0) ERR; + if ((uint64_t) (-1) <= 0) ERR; + if ((int_fast64_t) (-1) >= 0) ERR; + if ((uint_fast64_t) (-1) <= 0) ERR; + if ((int_least64_t) (-1) >= 0) ERR; + if ((uint_least64_t) (-1) <= 0) ERR; +#endif + if ((intptr_t) (-1) >= 0) ERR; + if ((uintptr_t) (-1) <= 0) ERR; + if ((intptr_t) (-1) >= 0) ERR; + if ((uintptr_t) (-1) <= 0) ERR; + + /* test printf */ + TEST_PRINTF(int32_t, PRId32, INT32_MIN, "-2147483648"); + TEST_PRINTF(int32_t, PRId32, INT32_MAX, "2147483647"); + TEST_PRINTF(int32_t, PRIi32, INT32_MIN, "-2147483648"); + TEST_PRINTF(int32_t, PRIi32, INT32_MAX, "2147483647"); + TEST_PRINTF(uint32_t, PRIu32, UINT32_MAX, "4294967295"); + TEST_PRINTF(uint32_t, PRIX32, UINT32_MAX, "FFFFFFFF"); + TEST_PRINTF(uint32_t, PRIx32, UINT32_MAX, "ffffffff"); + TEST_PRINTF(uint32_t, PRIo32, UINT32_MAX, "37777777777"); + + TEST_PRINTF(int_fast32_t, PRIdFAST32, INT32_MIN, "-2147483648"); + TEST_PRINTF(int_fast32_t, PRIdFAST32, INT32_MAX, "2147483647"); + TEST_PRINTF(int_fast32_t, PRIiFAST32, INT32_MIN, "-2147483648"); + TEST_PRINTF(int_fast32_t, PRIiFAST32, INT32_MAX, "2147483647"); + TEST_PRINTF(uint_fast32_t, PRIuFAST32, UINT32_MAX, "4294967295"); + TEST_PRINTF(uint_fast32_t, PRIXFAST32, UINT32_MAX, "FFFFFFFF"); + TEST_PRINTF(uint_fast32_t, PRIxFAST32, UINT32_MAX, "ffffffff"); + TEST_PRINTF(uint_fast32_t, PRIoFAST32, UINT32_MAX, "37777777777"); + + TEST_PRINTF(int_least32_t, PRIdLEAST32, INT32_MIN, "-2147483648"); + TEST_PRINTF(int_least32_t, PRIdLEAST32, INT32_MAX, "2147483647"); + TEST_PRINTF(int_least32_t, PRIiLEAST32, INT32_MIN, "-2147483648"); + TEST_PRINTF(int_least32_t, PRIiLEAST32, INT32_MAX, "2147483647"); + TEST_PRINTF(uint_least32_t, PRIuLEAST32, UINT32_MAX, "4294967295"); + TEST_PRINTF(uint_least32_t, PRIXLEAST32, UINT32_MAX, "FFFFFFFF"); + TEST_PRINTF(uint_least32_t, PRIxLEAST32, UINT32_MAX, "ffffffff"); + TEST_PRINTF(uint_least32_t, PRIoLEAST32, UINT32_MAX, "37777777777"); + +#ifdef TEST_64_BIT + TEST_PRINTF(int64_t, PRId64, INT64_MIN, "-9223372036854775808"); + TEST_PRINTF(int64_t, PRId64, INT64_MAX, "9223372036854775807"); + TEST_PRINTF(int64_t, PRIi64, INT64_MIN, "-9223372036854775808"); + TEST_PRINTF(int64_t, PRIi64, INT64_MAX, "9223372036854775807"); + TEST_PRINTF(uint64_t, PRIu64, UINT64_MAX, "18446744073709551615"); + TEST_PRINTF(uint64_t, PRIX64, UINT64_MAX, "FFFFFFFFFFFFFFFF"); + TEST_PRINTF(uint64_t, PRIx64, UINT64_MAX, "ffffffffffffffff"); + TEST_PRINTF(uint64_t, PRIo64, UINT64_MAX, "1777777777777777777777"); + + TEST_PRINTF(int_fast64_t, PRIdFAST64, INT64_MIN, "-9223372036854775808"); + TEST_PRINTF(int_fast64_t, PRIdFAST64, INT64_MAX, "9223372036854775807"); + TEST_PRINTF(int_fast64_t, PRIiFAST64, INT64_MIN, "-9223372036854775808"); + TEST_PRINTF(int_fast64_t, PRIiFAST64, INT64_MAX, "9223372036854775807"); + TEST_PRINTF(uint_fast64_t, PRIuFAST64, UINT64_MAX, "18446744073709551615"); + TEST_PRINTF(uint_fast64_t, PRIXFAST64, UINT64_MAX, "FFFFFFFFFFFFFFFF"); + TEST_PRINTF(uint_fast64_t, PRIxFAST64, UINT64_MAX, "ffffffffffffffff"); + TEST_PRINTF(uint_fast64_t, PRIoFAST64, UINT64_MAX, "1777777777777777777777"); + + TEST_PRINTF(int_least64_t, PRIdLEAST64, INT64_MIN, "-9223372036854775808"); + TEST_PRINTF(int_least64_t, PRIdLEAST64, INT64_MAX, "9223372036854775807"); + TEST_PRINTF(int_least64_t, PRIiLEAST64, INT64_MIN, "-9223372036854775808"); + TEST_PRINTF(int_least64_t, PRIiLEAST64, INT64_MAX, "9223372036854775807"); + TEST_PRINTF(uint_least64_t, PRIuLEAST64, UINT64_MAX, "18446744073709551615"); + TEST_PRINTF(uint_least64_t, PRIXLEAST64, UINT64_MAX, "FFFFFFFFFFFFFFFF"); + TEST_PRINTF(uint_least64_t, PRIxLEAST64, UINT64_MAX, "ffffffffffffffff"); + TEST_PRINTF(uint_least64_t, PRIoLEAST64, UINT64_MAX, "1777777777777777777777"); + + TEST_PRINTF(intmax_t, PRIdMAX, INT64_MIN, "-9223372036854775808"); + TEST_PRINTF(intmax_t, PRIdMAX, INT64_MAX, "9223372036854775807"); + TEST_PRINTF(intmax_t, PRIiMAX, INT64_MIN, "-9223372036854775808"); + TEST_PRINTF(intmax_t, PRIiMAX, INT64_MAX, "9223372036854775807"); + TEST_PRINTF(uintmax_t, PRIuMAX, UINT64_MAX, "18446744073709551615"); + TEST_PRINTF(uintmax_t, PRIXMAX, UINT64_MAX, "FFFFFFFFFFFFFFFF"); + TEST_PRINTF(uintmax_t, PRIxMAX, UINT64_MAX, "ffffffffffffffff"); + TEST_PRINTF(uintmax_t, PRIoMAX, UINT64_MAX, "1777777777777777777777"); +#else + TEST_PRINTF(intmax_t, PRIdMAX, INT32_MIN, "-2147483648"); + TEST_PRINTF(intmax_t, PRIdMAX, INT32_MAX, "2147483647"); + TEST_PRINTF(intmax_t, PRIiMAX, INT32_MIN, "-2147483648"); + TEST_PRINTF(intmax_t, PRIiMAX, INT32_MAX, "2147483647"); + TEST_PRINTF(uintmax_t, PRIuMAX, UINT32_MAX, "4294967295"); + TEST_PRINTF(uintmax_t, PRIXMAX, UINT32_MAX, "FFFFFFFF"); + TEST_PRINTF(uintmax_t, PRIxMAX, UINT32_MAX, "ffffffff"); + TEST_PRINTF(uintmax_t, PRIoMAX, UINT32_MAX, "37777777777"); +#endif + + TEST_PRINTF(intptr_t, PRIdPTR, INT32_MIN, "-2147483648"); + TEST_PRINTF(intptr_t, PRIdPTR, INT32_MAX, "2147483647"); + TEST_PRINTF(intptr_t, PRIiPTR, INT32_MIN, "-2147483648"); + TEST_PRINTF(intptr_t, PRIiPTR, INT32_MAX, "2147483647"); + TEST_PRINTF(uintptr_t, PRIuPTR, UINT32_MAX, "4294967295"); + TEST_PRINTF(uintptr_t, PRIXPTR, UINT32_MAX, "FFFFFFFF"); + TEST_PRINTF(uintptr_t, PRIxPTR, UINT32_MAX, "ffffffff"); + TEST_PRINTF(uintptr_t, PRIoPTR, UINT32_MAX, "37777777777"); + + /* done */ + quit(); + assert(0); + return -1; +}