From 93e7f28cc085827d65e28ce00a1240a60273753e Mon Sep 17 00:00:00 2001 From: Paul Masurel Date: Wed, 14 Jun 2017 10:46:06 +0900 Subject: [PATCH] Added unit test --- src/common/bitpacker.rs | 30 +++++++++++++++++++++++------- src/fastfield/mod.rs | 6 +++++- src/fastfield/reader.rs | 13 +++++++++++++ 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/common/bitpacker.rs b/src/common/bitpacker.rs index 5df1e7222..49ea9f9e6 100644 --- a/src/common/bitpacker.rs +++ b/src/common/bitpacker.rs @@ -133,23 +133,21 @@ impl BitUnpacker for val in output.iter_mut() { *val = 0; } - } - else { + } else { let data: &[u8] = &*self.data; let num_bits = self.num_bits; let mask = self.mask; - let mut addr_in_bits = (start as usize) * num_bits; - for i in 0..output.len() { + for output_val in output.iter_mut() { let addr = addr_in_bits >> 3; let bit_shift = addr_in_bits & 7; let val_unshifted_unmasked: u64 = unsafe { *(data[addr..].as_ptr() as *const u64) }; let val_shifted = (val_unshifted_unmasked >> bit_shift) as u64; - output[i] = val_shifted & mask; + *output_val = val_shifted & mask; addr_in_bits += num_bits; } } - + } } @@ -172,7 +170,7 @@ mod test { assert_eq!(compute_num_bits(5_000_000_000), 33u8); } - fn test_bitpacker_util(len: usize, num_bits: usize) { + fn create_fastfield_bitpacker(len: usize, num_bits: usize) -> (BitUnpacker>, Vec) { let mut data = Vec::new(); let mut bitpacker = BitPacker::new(num_bits); let max_val: u64 = (1 << num_bits) - 1; @@ -185,6 +183,11 @@ mod test { bitpacker.close(&mut data).unwrap(); assert_eq!(data.len(), (num_bits * len + 7) / 8 + 7); let bitunpacker = BitUnpacker::new(data, num_bits); + (bitunpacker, vals) + } + + fn test_bitpacker_util(len: usize, num_bits: usize) { + let (bitunpacker, vals) = create_fastfield_bitpacker(len, num_bits); for (i, val) in vals.iter().enumerate() { assert_eq!(bitunpacker.get(i), *val); } @@ -198,4 +201,17 @@ mod test { test_bitpacker_util(6, 14); test_bitpacker_util(1000, 14); } + + #[test] + fn test_bitpacker_range() { + let (bitunpacker, vals) = create_fastfield_bitpacker(100_000, 12); + let buffer_len = 100; + let mut buffer = vec![0u64; buffer_len]; + for start in vec![0, 10, 20, 100, 1_000] { + bitunpacker.get_range(start as u32, &mut buffer[..]); + for i in 0..buffer_len { + assert_eq!(buffer[i], vals[start + i]); + } + } + } } diff --git a/src/fastfield/mod.rs b/src/fastfield/mod.rs index 9ae143856..ae18705b9 100644 --- a/src/fastfield/mod.rs +++ b/src/fastfield/mod.rs @@ -211,7 +211,6 @@ mod tests { } } - #[test] fn test_signed_intfastfield() { let path = Path::new("test"); @@ -245,6 +244,11 @@ mod tests { for (doc, i) in (-100i64..10_000i64).enumerate() { assert_eq!(fast_field_reader.get(doc as u32), i); } + let mut buffer = vec![0i64; 100]; + fast_field_reader.get_range(53, &mut buffer[..]); + for i in 0..100 { + assert_eq!(buffer[i], -100i64 + 53i64 + i as i64); + } } } diff --git a/src/fastfield/reader.rs b/src/fastfield/reader.rs index 8413527b4..aae1dd797 100644 --- a/src/fastfield/reader.rs +++ b/src/fastfield/reader.rs @@ -28,8 +28,21 @@ pub trait FastFieldReader: Sized { /// Return the value associated to the given document. /// /// This accessor should return as fast as possible. + /// + /// # Panics + /// + /// May panic if `doc` is greater than the segment + // `maxdoc`. fn get(&self, doc: DocId) -> Self::ValueType; + /// Fills an output buffer with the fast field values + /// associated with the `DocId` going from + /// `start` to `start + output.len()`. + /// + /// # Panics + /// + /// May panic if `start + output.len()` is greater than + /// the segment's `maxdoc`. fn get_range(&self, start: u32, output: &mut [Self::ValueType]); /// Opens a fast field given a source.