Question
Transcribed Text
Solution Preview
This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.
#include <string.h>#include <stdlib.h>
#include <stdio.h>
#define MAX_SIZE (256)
#define ASSERT_EQUAL(x,y,msg) do {\
if ((x) != (y)) {\
printf("%s - Failed\n", msg);\
printf("Expected %d bytes and received %d\n", (x), (y));\
} else {\
printf("%s - Passed\n", msg);\
}\
} while(0)
#define ASSERT_STRING_EQUAL(x,y,msg) do {\
if (strncmp((x), (y), MAX_SIZE) != 0) { \
printf("%s - Failed\n", msg); \
printf("Expected:\n%s\nOutput:\n%s\n", (x), (y));\
} else {\
printf("%s - Passed\n", msg);\
}\
} while(0)
int format_to_string(char *buffer,const char *format,void *arg1,void *arg2,void *arg3);
int main(void)
{
char buffer[MAX_SIZE] = {0};
char test_buffer[MAX_SIZE] = {0};
int num_bytes = 0;
int test_num_bytes = 0;
int arg11 = 1;
char arg12 = 'd';
char *arg13 = "Hello";
num_bytes = sprintf(buffer, "arg1 = %d, arg2 = %c, arg3 = %s\n", arg11, arg12, arg13);
test_num_bytes = format_to_string(test_buffer, "arg1 = %d, arg2 = %c, arg3 = %s\n", &arg11, &arg12, arg13);
ASSERT_EQUAL(num_bytes, test_num_bytes, "Test1");
ASSERT_STRING_EQUAL(buffer, test_buffer, "Test2");
int arg21 = 33;
char *arg22 = "Hello";
int arg23 = 43;
memset(buffer, 0, MAX_SIZE);
memset(test_buffer, 0, MAX_SIZE);
num_bytes = sprintf(buffer, "%% arg1 = %d, arg2 = %s, %% arg3 = %x\n", arg21, arg22, arg23);
test_num_bytes = format_to_string(test_buffer, "%% arg1 = %d, arg2 = %s, %% arg3 = %x\n", &arg21, arg22, &arg23);
ASSERT_EQUAL(num_bytes, test_num_bytes, "Test3");
ASSERT_STRING_EQUAL(buffer, test_buffer, "Test4");
float arg31 = 1.0/3;
float arg32 = 5.0/4;
memset(buffer, 0, MAX_SIZE);
memset(test_buffer, 0, MAX_SIZE);
strcpy(buffer, "% arg1 = 0.3333333, arg2 = 1.25\n");
num_bytes = strlen(buffer);
test_num_bytes = format_to_string(test_buffer, "%% arg1 = %f, arg2 = %f\n", &arg31, &arg32, NULL);
ASSERT_EQUAL(num_bytes, test_num_bytes, "Test5");
ASSERT_STRING_EQUAL(buffer, test_buffer, "Test6");
int arg41 = 33;
char *arg42 = "Hello";
int arg43 = 43;
test_num_bytes = format_to_string(test_buffer, "arg1 = %d, arg2 = %s, arg3 = %d %c\n", &arg41, arg42, &arg43);
ASSERT_EQUAL(test_num_bytes...