brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.6 KiB · e2e273a Raw
66 lines · c
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-exception5 6/*7 *  variadic.c8 *  testObjects9 *10 *  Created by Blaine Garst on 2/17/09.11 *12 */13 14// PURPOSE Test that variadic arguments compile and work for Blocks15// CONFIG16 17#include <stdarg.h>18#include <stdio.h>19 20int 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