use textdb::{accessor, maps, Table}; #[test] fn test_tsv_text() { type Res<'a> = Result, std::str::Utf8Error>; let accessor = accessor::TsvText::<0>::default(); let map = maps::SafeMemoryMap::from_str("A\nB\nC"); let textdb = Table::new(map, accessor); assert_eq!(textdb.keys().collect::().unwrap(), ["A", "B", "C"]); assert_eq!(textdb.cols(0).collect::().unwrap(), ["A", "B", "C"]); assert_eq!(textdb.cols(1).collect::().unwrap(), ["", "", ""]); assert!(textdb.is_sorted().unwrap()); let map = maps::SafeMemoryMap::from_str("A\nB\nC\n"); let textdb = Table::new(map, accessor); assert!(textdb.is_sorted().unwrap()); assert_eq!(textdb.keys().collect::().unwrap(), ["A", "B", "C"]); assert_eq!(textdb.cols(1).collect::().unwrap(), ["", "", ""]); let map = maps::SafeMemoryMap::from_str("B\nA\nC"); let textdb = Table::new(map, accessor); assert_eq!(textdb.cols(1).collect::().unwrap(), ["", "", ""]); assert!(!textdb.is_sorted().unwrap()); assert_eq!(textdb.keys().collect::().unwrap(), ["B", "A", "C"]); assert_eq!(textdb.cols(0).collect::().unwrap(), ["B", "A", "C"]); assert_eq!(textdb.cols(1).collect::().unwrap(), ["", "", ""]); let map = maps::SafeMemoryMap::from_str("A\t1\nB\t2\nC\t3"); let textdb = Table::new(map, accessor); assert!(textdb.is_sorted().unwrap()); assert_eq!(textdb.keys().collect::().unwrap(), ["A", "B", "C"]); assert_eq!(textdb.cols(0).collect::().unwrap(), ["A", "B", "C"]); assert_eq!(textdb.cols(1).collect::().unwrap(), ["1", "2", "3"]); } #[test] fn test_tsv_parse() { // Not sorted by text let accessor = accessor::TsvText::<0>::default(); let map = maps::SafeMemoryMap::from_str("10\n0100\n"); let textdb = Table::new(map, accessor); assert!(!textdb.is_sorted().unwrap()); // Sorted by number let accessor = accessor::TsvParse::::default(); let map = maps::SafeMemoryMap::from_str("10\n0100\n"); let textdb = Table::new(map, accessor); assert!(textdb.is_sorted().unwrap()); } #[test] fn test_get_matching_lines_a() { let text = "A\nB\nC\nC\nD\nE\nF\nF\nF\nF\nF\nG\nH\nI\nJ\nK\nL\n"; let textdb = Table::text_tsv_from_str(text); assert!(textdb.is_sorted().unwrap()); assert_eq!(textdb.get_matching_lines("F".as_bytes()).count(), 5); assert_eq!(textdb.get_matching_lines("C".as_bytes()).count(), 2); } #[test] fn test_get_matching_lines_b() { let text = "F\nF\nF\nF\nF\n"; let textdb = Table::text_tsv_from_str(text); assert!(textdb.is_sorted().unwrap()); assert_eq!(textdb.get_matching_lines("F".as_bytes()).count(), 5); } #[test] fn test_get_matching_lines_1() { let kv = [(6, 6), (10, 1), (113, 2), (113, 5), (129, 3), (140, 0), (168, 7), (205, 9), (211, 8), (215, 4)]; let text = kv.iter().map(|(k, v)| format!("{k}\t{v}")).collect::>().join("\n"); let accessor = accessor::TsvParse::::default(); let map = maps::SafeMemoryMap::from_str(&text); let textdb = Table::new(map, accessor); assert!(textdb.is_sorted().unwrap()); assert_eq!(textdb.get_matching_lines(&113).count(), 2); } #[test] fn test_get_matching_lines_2() { for i in 0..1000 { let mut kv = (0..i).map(|i| { let key : u8 = rand::random(); (key, i) }).collect::>(); kv.sort(); let text = kv.iter().map(|(k, v)| format!("{k}\t{v}")).collect::>().join("\n"); // println!("{kv:?} {text}"); let accessor = accessor::TsvParse::::default(); let map = maps::SafeMemoryMap::from_str(&text); let textdb = Table::new(map, accessor); assert!(textdb.is_sorted().unwrap()); for (k, _v) in kv { // println!("finding k={k}"); for line in textdb.get_matching_lines(&k) { assert_eq!(line.col(0).unwrap().parse::().unwrap(), k); // assert_eq!(line.col(1).unwrap().parse::().unwrap(), v); // println!("{k} {v} {line}"); } } } }