summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2012-11-10 19:08:12 -0500
committerRich Felker <dalias@aerifal.cx>2012-11-10 19:08:12 -0500
commit10bd92db00eb26f6f29b91717f4bf8d0054ba071 (patch)
treea4a3881bd2a30eb6e1a79853e968dc5b5f4e1685
parentd204183f6fc492c15741e5486d8840b406ae89be (diff)
downloadlibc-testsuite-10bd92db00eb26f6f29b91717f4bf8d0054ba071.tar.gz
add setjmp test
this will be useful for testing libc ports to new targets where the asm could be buggy/broken.
-rw-r--r--setjmp.c54
-rw-r--r--testsuite.c1
2 files changed, 55 insertions, 0 deletions
diff --git a/setjmp.c b/setjmp.c
new file mode 100644
index 0000000..6728bef
--- /dev/null
+++ b/setjmp.c
@@ -0,0 +1,54 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+#include <signal.h>
+#include <setjmp.h>
+
+#define TEST2(c, l, ...) ((c) ? 1 : \
+(err++,printf(__FILE__":"#l": "#c" failed: " __VA_ARGS__),putchar('\n'),0))
+#define TEST1(c, l, ...) TEST2(c, l, __VA_ARGS__)
+#define TEST(c, ...) TEST1(c, __LINE__, __VA_ARGS__)
+
+#define TESTE(c) TEST(c, "errno = %s", strerror(errno))
+
+int test_setjmp(void)
+{
+ volatile int err = 0;
+ volatile int x = 0, r;
+ jmp_buf jb;
+ sigjmp_buf sjb;
+ volatile sigset_t oldset;
+ sigset_t set;
+
+ if (!setjmp(jb)) {
+ x = 1;
+ longjmp(jb, 1);
+ }
+ TEST(x==1, "setjmp/longjmp seems to have been bypassed");
+
+ x = 0;
+ r = setjmp(jb);
+ if (!x) {
+ x = 1;
+ longjmp(jb, 0);
+ }
+ TEST(r==1, "longjmp(jb, 0) caused setjmp to return %d", r);
+
+ sigemptyset(&set);
+ sigaddset(&set, SIGUSR1);
+ sigprocmask(SIG_UNBLOCK, &set, &set);
+ oldset = set;
+
+ if (!sigsetjmp(sjb, 1)) {
+ sigemptyset(&set);
+ sigaddset(&set, SIGUSR1);
+ sigprocmask(SIG_BLOCK, &set, 0);
+ siglongjmp(sjb, 1);
+ }
+ set = oldset;
+ sigprocmask(SIG_SETMASK, &set, &set);
+ TEST(sigismember(&set, SIGUSR1)==0, "siglongjmp failed to restore mask");
+
+ return err;
+}
diff --git a/testsuite.c b/testsuite.c
index 82b2e3e..58944a7 100644
--- a/testsuite.c
+++ b/testsuite.c
@@ -35,6 +35,7 @@ int main()
RUN_TEST(dirname);
RUN_TEST(memstream);
RUN_TEST(mbc);
+ RUN_TEST(setjmp);
RUN_TEST(sem);
RUN_TEST(pthread);
/* env is last because it will break subsequent tests */