pub fn somar(a: i32, b: i32) -> i32 {
a + b
}
pub fn subtrair(a: i32, b: i32) -> i32 {
a - b
}
pub fn multiplicar(a: i32, b: i32) -> i32 {
a * b
}
pub fn dividir(a: f64, b: f64) -> Result<f64, String> {
if b == 0.0 {
Err("Divisão por zero não é permitida".to_string())
} else {
Ok(a / b)
}
}
pub fn potencia(base: f64, expoente: f64) -> f64 {
base.powf(expoente)
}
pub fn logaritmo(numero: f64, base: f64) -> Result<f64, String> {
if numero <= 0.0 {
Err("Logaritmo não é definido para números menores ou iguais a zero".to_string())
} else if base <= 0.0 || base == 1.0 {
Err("Base do logaritmo deve ser positiva e diferente de 1".to_string())
} else {
Ok(numero.log(base))
}
}
pub fn funcao_com_bug(x: i32) -> i32 {
x * 2
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn teste_soma_positivos() {
assert_eq!(somar(2, 3), 5);
}
#[test]
fn teste_soma_negativos() {
assert_eq!(somar(-2, -3), -5);
}
#[test]
fn teste_soma_zero() {
assert_eq!(somar(5, 0), 5);
assert_eq!(somar(0, 5), 5);
}
#[test]
fn teste_subtracao_positivos() {
assert_eq!(subtrair(10, 3), 7);
}
#[test]
fn teste_subtracao_negativos() {
assert_eq!(subtrair(-5, -2), -3);
}
#[test]
fn teste_subtracao_resultado_negativo() {
assert_eq!(subtrair(3, 10), -7);
}
#[test]
fn teste_multiplicacao_positivos() {
assert_eq!(multiplicar(4, 5), 20);
}
#[test]
fn teste_multiplicacao_por_zero() {
assert_eq!(multiplicar(100, 0), 0);
assert_eq!(multiplicar(0, 50), 0);
}
#[test]
fn teste_multiplicacao_negativos() {
assert_eq!(multiplicar(-3, 4), -12);
assert_eq!(multiplicar(-2, -6), 12);
}
#[test]
fn teste_divisao_normal() {
assert_eq!(dividir(10.0, 2.0), Ok(5.0));
assert_eq!(dividir(15.0, 3.0), Ok(5.0));
}
#[test]
fn teste_divisao_por_zero() {
assert_eq!(dividir(10.0, 0.0), Err("Divisão por zero não é permitida".to_string()));
}
#[test]
fn teste_potencia_positivos() {
assert_eq!(potencia(2.0, 3.0), 8.0);
assert_eq!(potencia(5.0, 2.0), 25.0);
}
#[test]
fn teste_potencia_zero() {
assert_eq!(potencia(5.0, 0.0), 1.0);
assert_eq!(potencia(0.0, 5.0), 0.0);
}
#[test]
fn teste_potencia_negativa() {
assert_eq!(potencia(2.0, -2.0), 0.25);
}
#[test]
fn teste_logaritmo_normal() {
assert_eq!(logaritmo(8.0, 2.0), Ok(3.0));
assert_eq!(logaritmo(100.0, 10.0), Ok(2.0));
}
#[test]
fn teste_logaritmo_numero_negativo() {
assert_eq!(logaritmo(-5.0, 10.0), Err("Logaritmo não é definido para números menores ou iguais a zero".to_string()));
}
#[test]
fn teste_logaritmo_base_invalida() {
assert_eq!(logaritmo(10.0, 1.0), Err("Base do logaritmo deve ser positiva e diferente de 1".to_string()));
assert_eq!(logaritmo(10.0, -2.0), Err("Base do logaritmo deve ser positiva e diferente de 1".to_string()));
}
#[test]
#[should_panic]
fn teste_que_deveria_falhar_mas_passa() {
assert_eq!(somar(2, 2), 4);
}
#[test]
fn teste_funcao_com_bug() {
assert_eq!(funcao_com_bug(5), 10); }
#[test]
fn teste_soma_que_quebra() {
assert_eq!(somar(2, 3), 5); }
#[test]
fn teste_multiplicacao_que_quebra() {
assert_eq!(multiplicar(3, 4), 12); }
}