Skip to content

Commit

Permalink
Add test of GC
Browse files Browse the repository at this point in the history
  • Loading branch information
CAD97 committed Jun 17, 2020
1 parent 0b410b6 commit d0e62e8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/green/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ impl Builder {
Self::default()
}

/// The number of cached elements.
pub fn size(&self) -> usize {
self.nodes.len() + self.tokens.len()
}
}

impl Builder {
/// Create a new node or clone a new Arc to an existing equivalent one.
///
/// This checks children for identity equivalence, not structural,
Expand Down Expand Up @@ -149,7 +156,9 @@ impl Builder {
};
Arc::clone(token)
}
}

impl Builder {
fn turn_node_gc(&mut self) -> bool {
// NB: `drain_filter` is `retain` but with an iterator of the removed elements.
// i.e.: elements where the predicate is FALSE are removed and iterated over.
Expand Down
33 changes: 33 additions & 0 deletions tests/gc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use sorbus::*;

#[test]
fn works_properly() {
let kind0 = Kind(0);
let kind1 = Kind(1);
let kind2 = Kind(2);
let mut builder = green::TreeBuilder::new();

#[rustfmt::skip]
let inner = builder
.start_node(kind1)
.token(kind0, "kind")
.finish_node()
.finish();

#[rustfmt::skip]
let outer = builder
.start_node(kind2)
.add(inner.clone())
.finish_node()
.finish();

assert_eq!(builder.builder().size(), 3);

drop(outer);
builder.builder().gc();
assert_eq!(builder.builder().size(), 2);

drop(inner);
builder.builder().gc();
assert_eq!(builder.builder().size(), 0);
}

0 comments on commit d0e62e8

Please sign in to comment.