File: stdarg.c

package info (click to toggle)
c2go 0.26.11-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,052 kB
  • sloc: ansic: 6,037; sh: 82; makefile: 5
file content (130 lines) | stat: -rw-r--r-- 2,510 bytes parent folder | download | duplicates (3)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
#include "tests.h"

#define START_TEST(t) \
    diag(#t);         \
    test_##t();

void simple(const char* fmt, ...)
{
	char buffer[155];
	for (int i=0;i<155;i++){
		buffer[i] = 0;
	}
	is_streq(buffer, "");

    va_list args;
    va_start(args, fmt);
			
	char temp[100];
	for (int i=0;i<100;i++){
		temp[i] = 0;
	}

	int len = 4;
	for (int i=0;i<len;i++){
		char f = fmt[i];
        if (f == 'd') {
            int i = va_arg(args, int);
			sprintf(temp, "%d ",i);
			strcat(buffer , temp);
			is_streq(buffer, "3 ")
        } else if (f == 'c') {
            // note automatic conversion to integral type
            int c = va_arg(args, int);
			sprintf(temp, "%c ",c);
			strcat(buffer , temp);
			is_streq(buffer, "3 a ")
        } else if (f == 'f') {
            double d = va_arg(args, double);
			sprintf(temp, "%.3f ",d);
			strcat(buffer , temp);
        }
    }
 
    va_end(args);

	is_streq(buffer, "3 a 1.999 42.500 ")
}

void simple2(const char* fmt, va_list args)
{
	char buffer[155];
	for (int i=0;i<155;i++){
		buffer[i] = 0;
	}
	is_streq(buffer, "");

	char temp[100];
	for (int i=0;i<100;i++){
		temp[i] = 0;
	}

	int len = 4;
	for (int i=0;i<len;i++){
		char f = fmt[i];
	if (f == 'd') {
	    int i = va_arg(args, int);
			sprintf(temp, "%d ",i);
			strcat(buffer , temp);
			is_streq(buffer, "3 ")
	} else if (f == 'c') {
	    // note automatic conversion to integral type
		int c = va_arg(args, int);
			sprintf(temp, "%c ",c);
			strcat(buffer , temp);
			is_streq(buffer, "3 a ")
	} else if (f == 'f') {
		double d = va_arg(args, double);
			sprintf(temp, "%.3f ",d);
			strcat(buffer , temp);
		}
	}

	is_streq(buffer, "3 a 1.999 42.500 ")
}

void test_va_list()
{
	simple("dcff", 3, 'a', 1.999, 42.5); 
}

void test_va_list2(const char *format, ...)
{
	va_list args;
	va_start(args, format);
	simple2(format, args);
	va_end(args);
}

void test_va_list3(void (*f) (const char *format, va_list args), char *format, ...) {
	va_list args;
	va_start(args, format);
	f(format, args);
	va_end(args);
}

typedef void func_va (const char *format, va_list args);

void test_va_list4(func_va *f, char *format, ...) {
	va_list args;
	va_start(args, format);
	f(format, args);
	va_end(args);
}

int main()
{
    plan(16);

    START_TEST(va_list)

    test_va_list2("dcff", 3, 'a', 1.999, 42.5);
    test_va_list3(simple2, "dcff", 3, 'a', 1.999, 42.5);
    test_va_list4(simple2, "dcff", 3, 'a', 1.999, 42.5);

    done_testing();
}