summaryrefslogtreecommitdiffstats
path: root/test/self/progress-notifier.c
blob: af65b0900e42cbf870072d34e0d1bcbe07706c1f (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
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
// SPDX-License-Identifier: GPL-2.0-only

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <common.h>
#include <bselftest.h>
#include <progress.h>

BSELFTEST_GLOBALS();

static void __ok(bool cond, const char *func, int line)
{
	total_tests++;
	if (!cond) {
		failed_tests++;
		printf("%s:%d: assertion failure\n", func, line);
	}
}

#define ok(cond) \
	__ok(cond, __func__, __LINE__)

static unsigned long stage;
static const void *prefix;
static int counter;

static int dummy_notifier(struct notifier_block *r, unsigned long _stage, void *_prefix)
{
       prefix = _prefix;
       stage = _stage;
       counter++;
       return 0;
}

static struct notifier_block dummy_nb =  {
       .notifier_call = dummy_notifier
};

static void test_dummy_notifier(void)
{
	const char *arg = "ARGUMENT";
	int local_counter = 0;

	stage = 0;
	prefix = NULL;
	counter = 0;

	progress_register_client(&dummy_nb);
	ok(stage == 0);
	ok(prefix == NULL);
	ok(counter == local_counter);
	progress_notifier_call_chain(1, arg);

	if (IS_ENABLED(CONFIG_PROGRESS_NOTIFIER)) {
		ok(stage == 1);
		ok(prefix == arg);
		ok(counter == ++local_counter);
		progress_notifier_call_chain(0, NULL);
		local_counter++;
	} else {
		total_tests += 2;
		skipped_tests += 2;
	}

	ok(stage == 0);
	ok(prefix == NULL || *(const char *)prefix == '\0');
	ok(counter == local_counter);
	progress_unregister_client(&dummy_nb);

	ok(stage == 0);
	ok(prefix == NULL || *(const char *)prefix == '\0');
	ok(counter == local_counter);
}

static void test_notifier(void)
{
	test_dummy_notifier();
}
bselftest(core, test_notifier);