test_setjmp.c revision 479d9725ab0c5bc7d516e22e84c13d445bfd3c7c
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *  * Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 *  * Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in
12 *    the documentation and/or other materials provided with the
13 *    distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29/* Basic test of setjmp() functionality */
30
31#include <setjmp.h>
32#include <stdio.h>
33#include <stdlib.h>
34
35#define INT_VALUE1   0x12345678
36#define INT_VALUE2   0xfedcba98
37
38#define FLOAT_VALUE1   (1.2345678)
39#define FLOAT_VALUE2   (9.8765432)
40
41int     dummy_int;
42double  dummy_double;
43
44/* test that integer registers are restored properly */
45static void
46test_int(void)
47{
48    jmp_buf  jumper;
49    register int xx = INT_VALUE1;
50
51    if (setjmp(jumper) == 0) {
52        xx = INT_VALUE2;
53        longjmp(jumper, 1);
54    } else {
55        if (xx != INT_VALUE1) {
56            fprintf(stderr, "setjmp() is broken for integer registers !\n");
57            exit(1);
58        }
59    }
60    dummy_int = xx;
61}
62
63static void
64test_float(void)
65{
66    jmp_buf  jumper;
67    register double xx = FLOAT_VALUE1;
68
69    if (setjmp(jumper) == 0) {
70        xx = FLOAT_VALUE2;
71        longjmp(jumper, 1);
72    } else {
73        if (xx != FLOAT_VALUE1) {
74            fprintf(stderr, "setjmp() is broken for floating point registers !\n");
75            exit(1);
76        }
77    }
78    dummy_double = xx;
79}
80
81int main(void)
82{
83    test_int();
84    test_float();
85    return 0;
86}
87