summaryrefslogtreecommitdiff
path: root/tests/tests2/55_lshift_type.c
blob: aa3e51a1681a3bc132210cc6493d83fea7c6d73d (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
/* $Id: lshift-type.c 53089 2012-07-06 11:18:26Z vinc17/ypig $

Tests on left-shift type, written by Vincent Lefevre <vincent@vinc17.net>.

ISO C99 TC3 says: [6.5.7#3] "The integer promotions are performed on
each of the operands. The type of the result is that of the promoted
left operand."
*/

#include <stdio.h>

#define PTYPE(M) ((M) < 0 || -(M) < 0 ? -1 : 1) * (int) sizeof((M)+0)
#define CHECK(X,T) check(#X, PTYPE(X), PTYPE((X) << (T) 1))
#define TEST1(X,T) do { CHECK(X,T); CHECK(X,unsigned T); } while (0)
#define TEST2(X)                 \
  do                             \
    {                            \
      TEST1((X),short);          \
      TEST1((X),int);            \
      TEST1((X),long);           \
      TEST1((X),long long);      \
    }                            \
  while (0)
#define TEST3(X,T) do { TEST2((T)(X)); TEST2((unsigned T)(X)); } while (0)
#define TEST4(X)                 \
  do                             \
    {                            \
      TEST3((X),short);          \
      TEST3((X),int);            \
      TEST3((X),long);           \
      TEST3((X),long long);      \
    }                            \
 while (0)

static int debug, nfailed = 0;

static void check (const char *s, int arg1, int shift)
{
  int failed = arg1 != shift;
  if (debug || failed)
    printf ("%s %d %d\n", s, arg1, shift);
  nfailed += failed;
}

int main (int argc, char **argv)
{
  debug = argc > 1;
  TEST4(1);
  TEST4(-1);
  printf ("%d test(s) failed\n", nfailed);
  return nfailed != 0;
}