1//
2//                     The LLVM Compiler Infrastructure
3//
4// This file is distributed under the University of Illinois Open Source
5// License. See LICENSE.TXT for details.
6
7/*
8 *  variadic.c
9 *  testObjects
10 *
11 *  Created by Blaine Garst on 2/17/09.
12 *
13 */
14
15// PURPOSE Test that variadic arguments compile and work for Blocks
16// CONFIG
17
18#include <stdarg.h>
19#include <stdio.h>
20
21int main(int argc, char *argv[]) {
22
23    long (^addthem)(const char *, ...) = ^long (const char *format, ...){
24        va_list argp;
25        const char *p;
26        int i;
27        char c;
28        double d;
29        long result = 0;
30        va_start(argp, format);
31        //printf("starting...\n");
32        for (p = format; *p; p++) switch (*p) {
33            case 'i':
34                i = va_arg(argp, int);
35                //printf("i: %d\n", i);
36                result += i;
37                break;
38            case 'd':
39                d = va_arg(argp, double);
40                //printf("d: %g\n", d);
41                result += (int)d;
42                break;
43            case 'c':
44                c = va_arg(argp, int);
45                //printf("c: '%c'\n", c);
46                result += c;
47                break;
48        }
49        //printf("...done\n\n");
50        return result;
51    };
52    long testresult = addthem("ii", 10, 20);
53    if (testresult != 30) {
54        printf("got wrong result: %ld\n", testresult);
55        return 1;
56    }
57    testresult = addthem("idc", 30, 40.0, 'a');
58    if (testresult != (70+'a')) {
59        printf("got different wrong result: %ld\n", testresult);
60        return 1;
61    }
62    printf("%s: Success\n", argv[0]);
63    return 0;
64}
65
66
67