Skip to content

Commit 1bd6a7e

Browse files
committed
test: add doc tests for node table
1 parent 5bf86ae commit 1bd6a7e

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed

src/node_table.rs

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,21 @@ impl<'a> NodeTable<'a> {
8888
///
8989
/// * `Some(time)` if `row` is valid.
9090
/// * `None` otherwise.
91+
///
92+
/// # Examples
93+
///
94+
/// ```
95+
/// # use tskit::prelude::*;
96+
/// # let mut tables = tskit::TableCollection::new(10.).unwrap();
97+
/// # tables.add_node(0, 10.0, -1, -1).unwrap();
98+
/// if let Some(time) = tables.nodes().time(0) {
99+
/// // then node id 0 is a valid row id
100+
/// # assert_eq!(time, 10.0);
101+
/// }
102+
/// # else {
103+
/// # panic!("expected 0 to be a valid row id")
104+
/// # }
105+
/// ```
91106
pub fn time<N: Into<NodeId> + Copy>(&'a self, row: N) -> Option<Time> {
92107
unsafe_tsk_column_access!(row.into().0, 0, self.num_rows(), self.table_.time, Time)
93108
}
@@ -98,12 +113,86 @@ impl<'a> NodeTable<'a> {
98113
///
99114
/// * `Some(flags)` if `row` is valid.
100115
/// * `None` otherwise.
116+
///
117+
/// # Examples
118+
///
119+
/// ```
120+
/// # use tskit::prelude::*;
121+
/// # let mut tables = tskit::TableCollection::new(10.).unwrap();
122+
/// # tables.add_node(tskit::NodeFlags::IS_SAMPLE, 10.0, -1, -1).unwrap();
123+
/// if let Some(flags) = tables.nodes().flags(0) {
124+
/// // then node id 0 is a valid row id
125+
/// # assert!(flags.is_sample());
126+
/// }
127+
/// # else {
128+
/// # panic!("expected 0 to be a valid row id")
129+
/// # }
130+
/// ```
101131
pub fn flags<N: Into<NodeId> + Copy>(&'a self, row: N) -> Option<NodeFlags> {
102132
unsafe_tsk_column_access_and_map_into!(row.into().0, 0, self.num_rows(), self.table_.flags)
103133
}
104134

105135
/// Mutable access to node flags.
106136
///
137+
/// # Examples
138+
///
139+
/// For a [`crate::TableCollection`], accessing the table creates a temporary
140+
/// that will be dropped, causing this code to not compile:
141+
///
142+
/// ```compile_fail
143+
/// # use tskit::prelude::*;
144+
/// # let mut tables = tskit::TableCollection::new(10.).unwrap();
145+
/// # tables.add_node(tskit::NodeFlags::IS_SAMPLE, 10.0, -1, -1).unwrap();
146+
/// let flags = tables.nodes().flags_array_mut();
147+
/// println!("{}", flags.len()); // ERROR: the temporary node table is dropped by now
148+
/// ```
149+
///
150+
/// Treating the returned slice as an iterable succeeds:
151+
///
152+
/// ```
153+
/// # use tskit::prelude::*;
154+
/// # let mut tables = tskit::TableCollection::new(10.).unwrap();
155+
/// # tables.add_node(tskit::NodeFlags::IS_SAMPLE, 10.0, -1, -1).unwrap();
156+
/// for flag in tables.nodes().flags_array_mut() {
157+
/// # assert!(flag.is_sample());
158+
/// }
159+
/// ```
160+
///
161+
/// The returned slice is *mutable*, allowing one to do things like
162+
/// clear the sample status of all nodes:
163+
///
164+
/// A copy of the flags can be obtained by collecting results into `Vec`:
165+
///
166+
/// ```
167+
/// # use tskit::prelude::*;
168+
/// # let mut tables = tskit::TableCollection::new(10.).unwrap();
169+
/// # tables.add_node(tskit::NodeFlags::IS_SAMPLE, 10.0, -1, -1).unwrap();
170+
/// for flag in tables.nodes().flags_array_mut() {
171+
/// flag.remove(tskit::NodeFlags::IS_SAMPLE);
172+
/// }
173+
/// assert!(!tables.nodes().flags_array_mut().iter().any(|f| f.is_sample()));
174+
/// ```
175+
///
176+
/// ```
177+
/// # use tskit::prelude::*;
178+
/// # let mut tables = tskit::TableCollection::new(10.).unwrap();
179+
/// # tables.add_node(tskit::NodeFlags::IS_SAMPLE, 10.0, -1, -1).unwrap();
180+
/// let flags = tables.nodes().flags_array_mut().to_vec();
181+
/// # assert!(flags.iter().all(|f| f.is_sample()));
182+
/// ```
183+
///
184+
/// ## Standalone tables
185+
///
186+
/// The ownership semantics differ when tables are not part of a
187+
/// table collection:
188+
///
189+
/// ```
190+
/// // let mut nodes = tskit::OwnedNodeTable::default();
191+
/// // assert!(nodes.add_row(tskit::NodeFlags::IS_SAMPLE, 10., -1, -1).is_ok());
192+
/// // let flags = nodes.flags_array_mut();
193+
/// // assert!(flags.iter().all(|f| f.is_sample()));
194+
/// ```
195+
///
107196
/// # Note
108197
///
109198
/// Internally, we rely on a conversion of u64 to usize.
@@ -120,6 +209,31 @@ impl<'a> NodeTable<'a> {
120209

121210
/// Mutable access to node times.
122211
///
212+
/// # Examples
213+
///
214+
/// For a [`crate::TableCollection`], accessing the table creates a temporary
215+
/// that will be dropped, causing this code to not compile:
216+
///
217+
/// ```compile_fail
218+
/// # use tskit::prelude::*;
219+
/// # let mut tables = tskit::TableCollection::new(10.).unwrap();
220+
/// # tables.add_node(tskit::NodeFlags::IS_SAMPLE, 10.0, -1, -1).unwrap();
221+
/// let time = tables.nodes().time_array_mut();
222+
/// println!("{}", time.len()); // ERROR: the temporary node table is dropped by now
223+
/// ```
224+
///
225+
/// Treating the returned slice as an iterable succeeds:
226+
///
227+
/// ```
228+
/// # use tskit::prelude::*;
229+
/// # let mut tables = tskit::TableCollection::new(10.).unwrap();
230+
/// # tables.add_node(tskit::NodeFlags::IS_SAMPLE, 10.0, -1, -1).unwrap();
231+
/// for time in tables.nodes().time_array_mut() {
232+
/// *time = 55.0.into(); // change each node's time value
233+
/// }
234+
/// assert!(tables.nodes().time_array_mut().iter().all(|t| t == &55.0));
235+
/// ```
236+
///
123237
/// # Note
124238
///
125239
/// Internally, we rely on a conversion of u64 to usize.
@@ -136,6 +250,21 @@ impl<'a> NodeTable<'a> {
136250

137251
/// Return the ``population`` value from row ``row`` of the table.
138252
///
253+
/// # Examples
254+
///
255+
/// ```
256+
/// # use tskit::prelude::*;
257+
/// # let mut tables = tskit::TableCollection::new(10.).unwrap();
258+
/// # tables.add_node(0, 10.0, -1, -1).unwrap();
259+
/// if let Some(pop) = tables.nodes().population(0) {
260+
/// // then node id 0 is a valid row id
261+
/// # assert!(pop.is_null());
262+
/// }
263+
/// # else {
264+
/// # panic!("expected 0 to be a valid row id")
265+
/// # }
266+
/// ```
267+
///
139268
/// # Returns
140269
///
141270
/// * `Some(population)` if `row` is valid.
@@ -152,6 +281,10 @@ impl<'a> NodeTable<'a> {
152281

153282
/// Return the ``population`` value from row ``row`` of the table.
154283
///
284+
/// # Examples
285+
///
286+
/// See [`NodeTable::population`] for examples.
287+
///
155288
/// # Returns
156289
///
157290
/// * `Some(population)` if `row` is valid.
@@ -162,6 +295,21 @@ impl<'a> NodeTable<'a> {
162295

163296
/// Return the ``individual`` value from row ``row`` of the table.
164297
///
298+
/// # Examples
299+
///
300+
/// ```
301+
/// # use tskit::prelude::*;
302+
/// # let mut tables = tskit::TableCollection::new(10.).unwrap();
303+
/// # tables.add_node(0, 10.0, -1, -1).unwrap();
304+
/// if let Some(individual) = tables.nodes().individual(0) {
305+
/// // then node id 0 is a valid row id
306+
/// # assert!(individual.is_null());
307+
/// }
308+
/// # else {
309+
/// # panic!("expected 0 to be a valid row id")
310+
/// # }
311+
/// ```
312+
///
165313
/// # Returns
166314
///
167315
/// * `Some(individual)` if `row` is valid.

0 commit comments

Comments
 (0)