changeset 30:03ea3c83f499

Add tests to map module
author Lewin Bormann <lbo@spheniscida.de>
date Sun, 31 Jan 2016 17:05:33 +0000
parents f11aeab31e37
children ce0ed262a105
files src/map.rs
diffstat 1 files changed, 54 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/map.rs	Sun Jan 31 17:05:14 2016 +0000
+++ b/src/map.rs	Sun Jan 31 17:05:33 2016 +0000
@@ -1,6 +1,8 @@
 //! Implements the mapping phase.
 //!
 
+#![allow(dead_code)]
+
 use std::io::Write;
 use std::collections::{LinkedList, BTreeMap};
 use mapreducer::{Record, MapReducer, MEmitter};
@@ -74,6 +76,9 @@
                 self.insert_result(e);
             }
 
+            if key_buffer.len() < key_buffer_size {
+                break;
+            }
             key_buffer.clear();
         }
     }
@@ -114,3 +119,52 @@
         }
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use closure_mr::ClosureMapReducer;
+    use formats::util::RecordIterator;
+    use map::MapPartition;
+    use mapreducer::{MEmitter, REmitter, Record, MultiRecord};
+    use std::collections::LinkedList;
+    use std::io::Write;
+
+    fn mapper_func(e: &mut MEmitter, r: Record) {
+        for w in r.value.split_whitespace() {
+            e.emit(String::from(w), String::from("1"));
+        }
+    }
+
+    fn reducer_func(_: &mut REmitter, _: MultiRecord) {
+        // no-op
+    }
+
+    fn get_mr() -> ClosureMapReducer {
+        ClosureMapReducer::new(mapper_func, reducer_func)
+    }
+
+    fn get_input() -> LinkedList<Record> {
+        let inp: Vec<String> = vec!["abc def",
+                                    "xy yz za",
+                                    "hello world",
+                                    "let's do this",
+                                    "foo bar baz"]
+                                   .iter()
+                                   .map(move |s| String::from(*s))
+                                   .collect();
+        let ri: RecordIterator<_> = RecordIterator::new(inp.into_iter());
+        ri.collect()
+    }
+
+
+    fn get_output() -> Box<Write> {
+        use formats::lines::LinesWriter;
+        Box::new(LinesWriter::new_to_file(&String::from("mapphase_1.wlg")).unwrap())
+    }
+
+    #[test]
+    fn test_map_partition() {
+        let mp = MapPartition::_new(get_input(), get_mr(), get_output());
+        mp._run();
+    }
+}