खोज…


एक फ़ंक्शन का परीक्षण करें

fn to_test(output: bool) -> bool {
    output
}

#[cfg(test)] // The module is only compiled when testing.
mod test {
    use super::to_test;

    // This function is a test function. It will be executed and
    // the test will succeed if the function exits cleanly.
    #[test]
    fn test_to_test_ok() {
        assert_eq!(to_test(true), true);
    }

    // That test on the other hand will only succeed when the function
    // panics.
    #[test]
    #[should_panic]
    fn test_to_test_fail() {
        assert_eq!(to_test(true), false);
    }
}

( खेल का मैदान लिंक )

cargo test साथ चलाएं।

एकीकरण टेस्ट

lib.rs :

pub fn to_test(output: bool) -> bool {
    output
}

tests/ फ़ोल्डर में प्रत्येक फ़ाइल को एक टोकरा के रूप में संकलित किया गया है। tests/integration_test.rs

extern crate test_lib;
use test_lib::to_test;

#[test]
fn test_to_test(){
    assert_eq!(to_test(true), true);
}

बेंचमार्क परीक्षण

बेंचमार्क परीक्षणों के साथ आप कोड की गति का परीक्षण और माप कर सकते हैं, हालाँकि बेंचमार्क परीक्षण अभी भी अस्थिर हैं। अपने कार्गो प्रोजेक्ट में बेंचमार्क को सक्षम करने के लिए आपको रात के जंग की आवश्यकता होती है, अपने एकीकरण बेंचमार्क परीक्षणों को अपने कार्गो प्रोजेक्ट की जड़ में benches/ फ़ोल्डर में डालें, और cargo bench चलाएं।

उदाहरण llogiq.github.io से

extern crate test;
extern crate rand;

use test::Bencher;
use rand::Rng;
use std::mem::replace;

#[bench]
fn empty(b: &mut Bencher) {
    b.iter(|| 1)
}

#[bench]
fn setup_random_hashmap(b: &mut Bencher) {
    let mut val : u32 = 0;
    let mut rng = rand::IsaacRng::new_unseeded();
    let mut map = std::collections::HashMap::new();

    b.iter(|| { map.insert(rng.gen::<u8>() as usize, val); val += 1; })
}

#[bench]
fn setup_random_vecmap(b: &mut Bencher) {
    let mut val : u32 = 0;
    let mut rng = rand::IsaacRng::new_unseeded();
    let mut map = std::collections::VecMap::new();

    b.iter(|| { map.insert((rng.gen::<u8>()) as usize, val); val += 1; })
}

#[bench]
fn setup_random_vecmap_cap(b: &mut Bencher) {
    let mut val : u32 = 0;
    let mut rng = rand::IsaacRng::new_unseeded();
    let mut map = std::collections::VecMap::with_capacity(256);

    b.iter(|| { map.insert((rng.gen::<u8>()) as usize, val); val += 1; })
}


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow