forked from pubgrub-rs/pubgrub
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexamples.rs
More file actions
253 lines (224 loc) · 8.84 KB
/
examples.rs
File metadata and controls
253 lines (224 loc) · 8.84 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// SPDX-License-Identifier: MPL-2.0
use pubgrub::{
resolve, DefaultStringReporter, Map, OfflineDependencyProvider, PubGrubError, Range,
Reporter as _, SemanticVersion, Set,
};
type NumVS = Range<u32>;
type SemVS = Range<SemanticVersion>;
use std::io::Write;
use log::LevelFilter;
fn init_log() {
let _ = env_logger::builder()
.filter_level(LevelFilter::Trace)
.format(|buf, record| writeln!(buf, "{}", record.args()))
.is_test(true)
.try_init();
}
#[test]
/// https://github.com/dart-lang/pub/blob/master/doc/solver.md#no-conflicts
fn no_conflict() {
init_log();
let mut dependency_provider = OfflineDependencyProvider::<&str, SemVS>::new();
#[rustfmt::skip]
dependency_provider.add_dependencies(
"root", (1, 0, 0),
[("foo", Range::between((1, 0, 0), (2, 0, 0)))],
);
#[rustfmt::skip]
dependency_provider.add_dependencies(
"foo", (1, 0, 0),
[("bar", Range::between((1, 0, 0), (2, 0, 0)))],
);
dependency_provider.add_dependencies("bar", (1, 0, 0), []);
dependency_provider.add_dependencies("bar", (2, 0, 0), []);
// Run the algorithm.
let computed_solution = resolve(&dependency_provider, "root", (1, 0, 0)).unwrap();
// Solution.
let mut expected_solution = Map::default();
expected_solution.insert("root", (1, 0, 0).into());
expected_solution.insert("foo", (1, 0, 0).into());
expected_solution.insert("bar", (1, 0, 0).into());
// Comparing the true solution with the one computed by the algorithm.
assert_eq!(expected_solution, computed_solution);
}
#[test]
/// https://github.com/dart-lang/pub/blob/master/doc/solver.md#avoiding-conflict-during-decision-making
fn avoiding_conflict_during_decision_making() {
init_log();
let mut dependency_provider = OfflineDependencyProvider::<&str, SemVS>::new();
#[rustfmt::skip]
dependency_provider.add_dependencies(
"root", (1, 0, 0),
[
("foo", Range::between((1, 0, 0), (2, 0, 0))),
("bar", Range::between((1, 0, 0), (2, 0, 0))),
],
);
#[rustfmt::skip]
dependency_provider.add_dependencies(
"foo", (1, 1, 0),
[("bar", Range::between((2, 0, 0), (3, 0, 0)))],
);
dependency_provider.add_dependencies("foo", (1, 0, 0), []);
dependency_provider.add_dependencies("bar", (1, 0, 0), []);
dependency_provider.add_dependencies("bar", (1, 1, 0), []);
dependency_provider.add_dependencies("bar", (2, 0, 0), []);
// Run the algorithm.
let computed_solution = resolve(&dependency_provider, "root", (1, 0, 0)).unwrap();
// Solution.
let mut expected_solution = Map::default();
expected_solution.insert("root", (1, 0, 0).into());
expected_solution.insert("foo", (1, 0, 0).into());
expected_solution.insert("bar", (1, 1, 0).into());
// Comparing the true solution with the one computed by the algorithm.
assert_eq!(expected_solution, computed_solution);
}
#[test]
/// https://github.com/dart-lang/pub/blob/master/doc/solver.md#performing-conflict-resolution
fn conflict_resolution() {
init_log();
let mut dependency_provider = OfflineDependencyProvider::<&str, SemVS>::new();
#[rustfmt::skip]
dependency_provider.add_dependencies(
"root", (1, 0, 0),
[("foo", Range::higher_than((1, 0, 0)))],
);
#[rustfmt::skip]
dependency_provider.add_dependencies(
"foo", (2, 0, 0),
[("bar", Range::between((1, 0, 0), (2, 0, 0)))],
);
dependency_provider.add_dependencies("foo", (1, 0, 0), []);
#[rustfmt::skip]
dependency_provider.add_dependencies(
"bar", (1, 0, 0),
[("foo", Range::between((1, 0, 0), (2, 0, 0)))],
);
// Run the algorithm.
let computed_solution = resolve(&dependency_provider, "root", (1, 0, 0)).unwrap();
// Solution.
let mut expected_solution = Map::default();
expected_solution.insert("root", (1, 0, 0).into());
expected_solution.insert("foo", (1, 0, 0).into());
// Comparing the true solution with the one computed by the algorithm.
assert_eq!(expected_solution, computed_solution);
}
#[test]
/// https://github.com/dart-lang/pub/blob/master/doc/solver.md#conflict-resolution-with-a-partial-satisfier
fn conflict_with_partial_satisfier() {
init_log();
let mut dependency_provider = OfflineDependencyProvider::<&str, SemVS>::new();
#[rustfmt::skip]
// root 1.0.0 depends on foo ^1.0.0 and target ^2.0.0
dependency_provider.add_dependencies(
"root", (1, 0, 0),
[
("foo", Range::between((1, 0, 0), (2, 0, 0))),
("target", Range::between((2, 0, 0), (3, 0, 0))),
],
);
#[rustfmt::skip]
// foo 1.1.0 depends on left ^1.0.0 and right ^1.0.0
dependency_provider.add_dependencies(
"foo", (1, 1, 0),
[
("left", Range::between((1, 0, 0), (2, 0, 0))),
("right", Range::between((1, 0, 0), (2, 0, 0))),
],
);
dependency_provider.add_dependencies("foo", (1, 0, 0), []);
#[rustfmt::skip]
// left 1.0.0 depends on shared >=1.0.0
dependency_provider.add_dependencies(
"left", (1, 0, 0),
[("shared", Range::higher_than((1, 0, 0)))],
);
#[rustfmt::skip]
// right 1.0.0 depends on shared <2.0.0
dependency_provider.add_dependencies(
"right", (1, 0, 0),
[("shared", Range::strictly_lower_than((2, 0, 0)))],
);
dependency_provider.add_dependencies("shared", (2, 0, 0), []);
#[rustfmt::skip]
// shared 1.0.0 depends on target ^1.0.0
dependency_provider.add_dependencies(
"shared", (1, 0, 0),
[("target", Range::between((1, 0, 0), (2, 0, 0)))],
);
dependency_provider.add_dependencies("target", (2, 0, 0), []);
dependency_provider.add_dependencies("target", (1, 0, 0), []);
// Run the algorithm.
let computed_solution = resolve(&dependency_provider, "root", (1, 0, 0)).unwrap();
// Solution.
let mut expected_solution = Map::default();
expected_solution.insert("root", (1, 0, 0).into());
expected_solution.insert("foo", (1, 0, 0).into());
expected_solution.insert("target", (2, 0, 0).into());
// Comparing the true solution with the one computed by the algorithm.
assert_eq!(expected_solution, computed_solution);
}
#[test]
/// a0 dep on b and c
/// b0 dep on d0
/// b1 dep on d1 (not existing)
/// c0 has no dep
/// c1 dep on d2 (not existing)
/// d0 has no dep
///
/// Solution: a0, b0, c0, d0
fn double_choices() {
init_log();
let mut dependency_provider = OfflineDependencyProvider::<&str, NumVS>::new();
dependency_provider.add_dependencies("a", 0u32, [("b", Range::full()), ("c", Range::full())]);
dependency_provider.add_dependencies("b", 0u32, [("d", Range::singleton(0u32))]);
dependency_provider.add_dependencies("b", 1u32, [("d", Range::singleton(1u32))]);
dependency_provider.add_dependencies("c", 0u32, []);
dependency_provider.add_dependencies("c", 1u32, [("d", Range::singleton(2u32))]);
dependency_provider.add_dependencies("d", 0u32, []);
// Solution.
let mut expected_solution = Map::default();
expected_solution.insert("a", 0u32);
expected_solution.insert("b", 0u32);
expected_solution.insert("c", 0u32);
expected_solution.insert("d", 0u32);
// Run the algorithm.
let computed_solution = resolve(&dependency_provider, "a", 0u32).unwrap();
assert_eq!(expected_solution, computed_solution);
}
#[test]
fn confusing_with_lots_of_holes() {
let mut dependency_provider = OfflineDependencyProvider::<&str, NumVS>::new();
// root depends on foo...
dependency_provider.add_dependencies(
"root",
1u32,
vec![("foo", Range::full()), ("baz", Range::full())],
);
for i in 1..6 {
// foo depends on bar...
dependency_provider.add_dependencies("foo", i as u32, vec![("bar", Range::full())]);
}
// This package is part of the dependency tree, but it's not part of the conflict
dependency_provider.add_dependencies("baz", 1u32, vec![]);
let Err(PubGrubError::NoSolution(mut derivation_tree)) =
resolve(&dependency_provider, "root", 1u32)
else {
unreachable!()
};
assert_eq!(
&DefaultStringReporter::report(&derivation_tree),
r#"Because there is no available version for bar and foo ==1 | ==2 | ==3 | ==4 | ==5 depends on bar, foo ==1 | ==2 | ==3 | ==4 | ==5 is forbidden.
And because there is no version of foo in <1 | >1, <2 | >2, <3 | >3, <4 | >4, <5 | >5 and root ==1 depends on foo, root ==1 is forbidden."#
);
derivation_tree.collapse_no_versions();
assert_eq!(
&DefaultStringReporter::report(&derivation_tree),
"Because foo depends on bar and root ==1 depends on foo, root ==1 is forbidden."
);
assert_eq!(
derivation_tree.packages(),
// baz isn't shown.
Set::from_iter(&["root", "foo", "bar"])
);
}