File: make.rs

package info (click to toggle)
rust-cdb 0.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 248 kB
  • sloc: makefile: 10; sh: 9
file content (46 lines) | stat: -rw-r--r-- 1,490 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
extern crate cdb;
use std::fs;

macro_rules! noerr {
    ( $e:expr ) => {
        if let Err(x) = $e {
            panic!("{}", x);
        }
    }
}

#[test]
fn test_make() {
    let filename = "/tmp/make.cdb";

    let mut cdb = cdb::CDBWriter::create(filename).unwrap();
    noerr!(cdb.add(b"one", b"Hello"));
    noerr!(cdb.add(b"two", b"Goodbye"));
    noerr!(cdb.add(b"one", b", World!"));
    noerr!(cdb.add(b"this key will be split across two reads", b"Got it."));
    noerr!(cdb.finish());

    let cdb = cdb::CDB::open(filename).unwrap();
    assert_eq!(cdb.find(b"two").next().unwrap().unwrap(), b"Goodbye");
    assert_eq!(cdb.find(b"this key will be split across two reads").next().unwrap().unwrap(), b"Got it.");
    let mut i = cdb.find(b"one");
    assert_eq!(i.next().unwrap().unwrap(), b"Hello");
    assert_eq!(i.next().unwrap().unwrap(), b", World!");

    let mut i = cdb.iter();
    let next = i.next().unwrap().unwrap();
    assert_eq!(next.0, b"one");
    assert_eq!(next.1, b"Hello");
    let next = i.next().unwrap().unwrap();
    assert_eq!(next.0, b"two");
    assert_eq!(next.1, b"Goodbye");
    let next = i.next().unwrap().unwrap();
    assert_eq!(next.0, b"one");
    assert_eq!(next.1, b", World!");
    let next = i.next().unwrap().unwrap();
    // Can't do this key easily due to missing trait for [u8; 39]
    //assert_eq!(next.0, b"this key will be split across two reads");
    assert_eq!(next.1, b"Got it.");

    noerr!(fs::remove_file(filename));
}