added the E notation support to some string-to-integer functions
This commit is contained in:
		
							
								
								
									
										36
									
								
								mio/t/Makefile.am
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								mio/t/Makefile.am
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,36 @@
 | 
			
		||||
AUTOMAKE_OPTIONS = nostdinc
 | 
			
		||||
 | 
			
		||||
CPPFLAGS_COMMON = \
 | 
			
		||||
	-I$(abs_builddir) \
 | 
			
		||||
	-I$(abs_builddir)/../lib \
 | 
			
		||||
	-I$(abs_srcdir) \
 | 
			
		||||
	-I$(abs_srcdir)/../lib \
 | 
			
		||||
	-I$(includedir)
 | 
			
		||||
CFLAGS_COMMON =
 | 
			
		||||
LDFLAGS_COMMON=-L$(abs_builddir)/../lib  -L$(libdir)
 | 
			
		||||
## place $(LIBM)  here as all programs below are C only programs linked
 | 
			
		||||
## against the C/C++ hybrid library. Read comments in ../bin/Makefile.am
 | 
			
		||||
## for more information.
 | 
			
		||||
LIBADD_COMMON = ../lib/libmio.la $(LIBM)
 | 
			
		||||
 | 
			
		||||
check_SCRIPTS = 
 | 
			
		||||
##noinst_SCRIPTS = $(check_SCRIPTS)
 | 
			
		||||
EXTRA_DIST = $(check_SCRIPTS)
 | 
			
		||||
 | 
			
		||||
check_PROGRAMS = t-001 t-002
 | 
			
		||||
 | 
			
		||||
t_001_SOURCES = t-001.c t.h
 | 
			
		||||
t_001_CPPFLAGS = $(CPPFLAGS_COMMON)
 | 
			
		||||
t_001_CFLAGS = $(CFLAGS_COMMON)
 | 
			
		||||
t_001_LDFLAGS = $(LDFLAGS_COMMON)
 | 
			
		||||
t_001_LDADD = $(LIBADD_COMMON)
 | 
			
		||||
 | 
			
		||||
t_002_SOURCES = t-002.c t.h
 | 
			
		||||
t_002_CPPFLAGS = $(CPPFLAGS_COMMON)
 | 
			
		||||
t_002_CFLAGS = $(CFLAGS_COMMON)
 | 
			
		||||
t_002_LDFLAGS = $(LDFLAGS_COMMON)
 | 
			
		||||
t_002_LDADD = $(LIBADD_COMMON)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
TESTS = $(check_PROGRAMS) $(check_SCRIPTS)
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										1068
									
								
								mio/t/Makefile.in
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1068
									
								
								mio/t/Makefile.in
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										149
									
								
								mio/t/t-001.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										149
									
								
								mio/t/t-001.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,149 @@
 | 
			
		||||
/* test endian conversion macros */
 | 
			
		||||
 | 
			
		||||
#include <mio-utl.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include "t.h"
 | 
			
		||||
 | 
			
		||||
int main ()
 | 
			
		||||
{
 | 
			
		||||
	{
 | 
			
		||||
		union {
 | 
			
		||||
			mio_uint16_t u16;
 | 
			
		||||
			mio_uint8_t arr[2];
 | 
			
		||||
		} x;
 | 
			
		||||
 | 
			
		||||
		x.arr[0] = 0x11;
 | 
			
		||||
		x.arr[1] = 0x22;
 | 
			
		||||
 | 
			
		||||
		printf("x.u16 = 0x%04x\n", x.u16);
 | 
			
		||||
		printf("htole16(x.u16) = 0x%04x\n", mio_htole16(x.u16));
 | 
			
		||||
		printf("htobe16(x.u16) = 0x%04x\n", mio_htobe16(x.u16));
 | 
			
		||||
 | 
			
		||||
		T_ASSERT1 (x.u16 != mio_htole16(x.u16) || x.u16 != mio_htobe16(x.u16), "u16 endian conversion #0");
 | 
			
		||||
		T_ASSERT1 (x.u16 == mio_le16toh(mio_htole16(x.u16)), "u16 endian conversion #1");
 | 
			
		||||
		T_ASSERT1 (x.u16 == mio_be16toh(mio_htobe16(x.u16)), "u16 endian conversion #2");
 | 
			
		||||
		T_ASSERT1 (x.u16 == mio_ntoh16(mio_hton16(x.u16)), "u16 endian conversion #3");
 | 
			
		||||
 | 
			
		||||
		#define X_CONST (0x1122)
 | 
			
		||||
		T_ASSERT1 (X_CONST != MIO_CONST_HTOLE16(X_CONST) || X_CONST != MIO_CONST_HTOBE16(X_CONST), "u16 constant endian conversion #0");
 | 
			
		||||
		T_ASSERT1 (X_CONST == MIO_CONST_LE16TOH(MIO_CONST_HTOLE16(X_CONST)), "u16 constant endian conversion #1");
 | 
			
		||||
		T_ASSERT1 (X_CONST == MIO_CONST_BE16TOH(MIO_CONST_HTOBE16(X_CONST)), "u16 constant endian conversion #2");
 | 
			
		||||
		T_ASSERT1 (X_CONST == MIO_CONST_NTOH16(MIO_CONST_HTON16(X_CONST)), "u16 constant endian conversion #3");
 | 
			
		||||
		#undef X_CONST
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	{
 | 
			
		||||
		union {
 | 
			
		||||
			mio_uint32_t u32;
 | 
			
		||||
			mio_uint8_t arr[4];
 | 
			
		||||
		} x;
 | 
			
		||||
 | 
			
		||||
		x.arr[0] = 0x11;
 | 
			
		||||
		x.arr[1] = 0x22;
 | 
			
		||||
		x.arr[2] = 0x33;
 | 
			
		||||
		x.arr[3] = 0x44;
 | 
			
		||||
 | 
			
		||||
		printf("x.u32 = 0x%08x\n", (unsigned int)x.u32);
 | 
			
		||||
		printf("htole32(x.u32) = 0x%08x\n", (unsigned int)mio_htole32(x.u32));
 | 
			
		||||
		printf("htobe32(x.u32) = 0x%08x\n", (unsigned int)mio_htobe32(x.u32));
 | 
			
		||||
 | 
			
		||||
		T_ASSERT1 (x.u32 != mio_htole32(x.u32) || x.u32 != mio_htobe32(x.u32), "u32 endian conversion #0");
 | 
			
		||||
		T_ASSERT1 (x.u32 == mio_le32toh(mio_htole32(x.u32)), "u32 endian conversion #1");
 | 
			
		||||
		T_ASSERT1 (x.u32 == mio_be32toh(mio_htobe32(x.u32)), "u32 endian conversion #2");
 | 
			
		||||
		T_ASSERT1 (x.u32 == mio_ntoh32(mio_hton32(x.u32)), "u32 endian conversion #3");
 | 
			
		||||
 | 
			
		||||
		#define X_CONST (0x11223344)
 | 
			
		||||
		T_ASSERT1 (X_CONST != MIO_CONST_HTOLE32(X_CONST) || X_CONST != MIO_CONST_HTOBE32(X_CONST), "u32 constant endian conversion #0");
 | 
			
		||||
		T_ASSERT1 (X_CONST == MIO_CONST_LE32TOH(MIO_CONST_HTOLE32(X_CONST)), "u32 constant endian conversion #1");
 | 
			
		||||
		T_ASSERT1 (X_CONST == MIO_CONST_BE32TOH(MIO_CONST_HTOBE32(X_CONST)), "u32 constant endian conversion #2");
 | 
			
		||||
		T_ASSERT1 (X_CONST == MIO_CONST_NTOH32(MIO_CONST_HTON32(X_CONST)), "u32 constant endian conversion #3");
 | 
			
		||||
		#undef X_CONST
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
#if defined(MIO_HAVE_UINT64_T)
 | 
			
		||||
	{
 | 
			
		||||
		union {
 | 
			
		||||
			mio_uint64_t u64;
 | 
			
		||||
			mio_uint8_t arr[8];
 | 
			
		||||
		} x;
 | 
			
		||||
 | 
			
		||||
		x.arr[0] = 0x11;
 | 
			
		||||
		x.arr[1] = 0x22;
 | 
			
		||||
		x.arr[2] = 0x33;
 | 
			
		||||
		x.arr[3] = 0x44;
 | 
			
		||||
		x.arr[4] = 0x55;
 | 
			
		||||
		x.arr[5] = 0x66;
 | 
			
		||||
		x.arr[6] = 0x77;
 | 
			
		||||
		x.arr[7] = 0x88;
 | 
			
		||||
 | 
			
		||||
		printf("x.u64 = 0x%016llx\n", (unsigned long long)x.u64);
 | 
			
		||||
		printf("htole64(x.u64) = 0x%016llx\n", (unsigned long long)mio_htole64(x.u64));
 | 
			
		||||
		printf("htobe64(x.u64) = 0x%016llx\n", (unsigned long long)mio_htobe64(x.u64));
 | 
			
		||||
 | 
			
		||||
		T_ASSERT1 (x.u64 != mio_htole64(x.u64) || x.u64 != mio_htobe64(x.u64), "u64 endian conversion #0");
 | 
			
		||||
		T_ASSERT1 (x.u64 == mio_le64toh(mio_htole64(x.u64)), "u64 endian conversion #1");
 | 
			
		||||
		T_ASSERT1 (x.u64 == mio_be64toh(mio_htobe64(x.u64)), "u64 endian conversion #2");
 | 
			
		||||
		T_ASSERT1 (x.u64 == mio_ntoh64(mio_hton64(x.u64)), "u64 endian conversion #3");
 | 
			
		||||
 | 
			
		||||
		#define X_CONST (((mio_uint64_t)0x11223344 << 32) | (mio_uint64_t)0x55667788)
 | 
			
		||||
		T_ASSERT1 (X_CONST != MIO_CONST_HTOLE64(X_CONST) || X_CONST != MIO_CONST_HTOBE64(X_CONST), "u64 constant endian conversion #0");
 | 
			
		||||
		T_ASSERT1 (X_CONST == MIO_CONST_LE64TOH(MIO_CONST_HTOLE64(X_CONST)), "u64 constant endian conversion #1");
 | 
			
		||||
		T_ASSERT1 (X_CONST == MIO_CONST_BE64TOH(MIO_CONST_HTOBE64(X_CONST)), "u64 constant endian conversion #2");
 | 
			
		||||
		T_ASSERT1 (X_CONST == MIO_CONST_NTOH64(MIO_CONST_HTON64(X_CONST)), "u64 constant endian conversion #3");
 | 
			
		||||
		#undef X_CONST
 | 
			
		||||
	}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#if defined(MIO_HAVE_UINT128_T)
 | 
			
		||||
	{
 | 
			
		||||
		union {
 | 
			
		||||
			mio_uint128_t u128;
 | 
			
		||||
			mio_uint8_t arr[16];
 | 
			
		||||
		} x;
 | 
			
		||||
		mio_uint128_t tmp;
 | 
			
		||||
 | 
			
		||||
		x.arr[0] = 0x11;
 | 
			
		||||
		x.arr[1] = 0x22;
 | 
			
		||||
		x.arr[2] = 0x33;
 | 
			
		||||
		x.arr[3] = 0x44;
 | 
			
		||||
		x.arr[4] = 0x55;
 | 
			
		||||
		x.arr[5] = 0x66;
 | 
			
		||||
		x.arr[6] = 0x77;
 | 
			
		||||
		x.arr[7] = 0x88;
 | 
			
		||||
		x.arr[8] = 0x99;
 | 
			
		||||
		x.arr[9] = 0xaa;
 | 
			
		||||
		x.arr[10] = 0xbb;
 | 
			
		||||
		x.arr[11] = 0xcc;
 | 
			
		||||
		x.arr[12] = 0xdd;
 | 
			
		||||
		x.arr[13] = 0xee;
 | 
			
		||||
		x.arr[14] = 0xff;
 | 
			
		||||
		x.arr[15] = 0xfa;
 | 
			
		||||
 | 
			
		||||
		printf("x.u128 = 0x%016llx%016llx\n", (unsigned long long)(mio_uint64_t)(x.u128 >> 64), (unsigned long long)(mio_uint64_t)(x.u128 >> 0));
 | 
			
		||||
 | 
			
		||||
		tmp = mio_htole128(x.u128);
 | 
			
		||||
		printf("htole128(tmp) = 0x%016llx%016llx\n", (unsigned long long)(mio_uint64_t)(tmp >> 64), (unsigned long long)(mio_uint64_t)(tmp >> 0));
 | 
			
		||||
 | 
			
		||||
		tmp = mio_htobe128(x.u128);
 | 
			
		||||
		printf("htobe128(tmp) = 0x%016llx%016llx\n", (unsigned long long)(mio_uint64_t)(tmp >> 64), (unsigned long long)(mio_uint64_t)(tmp >> 0));
 | 
			
		||||
 | 
			
		||||
		T_ASSERT1 (x.u128 != mio_htole128(x.u128) || x.u128 != mio_htobe128(x.u128), "u128 endian conversion #0");
 | 
			
		||||
		T_ASSERT1 (x.u128 == mio_le128toh(mio_htole128(x.u128)), "u128 endian conversion #1");
 | 
			
		||||
		T_ASSERT1 (x.u128 == mio_be128toh(mio_htobe128(x.u128)), "u128 endian conversion #2");
 | 
			
		||||
		T_ASSERT1 (x.u128 == mio_ntoh128(mio_hton128(x.u128)), "u128 endian conversion #3");
 | 
			
		||||
 | 
			
		||||
		#define X_CONST (((mio_uint128_t)0x11223344 << 96) | ((mio_uint128_t)0x55667788 << 64) | ((mio_uint128_t)0x99aabbcc << 32)  | ((mio_uint128_t)0xddeefffa))
 | 
			
		||||
		T_ASSERT1 (X_CONST != MIO_CONST_HTOLE128(X_CONST) || X_CONST != MIO_CONST_HTOBE128(X_CONST), "u128 constant endian conversion #0");
 | 
			
		||||
		T_ASSERT1 (X_CONST == MIO_CONST_LE128TOH(MIO_CONST_HTOLE128(X_CONST)), "u128 constant endian conversion #1");
 | 
			
		||||
		T_ASSERT1 (X_CONST == MIO_CONST_BE128TOH(MIO_CONST_HTOBE128(X_CONST)), "u128 constant endian conversion #2");
 | 
			
		||||
		T_ASSERT1 (X_CONST == MIO_CONST_NTOH128(MIO_CONST_HTON128(X_CONST)), "u128 constant endian conversion #3");
 | 
			
		||||
		#undef X_CONST
 | 
			
		||||
	}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
 | 
			
		||||
oops:
 | 
			
		||||
	return -1;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										57
									
								
								mio/t/t-002.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								mio/t/t-002.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,57 @@
 | 
			
		||||
 | 
			
		||||
#include <mio-utl.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include "t.h"
 | 
			
		||||
 | 
			
		||||
int main ()
 | 
			
		||||
{
 | 
			
		||||
	{
 | 
			
		||||
		int is_sober;
 | 
			
		||||
		const mio_bch_t* endptr;
 | 
			
		||||
		mio_intmax_t v;
 | 
			
		||||
 | 
			
		||||
		v = mio_bchars_to_intmax("10 ", 3, MIO_BCHARS_TO_INTMAX_MAKE_OPTION(0,0,0,10), &endptr, &is_sober);
 | 
			
		||||
		T_ASSERT1 (v == 10 && *endptr == ' ' && is_sober == 1, "space after digits without rtrim");
 | 
			
		||||
 | 
			
		||||
		v = mio_bchars_to_intmax("10 ", 3, MIO_BCHARS_TO_INTMAX_MAKE_OPTION(0,0,1,10), &endptr, &is_sober);
 | 
			
		||||
		T_ASSERT1 (v == 10 && *endptr == '\0' && is_sober == 1, "space after digits with rtrim");
 | 
			
		||||
 | 
			
		||||
		v = mio_bchars_to_intmax("10E", 3, MIO_BCHARS_TO_INTMAX_MAKE_OPTION(0,0,1,10), &endptr, &is_sober);
 | 
			
		||||
		T_ASSERT1 (v == 10 && *endptr == 'E' && is_sober == 1, "number ending with E without the E option ");
 | 
			
		||||
 | 
			
		||||
		v = mio_bchars_to_intmax("10E", 3, MIO_BCHARS_TO_INTMAX_MAKE_OPTION(1,0,1,10), &endptr, &is_sober);
 | 
			
		||||
		T_ASSERT1 (v == 10 && *endptr == '\0' && is_sober == 1, "integer in E notation");
 | 
			
		||||
 | 
			
		||||
		v = mio_bchars_to_intmax("10E+0", 5, MIO_BCHARS_TO_INTMAX_MAKE_OPTION(1,0,1,10), &endptr, &is_sober);
 | 
			
		||||
		T_ASSERT1 (v == 10 && *endptr == '\0' && is_sober == 1, "integer in E notation");
 | 
			
		||||
 | 
			
		||||
		v = mio_bchars_to_intmax("10E+1", 5, MIO_BCHARS_TO_INTMAX_MAKE_OPTION(1,0,1,10), &endptr, &is_sober);
 | 
			
		||||
		T_ASSERT1 (v == 100 && *endptr == '\0' && is_sober == 1, "integer in E notation");
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		v = mio_bchars_to_intmax("10E+2", 5, MIO_BCHARS_TO_INTMAX_MAKE_OPTION(1,0,1,10), &endptr, &is_sober);
 | 
			
		||||
		T_ASSERT1 (v == 1000 && *endptr == '\0' && is_sober == 1, "integer in E notation");
 | 
			
		||||
 | 
			
		||||
		v = mio_bchars_to_intmax("10E3", 4, MIO_BCHARS_TO_INTMAX_MAKE_OPTION(1,0,1,10), &endptr, &is_sober);
 | 
			
		||||
		T_ASSERT1 (v == 10000 && *endptr == '\0' && is_sober == 1, "integer in E notation");
 | 
			
		||||
 | 
			
		||||
		v = mio_bchars_to_intmax("10E-", 4, MIO_BCHARS_TO_INTMAX_MAKE_OPTION(1,0,1,10), &endptr, &is_sober);
 | 
			
		||||
		T_ASSERT1 (v == 10 && *endptr == '\0' && is_sober == 1, "integer in E notation");
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		v = mio_bchars_to_intmax("10E-0", 5, MIO_BCHARS_TO_INTMAX_MAKE_OPTION(1,0,1,10), &endptr, &is_sober);
 | 
			
		||||
		T_ASSERT1 (v == 10 && *endptr == '\0' && is_sober == 1, "integer in E notation");
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		v = mio_bchars_to_intmax("10E-1", 5, MIO_BCHARS_TO_INTMAX_MAKE_OPTION(1,0,1,10), &endptr, &is_sober);
 | 
			
		||||
		T_ASSERT1 (v == 1 && *endptr == '\0' && is_sober == 1, "integer in E notation");
 | 
			
		||||
 | 
			
		||||
		v = mio_bchars_to_intmax("10E-2", 5, MIO_BCHARS_TO_INTMAX_MAKE_OPTION(1,0,1,10), &endptr, &is_sober);
 | 
			
		||||
		T_ASSERT1 (v == 0 && *endptr == '\0' && is_sober == 1, "integer in E notation");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
 | 
			
		||||
oops:
 | 
			
		||||
	return -1;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										21
									
								
								mio/t/t.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								mio/t/t.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,21 @@
 | 
			
		||||
#ifndef _HAWK_T_T_H_
 | 
			
		||||
#define _HAWK_T_T_H_
 | 
			
		||||
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
 | 
			
		||||
#if defined(__STDC_VERSION__) && (__STDC_VERSION__>=199901L)
 | 
			
		||||
#	define T_ASSERT_FAIL0() printf("FAILURE in %s:%s[%d]\n", __FILE__, __func__, (int)__LINE__)
 | 
			
		||||
#	define T_ASSERT_FAIL1(msg1) printf("FAILURE in %s:%s[%d] - %s\n", __FILE__, __func__, (int)__LINE__, msg1)
 | 
			
		||||
#	define T_ASSERT_FAIL2(msg1,msg2) printf("FAILURE in %s:%s[%d] - %s - %s\n", __FILE__, __func__, (int)__LINE__, msg1, msg2)
 | 
			
		||||
#else
 | 
			
		||||
#	define T_ASSERT_FAIL0() printf("FAILURE in %s[%d]\n", __FILE__, (int)__LINE__)
 | 
			
		||||
#	define T_ASSERT_FAIL1(msg1) printf("FAILURE in %s[%d] - %s\n", __FILE__, (int)__LINE__, msg1)
 | 
			
		||||
#	define T_ASSERT_FAIL2(msg1,msg2) printf("FAILURE in %s[%d] - %s - %s\n", __FILE__, (int)__LINE__, msg1, msg2)
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#define T_ASSERT0(test) do { if (!(test)) { T_ASSERT_FAIL0(); goto oops; } } while(0)
 | 
			
		||||
#define T_ASSERT1(test,msg1) do { if (!(test)) { T_ASSERT_FAIL1(msg1); goto oops; } } while(0)
 | 
			
		||||
#define T_ASSERT2(test,msg1,msg2) do { if (!(test)) { T_ASSERT_FAIL2(msg1,msg2); goto oops; } } while(0)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
		Reference in New Issue
	
	Block a user