Added unit tests

This commit is contained in:
Paul Masurel
2016-07-29 18:38:18 +09:00
parent 6ee776c926
commit a6b2d94119
3 changed files with 65 additions and 4 deletions
+4 -4
View File
@@ -81,12 +81,12 @@ mod tests {
let mut top_collector = TopCollector::with_limit(2);
let mut count_collector = CountCollector::new();
{
let mut chain = collector_chain()
let mut collectors = chain()
.add(&mut top_collector)
.add(&mut count_collector);
chained_collector.collect(ScoredDoc(0.2, 1));
chained_collector.collect(ScoredDoc(0.1, 2));
chained_collector.collect(ScoredDoc(0.5, 3));
collectors.collect(ScoredDoc(0.2, 1));
collectors.collect(ScoredDoc(0.1, 2));
collectors.collect(ScoredDoc(0.5, 3));
}
assert_eq!(count_collector.count(), 3);
assert!(top_collector.at_capacity());
+51
View File
@@ -103,3 +103,54 @@ impl Collector for TopCollector {
}
}
#[cfg(test)]
mod tests {
use super::*;
use ScoredDoc;
use DocId;
use Score;
use collector::Collector;
#[test]
fn test_top_collector_not_at_capacity() {
let mut top_collector = TopCollector::with_limit(4);
top_collector.collect(ScoredDoc(0.8, 1));
top_collector.collect(ScoredDoc(0.2, 3));
top_collector.collect(ScoredDoc(0.3, 5));
assert!(!top_collector.at_capacity());
let score_docs: Vec<(Score, DocId)> = top_collector.score_docs()
.into_iter()
.map(|(score, doc_address)| (score, doc_address.doc()))
.collect();
assert_eq!(score_docs, vec!(
(0.8, 1), (0.3, 5), (0.2, 3),
));
}
#[test]
fn test_top_collector_at_capacity() {
let mut top_collector = TopCollector::with_limit(4);
top_collector.collect(ScoredDoc(0.8, 1));
top_collector.collect(ScoredDoc(0.2, 3));
top_collector.collect(ScoredDoc(0.3, 5));
top_collector.collect(ScoredDoc(0.9, 7));
top_collector.collect(ScoredDoc(-0.2, 9));
assert!(top_collector.at_capacity());
let score_docs: Vec<(Score, DocId)> = top_collector.score_docs()
.into_iter()
.map(|(score, doc_address)| (score, doc_address.doc()))
.collect();
assert_eq!(score_docs, vec!(
(0.9, 7), (0.8, 1), (0.3, 5), (0.2, 3)
));
}
#[test]
#[should_panic]
fn test_top_0() {
TopCollector::with_limit(0);
}
}
+10
View File
@@ -72,6 +72,16 @@ pub type SegmentLocalId = u32;
#[derive(Debug, Clone, Copy)]
pub struct DocAddress(pub SegmentLocalId, pub DocId);
impl DocAddress {
pub fn segment_ord(&self,) -> SegmentLocalId {
self.0
}
pub fn doc(&self,) -> DocId {
self.1
}
}
#[derive(Clone, Copy)]
pub struct ScoredDoc(Score, DocId);