summaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.c-torture/execute/builtins/sprintf.c
blob: b45926469f693d9070aa443ab9175c314ce61e75 (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
/* Copyright (C) 2003  Free Software Foundation.

   Test sprintf optimizations don't break anything and return the
   correct results.

   Written by Roger Sayle, June 22, 2003.  */

static char buffer[32];

extern void abort ();
typedef __SIZE_TYPE__ size_t;
extern int sprintf(char*, const char*, ...);
extern void *memset(void*, int, size_t);
extern int memcmp(const void*, const void*, size_t);

void test1()
{
  sprintf(buffer,"foo");
}

int test2()
{
  return sprintf(buffer,"foo");
}

void test3()
{
  sprintf(buffer,"%s","bar");
}

int test4()
{
  return sprintf(buffer,"%s","bar");
}

void test5(char *ptr)
{
  sprintf(buffer,"%s",ptr);
}


void
main_test (void)
{
  memset (buffer, 'A', 32);
  test1 ();
  if (memcmp(buffer, "foo", 4) || buffer[4] != 'A')
    abort ();

  memset (buffer, 'A', 32);
  if (test2 () != 3)
    abort ();
  if (memcmp(buffer, "foo", 4) || buffer[4] != 'A')
    abort ();

  memset (buffer, 'A', 32);
  test3 ();
  if (memcmp(buffer, "bar", 4) || buffer[4] != 'A')
    abort ();

  memset (buffer, 'A', 32);
  if (test4 () != 3)
    abort ();
  if (memcmp(buffer, "bar", 4) || buffer[4] != 'A')
    abort ();

  memset (buffer, 'A', 32);
  test5 ("barf");
  if (memcmp(buffer, "barf", 5) || buffer[5] != 'A')
    abort ();
}