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