summaryrefslogtreecommitdiffstats
path: root/gcc/testsuite/obj-c++.dg/va-meth-1.mm
blob: 6d24b7f8d5b54ddff2c22dee16ea2a6238d6b7cd (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
/* Based on objc/execute/va_method.m, by Nicola Pero */

/* { dg-do run } */

#include <objc/Object.h>
#include <stdarg.h>
#include <stdlib.h>

/* Test methods with "C-style" trailing arguments, with or without ellipsis. */

@interface MathClass: Object
/* sum positive numbers; -1 ends the list */
+ (int) sum: (int) firstNumber, int secondNumber, ...;
+ (int) prod: (int) firstNumber, int secondNumber, int thirdNumber;
+ (int) minimum: (int) firstNumber, ...;
@end

extern "C" int some_func(id self, SEL _cmd, int firstN, int secondN, int thirdN, ...) {
  return firstN + secondN + thirdN;
}

@implementation MathClass
+ (int) sum: (int) firstNumber, int secondNumber, ...
{
  va_list ap;
  int sum = 0, number = 0;

  va_start (ap, secondNumber);
  number = firstNumber + secondNumber;

  while (number >= 0)
    {
      sum += number;
      number = va_arg (ap, int);
    }
  
  va_end (ap);

  return sum;
}
+ (int) prod: (int) firstNumber, int secondNumber, int thirdNumber {
  return firstNumber * secondNumber * thirdNumber;
}
+ (int) minimum: (int) firstNumber, ...
{
  va_list ap;
  int minimum = 999, number = 0;
  
  va_start (ap, firstNumber);
  number = firstNumber;
  
  while (number >= 0)
    {
      minimum = (minimum < number ? minimum: number);
      number = va_arg (ap, int);
    }
  
  va_end (ap);
  
  return minimum;
}
@end

int main (void)
{
  if ([MathClass sum: 1, 2, 3, 4, 5, -1] != 15)
    abort ();
  if ([MathClass prod: 4, 5, 6] != 120)
    abort ();
  if ([MathClass minimum: 17, 9, 133, 84, 35, -1] != 9)
    abort ();
  
  return 0;
}
OpenPOWER on IntegriCloud