(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.misc-tests/
gcov-4b.c
       1  /* Check that execution counts for various C constructs are reported
       2     correctly by gcov. */
       3  
       4  /* { dg-options "-fprofile-arcs -ftest-coverage" } */
       5  /* { dg-do run { target native } } */
       6  
       7  extern void abort (void);
       8  
       9  int do_something (int i)
      10  {
      11    return i;
      12  }
      13  
      14  /* Check for loops. */
      15  
      16  int for_val1;
      17  int for_val2;
      18  int for_temp;
      19  
      20  int
      21  test_for1 (int n)
      22  {
      23    int i;
      24    for_temp = 1;
      25    for (i = 0; i < n; i++)		/* branch(25) */
      26      					/* branch(end) */
      27      for_temp++;
      28    return for_temp;
      29  }
      30  
      31  int
      32  test_for2 (int m, int n, int o)
      33  {
      34    int i, j, k;
      35    for_temp = 1;
      36    for (i = 0; i < n; i++)		/* branch(30) */
      37      					/* branch(end) */
      38      for (j = 0; j < m; j++)		/* branch(32) */
      39      					/* branch(end) */
      40        for (k = 0; k < o; k++)		/* branch(27) */
      41      					/* branch(end) */
      42  	for_temp++;
      43    return for_temp;
      44  }
      45  
      46  void
      47  call_for ()
      48  {
      49    for_val1 += test_for1 (0);
      50    for_val1 += test_for1 (2);
      51    for_val1 += test_for1 (7);
      52  
      53    for_val2 += test_for2 (0, 0, 0);
      54    for_val2 += test_for2 (1, 0, 0);
      55    for_val2 += test_for2 (1, 3, 0);
      56    for_val2 += test_for2 (1, 3, 1);
      57    for_val2 += test_for2 (3, 1, 5);
      58    for_val2 += test_for2 (3, 7, 3);
      59  }
      60  
      61  /* Check the use of goto. */
      62  
      63  int goto_val;
      64  
      65  int
      66  test_goto1 (int f)
      67  {
      68    if (f)				/* branch(50) */
      69      					/* branch(end) */
      70      goto lab1;
      71    return 1;
      72  lab1:
      73    return 2;
      74  }
      75  
      76  int
      77  test_goto2 (int f)
      78  {
      79    int i;
      80    for (i = 0; i < 10; i++)		/* branch(7) */
      81      					/* branch(end) */
      82      if (i == f) goto lab2;
      83    return 4;
      84  lab2:
      85    return 8;
      86  }
      87  
      88  void
      89  call_goto ()
      90  {
      91    goto_val += test_goto1 (0);
      92    goto_val += test_goto1 (1);
      93    goto_val += test_goto2 (3);
      94    goto_val += test_goto2 (30);
      95  }
      96  
      97  /* Check nested if-then-else statements. */
      98  
      99  int ifelse_val1;
     100  int ifelse_val2;
     101  int ifelse_val3;
     102  
     103  int
     104  test_ifelse1 (int i, int j)
     105  {
     106    int result = 0;
     107    if (i)				/* branch(40) */
     108      					/* branch(end) */
     109      if (j)				/* branch(0) */
     110      					/* branch(end) */
     111        result = do_something (4);
     112      else
     113        result = do_something (1024);
     114    else
     115      if (j)				/* branch(50) */
     116      					/* branch(end) */
     117        result = do_something (1);
     118      else
     119        result = do_something (2);
     120    if (i > j)				/* branch(80) */
     121      					/* branch(end) */
     122      result = do_something (result*2);
     123    if (i > 10)				/* branch(80) */
     124      					/* branch(end) */
     125      if (j > 10)				/* branch(100) */
     126        result = do_something (result*4);
     127    return result;
     128  }
     129  
     130  int
     131  test_ifelse2 (int i)
     132  {
     133    int result = 0;
     134    if (!i)				/* branch(83) */
     135      					/* branch(end) */
     136      result = do_something (1);
     137    if (i == 1)				/* branch(100) */
     138      					/* branch(end) */
     139      result = do_something (1024);
     140    if (i == 2)				/* branch(50) */
     141      					/* branch(end) */
     142      result = do_something (2);
     143    if (i == 3)				/* branch(67) */
     144      					/* branch(end) */
     145      return do_something (8);
     146    if (i == 4)				/* branch(100) */
     147      					/* branch(end) */
     148      return do_something (2048);
     149    return result;
     150  }
     151  
     152  int
     153  test_ifelse3 (int i, int j)
     154  {
     155    int result = 1;
     156    if (i > 10 && j > i && j < 20)	/* branch(27 50 75) */
     157      					/* branch(end) */
     158      result = do_something (16);
     159    if (i > 20)				/* branch(55) */
     160      					/* branch(end) */
     161      if (j > i)				/* branch(60) */
     162      					/* branch(end) */
     163        if (j < 30)			/* branch(50) */
     164      					/* branch(end) */
     165  	result = do_something (32);
     166    if (i == 3 || j == 47 || i == j)	/* branch(9 10 89) */
     167      					/* branch(end) */
     168      result = do_something (64);
     169    return result;
     170  }
     171  
     172  void
     173  call_ifelse ()
     174  {
     175    ifelse_val1 += test_ifelse1 (0, 2);
     176    ifelse_val1 += test_ifelse1 (0, 0);
     177    ifelse_val1 += test_ifelse1 (1, 2);
     178    ifelse_val1 += test_ifelse1 (10, 2);
     179    ifelse_val1 += test_ifelse1 (11, 11);
     180  
     181    ifelse_val2 += test_ifelse2 (0);
     182    ifelse_val2 += test_ifelse2 (2);
     183    ifelse_val2 += test_ifelse2 (2);
     184    ifelse_val2 += test_ifelse2 (2);
     185    ifelse_val2 += test_ifelse2 (3);
     186    ifelse_val2 += test_ifelse2 (3);
     187  
     188    ifelse_val3 += test_ifelse3 (11, 19);
     189    ifelse_val3 += test_ifelse3 (25, 27);
     190    ifelse_val3 += test_ifelse3 (11, 22);
     191    ifelse_val3 += test_ifelse3 (11, 10);
     192    ifelse_val3 += test_ifelse3 (21, 32);
     193    ifelse_val3 += test_ifelse3 (21, 20);
     194    ifelse_val3 += test_ifelse3 (1, 2);
     195    ifelse_val3 += test_ifelse3 (32, 31);
     196    ifelse_val3 += test_ifelse3 (3, 0);
     197    ifelse_val3 += test_ifelse3 (0, 47);
     198    ifelse_val3 += test_ifelse3 (65, 65);
     199  }
     200  
     201  /* Check switch statements. */
     202  
     203  int switch_val, switch_m;
     204  
     205  int
     206  test_switch (int i, int j)
     207  {
     208    int result = 0;
     209  
     210    switch (i)				/* branch(20 0 60 20) */
     211      					/* branch(end) */
     212      {
     213        case 1:
     214          result = do_something (2);
     215          break;
     216        case 2:
     217          result = do_something (1024);
     218          break;
     219        case 3:
     220        case 4:
     221          if (j == 2)			/* branch(67) */
     222      					/* branch(end) */
     223            return do_something (4);
     224          result = do_something (8);
     225          break;
     226        default:
     227  	result = do_something (32);
     228  	switch_m++;
     229          break;
     230      }
     231    return result;
     232  }
     233  
     234  void
     235  call_switch ()
     236  {
     237    switch_val += test_switch (1, 0);
     238    switch_val += test_switch (3, 0);
     239    switch_val += test_switch (3, 2);
     240    switch_val += test_switch (4, 0);
     241    switch_val += test_switch (16, 0);	
     242    switch_val += switch_m;
     243  }
     244  
     245  int
     246  main()
     247  {
     248    call_for ();
     249    call_goto ();
     250    call_ifelse ();
     251    call_switch ();
     252    if ((for_val1 != 12)
     253        || (for_val2 != 87)
     254        || (goto_val != 15)
     255        || (ifelse_val1 != 31)
     256        || (ifelse_val2 != 23)
     257        || (ifelse_val3 != 246)
     258        || (switch_val != 55))
     259      abort ();
     260    return 0;
     261  }
     262  
     263  /* { dg-final { run-gcov branches { -b gcov-4b.c } } } */