Line data Source code
1 : #include "sute.h"
2 :
3 15 : static void test_conversion(
4 : int value,
5 : unsigned int base,
6 : const char *string)
7 : {
8 : char buffer[66]; /* 64 bits + sign + null terminator */
9 15 : itoa(value,buffer,base);
10 :
11 : /* Print original value in decimal and result in specified base */
12 15 : printf("Value: %d (decimal)\n",value);
13 15 : printf("Base %2u result: %s, %s\n",base,buffer,string);
14 15 : printf("-------------------\n");
15 15 : }
16 :
17 : /**
18 : * @brief Test program for itoa function
19 : *
20 : * @note Tests edge cases and different bases with special focus on
21 : * negative numbers and MIN/MAX integer values
22 : */
23 1 : static void test_itoa(void)
24 : {
25 : /* Test extreme values */
26 1 : printf("=== Testing extreme values ===\n");
27 1 : test_conversion(INT_MAX,10,"2147483647");
28 1 : test_conversion(INT_MIN,10,"-2147483648");
29 1 : test_conversion(INT_MIN,16,"Should show in hex");
30 :
31 : /* Test regular cases */
32 1 : printf("\n=== Testing regular values ===\n");
33 1 : test_conversion(255,16,"FF");
34 1 : test_conversion(255,2,"11111111");
35 1 : test_conversion(-255,10,"-255");
36 :
37 : /* Test zero handling */
38 1 : printf("\n=== Testing zero ===\n");
39 1 : test_conversion(0,10,"0");
40 1 : test_conversion(0,16,"0");
41 1 : test_conversion(0,2,"0");
42 :
43 : /* Test larger bases */
44 1 : printf("\n=== Testing different bases ===\n");
45 1 : test_conversion(12345,36,"Maximum supported base");
46 1 : test_conversion(12345,16,"Common hex value");
47 1 : test_conversion(12345,8,"Octal");
48 :
49 : /* Test negative values in different bases */
50 1 : printf("\n=== Testing negative values ===\n");
51 1 : test_conversion(-12345,10,"Only base 10 shows negative sign");
52 1 : test_conversion(-12345,16,"Should show unsigned hex");
53 1 : test_conversion(-1,2,"All bits set");
54 :
55 1 : printf("\n=== Few more examples ===\n");
56 : char buffer[33]; /* Buffer for 32-bit integer */
57 :
58 : /* Decimal conversion */
59 1 : itoa(12345,buffer,10);
60 1 : puts(buffer);
61 :
62 : /* Hexadecimal conversion */
63 1 : itoa(255,buffer,16);
64 1 : puts(buffer);
65 :
66 : /* Binary conversion */
67 1 : itoa(15,buffer,2);
68 1 : puts(buffer);
69 :
70 : /* Negative number */
71 1 : itoa(-789,buffer,10);
72 1 : puts(buffer);
73 :
74 : /* Zero case */
75 1 : itoa(0,buffer,10);
76 1 : puts(buffer);
77 :
78 : char str[100];
79 1 : printf("Number: %d\nBase: %d\tConverted String: %s\n",1567,10,itoa(1567,str,10));
80 1 : printf("Base: %d\t\tConverted String: %s\n",2,itoa(1567,str,2));
81 1 : printf("Base: %d\t\tConverted String: %s\n",8,itoa(1567,str,8));
82 1 : printf("Base: %d\tConverted String: %s\n",16,itoa(1567,str,16));
83 1 : }
84 :
85 : /**
86 : * @brief Test program for itoa function
87 : *
88 : */
89 1 : Return test0017(void)
90 : {
91 1 : INITTEST;
92 :
93 1 : create(char,captured_stdout);
94 1 : create(char,captured_stderr);
95 :
96 1 : create(char,pattern);
97 :
98 1 : ASSERT(SUCCESS == function_capture(test_itoa,captured_stdout,captured_stderr));
99 :
100 1 : if(captured_stderr->length > 0)
101 : {
102 0 : echo(STDERR,"ERROR: Stderr buffer is not empty. It contains characters: %zu\n",captured_stderr->length);
103 0 : status = FAILURE;
104 : #if 0
105 : echo(STDOUT,"%s\n",getcstring(captured_stderr));
106 : #endif
107 : }
108 :
109 1 : ASSERT(SUCCESS == get_file_content("templates/0017.txt",pattern));
110 :
111 : // Match the result against the pattern
112 1 : ASSERT(SUCCESS == match_pattern(captured_stdout,pattern));
113 :
114 1 : del(pattern);
115 :
116 1 : del(captured_stdout);
117 1 : del(captured_stderr);
118 :
119 1 : RETURN_STATUS;
120 : }
|