| 1 |
// ancestors.rs |
| 2 |
// |
| 3 |
// Copyright 2018 Georges Racinet <[email protected]> |
| 4 |
// |
| 5 |
// This software may be used and distributed according to the terms of the |
| 6 |
// GNU General Public License version 2 or any later version. |
| 7 |
|
| 8 |
//! Rust versions of generic DAG ancestors algorithms for Mercurial |
| 9 |
|
| 10 |
use std::cmp::max; |
| 11 |
use std::collections::BinaryHeap; |
| 12 |
use std::collections::HashSet; |
| 13 |
|
| 14 |
use bit_set::BitSet; |
| 15 |
|
| 16 |
use super::Graph; |
| 17 |
use super::GraphError; |
| 18 |
use super::Revision; |
| 19 |
use super::NULL_REVISION; |
| 20 |
use crate::dagops; |
| 21 |
|
| 22 |
/// A set of revisions backed by a bitset, optimized for descending insertion. |
| 23 |
struct DescendingRevisionSet { |
| 24 |
/// The underlying bitset storage. |
| 25 |
set: BitSet, |
| 26 |
/// For a revision `R` we store `ceiling - R` instead of `R` so that |
| 27 |
/// memory usage is proportional to how far we've descended. |
| 28 |
ceiling: i32, |
| 29 |
/// Track length separately because [`BitSet::len`] recounts every time. |
| 30 |
len: usize, |
| 31 |
} |
| 32 |
|
| 33 |
impl DescendingRevisionSet { |
| 34 |
/// Creates a new empty set that can store revisions up to `ceiling`. |
| 35 |
fn new(ceiling: Revision) -> Self { |
| 36 |
Self { set: BitSet::new(), ceiling: ceiling.0, len: 0 } |
| 37 |
} |
| 38 |
|
| 39 |
/// Returns the number of revisions in the set. |
| 40 |
fn len(&self) -> usize { |
| 41 |
self.len |
| 42 |
} |
| 43 |
|
| 44 |
/// Returns true if the set contains `value`. |
| 45 |
fn contains(&self, value: Revision) -> bool { |
| 46 |
match self.encode(value) { |
| 47 |
Ok(n) => self.set.contains(n), |
| 48 |
Err(_) => false, |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
/// Adds `value` to the set. Returns true if it was not already in the set. |
| 53 |
/// Returns `Err` if it cannot store it because it is above the ceiling. |
| 54 |
fn insert(&mut self, value: Revision) -> Result<bool, GraphError> { |
| 55 |
let inserted = self.set.insert(self.encode(value)?); |
| 56 |
self.len += inserted as usize; |
| 57 |
Ok(inserted) |
| 58 |
} |
| 59 |
|
| 60 |
fn encode(&self, value: Revision) -> Result<usize, GraphError> { |
| 61 |
usize::try_from(self.ceiling - value.0) |
| 62 |
.map_err(|_| GraphError::ParentOutOfOrder(value)) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/// Iterator over the ancestors of a given list of revisions |
| 67 |
/// This is a generic type, defined and implemented for any Graph, so that |
| 68 |
/// it's easy to |
| 69 |
/// |
| 70 |
/// - unit test in pure Rust |
| 71 |
/// - bind to main Mercurial code, potentially in several ways and have these |
| 72 |
/// bindings evolve over time |
| 73 |
pub struct AncestorsIterator<G: Graph> { |
| 74 |
graph: G, |
| 75 |
visit: BinaryHeap<Revision>, |
| 76 |
seen: DescendingRevisionSet, |
| 77 |
stoprev: Revision, |
| 78 |
} |
| 79 |
|
| 80 |
pub struct MissingAncestors<G: Graph> { |
| 81 |
graph: G, |
| 82 |
bases: HashSet<Revision>, |
| 83 |
max_base: Revision, |
| 84 |
} |
| 85 |
|
| 86 |
impl<G: Graph> AncestorsIterator<G> { |
| 87 |
/// Constructor. |
| 88 |
/// |
| 89 |
/// if `inclusive` is true, then the init revisions are emitted in |
| 90 |
/// particular, otherwise iteration starts from their parents. |
| 91 |
pub fn new( |
| 92 |
graph: G, |
| 93 |
initrevs: impl IntoIterator<Item = Revision>, |
| 94 |
stoprev: Revision, |
| 95 |
inclusive: bool, |
| 96 |
) -> Result<Self, GraphError> { |
| 97 |
let filtered_initrevs = initrevs |
| 98 |
.into_iter() |
| 99 |
.filter(|&r| r >= stoprev) |
| 100 |
.collect::<BinaryHeap<_>>(); |
| 101 |
let max = *filtered_initrevs.peek().unwrap_or(&NULL_REVISION); |
| 102 |
let mut seen = DescendingRevisionSet::new(max); |
| 103 |
if inclusive { |
| 104 |
for &rev in &filtered_initrevs { |
| 105 |
seen.insert(rev).expect("revs cannot be above their max"); |
| 106 |
} |
| 107 |
return Ok(AncestorsIterator { |
| 108 |
visit: filtered_initrevs, |
| 109 |
seen, |
| 110 |
stoprev, |
| 111 |
graph, |
| 112 |
}); |
| 113 |
} |
| 114 |
let mut this = AncestorsIterator { |
| 115 |
visit: BinaryHeap::new(), |
| 116 |
seen, |
| 117 |
stoprev, |
| 118 |
graph, |
| 119 |
}; |
| 120 |
this.seen.insert(NULL_REVISION).expect("null is the smallest revision"); |
| 121 |
for rev in filtered_initrevs { |
| 122 |
for parent in this.graph.parents(rev)?.iter().cloned() { |
| 123 |
this.conditionally_push_rev(parent)?; |
| 124 |
} |
| 125 |
} |
| 126 |
Ok(this) |
| 127 |
} |
| 128 |
|
| 129 |
#[inline] |
| 130 |
fn conditionally_push_rev( |
| 131 |
&mut self, |
| 132 |
rev: Revision, |
| 133 |
) -> Result<(), GraphError> { |
| 134 |
if self.stoprev <= rev && self.seen.insert(rev)? { |
| 135 |
self.visit.push(rev); |
| 136 |
} |
| 137 |
Ok(()) |
| 138 |
} |
| 139 |
|
| 140 |
/// Consumes partially the iterator to tell if the given target |
| 141 |
/// revision |
| 142 |
/// is in the ancestors it emits. |
| 143 |
/// This is meant for iterators actually dedicated to that kind of |
| 144 |
/// purpose |
| 145 |
pub fn contains(&mut self, target: Revision) -> Result<bool, GraphError> { |
| 146 |
if self.seen.contains(target) && target != NULL_REVISION { |
| 147 |
return Ok(true); |
| 148 |
} |
| 149 |
for item in self { |
| 150 |
let rev = item?; |
| 151 |
if rev == target { |
| 152 |
return Ok(true); |
| 153 |
} |
| 154 |
if rev < target { |
| 155 |
return Ok(false); |
| 156 |
} |
| 157 |
} |
| 158 |
Ok(false) |
| 159 |
} |
| 160 |
|
| 161 |
pub fn peek(&self) -> Option<Revision> { |
| 162 |
self.visit.peek().cloned() |
| 163 |
} |
| 164 |
|
| 165 |
/// Tell if the iterator is about an empty set |
| 166 |
/// |
| 167 |
/// The result does not depend whether the iterator has been consumed |
| 168 |
/// or not. |
| 169 |
/// This is mostly meant for iterators backing a lazy ancestors set |
| 170 |
pub fn is_empty(&self) -> bool { |
| 171 |
if self.visit.len() > 0 { |
| 172 |
return false; |
| 173 |
} |
| 174 |
let seen_len = self.seen.len(); |
| 175 |
if seen_len > 1 { |
| 176 |
return false; |
| 177 |
} |
| 178 |
// at this point, the seen set is at most a singleton. |
| 179 |
// If not `self.inclusive`, it's still possible that it has only |
| 180 |
// the null revision |
| 181 |
seen_len == 0 || self.seen.contains(NULL_REVISION) |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
/// Main implementation for the iterator |
| 186 |
/// |
| 187 |
/// The algorithm is the same as in `_lazyancestorsiter()` from `ancestors.py` |
| 188 |
/// with a few non crucial differences: |
| 189 |
/// |
| 190 |
/// - there's no filtering of invalid parent revisions. Actually, it should be |
| 191 |
/// consistent and more efficient to filter them from the end caller. |
| 192 |
/// - we don't have the optimization for adjacent revisions (i.e., the case |
| 193 |
/// where `p1 == rev - 1`), because it amounts to update the first element of |
| 194 |
/// the heap without sifting, which Rust's BinaryHeap doesn't let us do. |
| 195 |
/// - we save a few pushes by comparing with `stoprev` before pushing |
| 196 |
impl<G: Graph> Iterator for AncestorsIterator<G> { |
| 197 |
type Item = Result<Revision, GraphError>; |
| 198 |
|
| 199 |
fn next(&mut self) -> Option<Self::Item> { |
| 200 |
let current = match self.visit.peek() { |
| 201 |
None => { |
| 202 |
return None; |
| 203 |
} |
| 204 |
Some(c) => *c, |
| 205 |
}; |
| 206 |
let [p1, p2] = match self.graph.parents(current) { |
| 207 |
Ok(ps) => ps, |
| 208 |
Err(e) => return Some(Err(e)), |
| 209 |
}; |
| 210 |
let pop = if p1 < self.stoprev { |
| 211 |
true |
| 212 |
} else { |
| 213 |
match self.seen.insert(p1) { |
| 214 |
Ok(inserted) => !inserted, |
| 215 |
Err(e) => return Some(Err(e)), |
| 216 |
} |
| 217 |
}; |
| 218 |
if pop { |
| 219 |
self.visit.pop(); |
| 220 |
} else { |
| 221 |
*(self.visit.peek_mut().unwrap()) = p1; |
| 222 |
}; |
| 223 |
|
| 224 |
if let Err(e) = self.conditionally_push_rev(p2) { |
| 225 |
return Some(Err(e)); |
| 226 |
} |
| 227 |
Some(Ok(current)) |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
impl<G: Graph> MissingAncestors<G> { |
| 232 |
pub fn new(graph: G, bases: impl IntoIterator<Item = Revision>) -> Self { |
| 233 |
let mut created = MissingAncestors { |
| 234 |
graph, |
| 235 |
bases: HashSet::new(), |
| 236 |
max_base: NULL_REVISION, |
| 237 |
}; |
| 238 |
created.add_bases(bases); |
| 239 |
created |
| 240 |
} |
| 241 |
|
| 242 |
pub fn has_bases(&self) -> bool { |
| 243 |
!self.bases.is_empty() |
| 244 |
} |
| 245 |
|
| 246 |
/// Return a reference to current bases. |
| 247 |
/// |
| 248 |
/// This is useful in unit tests, but also setdiscovery.py does |
| 249 |
/// read the bases attribute of a ancestor.missingancestors instance. |
| 250 |
pub fn get_bases(&self) -> &HashSet<Revision> { |
| 251 |
&self.bases |
| 252 |
} |
| 253 |
|
| 254 |
/// Computes the relative heads of current bases. |
| 255 |
/// |
| 256 |
/// The object is still usable after this. |
| 257 |
pub fn bases_heads(&self) -> Result<HashSet<Revision>, GraphError> { |
| 258 |
dagops::heads(&self.graph, self.bases.iter()) |
| 259 |
} |
| 260 |
|
| 261 |
/// Consumes the object and returns the relative heads of its bases. |
| 262 |
pub fn into_bases_heads(mut self) -> Result<HashSet<Revision>, GraphError> { |
| 263 |
dagops::retain_heads(&self.graph, &mut self.bases)?; |
| 264 |
Ok(self.bases) |
| 265 |
} |
| 266 |
|
| 267 |
/// Add some revisions to `self.bases` |
| 268 |
/// |
| 269 |
/// Takes care of keeping `self.max_base` up to date. |
| 270 |
pub fn add_bases(&mut self, new_bases: impl IntoIterator<Item = Revision>) { |
| 271 |
let mut max_base = self.max_base; |
| 272 |
self.bases.extend( |
| 273 |
new_bases.into_iter().filter(|&rev| rev != NULL_REVISION).inspect( |
| 274 |
|&r| { |
| 275 |
if r > max_base { |
| 276 |
max_base = r; |
| 277 |
} |
| 278 |
}, |
| 279 |
), |
| 280 |
); |
| 281 |
self.max_base = max_base; |
| 282 |
} |
| 283 |
|
| 284 |
/// Remove all ancestors of self.bases from the revs set (in place) |
| 285 |
pub fn remove_ancestors_from( |
| 286 |
&mut self, |
| 287 |
revs: &mut HashSet<Revision>, |
| 288 |
) -> Result<(), GraphError> { |
| 289 |
revs.retain(|r| !self.bases.contains(r)); |
| 290 |
// the null revision is always an ancestor. Logically speaking |
| 291 |
// it's debatable in case bases is empty, but the Python |
| 292 |
// implementation always adds NULL_REVISION to bases, making it |
| 293 |
// unconditionnally true. |
| 294 |
revs.remove(&NULL_REVISION); |
| 295 |
if revs.is_empty() { |
| 296 |
return Ok(()); |
| 297 |
} |
| 298 |
// anything in revs > start is definitely not an ancestor of bases |
| 299 |
// revs <= start need to be investigated |
| 300 |
if self.max_base == NULL_REVISION { |
| 301 |
return Ok(()); |
| 302 |
} |
| 303 |
|
| 304 |
// whatever happens, we'll keep at least keepcount of them |
| 305 |
// knowing this gives us a earlier stop condition than |
| 306 |
// going all the way to the root |
| 307 |
let keepcount = revs.iter().filter(|r| **r > self.max_base).count(); |
| 308 |
|
| 309 |
let mut curr = self.max_base; |
| 310 |
while curr != NULL_REVISION && revs.len() > keepcount { |
| 311 |
if self.bases.contains(&curr) { |
| 312 |
revs.remove(&curr); |
| 313 |
self.add_parents(curr)?; |
| 314 |
} |
| 315 |
// We know this revision is safe because we've checked the bounds |
| 316 |
// before. |
| 317 |
curr = Revision(curr.0 - 1); |
| 318 |
} |
| 319 |
Ok(()) |
| 320 |
} |
| 321 |
|
| 322 |
/// Add the parents of `rev` to `self.bases` |
| 323 |
/// |
| 324 |
/// This has no effect on `self.max_base` |
| 325 |
#[inline] |
| 326 |
fn add_parents(&mut self, rev: Revision) -> Result<(), GraphError> { |
| 327 |
if rev == NULL_REVISION { |
| 328 |
return Ok(()); |
| 329 |
} |
| 330 |
for p in self.graph.parents(rev)?.iter().cloned() { |
| 331 |
// No need to bother the set with inserting NULL_REVISION over and |
| 332 |
// over |
| 333 |
if p != NULL_REVISION { |
| 334 |
self.bases.insert(p); |
| 335 |
} |
| 336 |
} |
| 337 |
Ok(()) |
| 338 |
} |
| 339 |
|
| 340 |
/// Return all the ancestors of revs that are not ancestors of self.bases |
| 341 |
/// |
| 342 |
/// This may include elements from revs. |
| 343 |
/// |
| 344 |
/// Equivalent to the revset (::revs - ::self.bases). Revs are returned in |
| 345 |
/// revision number order, which is a topological order. |
| 346 |
pub fn missing_ancestors( |
| 347 |
&mut self, |
| 348 |
revs: impl IntoIterator<Item = Revision>, |
| 349 |
) -> Result<Vec<Revision>, GraphError> { |
| 350 |
// just for convenience and comparison with Python version |
| 351 |
let bases_visit = &mut self.bases; |
| 352 |
let mut revs: HashSet<Revision> = |
| 353 |
revs.into_iter().filter(|r| !bases_visit.contains(r)).collect(); |
| 354 |
let revs_visit = &mut revs; |
| 355 |
let mut both_visit: HashSet<Revision> = |
| 356 |
revs_visit.intersection(bases_visit).cloned().collect(); |
| 357 |
if revs_visit.is_empty() { |
| 358 |
return Ok(Vec::new()); |
| 359 |
} |
| 360 |
let max_revs = revs_visit.iter().cloned().max().unwrap(); |
| 361 |
let start = max(self.max_base, max_revs); |
| 362 |
|
| 363 |
// TODO heuristics for with_capacity()? |
| 364 |
let mut missing: Vec<Revision> = Vec::new(); |
| 365 |
for curr in (0..=start.0).rev() { |
| 366 |
if revs_visit.is_empty() { |
| 367 |
break; |
| 368 |
} |
| 369 |
if both_visit.remove(&Revision(curr)) { |
| 370 |
// curr's parents might have made it into revs_visit through |
| 371 |
// another path |
| 372 |
for p in self.graph.parents(Revision(curr))?.iter().cloned() { |
| 373 |
if p == NULL_REVISION { |
| 374 |
continue; |
| 375 |
} |
| 376 |
revs_visit.remove(&p); |
| 377 |
bases_visit.insert(p); |
| 378 |
both_visit.insert(p); |
| 379 |
} |
| 380 |
} else if revs_visit.remove(&Revision(curr)) { |
| 381 |
missing.push(Revision(curr)); |
| 382 |
for p in self.graph.parents(Revision(curr))?.iter().cloned() { |
| 383 |
if p == NULL_REVISION { |
| 384 |
continue; |
| 385 |
} |
| 386 |
if bases_visit.contains(&p) { |
| 387 |
// p is already known to be an ancestor of revs_visit |
| 388 |
revs_visit.remove(&p); |
| 389 |
both_visit.insert(p); |
| 390 |
} else if both_visit.contains(&p) { |
| 391 |
// p should have been in bases_visit |
| 392 |
revs_visit.remove(&p); |
| 393 |
bases_visit.insert(p); |
| 394 |
} else { |
| 395 |
// visit later |
| 396 |
revs_visit.insert(p); |
| 397 |
} |
| 398 |
} |
| 399 |
} else if bases_visit.contains(&Revision(curr)) { |
| 400 |
for p in self.graph.parents(Revision(curr))?.iter().cloned() { |
| 401 |
if p == NULL_REVISION { |
| 402 |
continue; |
| 403 |
} |
| 404 |
if revs_visit.remove(&p) || both_visit.contains(&p) { |
| 405 |
// p is an ancestor of bases_visit, and is implicitly |
| 406 |
// in revs_visit, which means p is ::revs & ::bases. |
| 407 |
bases_visit.insert(p); |
| 408 |
both_visit.insert(p); |
| 409 |
} else { |
| 410 |
bases_visit.insert(p); |
| 411 |
} |
| 412 |
} |
| 413 |
} |
| 414 |
} |
| 415 |
missing.reverse(); |
| 416 |
Ok(missing) |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
#[cfg(test)] |
| 421 |
mod tests { |
| 422 |
|
| 423 |
use super::*; |
| 424 |
use crate::testing::SampleGraph; |
| 425 |
use crate::testing::VecGraph; |
| 426 |
use crate::BaseRevision; |
| 427 |
|
| 428 |
impl From<BaseRevision> for Revision { |
| 429 |
fn from(value: BaseRevision) -> Self { |
| 430 |
if !cfg!(test) { |
| 431 |
panic!("should only be used in tests") |
| 432 |
} |
| 433 |
Revision(value) |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
impl PartialEq<BaseRevision> for Revision { |
| 438 |
fn eq(&self, other: &BaseRevision) -> bool { |
| 439 |
if !cfg!(test) { |
| 440 |
panic!("should only be used in tests") |
| 441 |
} |
| 442 |
self.0.eq(other) |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
impl PartialEq<u32> for Revision { |
| 447 |
fn eq(&self, other: &u32) -> bool { |
| 448 |
if !cfg!(test) { |
| 449 |
panic!("should only be used in tests") |
| 450 |
} |
| 451 |
let check: Result<u32, _> = self.0.try_into(); |
| 452 |
match check { |
| 453 |
Ok(value) => value.eq(other), |
| 454 |
Err(_) => false, |
| 455 |
} |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
fn list_ancestors<G: Graph>( |
| 460 |
graph: G, |
| 461 |
initrevs: Vec<Revision>, |
| 462 |
stoprev: Revision, |
| 463 |
inclusive: bool, |
| 464 |
) -> Vec<Revision> { |
| 465 |
AncestorsIterator::new(graph, initrevs, stoprev, inclusive) |
| 466 |
.unwrap() |
| 467 |
.map(|res| res.unwrap()) |
| 468 |
.collect() |
| 469 |
} |
| 470 |
|
| 471 |
#[test] |
| 472 |
fn test_descending_revision_set() { |
| 473 |
let mut set = DescendingRevisionSet::new(Revision(1_000_000)); |
| 474 |
|
| 475 |
assert_eq!(set.len(), 0); |
| 476 |
assert!(!set.contains(Revision(999_950))); |
| 477 |
|
| 478 |
assert_eq!(set.insert(Revision(999_950)), Ok(true)); |
| 479 |
assert_eq!(set.insert(Revision(999_950)), Ok(false)); |
| 480 |
assert_eq!(set.insert(Revision(1_000_000)), Ok(true)); |
| 481 |
assert_eq!( |
| 482 |
set.insert(Revision(1_000_001)), |
| 483 |
Err(GraphError::ParentOutOfOrder(Revision(1_000_001))) |
| 484 |
); |
| 485 |
|
| 486 |
assert_eq!(set.len(), 2); |
| 487 |
assert!(set.contains(Revision(999_950))); |
| 488 |
assert!(!set.contains(Revision(999_951))); |
| 489 |
assert!(set.contains(Revision(1_000_000))); |
| 490 |
assert!(!set.contains(Revision(1_000_001))); |
| 491 |
} |
| 492 |
|
| 493 |
#[test] |
| 494 |
/// Same tests as test-ancestor.py, without membership |
| 495 |
/// (see also test-ancestor.py.out) |
| 496 |
fn test_list_ancestor() { |
| 497 |
assert_eq!( |
| 498 |
list_ancestors(SampleGraph, vec![], 0.into(), false), |
| 499 |
Vec::<Revision>::new() |
| 500 |
); |
| 501 |
assert_eq!( |
| 502 |
list_ancestors( |
| 503 |
SampleGraph, |
| 504 |
vec![11.into(), 13.into()], |
| 505 |
0.into(), |
| 506 |
false |
| 507 |
), |
| 508 |
vec![8, 7, 4, 3, 2, 1, 0] |
| 509 |
); |
| 510 |
// it works as well on references, because &Graph implements Graph |
| 511 |
// this is needed as of this writing by RHGitaly |
| 512 |
assert_eq!( |
| 513 |
list_ancestors( |
| 514 |
&SampleGraph, |
| 515 |
vec![11.into(), 13.into()], |
| 516 |
0.into(), |
| 517 |
false |
| 518 |
), |
| 519 |
vec![8, 7, 4, 3, 2, 1, 0] |
| 520 |
); |
| 521 |
|
| 522 |
assert_eq!( |
| 523 |
list_ancestors( |
| 524 |
SampleGraph, |
| 525 |
vec![1.into(), 3.into()], |
| 526 |
0.into(), |
| 527 |
false |
| 528 |
), |
| 529 |
vec![1, 0] |
| 530 |
); |
| 531 |
assert_eq!( |
| 532 |
list_ancestors( |
| 533 |
SampleGraph, |
| 534 |
vec![11.into(), 13.into()], |
| 535 |
0.into(), |
| 536 |
true |
| 537 |
), |
| 538 |
vec![13, 11, 8, 7, 4, 3, 2, 1, 0] |
| 539 |
); |
| 540 |
assert_eq!( |
| 541 |
list_ancestors( |
| 542 |
SampleGraph, |
| 543 |
vec![11.into(), 13.into()], |
| 544 |
6.into(), |
| 545 |
false |
| 546 |
), |
| 547 |
vec![8, 7] |
| 548 |
); |
| 549 |
assert_eq!( |
| 550 |
list_ancestors( |
| 551 |
SampleGraph, |
| 552 |
vec![11.into(), 13.into()], |
| 553 |
6.into(), |
| 554 |
true |
| 555 |
), |
| 556 |
vec![13, 11, 8, 7] |
| 557 |
); |
| 558 |
assert_eq!( |
| 559 |
list_ancestors( |
| 560 |
SampleGraph, |
| 561 |
vec![11.into(), 13.into()], |
| 562 |
11.into(), |
| 563 |
true |
| 564 |
), |
| 565 |
vec![13, 11] |
| 566 |
); |
| 567 |
assert_eq!( |
| 568 |
list_ancestors( |
| 569 |
SampleGraph, |
| 570 |
vec![11.into(), 13.into()], |
| 571 |
12.into(), |
| 572 |
true |
| 573 |
), |
| 574 |
vec![13] |
| 575 |
); |
| 576 |
assert_eq!( |
| 577 |
list_ancestors( |
| 578 |
SampleGraph, |
| 579 |
vec![10.into(), 1.into()], |
| 580 |
0.into(), |
| 581 |
true |
| 582 |
), |
| 583 |
vec![10, 5, 4, 2, 1, 0] |
| 584 |
); |
| 585 |
} |
| 586 |
|
| 587 |
#[test] |
| 588 |
/// Corner case that's not directly in test-ancestors.py, but |
| 589 |
/// that happens quite often, as demonstrated by running the whole |
| 590 |
/// suite. |
| 591 |
/// For instance, run tests/test-obsolete-checkheads.t |
| 592 |
fn test_nullrev_input() { |
| 593 |
let mut iter = AncestorsIterator::new( |
| 594 |
SampleGraph, |
| 595 |
vec![Revision(-1)], |
| 596 |
0.into(), |
| 597 |
false, |
| 598 |
) |
| 599 |
.unwrap(); |
| 600 |
assert_eq!(iter.next(), None) |
| 601 |
} |
| 602 |
|
| 603 |
#[test] |
| 604 |
fn test_contains() { |
| 605 |
let mut lazy = AncestorsIterator::new( |
| 606 |
SampleGraph, |
| 607 |
vec![10.into(), 1.into()], |
| 608 |
0.into(), |
| 609 |
true, |
| 610 |
) |
| 611 |
.unwrap(); |
| 612 |
assert!(lazy.contains(1.into()).unwrap()); |
| 613 |
assert!(!lazy.contains(3.into()).unwrap()); |
| 614 |
|
| 615 |
let mut lazy = AncestorsIterator::new( |
| 616 |
SampleGraph, |
| 617 |
vec![0.into()], |
| 618 |
0.into(), |
| 619 |
false, |
| 620 |
) |
| 621 |
.unwrap(); |
| 622 |
assert!(!lazy.contains(NULL_REVISION).unwrap()); |
| 623 |
} |
| 624 |
|
| 625 |
#[test] |
| 626 |
fn test_peek() { |
| 627 |
let mut iter = AncestorsIterator::new( |
| 628 |
SampleGraph, |
| 629 |
vec![10.into()], |
| 630 |
0.into(), |
| 631 |
true, |
| 632 |
) |
| 633 |
.unwrap(); |
| 634 |
// peek() gives us the next value |
| 635 |
assert_eq!(iter.peek(), Some(10.into())); |
| 636 |
// but it's not been consumed |
| 637 |
assert_eq!(iter.next(), Some(Ok(10.into()))); |
| 638 |
// and iteration resumes normally |
| 639 |
assert_eq!(iter.next(), Some(Ok(5.into()))); |
| 640 |
|
| 641 |
// let's drain the iterator to test peek() at the end |
| 642 |
while iter.next().is_some() {} |
| 643 |
assert_eq!(iter.peek(), None); |
| 644 |
} |
| 645 |
|
| 646 |
#[test] |
| 647 |
fn test_empty() { |
| 648 |
let mut iter = AncestorsIterator::new( |
| 649 |
SampleGraph, |
| 650 |
vec![10.into()], |
| 651 |
0.into(), |
| 652 |
true, |
| 653 |
) |
| 654 |
.unwrap(); |
| 655 |
assert!(!iter.is_empty()); |
| 656 |
while iter.next().is_some() {} |
| 657 |
assert!(!iter.is_empty()); |
| 658 |
|
| 659 |
let iter = AncestorsIterator::new(SampleGraph, vec![], 0.into(), true) |
| 660 |
.unwrap(); |
| 661 |
assert!(iter.is_empty()); |
| 662 |
|
| 663 |
// case where iter.seen == {NULL_REVISION} |
| 664 |
let iter = AncestorsIterator::new( |
| 665 |
SampleGraph, |
| 666 |
vec![0.into()], |
| 667 |
0.into(), |
| 668 |
false, |
| 669 |
) |
| 670 |
.unwrap(); |
| 671 |
assert!(iter.is_empty()); |
| 672 |
} |
| 673 |
|
| 674 |
/// A corrupted Graph, supporting error handling tests |
| 675 |
#[derive(Clone, Debug)] |
| 676 |
struct Corrupted; |
| 677 |
|
| 678 |
impl Graph for Corrupted { |
| 679 |
// FIXME what to do about this? Are we just not supposed to get them |
| 680 |
// anymore? |
| 681 |
fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError> { |
| 682 |
match rev { |
| 683 |
Revision(1) => Ok([0.into(), (-1).into()]), |
| 684 |
r => Err(GraphError::ParentOutOfRange(r)), |
| 685 |
} |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
#[test] |
| 690 |
fn test_initrev_out_of_range() { |
| 691 |
// inclusive=false looks up initrev's parents right away |
| 692 |
match AncestorsIterator::new( |
| 693 |
SampleGraph, |
| 694 |
vec![25.into()], |
| 695 |
0.into(), |
| 696 |
false, |
| 697 |
) { |
| 698 |
Ok(_) => panic!("Should have been ParentOutOfRange"), |
| 699 |
Err(e) => assert_eq!(e, GraphError::ParentOutOfRange(25.into())), |
| 700 |
} |
| 701 |
} |
| 702 |
|
| 703 |
#[test] |
| 704 |
fn test_next_out_of_range() { |
| 705 |
// inclusive=false looks up initrev's parents right away |
| 706 |
let mut iter = |
| 707 |
AncestorsIterator::new(Corrupted, vec![1.into()], 0.into(), false) |
| 708 |
.unwrap(); |
| 709 |
assert_eq!( |
| 710 |
iter.next(), |
| 711 |
Some(Err(GraphError::ParentOutOfRange(0.into()))) |
| 712 |
); |
| 713 |
} |
| 714 |
|
| 715 |
#[test] |
| 716 |
/// Test constructor, add/get bases and heads |
| 717 |
fn test_missing_bases() -> Result<(), GraphError> { |
| 718 |
let mut missing_ancestors = MissingAncestors::new( |
| 719 |
SampleGraph, |
| 720 |
[5.into(), 3.into(), 1.into(), 3.into()].iter().cloned(), |
| 721 |
); |
| 722 |
let mut as_vec: Vec<Revision> = |
| 723 |
missing_ancestors.get_bases().iter().cloned().collect(); |
| 724 |
as_vec.sort_unstable(); |
| 725 |
assert_eq!(as_vec, [1, 3, 5]); |
| 726 |
assert_eq!(missing_ancestors.max_base, 5); |
| 727 |
|
| 728 |
missing_ancestors |
| 729 |
.add_bases([3.into(), 7.into(), 8.into()].iter().cloned()); |
| 730 |
as_vec = missing_ancestors.get_bases().iter().cloned().collect(); |
| 731 |
as_vec.sort_unstable(); |
| 732 |
assert_eq!(as_vec, [1, 3, 5, 7, 8]); |
| 733 |
assert_eq!(missing_ancestors.max_base, 8); |
| 734 |
|
| 735 |
as_vec = missing_ancestors.bases_heads()?.iter().cloned().collect(); |
| 736 |
as_vec.sort_unstable(); |
| 737 |
assert_eq!(as_vec, [3, 5, 7, 8]); |
| 738 |
Ok(()) |
| 739 |
} |
| 740 |
|
| 741 |
fn assert_missing_remove( |
| 742 |
bases: &[BaseRevision], |
| 743 |
revs: &[BaseRevision], |
| 744 |
expected: &[BaseRevision], |
| 745 |
) { |
| 746 |
let mut missing_ancestors = MissingAncestors::new( |
| 747 |
SampleGraph, |
| 748 |
bases.iter().map(|r| Revision(*r)), |
| 749 |
); |
| 750 |
let mut revset: HashSet<Revision> = |
| 751 |
revs.iter().map(|r| Revision(*r)).collect(); |
| 752 |
missing_ancestors.remove_ancestors_from(&mut revset).unwrap(); |
| 753 |
let mut as_vec: Vec<Revision> = revset.into_iter().collect(); |
| 754 |
as_vec.sort_unstable(); |
| 755 |
assert_eq!(as_vec.as_slice(), expected); |
| 756 |
} |
| 757 |
|
| 758 |
#[test] |
| 759 |
fn test_missing_remove() { |
| 760 |
assert_missing_remove( |
| 761 |
&[1, 2, 3, 4, 7], |
| 762 |
Vec::from_iter(1..10).as_slice(), |
| 763 |
&[5, 6, 8, 9], |
| 764 |
); |
| 765 |
assert_missing_remove(&[10], &[11, 12, 13, 14], &[11, 12, 13, 14]); |
| 766 |
assert_missing_remove(&[7], &[1, 2, 3, 4, 5], &[3, 5]); |
| 767 |
} |
| 768 |
|
| 769 |
fn assert_missing_ancestors( |
| 770 |
bases: &[BaseRevision], |
| 771 |
revs: &[BaseRevision], |
| 772 |
expected: &[BaseRevision], |
| 773 |
) { |
| 774 |
let mut missing_ancestors = MissingAncestors::new( |
| 775 |
SampleGraph, |
| 776 |
bases.iter().map(|r| Revision(*r)), |
| 777 |
); |
| 778 |
let missing = missing_ancestors |
| 779 |
.missing_ancestors(revs.iter().map(|r| Revision(*r))) |
| 780 |
.unwrap(); |
| 781 |
assert_eq!(missing.as_slice(), expected); |
| 782 |
} |
| 783 |
|
| 784 |
#[test] |
| 785 |
fn test_missing_ancestors() { |
| 786 |
// examples taken from test-ancestors.py by having it run |
| 787 |
// on the same graph (both naive and fast Python algs) |
| 788 |
assert_missing_ancestors(&[10], &[11], &[3, 7, 11]); |
| 789 |
assert_missing_ancestors(&[11], &[10], &[5, 10]); |
| 790 |
assert_missing_ancestors(&[7], &[9, 11], &[3, 6, 9, 11]); |
| 791 |
} |
| 792 |
|
| 793 |
/// An interesting case found by a random generator similar to |
| 794 |
/// the one in test-ancestor.py. An early version of Rust MissingAncestors |
| 795 |
/// failed this, yet none of the integration tests of the whole suite |
| 796 |
/// catched it. |
| 797 |
#[allow(clippy::unnecessary_cast)] |
| 798 |
#[test] |
| 799 |
fn test_remove_ancestors_from_case1() { |
| 800 |
const FAKE_NULL_REVISION: BaseRevision = -1; |
| 801 |
assert_eq!(FAKE_NULL_REVISION, NULL_REVISION.0); |
| 802 |
let graph: VecGraph = vec![ |
| 803 |
[FAKE_NULL_REVISION, FAKE_NULL_REVISION], |
| 804 |
[0, FAKE_NULL_REVISION], |
| 805 |
[1, 0], |
| 806 |
[2, 1], |
| 807 |
[3, FAKE_NULL_REVISION], |
| 808 |
[4, FAKE_NULL_REVISION], |
| 809 |
[5, 1], |
| 810 |
[2, FAKE_NULL_REVISION], |
| 811 |
[7, FAKE_NULL_REVISION], |
| 812 |
[8, FAKE_NULL_REVISION], |
| 813 |
[9, FAKE_NULL_REVISION], |
| 814 |
[10, 1], |
| 815 |
[3, FAKE_NULL_REVISION], |
| 816 |
[12, FAKE_NULL_REVISION], |
| 817 |
[13, FAKE_NULL_REVISION], |
| 818 |
[14, FAKE_NULL_REVISION], |
| 819 |
[4, FAKE_NULL_REVISION], |
| 820 |
[16, FAKE_NULL_REVISION], |
| 821 |
[17, FAKE_NULL_REVISION], |
| 822 |
[18, FAKE_NULL_REVISION], |
| 823 |
[19, 11], |
| 824 |
[20, FAKE_NULL_REVISION], |
| 825 |
[21, FAKE_NULL_REVISION], |
| 826 |
[22, FAKE_NULL_REVISION], |
| 827 |
[23, FAKE_NULL_REVISION], |
| 828 |
[2, FAKE_NULL_REVISION], |
| 829 |
[3, FAKE_NULL_REVISION], |
| 830 |
[26, 24], |
| 831 |
[27, FAKE_NULL_REVISION], |
| 832 |
[28, FAKE_NULL_REVISION], |
| 833 |
[12, FAKE_NULL_REVISION], |
| 834 |
[1, FAKE_NULL_REVISION], |
| 835 |
[1, 9], |
| 836 |
[32, FAKE_NULL_REVISION], |
| 837 |
[33, FAKE_NULL_REVISION], |
| 838 |
[34, 31], |
| 839 |
[35, FAKE_NULL_REVISION], |
| 840 |
[36, 26], |
| 841 |
[37, FAKE_NULL_REVISION], |
| 842 |
[38, FAKE_NULL_REVISION], |
| 843 |
[39, FAKE_NULL_REVISION], |
| 844 |
[40, FAKE_NULL_REVISION], |
| 845 |
[41, FAKE_NULL_REVISION], |
| 846 |
[42, 26], |
| 847 |
[0, FAKE_NULL_REVISION], |
| 848 |
[44, FAKE_NULL_REVISION], |
| 849 |
[45, 4], |
| 850 |
[40, FAKE_NULL_REVISION], |
| 851 |
[47, FAKE_NULL_REVISION], |
| 852 |
[36, 0], |
| 853 |
[49, FAKE_NULL_REVISION], |
| 854 |
[FAKE_NULL_REVISION, FAKE_NULL_REVISION], |
| 855 |
[51, FAKE_NULL_REVISION], |
| 856 |
[52, FAKE_NULL_REVISION], |
| 857 |
[53, FAKE_NULL_REVISION], |
| 858 |
[14, FAKE_NULL_REVISION], |
| 859 |
[55, FAKE_NULL_REVISION], |
| 860 |
[15, FAKE_NULL_REVISION], |
| 861 |
[23, FAKE_NULL_REVISION], |
| 862 |
[58, FAKE_NULL_REVISION], |
| 863 |
[59, FAKE_NULL_REVISION], |
| 864 |
[2, FAKE_NULL_REVISION], |
| 865 |
[61, 59], |
| 866 |
[62, FAKE_NULL_REVISION], |
| 867 |
[63, FAKE_NULL_REVISION], |
| 868 |
[FAKE_NULL_REVISION, FAKE_NULL_REVISION], |
| 869 |
[65, FAKE_NULL_REVISION], |
| 870 |
[66, FAKE_NULL_REVISION], |
| 871 |
[67, FAKE_NULL_REVISION], |
| 872 |
[68, FAKE_NULL_REVISION], |
| 873 |
[37, 28], |
| 874 |
[69, 25], |
| 875 |
[71, FAKE_NULL_REVISION], |
| 876 |
[72, FAKE_NULL_REVISION], |
| 877 |
[50, 2], |
| 878 |
[74, FAKE_NULL_REVISION], |
| 879 |
[12, FAKE_NULL_REVISION], |
| 880 |
[18, FAKE_NULL_REVISION], |
| 881 |
[77, FAKE_NULL_REVISION], |
| 882 |
[78, FAKE_NULL_REVISION], |
| 883 |
[79, FAKE_NULL_REVISION], |
| 884 |
[43, 33], |
| 885 |
[81, FAKE_NULL_REVISION], |
| 886 |
[82, FAKE_NULL_REVISION], |
| 887 |
[83, FAKE_NULL_REVISION], |
| 888 |
[84, 45], |
| 889 |
[85, FAKE_NULL_REVISION], |
| 890 |
[86, FAKE_NULL_REVISION], |
| 891 |
[FAKE_NULL_REVISION, FAKE_NULL_REVISION], |
| 892 |
[88, FAKE_NULL_REVISION], |
| 893 |
[FAKE_NULL_REVISION, FAKE_NULL_REVISION], |
| 894 |
[76, 83], |
| 895 |
[44, FAKE_NULL_REVISION], |
| 896 |
[92, FAKE_NULL_REVISION], |
| 897 |
[93, FAKE_NULL_REVISION], |
| 898 |
[9, FAKE_NULL_REVISION], |
| 899 |
[95, 67], |
| 900 |
[96, FAKE_NULL_REVISION], |
| 901 |
[97, FAKE_NULL_REVISION], |
| 902 |
[FAKE_NULL_REVISION, FAKE_NULL_REVISION], |
| 903 |
] |
| 904 |
.into_iter() |
| 905 |
.map(|[a, b]| [Revision(a), Revision(b)]) |
| 906 |
.collect(); |
| 907 |
let problem_rev = 28.into(); |
| 908 |
let problem_base = 70.into(); |
| 909 |
// making the problem obvious: problem_rev is a parent of problem_base |
| 910 |
assert_eq!(graph.parents(problem_base).unwrap()[1], problem_rev); |
| 911 |
|
| 912 |
let mut missing_ancestors: MissingAncestors<VecGraph> = |
| 913 |
MissingAncestors::new( |
| 914 |
graph, |
| 915 |
[60, 26, 70, 3, 96, 19, 98, 49, 97, 47, 1, 6] |
| 916 |
.iter() |
| 917 |
.map(|r| Revision(*r)), |
| 918 |
); |
| 919 |
assert!(missing_ancestors.bases.contains(&problem_base)); |
| 920 |
|
| 921 |
let mut revs: HashSet<Revision> = |
| 922 |
[4, 12, 41, 28, 68, 38, 1, 30, 56, 44] |
| 923 |
.iter() |
| 924 |
.map(|r| Revision(*r)) |
| 925 |
.collect(); |
| 926 |
missing_ancestors.remove_ancestors_from(&mut revs).unwrap(); |
| 927 |
assert!(!revs.contains(&problem_rev)); |
| 928 |
} |
| 929 |
} |