#[cfg(test)]
mod comparator_tests {
    use crate::utils::{db_put_simple, tmpdir};
    use leveldb::comparator::{Comparator, OrdComparator};
    use leveldb::database::Database;
    use leveldb::database::Slice;
    use leveldb::iterator::{EntryIterator, Iterable};
    use leveldb::options::{Options, ReadOptions};
    use libc::c_char;
    use std::cmp::Ordering;

    struct ReverseComparator;

    impl Comparator for ReverseComparator {
        fn name(&self) -> *const c_char {
            "reverse".as_ptr() as *const c_char
        }

        fn compare(&self, a: &Slice<'_>, b: &Slice<'_>) -> Ordering {
            b.as_bytes().cmp(a.as_bytes())
        }
    }

    #[test]
    fn test_comparator() {
        let comparator = ReverseComparator;
        let mut opts = Options::new();
        opts.create_if_missing = true;
        opts.set_comparator(comparator);
        let tmp = tmpdir("reverse_comparator");
        let database = &mut Database::open(tmp.path(), opts).unwrap();
        db_put_simple(database, &[1], &[1]);
        db_put_simple(database, &[2], &[2]);

        let read_opts = ReadOptions::new();
        let mut iter: EntryIterator<'_> = database.iter(read_opts);

        let (key1, val1) = iter.next().unwrap();
        let (key2, val2) = iter.next().unwrap();
        assert_eq!(key1.as_bytes(), &[2]);
        assert_eq!(val1.as_bytes(), &[2]);
        assert_eq!(key2.as_bytes(), &[1]);
        assert_eq!(val2.as_bytes(), &[1]);
    }

    #[test]
    fn test_ord_comparator() {
        let comparator = OrdComparator::new("foo");
        let mut opts = Options::new();
        opts.create_if_missing = true;
        opts.set_comparator(comparator);
        let tmp = tmpdir("ord_comparator");
        let database = &mut Database::open(tmp.path(), opts).unwrap();
        db_put_simple(database, &[1], &[1]);
        db_put_simple(database, &[2], &[2]);

        let read_opts = ReadOptions::new();
        let mut iter: EntryIterator<'_> = database.iter(read_opts);

        let (key1, val1) = iter.next().unwrap();
        let (key2, val2) = iter.next().unwrap();
        assert_eq!(key1.as_bytes(), &[1]);
        assert_eq!(val1.as_bytes(), &[1]);
        assert_eq!(key2.as_bytes(), &[2]);
        assert_eq!(val2.as_bytes(), &[2]);
    }
}
