summaryrefslogtreecommitdiff
path: root/lib/libc_r/TEST/test_stdarg.c
blob: f2b4781cff258e437289c39276a62121b2c45066 (plain)
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
/*	$OpenBSD: test_stdarg.c,v 1.2 2000/01/04 02:31:44 d Exp $	*/
/*
 * Test <stdarg.h>
 */

#include <pthread.h>
#include <stdio.h>
#include <stdarg.h>
#include "test.h"

int thing;

int
test1(char *fmt, ...)
{
	va_list	ap;

	int	i;
	char	c;
	long	l;
	void *	p;

	va_start(ap, fmt);
	for (; *fmt; fmt++)
	    switch (*fmt) {
	    case 'i':		
		i = va_arg(ap, int); 
		ASSERT(i == 1234);
		break;
	    case 'c':		
		c = va_arg(ap, char); 
		ASSERT(c == 'x');
		break;
	    case 'l':		
		l = va_arg(ap, long); 
		ASSERT(l == 123456789L);
		break;
	    case 'p':		
		p = va_arg(ap, void *); 
		ASSERT(p == &thing);
		break;
	    default:
		ASSERT(0);
	    }
	va_end(ap);
	return 9;
}

void * 
run_test(arg)
	void *arg;
{
	char *msg = (char *)arg;
	int i;

	printf("Testing stdarg: %s\n", msg);
	for (i = 0; i < 1000000; i++) {
		ASSERT(test1("iclp", 1234, 'x', 123456789L, &thing) == 9);
	}
	printf("ok\n");
	return NULL;
}

int
main()
{
	pthread_t t1, t2;

	run_test("in main thread");
	CHECKr(pthread_create(&t1, NULL, run_test, "in child thread 1"));
	CHECKr(pthread_create(&t2, NULL, run_test, "in child thread 2"));
	CHECKr(pthread_join(t1, NULL));
	CHECKr(pthread_join(t2, NULL));
	SUCCEED;
}