File: testlog.c

package info (click to toggle)
mmlib 1.4.2-2.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,716 kB
  • sloc: ansic: 18,071; makefile: 431; sh: 135; python: 63
file content (109 lines) | stat: -rw-r--r-- 1,670 bytes parent folder | download
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
   @mindmaze_header@
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdlib.h>
#include <mmlog.h>
#include <setjmp.h>
#include <signal.h>

static
void logged_func(void)
{
	mm_log_warn("logged_func");
}

#undef MM_LOG_MODULE_NAME
#define MM_LOG_MODULE_NAME "testlog"

// Stolen from here:
// https://stackoverflow.com/questions/8934879/how-to-handle-sigabrt-signal
jmp_buf env;

static
void on_sigabrt (int signum)
{
	(void)signum;
	longjmp (env, 1);
}

// Returns 1 if the function does not abort
static
int try_and_catch_abort (void (*func)(void))
{
	if (setjmp (env) == 0) {
		signal(SIGABRT, &on_sigabrt);
		(*func)();
		return 1;
	} else {
		return 0;
	}
}

static
void provoke_a_crash(void)
{
	mm_crash("We should crash");
}

static
void do_not_provoke_a_crash(void)
{
	mm_log_info("This should not crash");
}

static
int test_crash(void)
{
	return !try_and_catch_abort(&provoke_a_crash)
		&& try_and_catch_abort(&do_not_provoke_a_crash);
}

static
void provoke_a_check_failure(void)
{
	mm_check(119 == 7*16);
}

static
void do_not_provoke_a_check_failure(void)
{
	mm_check(119 == 7*17);
}

static
int test_check(void)
{
	return !try_and_catch_abort(&provoke_a_check_failure)
		&& try_and_catch_abort(&do_not_provoke_a_check_failure);
}

static
int test_basic_logging(void)
{
	int i;

	mm_log_info("Starting logging...");

	for (i = -5; i<5; i++) {
		logged_func();
		if (i != 0)
			mm_log_warn("Everything is fine (%i)", i);
		else
			mm_log_error("Null value (i == %i)", i);
	}

	mm_log_info("Stop logging.");
	return 1;
}

int main(void)
{
	return (test_basic_logging()
					&& test_crash()
					&& test_check())?
		EXIT_SUCCESS : EXIT_FAILURE;
}