##// END OF EJS Templates
rust-ancestors: adjust branches and inline comments per previous change...
Yuya Nishihara -
r41170:17a19567 default
parent child Browse files
Show More
@@ -1,780 +1,775
1 1 // ancestors.rs
2 2 //
3 3 // Copyright 2018 Georges Racinet <gracinet@anybox.fr>
4 4 //
5 5 // This software may be used and distributed according to the terms of the
6 6 // GNU General Public License version 2 or any later version.
7 7
8 8 //! Rust versions of generic DAG ancestors algorithms for Mercurial
9 9
10 10 use super::{Graph, GraphError, Revision, NULL_REVISION};
11 11 use std::cmp::max;
12 12 use std::collections::{BinaryHeap, HashSet};
13 13
14 14 /// Iterator over the ancestors of a given list of revisions
15 15 /// This is a generic type, defined and implemented for any Graph, so that
16 16 /// it's easy to
17 17 ///
18 18 /// - unit test in pure Rust
19 19 /// - bind to main Mercurial code, potentially in several ways and have these
20 20 /// bindings evolve over time
21 21 pub struct AncestorsIterator<G: Graph> {
22 22 graph: G,
23 23 visit: BinaryHeap<Revision>,
24 24 seen: HashSet<Revision>,
25 25 stoprev: Revision,
26 26 }
27 27
28 28 /// Lazy ancestors set, backed by AncestorsIterator
29 29 pub struct LazyAncestors<G: Graph + Clone> {
30 30 graph: G,
31 31 containsiter: AncestorsIterator<G>,
32 32 initrevs: Vec<Revision>,
33 33 stoprev: Revision,
34 34 inclusive: bool,
35 35 }
36 36
37 37 pub struct MissingAncestors<G: Graph> {
38 38 graph: G,
39 39 bases: HashSet<Revision>,
40 40 }
41 41
42 42 impl<G: Graph> AncestorsIterator<G> {
43 43 /// Constructor.
44 44 ///
45 45 /// if `inclusive` is true, then the init revisions are emitted in
46 46 /// particular, otherwise iteration starts from their parents.
47 47 pub fn new(
48 48 graph: G,
49 49 initrevs: impl IntoIterator<Item = Revision>,
50 50 stoprev: Revision,
51 51 inclusive: bool,
52 52 ) -> Result<Self, GraphError> {
53 53 let filtered_initrevs = initrevs.into_iter().filter(|&r| r >= stoprev);
54 54 if inclusive {
55 55 let visit: BinaryHeap<Revision> = filtered_initrevs.collect();
56 56 let seen = visit.iter().map(|&x| x).collect();
57 57 return Ok(AncestorsIterator {
58 58 visit: visit,
59 59 seen: seen,
60 60 stoprev: stoprev,
61 61 graph: graph,
62 62 });
63 63 }
64 64 let mut this = AncestorsIterator {
65 65 visit: BinaryHeap::new(),
66 66 seen: HashSet::new(),
67 67 stoprev: stoprev,
68 68 graph: graph,
69 69 };
70 70 this.seen.insert(NULL_REVISION);
71 71 for rev in filtered_initrevs {
72 72 for parent in this.graph.parents(rev)?.iter().cloned() {
73 73 this.conditionally_push_rev(parent);
74 74 }
75 75 }
76 76 Ok(this)
77 77 }
78 78
79 79 #[inline]
80 80 fn conditionally_push_rev(&mut self, rev: Revision) {
81 81 if self.stoprev <= rev && !self.seen.contains(&rev) {
82 82 self.seen.insert(rev);
83 83 self.visit.push(rev);
84 84 }
85 85 }
86 86
87 87 /// Consumes partially the iterator to tell if the given target
88 88 /// revision
89 89 /// is in the ancestors it emits.
90 90 /// This is meant for iterators actually dedicated to that kind of
91 91 /// purpose
92 92 pub fn contains(&mut self, target: Revision) -> Result<bool, GraphError> {
93 93 if self.seen.contains(&target) && target != NULL_REVISION {
94 94 return Ok(true);
95 95 }
96 96 for item in self {
97 97 let rev = item?;
98 98 if rev == target {
99 99 return Ok(true);
100 100 }
101 101 if rev < target {
102 102 return Ok(false);
103 103 }
104 104 }
105 105 Ok(false)
106 106 }
107 107
108 108 pub fn peek(&self) -> Option<Revision> {
109 109 self.visit.peek().map(|&r| r)
110 110 }
111 111
112 112 /// Tell if the iterator is about an empty set
113 113 ///
114 114 /// The result does not depend whether the iterator has been consumed
115 115 /// or not.
116 116 /// This is mostly meant for iterators backing a lazy ancestors set
117 117 pub fn is_empty(&self) -> bool {
118 118 if self.visit.len() > 0 {
119 119 return false;
120 120 }
121 121 if self.seen.len() > 1 {
122 122 return false;
123 123 }
124 124 // at this point, the seen set is at most a singleton.
125 125 // If not `self.inclusive`, it's still possible that it has only
126 126 // the null revision
127 127 self.seen.is_empty() || self.seen.contains(&NULL_REVISION)
128 128 }
129 129 }
130 130
131 131 /// Main implementation for the iterator
132 132 ///
133 133 /// The algorithm is the same as in `_lazyancestorsiter()` from `ancestors.py`
134 134 /// with a few non crucial differences:
135 135 ///
136 136 /// - there's no filtering of invalid parent revisions. Actually, it should be
137 137 /// consistent and more efficient to filter them from the end caller.
138 138 /// - we don't have the optimization for adjacent revisions (i.e., the case
139 139 /// where `p1 == rev - 1`), because it amounts to update the first element of
140 140 /// the heap without sifting, which Rust's BinaryHeap doesn't let us do.
141 141 /// - we save a few pushes by comparing with `stoprev` before pushing
142 142 impl<G: Graph> Iterator for AncestorsIterator<G> {
143 143 type Item = Result<Revision, GraphError>;
144 144
145 145 fn next(&mut self) -> Option<Self::Item> {
146 146 let current = match self.visit.peek() {
147 147 None => {
148 148 return None;
149 149 }
150 150 Some(c) => *c,
151 151 };
152 152 let [p1, p2] = match self.graph.parents(current) {
153 153 Ok(ps) => ps,
154 154 Err(e) => return Some(Err(e)),
155 155 };
156 156 if p1 < self.stoprev || self.seen.contains(&p1) {
157 157 self.visit.pop();
158 158 } else {
159 159 *(self.visit.peek_mut().unwrap()) = p1;
160 160 self.seen.insert(p1);
161 161 };
162 162
163 163 self.conditionally_push_rev(p2);
164 164 Some(Ok(current))
165 165 }
166 166 }
167 167
168 168 impl<G: Graph + Clone> LazyAncestors<G> {
169 169 pub fn new(
170 170 graph: G,
171 171 initrevs: impl IntoIterator<Item = Revision>,
172 172 stoprev: Revision,
173 173 inclusive: bool,
174 174 ) -> Result<Self, GraphError> {
175 175 let v: Vec<Revision> = initrevs.into_iter().collect();
176 176 Ok(LazyAncestors {
177 177 graph: graph.clone(),
178 178 containsiter: AncestorsIterator::new(
179 179 graph,
180 180 v.iter().cloned(),
181 181 stoprev,
182 182 inclusive,
183 183 )?,
184 184 initrevs: v,
185 185 stoprev: stoprev,
186 186 inclusive: inclusive,
187 187 })
188 188 }
189 189
190 190 pub fn contains(&mut self, rev: Revision) -> Result<bool, GraphError> {
191 191 self.containsiter.contains(rev)
192 192 }
193 193
194 194 pub fn is_empty(&self) -> bool {
195 195 self.containsiter.is_empty()
196 196 }
197 197
198 198 pub fn iter(&self) -> AncestorsIterator<G> {
199 199 // the arguments being the same as for self.containsiter, we know
200 200 // for sure that AncestorsIterator constructor can't fail
201 201 AncestorsIterator::new(
202 202 self.graph.clone(),
203 203 self.initrevs.iter().cloned(),
204 204 self.stoprev,
205 205 self.inclusive,
206 206 )
207 207 .unwrap()
208 208 }
209 209 }
210 210
211 211 impl<G: Graph> MissingAncestors<G> {
212 212 pub fn new(graph: G, bases: impl IntoIterator<Item = Revision>) -> Self {
213 213 let mut bases: HashSet<Revision> = bases.into_iter().collect();
214 214 if bases.is_empty() {
215 215 bases.insert(NULL_REVISION);
216 216 }
217 217 MissingAncestors { graph, bases }
218 218 }
219 219
220 220 pub fn has_bases(&self) -> bool {
221 221 self.bases.iter().any(|&b| b != NULL_REVISION)
222 222 }
223 223
224 224 /// Return a reference to current bases.
225 225 ///
226 226 /// This is useful in unit tests, but also setdiscovery.py does
227 227 /// read the bases attribute of a ancestor.missingancestors instance.
228 228 pub fn get_bases<'a>(&'a self) -> &'a HashSet<Revision> {
229 229 &self.bases
230 230 }
231 231
232 232 pub fn add_bases(
233 233 &mut self,
234 234 new_bases: impl IntoIterator<Item = Revision>,
235 235 ) {
236 236 self.bases.extend(new_bases);
237 237 }
238 238
239 239 /// Remove all ancestors of self.bases from the revs set (in place)
240 240 pub fn remove_ancestors_from(
241 241 &mut self,
242 242 revs: &mut HashSet<Revision>,
243 243 ) -> Result<(), GraphError> {
244 244 revs.retain(|r| !self.bases.contains(r));
245 245 // the null revision is always an ancestor
246 246 revs.remove(&NULL_REVISION);
247 247 if revs.is_empty() {
248 248 return Ok(());
249 249 }
250 250 // anything in revs > start is definitely not an ancestor of bases
251 251 // revs <= start need to be investigated
252 252 // TODO optim: if a missingancestors is to be used several times,
253 253 // we shouldn't need to iterate each time on bases
254 254 let start = match self.bases.iter().cloned().max() {
255 255 Some(m) => m,
256 256 None => {
257 257 // bases is empty (shouldn't happen, but let's be safe)
258 258 return Ok(());
259 259 }
260 260 };
261 261 // whatever happens, we'll keep at least keepcount of them
262 262 // knowing this gives us a earlier stop condition than
263 263 // going all the way to the root
264 264 let keepcount = revs.iter().filter(|r| **r > start).count();
265 265
266 266 let mut curr = start;
267 267 while curr != NULL_REVISION && revs.len() > keepcount {
268 268 if self.bases.contains(&curr) {
269 269 revs.remove(&curr);
270 270 self.add_parents(curr)?;
271 271 }
272 272 curr -= 1;
273 273 }
274 274 Ok(())
275 275 }
276 276
277 277 /// Add rev's parents to self.bases
278 278 #[inline]
279 279 fn add_parents(&mut self, rev: Revision) -> Result<(), GraphError> {
280 280 // No need to bother the set with inserting NULL_REVISION over and
281 281 // over
282 282 for p in self.graph.parents(rev)?.iter().cloned() {
283 283 if p != NULL_REVISION {
284 284 self.bases.insert(p);
285 285 }
286 286 }
287 287 Ok(())
288 288 }
289 289
290 290 /// Return all the ancestors of revs that are not ancestors of self.bases
291 291 ///
292 292 /// This may include elements from revs.
293 293 ///
294 294 /// Equivalent to the revset (::revs - ::self.bases). Revs are returned in
295 295 /// revision number order, which is a topological order.
296 296 pub fn missing_ancestors(
297 297 &mut self,
298 298 revs: impl IntoIterator<Item = Revision>,
299 299 ) -> Result<Vec<Revision>, GraphError> {
300 300 // just for convenience and comparison with Python version
301 301 let bases_visit = &mut self.bases;
302 302 let mut revs: HashSet<Revision> = revs
303 303 .into_iter()
304 304 .filter(|r| !bases_visit.contains(r))
305 305 .collect();
306 306 let revs_visit = &mut revs;
307 307 let mut both_visit: HashSet<Revision> =
308 308 revs_visit.intersection(&bases_visit).cloned().collect();
309 309 if revs_visit.is_empty() {
310 310 return Ok(Vec::new());
311 311 }
312 312
313 313 let max_bases =
314 314 bases_visit.iter().cloned().max().unwrap_or(NULL_REVISION);
315 315 let max_revs =
316 316 revs_visit.iter().cloned().max().unwrap_or(NULL_REVISION);
317 317 let start = max(max_bases, max_revs);
318 318
319 319 // TODO heuristics for with_capacity()?
320 320 let mut missing: Vec<Revision> = Vec::new();
321 321 for curr in (0..=start).rev() {
322 322 if revs_visit.is_empty() {
323 323 break;
324 324 }
325 325 if both_visit.contains(&curr) {
326 326 // curr's parents might have made it into revs_visit through
327 327 // another path
328 328 // TODO optim: Rust's HashSet.remove returns a boolean telling
329 329 // if it happened. This will spare us one set lookup
330 330 both_visit.remove(&curr);
331 331 for p in self.graph.parents(curr)?.iter().cloned() {
332 332 if p == NULL_REVISION {
333 333 continue;
334 334 }
335 335 revs_visit.remove(&p);
336 336 bases_visit.insert(p);
337 337 both_visit.insert(p);
338 338 }
339 continue;
340 }
341 if revs_visit.remove(&curr) {
339 } else if revs_visit.remove(&curr) {
342 340 missing.push(curr);
343 341 for p in self.graph.parents(curr)?.iter().cloned() {
344 342 if p == NULL_REVISION {
345 343 continue;
346 344 }
347 345 if bases_visit.contains(&p) || both_visit.contains(&p) {
348 // p is implicitely in this_visit.
349 // This means p is or should be in bothvisit
346 // p is an ancestor of revs_visit, and is implicitly
347 // in bases_visit, which means p is ::revs & ::bases.
350 348 // TODO optim: hence if bothvisit, we look up twice
351 349 revs_visit.remove(&p);
352 350 bases_visit.insert(p);
353 351 both_visit.insert(p);
354 352 } else {
355 353 // visit later
356 354 revs_visit.insert(p);
357 355 }
358 356 }
359 357 } else if bases_visit.contains(&curr) {
360 358 for p in self.graph.parents(curr)?.iter().cloned() {
361 359 if p == NULL_REVISION {
362 360 continue;
363 361 }
364 362 if revs_visit.contains(&p) || both_visit.contains(&p) {
365 // p is implicitely in this_visit.
366 // This means p is or should be in bothvisit
363 // p is an ancestor of bases_visit, and is implicitly
364 // in revs_visit, which means p is ::revs & ::bases.
367 365 // TODO optim: hence if bothvisit, we look up twice
368 366 revs_visit.remove(&p);
369 367 bases_visit.insert(p);
370 368 both_visit.insert(p);
371 369 } else {
372 // visit later
373 370 bases_visit.insert(p);
374 371 }
375 372 }
376 } else {
377 // not an ancestor of revs or bases: ignore
378 373 }
379 374 }
380 375 missing.reverse();
381 376 Ok(missing)
382 377 }
383 378 }
384 379
385 380 #[cfg(test)]
386 381 mod tests {
387 382
388 383 use super::*;
389 384 use std::iter::FromIterator;
390 385
391 386 #[derive(Clone, Debug)]
392 387 struct Stub;
393 388
394 389 /// This is the same as the dict from test-ancestors.py
395 390 impl Graph for Stub {
396 391 fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError> {
397 392 match rev {
398 393 0 => Ok([-1, -1]),
399 394 1 => Ok([0, -1]),
400 395 2 => Ok([1, -1]),
401 396 3 => Ok([1, -1]),
402 397 4 => Ok([2, -1]),
403 398 5 => Ok([4, -1]),
404 399 6 => Ok([4, -1]),
405 400 7 => Ok([4, -1]),
406 401 8 => Ok([-1, -1]),
407 402 9 => Ok([6, 7]),
408 403 10 => Ok([5, -1]),
409 404 11 => Ok([3, 7]),
410 405 12 => Ok([9, -1]),
411 406 13 => Ok([8, -1]),
412 407 r => Err(GraphError::ParentOutOfRange(r)),
413 408 }
414 409 }
415 410 }
416 411
417 412 fn list_ancestors<G: Graph>(
418 413 graph: G,
419 414 initrevs: Vec<Revision>,
420 415 stoprev: Revision,
421 416 inclusive: bool,
422 417 ) -> Vec<Revision> {
423 418 AncestorsIterator::new(graph, initrevs, stoprev, inclusive)
424 419 .unwrap()
425 420 .map(|res| res.unwrap())
426 421 .collect()
427 422 }
428 423
429 424 #[test]
430 425 /// Same tests as test-ancestor.py, without membership
431 426 /// (see also test-ancestor.py.out)
432 427 fn test_list_ancestor() {
433 428 assert_eq!(list_ancestors(Stub, vec![], 0, false), vec![]);
434 429 assert_eq!(
435 430 list_ancestors(Stub, vec![11, 13], 0, false),
436 431 vec![8, 7, 4, 3, 2, 1, 0]
437 432 );
438 433 assert_eq!(list_ancestors(Stub, vec![1, 3], 0, false), vec![1, 0]);
439 434 assert_eq!(
440 435 list_ancestors(Stub, vec![11, 13], 0, true),
441 436 vec![13, 11, 8, 7, 4, 3, 2, 1, 0]
442 437 );
443 438 assert_eq!(list_ancestors(Stub, vec![11, 13], 6, false), vec![8, 7]);
444 439 assert_eq!(
445 440 list_ancestors(Stub, vec![11, 13], 6, true),
446 441 vec![13, 11, 8, 7]
447 442 );
448 443 assert_eq!(list_ancestors(Stub, vec![11, 13], 11, true), vec![13, 11]);
449 444 assert_eq!(list_ancestors(Stub, vec![11, 13], 12, true), vec![13]);
450 445 assert_eq!(
451 446 list_ancestors(Stub, vec![10, 1], 0, true),
452 447 vec![10, 5, 4, 2, 1, 0]
453 448 );
454 449 }
455 450
456 451 #[test]
457 452 /// Corner case that's not directly in test-ancestors.py, but
458 453 /// that happens quite often, as demonstrated by running the whole
459 454 /// suite.
460 455 /// For instance, run tests/test-obsolete-checkheads.t
461 456 fn test_nullrev_input() {
462 457 let mut iter =
463 458 AncestorsIterator::new(Stub, vec![-1], 0, false).unwrap();
464 459 assert_eq!(iter.next(), None)
465 460 }
466 461
467 462 #[test]
468 463 fn test_contains() {
469 464 let mut lazy =
470 465 AncestorsIterator::new(Stub, vec![10, 1], 0, true).unwrap();
471 466 assert!(lazy.contains(1).unwrap());
472 467 assert!(!lazy.contains(3).unwrap());
473 468
474 469 let mut lazy =
475 470 AncestorsIterator::new(Stub, vec![0], 0, false).unwrap();
476 471 assert!(!lazy.contains(NULL_REVISION).unwrap());
477 472 }
478 473
479 474 #[test]
480 475 fn test_peek() {
481 476 let mut iter =
482 477 AncestorsIterator::new(Stub, vec![10], 0, true).unwrap();
483 478 // peek() gives us the next value
484 479 assert_eq!(iter.peek(), Some(10));
485 480 // but it's not been consumed
486 481 assert_eq!(iter.next(), Some(Ok(10)));
487 482 // and iteration resumes normally
488 483 assert_eq!(iter.next(), Some(Ok(5)));
489 484
490 485 // let's drain the iterator to test peek() at the end
491 486 while iter.next().is_some() {}
492 487 assert_eq!(iter.peek(), None);
493 488 }
494 489
495 490 #[test]
496 491 fn test_empty() {
497 492 let mut iter =
498 493 AncestorsIterator::new(Stub, vec![10], 0, true).unwrap();
499 494 assert!(!iter.is_empty());
500 495 while iter.next().is_some() {}
501 496 assert!(!iter.is_empty());
502 497
503 498 let iter = AncestorsIterator::new(Stub, vec![], 0, true).unwrap();
504 499 assert!(iter.is_empty());
505 500
506 501 // case where iter.seen == {NULL_REVISION}
507 502 let iter = AncestorsIterator::new(Stub, vec![0], 0, false).unwrap();
508 503 assert!(iter.is_empty());
509 504 }
510 505
511 506 /// A corrupted Graph, supporting error handling tests
512 507 #[derive(Clone, Debug)]
513 508 struct Corrupted;
514 509
515 510 impl Graph for Corrupted {
516 511 fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError> {
517 512 match rev {
518 513 1 => Ok([0, -1]),
519 514 r => Err(GraphError::ParentOutOfRange(r)),
520 515 }
521 516 }
522 517 }
523 518
524 519 #[test]
525 520 fn test_initrev_out_of_range() {
526 521 // inclusive=false looks up initrev's parents right away
527 522 match AncestorsIterator::new(Stub, vec![25], 0, false) {
528 523 Ok(_) => panic!("Should have been ParentOutOfRange"),
529 524 Err(e) => assert_eq!(e, GraphError::ParentOutOfRange(25)),
530 525 }
531 526 }
532 527
533 528 #[test]
534 529 fn test_next_out_of_range() {
535 530 // inclusive=false looks up initrev's parents right away
536 531 let mut iter =
537 532 AncestorsIterator::new(Corrupted, vec![1], 0, false).unwrap();
538 533 assert_eq!(iter.next(), Some(Err(GraphError::ParentOutOfRange(0))));
539 534 }
540 535
541 536 #[test]
542 537 fn test_lazy_iter_contains() {
543 538 let mut lazy =
544 539 LazyAncestors::new(Stub, vec![11, 13], 0, false).unwrap();
545 540
546 541 let revs: Vec<Revision> = lazy.iter().map(|r| r.unwrap()).collect();
547 542 // compare with iterator tests on the same initial revisions
548 543 assert_eq!(revs, vec![8, 7, 4, 3, 2, 1, 0]);
549 544
550 545 // contains() results are correct, unaffected by the fact that
551 546 // we consumed entirely an iterator out of lazy
552 547 assert_eq!(lazy.contains(2), Ok(true));
553 548 assert_eq!(lazy.contains(9), Ok(false));
554 549 }
555 550
556 551 #[test]
557 552 fn test_lazy_contains_iter() {
558 553 let mut lazy =
559 554 LazyAncestors::new(Stub, vec![11, 13], 0, false).unwrap(); // reminder: [8, 7, 4, 3, 2, 1, 0]
560 555
561 556 assert_eq!(lazy.contains(2), Ok(true));
562 557 assert_eq!(lazy.contains(6), Ok(false));
563 558
564 559 // after consumption of 2 by the inner iterator, results stay
565 560 // consistent
566 561 assert_eq!(lazy.contains(2), Ok(true));
567 562 assert_eq!(lazy.contains(5), Ok(false));
568 563
569 564 // iter() still gives us a fresh iterator
570 565 let revs: Vec<Revision> = lazy.iter().map(|r| r.unwrap()).collect();
571 566 assert_eq!(revs, vec![8, 7, 4, 3, 2, 1, 0]);
572 567 }
573 568
574 569 #[test]
575 570 /// Test constructor, add/get bases
576 571 fn test_missing_bases() {
577 572 let mut missing_ancestors =
578 573 MissingAncestors::new(Stub, [5, 3, 1, 3].iter().cloned());
579 574 let mut as_vec: Vec<Revision> =
580 575 missing_ancestors.get_bases().iter().cloned().collect();
581 576 as_vec.sort();
582 577 assert_eq!(as_vec, [1, 3, 5]);
583 578
584 579 missing_ancestors.add_bases([3, 7, 8].iter().cloned());
585 580 as_vec = missing_ancestors.get_bases().iter().cloned().collect();
586 581 as_vec.sort();
587 582 assert_eq!(as_vec, [1, 3, 5, 7, 8]);
588 583 }
589 584
590 585 fn assert_missing_remove(
591 586 bases: &[Revision],
592 587 revs: &[Revision],
593 588 expected: &[Revision],
594 589 ) {
595 590 let mut missing_ancestors =
596 591 MissingAncestors::new(Stub, bases.iter().cloned());
597 592 let mut revset: HashSet<Revision> = revs.iter().cloned().collect();
598 593 missing_ancestors
599 594 .remove_ancestors_from(&mut revset)
600 595 .unwrap();
601 596 let mut as_vec: Vec<Revision> = revset.into_iter().collect();
602 597 as_vec.sort();
603 598 assert_eq!(as_vec.as_slice(), expected);
604 599 }
605 600
606 601 #[test]
607 602 fn test_missing_remove() {
608 603 assert_missing_remove(
609 604 &[1, 2, 3, 4, 7],
610 605 Vec::from_iter(1..10).as_slice(),
611 606 &[5, 6, 8, 9],
612 607 );
613 608 assert_missing_remove(&[10], &[11, 12, 13, 14], &[11, 12, 13, 14]);
614 609 assert_missing_remove(&[7], &[1, 2, 3, 4, 5], &[3, 5]);
615 610 }
616 611
617 612 fn assert_missing_ancestors(
618 613 bases: &[Revision],
619 614 revs: &[Revision],
620 615 expected: &[Revision],
621 616 ) {
622 617 let mut missing_ancestors =
623 618 MissingAncestors::new(Stub, bases.iter().cloned());
624 619 let missing = missing_ancestors
625 620 .missing_ancestors(revs.iter().cloned())
626 621 .unwrap();
627 622 assert_eq!(missing.as_slice(), expected);
628 623 }
629 624
630 625 #[test]
631 626 fn test_missing_ancestors() {
632 627 // examples taken from test-ancestors.py by having it run
633 628 // on the same graph (both naive and fast Python algs)
634 629 assert_missing_ancestors(&[10], &[11], &[3, 7, 11]);
635 630 assert_missing_ancestors(&[11], &[10], &[5, 10]);
636 631 assert_missing_ancestors(&[7], &[9, 11], &[3, 6, 9, 11]);
637 632 }
638 633
639 634 // A Graph represented by a vector whose indices are revisions
640 635 // and values are parents of the revisions
641 636 type VecGraph = Vec<[Revision; 2]>;
642 637
643 638 impl Graph for VecGraph {
644 639 fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError> {
645 640 Ok(self[rev as usize])
646 641 }
647 642 }
648 643
649 644 /// An interesting case found by a random generator similar to
650 645 /// the one in test-ancestor.py. An early version of Rust MissingAncestors
651 646 /// failed this, yet none of the integration tests of the whole suite
652 647 /// catched it.
653 648 #[test]
654 649 fn test_remove_ancestors_from_case1() {
655 650 let graph: VecGraph = vec![
656 651 [NULL_REVISION, NULL_REVISION],
657 652 [0, NULL_REVISION],
658 653 [1, 0],
659 654 [2, 1],
660 655 [3, NULL_REVISION],
661 656 [4, NULL_REVISION],
662 657 [5, 1],
663 658 [2, NULL_REVISION],
664 659 [7, NULL_REVISION],
665 660 [8, NULL_REVISION],
666 661 [9, NULL_REVISION],
667 662 [10, 1],
668 663 [3, NULL_REVISION],
669 664 [12, NULL_REVISION],
670 665 [13, NULL_REVISION],
671 666 [14, NULL_REVISION],
672 667 [4, NULL_REVISION],
673 668 [16, NULL_REVISION],
674 669 [17, NULL_REVISION],
675 670 [18, NULL_REVISION],
676 671 [19, 11],
677 672 [20, NULL_REVISION],
678 673 [21, NULL_REVISION],
679 674 [22, NULL_REVISION],
680 675 [23, NULL_REVISION],
681 676 [2, NULL_REVISION],
682 677 [3, NULL_REVISION],
683 678 [26, 24],
684 679 [27, NULL_REVISION],
685 680 [28, NULL_REVISION],
686 681 [12, NULL_REVISION],
687 682 [1, NULL_REVISION],
688 683 [1, 9],
689 684 [32, NULL_REVISION],
690 685 [33, NULL_REVISION],
691 686 [34, 31],
692 687 [35, NULL_REVISION],
693 688 [36, 26],
694 689 [37, NULL_REVISION],
695 690 [38, NULL_REVISION],
696 691 [39, NULL_REVISION],
697 692 [40, NULL_REVISION],
698 693 [41, NULL_REVISION],
699 694 [42, 26],
700 695 [0, NULL_REVISION],
701 696 [44, NULL_REVISION],
702 697 [45, 4],
703 698 [40, NULL_REVISION],
704 699 [47, NULL_REVISION],
705 700 [36, 0],
706 701 [49, NULL_REVISION],
707 702 [NULL_REVISION, NULL_REVISION],
708 703 [51, NULL_REVISION],
709 704 [52, NULL_REVISION],
710 705 [53, NULL_REVISION],
711 706 [14, NULL_REVISION],
712 707 [55, NULL_REVISION],
713 708 [15, NULL_REVISION],
714 709 [23, NULL_REVISION],
715 710 [58, NULL_REVISION],
716 711 [59, NULL_REVISION],
717 712 [2, NULL_REVISION],
718 713 [61, 59],
719 714 [62, NULL_REVISION],
720 715 [63, NULL_REVISION],
721 716 [NULL_REVISION, NULL_REVISION],
722 717 [65, NULL_REVISION],
723 718 [66, NULL_REVISION],
724 719 [67, NULL_REVISION],
725 720 [68, NULL_REVISION],
726 721 [37, 28],
727 722 [69, 25],
728 723 [71, NULL_REVISION],
729 724 [72, NULL_REVISION],
730 725 [50, 2],
731 726 [74, NULL_REVISION],
732 727 [12, NULL_REVISION],
733 728 [18, NULL_REVISION],
734 729 [77, NULL_REVISION],
735 730 [78, NULL_REVISION],
736 731 [79, NULL_REVISION],
737 732 [43, 33],
738 733 [81, NULL_REVISION],
739 734 [82, NULL_REVISION],
740 735 [83, NULL_REVISION],
741 736 [84, 45],
742 737 [85, NULL_REVISION],
743 738 [86, NULL_REVISION],
744 739 [NULL_REVISION, NULL_REVISION],
745 740 [88, NULL_REVISION],
746 741 [NULL_REVISION, NULL_REVISION],
747 742 [76, 83],
748 743 [44, NULL_REVISION],
749 744 [92, NULL_REVISION],
750 745 [93, NULL_REVISION],
751 746 [9, NULL_REVISION],
752 747 [95, 67],
753 748 [96, NULL_REVISION],
754 749 [97, NULL_REVISION],
755 750 [NULL_REVISION, NULL_REVISION],
756 751 ];
757 752 let problem_rev = 28 as Revision;
758 753 let problem_base = 70 as Revision;
759 754 // making the problem obvious: problem_rev is a parent of problem_base
760 755 assert_eq!(graph.parents(problem_base).unwrap()[1], problem_rev);
761 756
762 757 let mut missing_ancestors: MissingAncestors<VecGraph> =
763 758 MissingAncestors::new(
764 759 graph,
765 760 [60, 26, 70, 3, 96, 19, 98, 49, 97, 47, 1, 6]
766 761 .iter()
767 762 .cloned(),
768 763 );
769 764 assert!(missing_ancestors.bases.contains(&problem_base));
770 765
771 766 let mut revs: HashSet<Revision> =
772 767 [4, 12, 41, 28, 68, 38, 1, 30, 56, 44]
773 768 .iter()
774 769 .cloned()
775 770 .collect();
776 771 missing_ancestors.remove_ancestors_from(&mut revs).unwrap();
777 772 assert!(!revs.contains(&problem_rev));
778 773 }
779 774
780 775 }
General Comments 0
You need to be logged in to leave comments. Login now