xref: /openbsd-src/regress/lib/libpthread/stdarg/stdarg.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1*db3296cfSderaadt /*	$OpenBSD: stdarg.c,v 1.5 2003/07/31 21:48:06 deraadt Exp $	*/
2b2ea75c1Sfgsch /* David Leonard <d@openbsd.org>, 2001. Public Domain. */
3b2ea75c1Sfgsch 
4b2ea75c1Sfgsch /*
5b2ea75c1Sfgsch  * Test <stdarg.h>
6b2ea75c1Sfgsch  */
7b2ea75c1Sfgsch 
8b2ea75c1Sfgsch #include <pthread.h>
9b2ea75c1Sfgsch #include <stdio.h>
10b2ea75c1Sfgsch #include <stdarg.h>
118445c537Stodd #include <stdlib.h>
12b2ea75c1Sfgsch #include "test.h"
13b2ea75c1Sfgsch 
14b2ea75c1Sfgsch #define EQ(v,exp) _CHECK(v, == exp, NULL)
15b2ea75c1Sfgsch 
16b2ea75c1Sfgsch int thing;
17b2ea75c1Sfgsch 
18*db3296cfSderaadt static int
test1(char * fmt,...)19b2ea75c1Sfgsch test1(char *fmt, ...)
20b2ea75c1Sfgsch {
21b2ea75c1Sfgsch 	va_list	ap;
22b2ea75c1Sfgsch 
23b2ea75c1Sfgsch 	char	ch;
24b2ea75c1Sfgsch 	int	i;
25a8d37202Sfgsch 	int	c;
26b2ea75c1Sfgsch 	long	l;
27b2ea75c1Sfgsch 	void 	*p;
28b2ea75c1Sfgsch 	char 	*ofmt = fmt;
29b2ea75c1Sfgsch 
30b2ea75c1Sfgsch 	va_start(ap, fmt);
31b2ea75c1Sfgsch 	for (; *fmt; fmt++)
32b2ea75c1Sfgsch 	    switch ((ch =*fmt)) {
33b2ea75c1Sfgsch 	    case 'i':
34b2ea75c1Sfgsch 		i = va_arg(ap, int);
35b2ea75c1Sfgsch 		EQ(i, 1234);
36b2ea75c1Sfgsch 		break;
37b2ea75c1Sfgsch 	    case 'c':
38a8d37202Sfgsch 		c = va_arg(ap, int);
39b2ea75c1Sfgsch 		EQ(c, 'x');
40b2ea75c1Sfgsch 		break;
41b2ea75c1Sfgsch 	    case 'l':
42b2ea75c1Sfgsch 		l = va_arg(ap, long);
43b2ea75c1Sfgsch 		EQ(l, 123456789L);
44b2ea75c1Sfgsch 		break;
45b2ea75c1Sfgsch 	    case 'p':
46b2ea75c1Sfgsch 		p = va_arg(ap, void *);
47b2ea75c1Sfgsch 		EQ(p, &thing);
48b2ea75c1Sfgsch 		break;
49b2ea75c1Sfgsch 	    default:
50f3008457Sfgsch 		fprintf(stderr,
51f3008457Sfgsch 		    "unexpected character 0x%02x `%c' in %s(%p) at %p\n",
52b2ea75c1Sfgsch 		    ch, ch, ofmt, ofmt, fmt);
53b2ea75c1Sfgsch 		ASSERT(0);
54b2ea75c1Sfgsch 	    }
55b2ea75c1Sfgsch 	va_end(ap);
56b2ea75c1Sfgsch 	return 9;
57b2ea75c1Sfgsch }
58b2ea75c1Sfgsch 
59*db3296cfSderaadt static void *
run_test(void * arg)60*db3296cfSderaadt run_test(void *arg)
61b2ea75c1Sfgsch {
62b2ea75c1Sfgsch 	char *msg = (char *)arg;
63b2ea75c1Sfgsch 	int i;
64b2ea75c1Sfgsch 
65b2ea75c1Sfgsch 	SET_NAME(msg);
66b2ea75c1Sfgsch 
67b2ea75c1Sfgsch 	puts(msg);
68b2ea75c1Sfgsch 	for (i = 0; i < 1000000; i++) {
69b2ea75c1Sfgsch 		ASSERT(test1("iclp", 1234, 'x', 123456789L, &thing) == 9);
70b2ea75c1Sfgsch 	}
71b2ea75c1Sfgsch 	printf("ok\n");
72b2ea75c1Sfgsch 	return NULL;
73b2ea75c1Sfgsch }
74b2ea75c1Sfgsch 
75b2ea75c1Sfgsch int
main(int argc,char * argv[])76*db3296cfSderaadt main(int argc, char *argv[])
77b2ea75c1Sfgsch {
78b2ea75c1Sfgsch 	pthread_t t1, t2;
79b2ea75c1Sfgsch 
80b2ea75c1Sfgsch 	printf("trying loop in single-threaded mode:\n");
81b2ea75c1Sfgsch 	run_test("main");
82b2ea75c1Sfgsch 	printf("now running loop with 2 threads:\n");
83b2ea75c1Sfgsch 	CHECKr(pthread_create(&t1, NULL, run_test, "child 1"));
84b2ea75c1Sfgsch 	CHECKr(pthread_create(&t2, NULL, run_test, "child 2"));
85b2ea75c1Sfgsch 	CHECKr(pthread_join(t1, NULL));
86b2ea75c1Sfgsch 	CHECKr(pthread_join(t2, NULL));
87b2ea75c1Sfgsch 	SUCCEED;
88b2ea75c1Sfgsch }
89