aboutsummaryrefslogtreecommitdiff
path: root/lib/mlibc/tests/ansi/longjmp.c
blob: 9f9c1ed414c8f7d9978abff2c6ad79e4105a1d34 (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
#include <stdio.h>
#include <setjmp.h>
#include <stdnoreturn.h>
#include <assert.h>

jmp_buf buf;

noreturn void do_jump(int arg) {
	longjmp(buf, 2 * arg);
}

int main(void) {
	volatile int times_called = 0;

	if (setjmp(buf) != 8) {
		do_jump(++times_called);
	}

	assert(times_called == 4);

	times_called = 0;
	int ret = setjmp(buf);
	assert(ret == times_called);

	if (!ret) {
		times_called = 1;
		longjmp(buf, 0);
	}
}