Pick three bugfixes from next branch to trunk for inclusion in 4.5.0 RC2, as discusse...
[sdcc.git] / sdcc / support / regression / tests / setjmp.c
blob4b9e95a3fa295ee3f3f8761972df747fce1789af
1 /** setjmp/longjmp tests (focusing on getting the jump right).
2 */
3 #include <testfwk.h>
5 #if !defined(__SDCC_pic14) // Unimplemented setjmp
6 #include <setjmp.h>
8 unsigned int global_int = 0;
9 unsigned int *gpInt;
11 #if defined(__SDCC_mcs51)
12 #include <8052.h>
14 void
15 T2_isr (void) __interrupt (5) //no using
17 //do not clear flag TF2 so it keeps interrupting !
18 (*gpInt)++;
20 #endif
22 void
23 try_fun (jmp_buf catch, int except)
25 longjmp (catch, except);
28 jmp_buf buf;
30 void g(void)
32 longjmp(buf, 0); // When called with an argument of 0, longjmp() makes setjmp() return 1 instead.
33 g();
36 void f1(void)
38 static int i;
39 int j;
40 i= 0;
41 j= setjmp(buf);
42 ASSERT(i == j);
43 i++;
44 if(!j)
45 g();
48 #endif
50 // Get FreeBSD version to skip part of test for known broken setjmp (FreeBSD bug #255320, affecting at least FreeBSD 13.0 and FreeBSD 13.1).
51 #ifdef __FreeBSD__
52 #include <sys/param.h>
53 #endif
55 void
56 testJmp (void)
58 #if !defined(__SDCC_pic14) // Unimplemented setjmp
59 jmp_buf catch;
60 int exception;
62 #if defined(__SDCC_mcs51)
63 gpInt = &global_int;
64 //enable the interrupt and set it's flag to generate some heavy stack usage
65 ET2 = 1;
66 EA = 1;
67 TF2 = 1;
68 #endif
70 exception = setjmp (catch);
71 if (exception == 0)
73 try_fun (catch, 1);
74 //should not get here!
75 ASSERT (0);
77 ASSERT (exception == 1);
79 #if !defined(__FreeBSD__) || __FreeBSD_version >= 1302000 // Known FreeBSD 13.0 and 13.1 bug #255320.
80 f1();
81 #endif
82 #endif
84 // C99 might require setjmp to be a macro. The standard seems self-contradicting on this issue.
85 //#ifndef setjmp
86 // ASSERT(0);
87 //#endif