自己实现的vsprintf函数(未完待续)

#include <ctype.h>
#include <stdarg.h>
#include <stdint.h>

#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define FLAG_ALTNT_FORM 0x01
#define FLAG_ALTNT_FORM_CH '#'

#define FLAG_ZERO_PAD 0x02
#define FLAG_ZERO_PAD_CH '0'

#define FLAG_LEFT_ADJUST 0x04
#define FLAG_LEFT_ADJUST_CH '-'

#define FLAG_SPACE_BEFORE_POS_NUM 0x08
#define FLAG_SPACE_BEFORE_POS_NUM_CH ' '

#define FLAG_SIGN 0x10
#define FLAG_SIGN_CH '+'

#define FLAG_LOWER 0x20

#define INT_TYPE_CHAR 0x1
#define INT_TYPE_SHORT 0x2
#define INT_TYPE_INT 0x4
#define INT_TYPE_LONG 0x8
#define INT_TYPE_LONG_LONG 0x10
#define INT_TYPE_MIN INT_TYPE_CHAR
#define INT_TYPE_MAX INT_TYPE_LONG_LONG

#define BUF_SIZE 4096

static char str_buf[BUF_SIZE];
static char num_str_buf[BUF_SIZE];

static char *int32_to_str_dec(int32_t num, int flag, int width)
{
	int32_t num_tmp = num;
	char *p = num_str_buf;
	char *q = NULL;
	int len = 0;
	char *str_first = NULL;
	char *str_end = NULL;
	char ch = 0;

	memset(num_str_buf, 0, sizeof(num_str_buf));

	char dic[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

	if (num_tmp < 0)
	{
		*p++ = '-';
	}
	else if (flag & FLAG_SIGN)
	{
		*p++ = '+';
	}
	else
	{
		*p++ = ' ';
	}
	str_first = num_str_buf;

	if (num_tmp >= 0 && !(flag & FLAG_SIGN))
	{
		str_first++;
	}

	if (num_tmp == 0)
	{
		*p++ = '0';
	}
	else
	{
		if (num_tmp < 0)
			num_tmp = -num_tmp;

		while (num_tmp)
		{
			*p++ = dic[num_tmp % 10];
			num_tmp /= 10;
		}
	}
	*p = '\0';

	str_end = p;
	len = str_end - str_first;

	q = num_str_buf + 1;
	p--;
	while (q < p)
	{
		ch = *q;
		*q = *p;
		*p = ch;
		p--;
		q++;
	}

	if (len < width)
	{
		p = str_end;

		if(flag & FLAG_LEFT_ADJUST)
		{
			for(int i = 0; i < width - len; i++)
				*p++ = ' ';
			*p = '\0';
			str_end = p;
		}
		else
		{
			while (p >= str_first)
			{
				*(p + width - len) = *p;
				p--;
			}

			if (flag & FLAG_ZERO_PAD)
			{
				for (int i = 0; i < width - len; i++)
				{
					num_str_buf[i + 1] = '0';
				}
			}
			else
			{
				for (int i = 0; i < width - len; i++)
					str_first[i] = ' ';
			}
		}
	}

	return str_first;
}

static char *int64_to_str_dec(int64_t num, int flag, int width)
{
	int64_t num_tmp = num;
	char *p = num_str_buf;
	char *q = NULL;
	int len = 0;
	char *str_first = NULL;
	char *str_end = NULL;
	char ch = 0;

	memset(num_str_buf, 0, sizeof(num_str_buf));

	char dic[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

	if (num_tmp < 0)
	{
		*p++ = '-';
	}
	else if (flag & FLAG_SIGN)
	{
		*p++ = '+';
	}
	else
	{
		*p++ = ' ';
	}
	str_first = num_str_buf;

	if (num_tmp >= 0 && !(flag & FLAG_SIGN))
	{
		str_first++;
	}

	if (num_tmp == 0)
	{
		*p++ = '0';
	}
	else
	{
		if (num_tmp < 0)
			num_tmp = -num_tmp;

		while (num_tmp)
		{
			*p++ = dic[num_tmp % 10];
			num_tmp /= 10;
		}
	}
	*p = '\0';

	str_end = p;
	len = str_end - str_first;

	q = num_str_buf + 1;
	p--;
	while (q < p)
	{
		ch = *q;
		*q = *p;
		*p = ch;
		p--;
		q++;
	}

	if (len < width)
	{
		p = str_end;

		if(flag & FLAG_LEFT_ADJUST)
		{
			for(int i = 0; i < width - len; i++)
				*p++ = ' ';
			*p = '\0';
			str_end = p;
		}
		else
		{
			while (p >= str_first)
			{
				*(p + width - len) = *p;
				p--;
			}

			if (flag & FLAG_ZERO_PAD)
			{
				for (int i = 0; i < width - len; i++)
				{
					num_str_buf[i + 1] = '0';
				}
			}
			else
			{
				for (int i = 0; i < width - len; i++)
					str_first[i] = ' ';
			}
		}
	}

	return str_first;
}

static char *uint32_to_str_hex(uint32_t num, int flag, int width)
{
	uint32_t num_tmp = num;
	char *p = num_str_buf;
	char *q = NULL;
	int len = 0;
	char *str_first = NULL;
	char *str_end = NULL;
	char ch = 0;

	memset(num_str_buf, 0, sizeof(num_str_buf));

	char dic_lower[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
	char dic_upper[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
	char *dic = (flag & FLAG_LOWER) ? dic_lower : dic_upper;

	str_first = num_str_buf;

	*p++ = '0';
	*p++ = (flag & FLAG_LOWER) ? 'x' : 'X';

	if (!(flag & FLAG_ALTNT_FORM))
	{
		str_first += 2;
	}

	do
	{
		*p++ = dic[num_tmp % 16];
		num_tmp /= 16;
	} while (num_tmp);
	*p = '\0';

	str_end = p;
	len = str_end - str_first;

	q = num_str_buf + 2;
	p--;
	while (q < p)
	{
		ch = *q;
		*q = *p;
		*p = ch;
		p--;
		q++;
	}

	if (len < width)
	{
		p = str_end;

		if(flag & FLAG_LEFT_ADJUST)
		{
			for(int i = 0; i < width - len; i++)
				*p++ = ' ';
			*p = '\0';
			str_end = p;
		}
		else
		{
			while (p >= str_first)
			{
				*(p + width - len) = *p;
				p--;
			}
			if(flag & FLAG_ALTNT_FORM)
				str_first[1] = (flag & FLAG_LOWER) ? 'x' : 'X';

			if (flag & FLAG_ZERO_PAD)
			{
				for (int i = 0; i < width - len; i++)
				{
					num_str_buf[i + 2] = '0';
				}
			}
			else
			{
				for (int i = 0; i < width - len; i++)
					str_first[i] = ' ';
			}
		}
	}

	if(num == 0 && flag & FLAG_ALTNT_FORM)
		str_first[1] = '0';

	return str_first;
}

static char *uint64_to_str_hex(uint64_t num, int flag, int width)
{
	uint64_t num_tmp = num;
	char *p = num_str_buf;
	char *q = NULL;
	int len = 0;
	char *str_first = NULL;
	char *str_end = NULL;
	char ch = 0;

	memset(num_str_buf, 0, sizeof(num_str_buf));

	char dic_lower[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
	char dic_upper[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
	char *dic = (flag & FLAG_LOWER) ? dic_lower : dic_upper;

	str_first = num_str_buf;

	*p++ = '0';
	*p++ = (flag & FLAG_LOWER) ? 'x' : 'X';

	if (!(flag & FLAG_ALTNT_FORM))
	{
		str_first += 2;
	}

	while (num_tmp)
	{
		*p++ = dic[num_tmp % 16];
		num_tmp /= 16;
	}
	*p = '\0';

	str_end = p;
	len = str_end - str_first;

	q = num_str_buf + 2;
	p--;
	while (q < p)
	{
		ch = *q;
		*q = *p;
		*p = ch;
		p--;
		q++;
	}

	if (len < width)
	{
		p = str_end;

		while (p >= str_first)
		{
			*(p + width - len) = *p;
			p--;
		}

		if (flag & FLAG_ZERO_PAD)
		{
			for (int i = 0; i < width - len; i++)
			{
				num_str_buf[i + 2] = '0';
			}
		}
		else
		{
			for (int i = 0; i < width - len; i++)
				str_first[i] = ' ';
		}
	}

	return str_first;
}

static char *uint32_to_str_oct(uint32_t num, int flag, int width)
{
	uint32_t num_tmp = num;
	char *p = num_str_buf;
	char *q = NULL;
	int len = 0;
	char *str_first = NULL;
	char *str_end = NULL;
	char ch = 0;

	memset(num_str_buf, 0, sizeof(num_str_buf));

	char dic[] = {'0', '1', '2', '3', '4', '5', '6', '7'};

	str_first = num_str_buf;

	*p++ = '0';

	if (!(flag & FLAG_ALTNT_FORM))
	{
		str_first += 1;
	}

	while (num_tmp)
	{
		*p++ = dic[num_tmp % 8];
		num_tmp /= 8;
	}
	*p = '\0';

	str_end = p;
	len = str_end - str_first;

	q = num_str_buf + 1;
	p--;
	while (q < p)
	{
		ch = *q;
		*q = *p;
		*p = ch;
		p--;
		q++;
	}

	if (len < width)
	{
		p = str_end;

		if(flag & FLAG_LEFT_ADJUST)
		{
			for(int i = 0; i < width - len; i++)
				*p++ = ' ';
			*p = '\0';
			str_end = p;
		}
		else
		{
			while (p >= str_first)
			{
				*(p + width - len) = *p;
				p--;
			}

			if (flag & FLAG_ZERO_PAD)
			{
				for (int i = 0; i < width - len; i++)
				{
					num_str_buf[i + 1] = '0';
				}
			}
			else
			{
				for (int i = 0; i < width - len; i++)
					str_first[i] = ' ';
			}
		}
	}

	return str_first;
}

static char *insert_str(char *buf, const char *str)
{
	char *p = buf;

	while (*str)
	{
		*p++ = *str++;
	}

	return p;
}

int my_vsprintf(char *buf, const char *fmt, va_list args)
{
	char *str = buf;
	int flag = 0;
	int int_type = INT_TYPE_INT;
	int tot_width = 0;
	int sub_width = 0;

	char *s = NULL;
	char ch = 0;
	int8_t num_8 = 0;
	uint8_t num_u8 = 0;
	int16_t num_16 = 0;
	uint16_t num_u16 = 0;
	int32_t num_32 = 0;
	uint32_t num_u32 = 0;
	int64_t num_64 = 0;
	uint64_t num_u64 = 0;

	for (const char *p = fmt; *p; p++)
	{
		if (*p != '%')
		{
			*str++ = *p;
			continue;
		}

		flag = 0;
		tot_width = 0;
		sub_width = 0;
		int_type = INT_TYPE_INT;

		p++;

		while (*p == FLAG_ALTNT_FORM_CH || *p == FLAG_ZERO_PAD_CH || *p == FLAG_LEFT_ADJUST_CH || *p == FLAG_SPACE_BEFORE_POS_NUM_CH || *p == FLAG_SIGN_CH)
		{
			if (*p == FLAG_ALTNT_FORM_CH)
			{
				flag |= FLAG_ALTNT_FORM;
			}
			else if (*p == FLAG_ZERO_PAD_CH)
			{
				flag |= FLAG_ZERO_PAD;
			}
			else if (*p == FLAG_LEFT_ADJUST_CH)
			{
				flag |= FLAG_LEFT_ADJUST;
				flag &= ~FLAG_ZERO_PAD;
			}
			else if (*p == FLAG_SPACE_BEFORE_POS_NUM_CH)
			{
				flag |= FLAG_SPACE_BEFORE_POS_NUM;
			}
			else if (*p == FLAG_SIGN_CH)
			{
				flag |= FLAG_SIGN;
			}
			else
			{
			}

			p++;
		}

		if(*p == '*')
		{
			tot_width = va_arg(args, int);
			if(tot_width < 0)
				tot_width = 0;
			p++;
		}
		else
		{
			while (isdigit(*p))
			{
				tot_width = tot_width * 10 + *p - '0';
				p++;
			}
		}
		if (*p == '.')
		{
			if(*p == '*')
			{
				sub_width = va_arg(args, int);
				if(sub_width < 0)
					sub_width = 0;
				p++;
			}
			else
			{
				while (isdigit(*p))
				{
					sub_width = sub_width * 10 + *p - '0';
					p++;
				}
			}
		}

LOOP_switch:
		switch (*p)
		{
		case 'h':
			p++;
			if(int_type >= INT_TYPE_MIN)
			{
				int_type >>= 1;
				goto LOOP_switch;
			}
			else
			{
				*str++ = '%';
				break;
			}
		case 'l':
			p++;
			if(int_type <= INT_TYPE_MAX)
			{
				int_type <<= 1;
				goto LOOP_switch;
			}
			else
			{
				*str++ = '%';
				break;
			}
		case 's':
			s = va_arg(args, char *);
			str = insert_str(str, s);
			break;
		case 'c':
			ch = (char)(va_arg(args, int) & 0xFF);
			*str++ = ch;
			break;
		case 'd':
			switch(int_type)
			{
				case INT_TYPE_CHAR:
					num_8 = (int8_t)va_arg(args, int32_t);
					str = insert_str(str, int32_to_str_dec(num_8, flag, tot_width));
					break;
				case INT_TYPE_SHORT:
					num_16 = (int16_t)va_arg(args, int32_t);
					str = insert_str(str, int32_to_str_dec(num_16, flag, tot_width));
					break;
				case INT_TYPE_INT:
					num_32 = va_arg(args, int32_t);
					str = insert_str(str, int32_to_str_dec(num_32, flag, tot_width));
					break;
				case INT_TYPE_LONG:
					num_64 = va_arg(args, int64_t);
					str = insert_str(str, int64_to_str_dec(num_64, flag, tot_width));
					break;
				case INT_TYPE_LONG_LONG:
					num_64 = va_arg(args, int64_t);
					str = insert_str(str, int64_to_str_dec(num_64, flag, tot_width));
					break;
			}
			break;
		case 'x':
			flag |= FLAG_LOWER;
		case 'X':
			switch(int_type)
			{
				case INT_TYPE_CHAR:
					num_u8 = (uint8_t)va_arg(args, uint32_t);
					str = insert_str(str, uint32_to_str_hex(num_u8, flag, tot_width));
					break;
				case INT_TYPE_SHORT:
					num_u16 = (uint16_t)va_arg(args, uint32_t);
					str = insert_str(str, uint32_to_str_hex(num_u16, flag, tot_width));
					break;
				case INT_TYPE_INT:
					num_u32 = va_arg(args, uint32_t);
					str = insert_str(str, uint32_to_str_hex(num_u32, flag, tot_width));
					break;
				case INT_TYPE_LONG:
					num_u64 = va_arg(args, uint64_t);
					str = insert_str(str, uint64_to_str_hex(num_u64, flag, tot_width));
					break;
				case INT_TYPE_LONG_LONG:
					num_u64 = va_arg(args, uint64_t);
					str = insert_str(str, uint64_to_str_hex(num_u64, flag, tot_width));
					break;
			}
			break;
		case 'o':
			num_u32 = va_arg(args, uint32_t);
			str = insert_str(str, uint32_to_str_oct(num_u32, flag, tot_width));
			break;
		case '%':
			*str++ = '%';
			break;
		default:
			*str++ = '%';
			*str++ = *p;
			break;
		}
	}
	*str = '\0';

	return str - buf;
}

int my_sprintf(char * buf, const char *fmt, ...)
{
	int char_cnt = 0;
	va_list args;

	va_start(args, fmt);
	char_cnt = my_vsprintf(buf, fmt, args);
	va_end(args);

	return char_cnt;
}

static char str_buf_right[BUF_SIZE];

int main(void)
{
	int len_right = 0;
	int len = 0;
	
	const int times = 1000000;
	const int times2 = 10;
	bool is_all_succ = true;
	int i = 0;
	int j = 0;
	char * fmt;
	char buf_fmt[100];
	int8_t num_8 = 0;
	uint8_t num_u8 = 0;
	int16_t num_16 = 0;
	uint16_t num_u16 = 0;
	int32_t num_32 = 0;
	uint32_t num_u32 = 0;
	int64_t num_64 = 0;
	uint64_t num_u64 = 0;
	int width = 0;

	/*
	int32_t kkk_32 = 0x12345678;
	uint32_t kkk_u32 = 0x12345678;
	int64_t kkk_64 = 0x123456789bcdef0;
	uint64_t kkk_u64 = 0x123456789bcdef0;
	unsigned long long int kkk1 = 0x123456789bcdef0;
	printf("%hx\n", kkk_u32);
	printf("%hhx\n", kkk_u32);
	printf("%lx\n", kkk_u64);
	printf("%llx\n", kkk1);
	*/

	srand(time(NULL));

	for(j = 0; j < times2; j++)
	{
		printf("----- Testing %d times -----\n", j + 1);

		/******************** %d ********************/
		fmt = "%d";
		for(i = 0; i < times; i++)
		{
			memset(str_buf_right, 0, sizeof(str_buf_right));
			memset(str_buf, 0, sizeof(str_buf));
			num_32 = rand();
			len_right = sprintf(str_buf_right, fmt, num_32);
			len = my_sprintf(str_buf, fmt, num_32);
			if(len_right != len || strncmp(str_buf_right, str_buf, len))
			{
				printf("' %s ' occurred error, and has been tested %d times successfully only!\n", fmt, i);
				is_all_succ = false;
				break;
			}
		}
		if(i == times)
			printf("' %s ' has been tested %d times successfully!\n", fmt, times);
		
		/******************** %hd ********************/
		fmt = "%hd";
		for(i = 0; i < times; i++)
		{
			memset(str_buf_right, 0, sizeof(str_buf_right));
			memset(str_buf, 0, sizeof(str_buf));
			num_16 = (int16_t)rand() % SHRT_MAX;
			len_right = sprintf(str_buf_right, fmt, num_16);
			len = my_sprintf(str_buf, fmt, num_16);
			if(len_right != len || strncmp(str_buf_right, str_buf, len))
			{
				printf("' %s ' occurred error, and has been tested %d times successfully only!\n", fmt, i);
				is_all_succ = false;
				break;
			}
		}
		if(i == times)
			printf("' %s ' has been tested %d times successfully!\n", fmt, times);
		
		/******************** %hhd ********************/
		fmt = "%hhd";
		for(i = 0; i < times; i++)
		{
			memset(str_buf_right, 0, sizeof(str_buf_right));
			memset(str_buf, 0, sizeof(str_buf));
			num_8 = rand() % CHAR_MAX;
			len_right = sprintf(str_buf_right, fmt, num_8);
			len = my_sprintf(str_buf, fmt, num_8);
			if(len_right != len || strncmp(str_buf_right, str_buf, len))
			{
				printf("' %s ' occurred error, and has been tested %d times successfully only!\n", fmt, i);
				is_all_succ = false;
				break;
			}
		}
		if(i == times)
			printf("' %s ' has been tested %d times successfully!\n", fmt, times);
		
		/******************** %ld ********************/
		fmt = "%ld";
		for(i = 0; i < times; i++)
		{
			memset(str_buf_right, 0, sizeof(str_buf_right));
			memset(str_buf, 0, sizeof(str_buf));
			*(int *)&num_64 = rand();
			*((int *)&num_64 + 1) = rand();
			len_right = sprintf(str_buf_right, fmt, num_64);
			len = my_sprintf(str_buf, fmt, num_64);
			if(len_right != len || strncmp(str_buf_right, str_buf, len))
			{
				printf("' %s ' occurred error, and has been tested %d times successfully only!\n", fmt, i);
				is_all_succ = false;
				break;
			}
		}
		if(i == times)
			printf("' %s ' has been tested %d times successfully!\n", fmt, times);
		
		/******************** %lld ********************/
		fmt = "%lld";
		for(i = 0; i < times; i++)
		{
			memset(str_buf_right, 0, sizeof(str_buf_right));
			memset(str_buf, 0, sizeof(str_buf));
			*(int *)&num_64 = rand();
			*((int *)&num_64 + 1) = rand();
			len_right = sprintf(str_buf_right, fmt, num_64);
			len = my_sprintf(str_buf, fmt, num_64);
			if(len_right != len || strncmp(str_buf_right, str_buf, len))
			{
				printf("' %s ' occurred error, and has been tested %d times successfully only!\n", fmt, i);
				is_all_succ = false;
				break;
			}
		}
		if(i == times)
			printf("' %s ' has been tested %d times successfully!\n", fmt, times);

		/******************** %+d ********************/
		fmt = "%+d";
		for(i = 0; i < times; i++)
		{
			memset(str_buf_right, 0, sizeof(str_buf_right));
			memset(str_buf, 0, sizeof(str_buf));
			num_32 = rand();
			len_right = sprintf(str_buf_right, fmt, num_32);
			len = my_sprintf(str_buf, fmt, num_32);
			if(len_right != len || strncmp(str_buf_right, str_buf, len))
			{
				printf("' %s ' occurred error, and has been tested %d times successfully only!\n", fmt, i);
				is_all_succ = false;
				break;
			}
		}
		if(i == times)
			printf("' %s ' has been tested %d times successfully!\n", fmt, times);

		/******************** %5d ********************/
		fmt = buf_fmt;
		for(i = 0; i < times; i++)
		{
			memset(buf_fmt, 0, sizeof(buf_fmt));
			width = abs(rand()) % 100;
			sprintf(buf_fmt, "%%%dd", width);

			memset(str_buf_right, 0, sizeof(str_buf_right));
			memset(str_buf, 0, sizeof(str_buf));
			num_32 = rand();
			len_right = sprintf(str_buf_right, fmt, num_32);
			len = my_sprintf(str_buf, fmt, num_32);
			if(len_right != len || strncmp(str_buf_right, str_buf, len))
			{
				printf("' %s ' occurred error, and has been tested %d times successfully only!\n", fmt, i);
				is_all_succ = false;
				break;
			}
		}
		if(i == times)
			printf("' %s ' has been tested %d times successfully!\n", fmt, times);
		
		/******************** %05d ********************/
		fmt = buf_fmt;
		for(i = 0; i < times; i++)
		{
			memset(buf_fmt, 0, sizeof(buf_fmt));
			width = abs(rand()) % 100;
			sprintf(buf_fmt, "%%0%dd", width);

			memset(str_buf_right, 0, sizeof(str_buf_right));
			memset(str_buf, 0, sizeof(str_buf));
			num_32 = rand();
			len_right = sprintf(str_buf_right, fmt, num_32);
			len = my_sprintf(str_buf, fmt, num_32);
			if(len_right != len || strncmp(str_buf_right, str_buf, len))
			{
				printf("' %s ' occurred error, and has been tested %d times successfully only!\n", fmt, i);
				is_all_succ = false;
				break;
			}
		}
		if(i == times)
			printf("' %s ' has been tested %d times successfully!\n", fmt, times);
		
		/******************** %+5d ********************/
		fmt = buf_fmt;
		for(i = 0; i < times; i++)
		{
			memset(buf_fmt, 0, sizeof(buf_fmt));
			width = abs(rand()) % 100;
			sprintf(buf_fmt, "%%+%dd", width);

			memset(str_buf_right, 0, sizeof(str_buf_right));
			memset(str_buf, 0, sizeof(str_buf));
			num_32 = rand();
			len_right = sprintf(str_buf_right, fmt, num_32);
			len = my_sprintf(str_buf, fmt, num_32);
			if(len_right != len || strncmp(str_buf_right, str_buf, len))
			{
				printf("' %s ' occurred error, and has been tested %d times successfully only!\n", fmt, i);
				is_all_succ = false;
				break;
			}
		}
		if(i == times)
			printf("' %s ' has been tested %d times successfully!\n", fmt, times);
		
		/******************** %+05d ********************/
		fmt = buf_fmt;
		for(i = 0; i < times; i++)
		{
			memset(buf_fmt, 0, sizeof(buf_fmt));
			width = abs(rand()) % 100;
			sprintf(buf_fmt, "%%+0%dd", width);

			memset(str_buf_right, 0, sizeof(str_buf_right));
			memset(str_buf, 0, sizeof(str_buf));
			num_32 = rand();
			len_right = sprintf(str_buf_right, fmt, num_32);
			len = my_sprintf(str_buf, fmt, num_32);
			if(len_right != len || strncmp(str_buf_right, str_buf, len))
			{
				printf("' %s ' occurred error, and has been tested %d times successfully only!\n", fmt, i);
				is_all_succ = false;
				break;
			}
		}
		if(i == times)
			printf("' %s ' has been tested %d times successfully!\n", fmt, times);
		
		/******************** %-5d ********************/
		fmt = buf_fmt;
		for(i = 0; i < times; i++)
		{
			memset(buf_fmt, 0, sizeof(buf_fmt));
			width = abs(rand()) % 100;
			sprintf(buf_fmt, "%%-%dd", width);

			memset(str_buf_right, 0, sizeof(str_buf_right));
			memset(str_buf, 0, sizeof(str_buf));
			num_32 = rand();
			len_right = sprintf(str_buf_right, fmt, num_32);
			len = my_sprintf(str_buf, fmt, num_32);
			if(len_right != len || strncmp(str_buf_right, str_buf, len))
			{
				printf("' %s ' occurred error, and has been tested %d times successfully only!\n", fmt, i);
				is_all_succ = false;
				break;
			}
		}
		if(i == times)
			printf("' %s ' has been tested %d times successfully!\n", fmt, times);
		
		/******************** %-*d ********************/
		fmt = "%-*d";
		for(i = 0; i < times; i++)
		{
			width = abs(rand()) % 100;

			memset(str_buf_right, 0, sizeof(str_buf_right));
			memset(str_buf, 0, sizeof(str_buf));
			num_32 = rand();
			len_right = sprintf(str_buf_right, fmt, width, num_32);
			len = my_sprintf(str_buf, fmt, width, num_32);
			if(len_right != len || strncmp(str_buf_right, str_buf, len))
			{
				printf("' %s ' occurred error, and has been tested %d times successfully only!\n", fmt, i);
				is_all_succ = false;
				break;
			}
		}
		if(i == times)
			printf("' %s ' has been tested %d times successfully!\n", fmt, times);

		/******************** %x ********************/
		fmt = "%x";
		for(i = 0; i < times; i++)
		{
			memset(str_buf_right, 0, sizeof(str_buf_right));
			memset(str_buf, 0, sizeof(str_buf));
			num_u32 = rand();
			len_right = sprintf(str_buf_right, fmt, num_u32);
			len = my_sprintf(str_buf, fmt, num_u32);
			if(len_right != len || strncmp(str_buf_right, str_buf, len))
			{
				printf("' %s ' occurred error, and has been tested %d times successfully only!\n", fmt, i);
				is_all_succ = false;
				break;
			}
		}
		if(i == times)
			printf("' %s ' has been tested %d times successfully!\n", fmt, times);
		
		/******************** %X ********************/
		fmt = "%X";
		for(i = 0; i < times; i++)
		{
			memset(str_buf_right, 0, sizeof(str_buf_right));
			memset(str_buf, 0, sizeof(str_buf));
			num_u32 = rand();
			len_right = sprintf(str_buf_right, fmt, num_u32);
			len = my_sprintf(str_buf, fmt, num_u32);
			if(len_right != len || strncmp(str_buf_right, str_buf, len))
			{
				printf("' %s ' occurred error, and has been tested %d times successfully only!\n", fmt, i);
				is_all_succ = false;
				break;
			}
		}
		if(i == times)
			printf("' %s ' has been tested %d times successfully!\n", fmt, times);
		
		/******************** %hx ********************/
		fmt = "%hx";
		for(i = 0; i < times; i++)
		{
			memset(str_buf_right, 0, sizeof(str_buf_right));
			memset(str_buf, 0, sizeof(str_buf));
			num_u16 = rand() % SHRT_MAX;
			len_right = sprintf(str_buf_right, fmt, num_u16);
			len = my_sprintf(str_buf, fmt, num_u16);
			if(len_right != len || strncmp(str_buf_right, str_buf, len))
			{
				printf("' %s ' occurred error, and has been tested %d times successfully only!\n", fmt, i);
				is_all_succ = false;
				break;
			}
		}
		if(i == times)
			printf("' %s ' has been tested %d times successfully!\n", fmt, times);
		
		/******************** %hhx ********************/
		fmt = "%hhx";
		for(i = 0; i < times; i++)
		{
			memset(str_buf_right, 0, sizeof(str_buf_right));
			memset(str_buf, 0, sizeof(str_buf));
			num_u8 = rand() % CHAR_MAX;
			len_right = sprintf(str_buf_right, fmt, num_u8);
			len = my_sprintf(str_buf, fmt, num_u8);
			if(len_right != len || strncmp(str_buf_right, str_buf, len))
			{
				printf("' %s ' occurred error, and has been tested %d times successfully only!\n", fmt, i);
				is_all_succ = false;
				break;
			}
		}
		if(i == times)
			printf("' %s ' has been tested %d times successfully!\n", fmt, times);
		
		/******************** %lx ********************/
		fmt = "%lx";
		for(i = 0; i < times; i++)
		{
			memset(str_buf_right, 0, sizeof(str_buf_right));
			memset(str_buf, 0, sizeof(str_buf));
			*(int *)&num_u64 = rand();
			*((int *)&num_u64 + 1) = rand();
			len_right = sprintf(str_buf_right, fmt, num_u64);
			len = my_sprintf(str_buf, fmt, num_u64);
			if(len_right != len || strncmp(str_buf_right, str_buf, len))
			{
				printf("' %s ' occurred error, and has been tested %d times successfully only!\n", fmt, i);
				is_all_succ = false;
				break;
			}
		}
		if(i == times)
			printf("' %s ' has been tested %d times successfully!\n", fmt, times);
		
		/******************** %llx ********************/
		fmt = "%llx";
		for(i = 0; i < times; i++)
		{
			memset(str_buf_right, 0, sizeof(str_buf_right));
			memset(str_buf, 0, sizeof(str_buf));
			*(int *)&num_u64 = rand();
			*((int *)&num_u64 + 1) = rand();
			len_right = sprintf(str_buf_right, fmt, num_u64);
			len = my_sprintf(str_buf, fmt, num_u64);
			if(len_right != len || strncmp(str_buf_right, str_buf, len))
			{
				printf("' %s ' occurred error, and has been tested %d times successfully only!\n", fmt, i);
				is_all_succ = false;
				break;
			}
		}
		if(i == times)
			printf("' %s ' has been tested %d times successfully!\n", fmt, times);
		
		/******************** %#x ********************/
		fmt = "%#x";
		for(i = 0; i < times; i++)
		{
			memset(str_buf_right, 0, sizeof(str_buf_right));
			memset(str_buf, 0, sizeof(str_buf));
			num_u32 = rand();
			len_right = sprintf(str_buf_right, fmt, num_u32);
			len = my_sprintf(str_buf, fmt, num_u32);
			if(len_right != len || strncmp(str_buf_right, str_buf, len))
			{
				printf("' %s ' occurred error, and has been tested %d times successfully only!\n", fmt, i);
				is_all_succ = false;
				break;
			}
		}
		if(i == times)
			printf("' %s ' has been tested %d times successfully!\n", fmt, times);
		
		/******************** %5x ********************/
		fmt = buf_fmt;
		for(i = 0; i < times; i++)
		{
			memset(buf_fmt, 0, sizeof(buf_fmt));
			width = abs(rand()) % 100;
			sprintf(buf_fmt, "%%%dx", width);

			memset(str_buf_right, 0, sizeof(str_buf_right));
			memset(str_buf, 0, sizeof(str_buf));
			num_u32 = rand();
			len_right = sprintf(str_buf_right, fmt, num_u32);
			len = my_sprintf(str_buf, fmt, num_u32);
			if(len_right != len || strncmp(str_buf_right, str_buf, len))
			{
				printf("' %s ' occurred error, and has been tested %d times successfully only!\n", fmt, i);
				is_all_succ = false;
				break;
			}
		}
		if(i == times)
			printf("' %s ' has been tested %d times successfully!\n", fmt, times);
		
		/******************** %05x ********************/
		fmt = buf_fmt;
		for(i = 0; i < times; i++)
		{
			memset(buf_fmt, 0, sizeof(buf_fmt));
			width = abs(rand()) % 100;
			sprintf(buf_fmt, "%%0%dx", width);

			memset(str_buf_right, 0, sizeof(str_buf_right));
			memset(str_buf, 0, sizeof(str_buf));
			num_u32 = rand();
			len_right = sprintf(str_buf_right, fmt, num_u32);
			len = my_sprintf(str_buf, fmt, num_u32);
			if(len_right != len || strncmp(str_buf_right, str_buf, len))
			{
				printf("' %s ' occurred error, and has been tested %d times successfully only!\n", fmt, i);
				is_all_succ = false;
				break;
			}
		}
		if(i == times)
			printf("' %s ' has been tested %d times successfully!\n", fmt, times);
		
		/******************** %#05x ********************/
		fmt = buf_fmt;
		for(i = 0; i < times; i++)
		{
			memset(buf_fmt, 0, sizeof(buf_fmt));
			width = abs(rand()) % 100;
			sprintf(buf_fmt, "%%#0%dx", width);

			memset(str_buf_right, 0, sizeof(str_buf_right));
			memset(str_buf, 0, sizeof(str_buf));
			num_u32 = rand();
			len_right = sprintf(str_buf_right, fmt, num_u32);
			len = my_sprintf(str_buf, fmt, num_u32);
			if(len_right != len || strncmp(str_buf_right, str_buf, len))
			{
				printf("' %s ' occurred error, and has been tested %d times successfully only!\n", fmt, i);
				is_all_succ = false;
				break;
			}
		}
		if(i == times)
			printf("' %s ' has been tested %d times successfully!\n", fmt, times);
		
		/******************** %-5x ********************/
		fmt = buf_fmt;
		for(i = 0; i < times; i++)
		{
			memset(buf_fmt, 0, sizeof(buf_fmt));
			width = abs(rand()) % 100;
			sprintf(buf_fmt, "%%-%dx", width);

			memset(str_buf_right, 0, sizeof(str_buf_right));
			memset(str_buf, 0, sizeof(str_buf));
			num_u32 = rand();
			len_right = sprintf(str_buf_right, fmt, num_u32);
			len = my_sprintf(str_buf, fmt, num_u32);
			if(len_right != len || strncmp(str_buf_right, str_buf, len))
			{
				printf("' %s ' occurred error, and has been tested %d times successfully only!\n", fmt, i);
				is_all_succ = false;
				break;
			}
		}
		if(i == times)
			printf("' %s ' has been tested %d times successfully!\n", fmt, times);
	}
	if(is_all_succ)
	{
		printf("All done!\n");
	}
	else
	{
		printf("Occurred error!\n");
	}
	

	return EXIT_SUCCESS;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值