Imported Upstream version 2.9.0
[debian/cc1111] / support / regression / tests / bug-136564.c
1 /*
2    bug-136564.c
3
4    loop induction
5 */
6
7 #include <testfwk.h>
8
9 volatile unsigned char p;
10 unsigned char i_result[3];
11
12 void
13 foo (void)
14 {
15   unsigned char i;
16   unsigned char u;
17   static unsigned char c = '?';
18
19   u = 0;
20   while (1)
21     {
22       i = 0;
23       switch (u)
24         {
25           case 0:
26             /* this BBlock _case_0_0:
27                - changes i
28                - jumps to the successor "_swBrk_0" */
29             p = 0;
30             i = 42; /* fixed: Absent in "main.asm" */
31             break;
32           case 1:
33            /* the while loop:
34                - inducts i
35                - has the exit block "_swBrk_0"
36                sdcc inserts a new BBlock "loopExitLbl" before "_swBrk_0".
37                "loopExitLbl" becomes the new successor for the exit blocks of the while loop.
38                In "loopExitLbl" i can be restored without interference from
39                "_swBrk_0". */
40             while (c != 'x' && i < 9 )
41               i++;
42             break;
43           default:
44             p = 2;
45             i = 24; /* fixed: Absent in "main.asm" */
46         }
47       p = i;
48       i_result[u] = i;
49       if (u >= 2)
50         return;
51       u++;
52     }
53 }
54
55 int _strncmp (
56   char * first,
57   char * last,
58   unsigned count
59   )
60 {
61   while (--count && *first && *first == *last) {
62     first++;
63     last++;
64   }
65
66   return( *first - *last );
67 }
68
69 void
70 testInducion(void)
71 {
72   foo();
73   ASSERT(i_result[0] == 42);
74   ASSERT(i_result[1] ==  9);
75   ASSERT(i_result[2] == 24);
76
77   ASSERT(_strncmp ("SDCC is great", "SDCC is buggy", sizeof("SDCC is" )) == 0);
78   ASSERT(_strncmp ("SDCC is great", "SDCC is buggy", sizeof("SDCC is ")) != 0);
79 }