Mercurial > lbo > hg > leveldb-rs
changeset 185:89e7e8629ba5
cmp: Add cmp_inner method and proper tests for InternalKeyCmp.
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Wed, 23 Aug 2017 19:09:22 +0200 |
parents | b3da8e0fe92d |
children | b573f3b55cf2 |
files | src/cmp.rs |
diffstat | 1 files changed, 25 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/src/cmp.rs Fri Aug 18 06:18:25 2017 +0000 +++ b/src/cmp.rs Wed Aug 23 19:09:22 2017 +0200 @@ -121,6 +121,13 @@ } } +impl InternalKeyCmp { + /// cmp_inner compares a and b using the underlying comparator (the "user comparator"). + fn cmp_inner(&self, a: &[u8], b: &[u8]) -> Ordering { + self.0.cmp(a, b) + } +} + /// An internal comparator wrapping a user-supplied comparator. This comparator is used to compare /// memtable keys, which contain length prefixes and a sequence number. /// The ordering is determined by asking the wrapped comparator; ties are broken by *reverse* @@ -249,6 +256,24 @@ } #[test] + fn test_cmp_internalkeycmp() { + let cmp = InternalKeyCmp(Arc::new(Box::new(DefaultCmp))); + // a < b < c + let a = LookupKey::new("abc".as_bytes(), 2).internal_key().to_vec(); + let b = LookupKey::new("abc".as_bytes(), 1).internal_key().to_vec(); + let c = LookupKey::new("abd".as_bytes(), 3).internal_key().to_vec(); + let d = "xyy".as_bytes(); + let e = "xyz".as_bytes(); + + assert_eq!(Ordering::Less, cmp.cmp(&a, &b)); + assert_eq!(Ordering::Equal, cmp.cmp(&a, &a)); + assert_eq!(Ordering::Greater, cmp.cmp(&b, &a)); + assert_eq!(Ordering::Less, cmp.cmp(&a, &c)); + assert_eq!(Ordering::Less, cmp.cmp_inner(d, e)); + assert_eq!(Ordering::Greater, cmp.cmp_inner(e, d)); + } + + #[test] #[should_panic] fn test_cmp_memtablekeycmp_panics() { let cmp = MemtableKeyCmp(Arc::new(Box::new(DefaultCmp)));