Portal/Notes πŸ“
RustRustlings_exercise

Test

Versi Bahasa Indonesia. Baca dalam Bahasa Inggris

  • Untuk mengubah fungsi menjadi fungsi test, tambahkan #[test] pada baris sebelum fn.
  • Kamu dapat menjalankan test menggunakan perintah cargo test.
  • Secara konvensi, fungsi test berada dalam modul bernama tests, yang biasanya ditempatkan di bagian bawah file yang berisi kode yang akan diuji.
  • Kita dapat memeriksa panic dengan menambahkan atribut #[should_panic].
  • Referensi:

tests1.rs

// Tests are important to ensure that your code does what you think it should
// do.

fn is_even(n: i64) -> bool {
    n % 2 == 0
}

fn main() {
    // You can optionally experiment here.
}

#[cfg(test)]
mod tests {
    // Import `is_even`. You can use a wildcard to import everything in
    // the outer module.
    use super::is_even;

    #[test]
    fn you_can_assert() {
        // Test the function `is_even` with some values.
        assert!(is_even(10));
        assert!(!is_even(5));
    }
}
  • Di latihan ini kita perlu menguji fungsi is_even menggunakan assert!.

  • Macro assert menerima tipe bool, akan sukses jika nilainya true dan gagal/panic jika nilainya false.

  • Jadi kita perlu menambahkan dua case untuk input genap dan ganjil seperti ini.

    assert!(is_even(10)); // expect true
    assert!(!is_even(5)); // expect false

test2.rs

// Calculates the power of 2 using a bit shift.
// `1 << n` is equivalent to "2 to the power of n".
fn power_of_2(n: u8) -> u64 {
    1 << n
}

fn main() {
    // You can optionally experiment here.
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn you_can_assert_eq() {
        // Test the function `power_of_2` with some values.
        assert_eq!(power_of_2(0), 1);
        assert_eq!(power_of_2(1), 2);
        assert_eq!(power_of_2(2), 4);
        assert_eq!(power_of_2(3), 8);
    }
}
  • Mirip seperti latihan sebelumnya, kita perlu menambahkan test case untuk fungsi power_of_2.

  • Tapi kali ini menggunakan assert_eq.

    • Ia akan menegaskan jika dua nilai yang diberikan sama.
    • Jika tidak, ia akan gagal/panic.
  • Jadi mari tambahkan test case seperti ini:

    assert_eq!(power_of_2(0), 1);
    assert_eq!(power_of_2(1), 2);
    assert_eq!(power_of_2(2), 4);
    assert_eq!(power_of_2(3), 8);

tests3.rs

struct Rectangle {
    width: i32,
    height: i32,
}

impl Rectangle {
    // Don't change this function.
    fn new(width: i32, height: i32) -> Self {
        if width <= 0 || height <= 0 {
            // Returning a `Result` would be better here. But we want to learn
            // how to test functions that can panic.
            panic!("Rectangle width and height must be positive");
        }

        Rectangle { width, height }
    }
}

fn main() {
    // You can optionally experiment here.
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn correct_width_and_height() {
        // This test should check if the rectangle has the size that we
        // pass to its constructor.
        let rect = Rectangle::new(10, 20);
        assert_eq!(rect.width, 10); // Check width
        assert_eq!(rect.height, 20); // Check height
    }

    // This test should check if the program panics when we try to create
    // a rectangle with negative width.
    #[test]
    #[should_panic]
    fn negative_width() {
        let _rect = Rectangle::new(-10, 10);
    }

    // This test should check if the program panics when we try to create
    // a rectangle with negative height.
    #[test]
    #[should_panic]
    fn negative_height() {
        let _rect = Rectangle::new(10, -10);
    }
}
  • Tugas pertama di latihan ini adalah memperbaiki assert_eq! di fungsi test correct_width_and_height().

  • Kita hanya perlu menempatkan field yang benar seperti ini:

    assert_eq!(rect.width, 10); // Check width
    assert_eq!(rect.height, 20); // Check height
  • Kedua, ada fungsi test negative_width dan negative_height yang memeriksa jika kita memberikan nilai negatif saat memanggil Rectangle::new maka harus panic.

  • Kita dapat memeriksa panic dengan menambahkan atribut #[should_panic].


Edit on GitHub

Last updated on

Test | Faisal Affan