File: solve_loop.c

package info (click to toggle)
openrc 0.63-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,412 kB
  • sloc: ansic: 16,255; sh: 2,225; perl: 28; makefile: 26
file content (175 lines) | stat: -rw-r--r-- 4,561 bytes parent folder | download | duplicates (3)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* unit test for the code of early loop detector (2014-01-27)
* Author: Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
*/

#include "../../src/librc/librc-depend.c"

int main() {
	service_id_t **unap_matrix[UNAPM_MAX], **unap_matrix_result[UNAPM_MAX];
	service_id_t service_id;
	unapm_type_t unapm_type, type;
	int useneedafter_count = 9;
	int loopsolver_counter, loopfound, errfound;
	char *service_id2str[] = {
		[1] = "hwclock",
		[2] = "mountall",
		[3] = "mountall-bootclean",
		[4] = "checkroot",
	};
	char *deptype_id2str[] = {
		[UNAPM_USE]	= "use",
		[UNAPM_AFTER]	= "after",
		[UNAPM_NEED]	= "need",
	};

	unapm_type = 0;
	while (unapm_type < UNAPM_MAX) {
		unap_matrix[unapm_type]        = xcalloc((useneedafter_count+1), sizeof(*unap_matrix));
		unap_matrix_result[unapm_type] = xcalloc((useneedafter_count+1), sizeof(*unap_matrix));
		service_id = 0;
		while(service_id < (useneedafter_count+1)) {
			unap_matrix       [unapm_type][service_id] = xcalloc((useneedafter_count+1), sizeof(**unap_matrix));
			unap_matrix_result[unapm_type][service_id] = xcalloc((useneedafter_count+1), sizeof(**unap_matrix));
			service_id++;
		}
		unapm_type++;
	}

	/* hwclock */
	unap_matrix[UNAPM_USE  ][1][0] = 2;	/* the count */
	unap_matrix[UNAPM_USE  ][1][1] = 2;	/* "mountall" */
	unap_matrix[UNAPM_USE  ][1][2] = 3;	/* "mountall-bootclean" */

	/* mountall */
	unap_matrix[UNAPM_NEED ][2][0] = 1;	/* the count */
	unap_matrix[UNAPM_NEED ][2][1] = 4;	/* "checkroot" */

	/* mountall-bootclean */
	unap_matrix[UNAPM_NEED ][3][0] = 1;	/* the count */
	unap_matrix[UNAPM_NEED ][3][1] = 2;	/* "mountall" */

	/* checkroot */
	unap_matrix[UNAPM_AFTER][4][0] = 1;	/* the count */
	unap_matrix[UNAPM_AFTER][4][1] = 1;	/* "hwclock" */

	unap_matrix[0][0][0] = 5; /* number of services+1 */

	/* expected result */

	unap_matrix_result[UNAPM_USE  ][1][0] = 0;

	unap_matrix_result[UNAPM_NEED ][2][0] = 1;
	unap_matrix_result[UNAPM_NEED ][2][1] = 4;

	unap_matrix_result[UNAPM_NEED ][3][0] = 1;
	unap_matrix_result[UNAPM_NEED ][3][1] = 2;

	unap_matrix_result[UNAPM_AFTER][4][0] = 1;
	unap_matrix_result[UNAPM_AFTER][4][1] = 1;

	/* solving */

	loopsolver_counter = 0;
	do {
		loopfound = 0;

		unapm_prepare_mixed(unap_matrix, useneedafter_count);

		service_id=1;
		while ((service_id < (useneedafter_count+1)) && !loopfound) {
			int dep_num, dep_count;

			dep_num = 0;
			dep_count = unap_matrix[UNAPM_MIXED_EXPANDED][service_id][0];
			while (dep_num < dep_count) {
				dep_num++;
				if (unap_matrix[UNAPM_MIXED_EXPANDED][service_id][dep_num] == service_id) {
					loopfound = solve_loop(unap_matrix, service_id, NULL, dep_num, RCDTFLAGS_LOOPSOLVER);
					loopsolver_counter++;
					break;
				}
			}

			service_id++;
		}
	} while (loopfound == LOOP_SOLVABLE && loopsolver_counter < LOOPSOLVER_LIMIT);
	/* don't free memory, it's just a unittest */

	/* checking the result */


	errfound = 0;
	type = 0;
	while (type <= UNAPM_AFTER) {
		service_id = 1;
		while (service_id < (useneedafter_count+1)) {
			int dep_count, dep_num;

			dep_num   = 0;
			dep_count = unap_matrix_result[type][service_id][0];

			while (dep_num++ < dep_count) {
				int dep_count2, dep_num2, found;
				found = 0;

				dep_num2   = 0;
				dep_count2 = unap_matrix[type][service_id][0];

				while (dep_num2++ < dep_count2)
					if (
						unap_matrix_result[type][service_id][dep_num]
						  ==
						unap_matrix       [type][service_id][dep_num2]
					) {
						found = 1;
						break;
					}

				if (!found) {
					printf("unit-test: early loop detector: error: There's no \"%s\"\tdependency: %s -> %s\n",
							deptype_id2str[type],
							service_id2str[service_id],
							service_id2str[unap_matrix_result[type][service_id][dep_num]]
						);
					errfound = 1;
				}
			}

			dep_num   = 0;
			dep_count = unap_matrix[type][service_id][0];

			while (dep_num++ < dep_count) {
				int dep_count2, dep_num2, found;
				found = 0;

				dep_num2   = 0;
				dep_count2 = unap_matrix_result[type][service_id][0];

				while (dep_num2++ < dep_count2)
					if (
						unap_matrix_result[type][service_id][dep_num2]
						  ==
						unap_matrix       [type][service_id][dep_num]
					) {
						found = 1;
						break;
					}

				if (!found) {
					printf("unit-test: early loop detector: error: There's extra \"%s\"\tdependency found: %s -> %s\n",
							deptype_id2str[type],
							service_id2str[service_id],
							service_id2str[unap_matrix[type][service_id][dep_num]]
						);
					errfound = 1;
				}
			}

			service_id++;
		}
		type++;
	}

	return errfound != 0;
}