Skip to content

Commit

Permalink
Merge pull request #126 from myyrakle/test/#124
Browse files Browse the repository at this point in the history
[#124] utils 테스트코드 작성
  • Loading branch information
myyrakle authored Jul 15, 2024
2 parents f095346 + 5e9d59b commit 32dec33
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/utils/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,15 @@ macro_rules! join_vec {
}

pub(crate) use join_vec;

#[cfg(test)]
mod tests {
#[test]
fn test_join_vec() {
let v1 = vec![1, 2, 3];
let v2 = vec![4, 5, 6];

assert_eq!(join_vec!(v1), vec![1, 2, 3]);
assert_eq!(join_vec!(v1, v2), vec![1, 2, 3, 4, 5, 6]);
}
}
144 changes: 144 additions & 0 deletions src/utils/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,147 @@ impl Ord for Float64 {
}
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_float64_hash() {
use super::Float64;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

let mut hasher = DefaultHasher::new();
Float64 { value: 1.0 }.hash(&mut hasher);
let hash1 = hasher.finish();

let mut hasher = DefaultHasher::new();
Float64 { value: 1.0 }.hash(&mut hasher);
let hash2 = hasher.finish();

assert_eq!(hash1, hash2);
}

#[test]
fn test_float64_to_string() {
use super::Float64;

let f = Float64 { value: 1.0 };
assert_eq!(f.to_string(), "1");
}

#[test]
fn test_float64_from_f64() {
use super::Float64;

let f: Float64 = 1.0.into();
assert_eq!(f, Float64 { value: 1.0 });
}

#[test]
fn test_float64_from_float64() {
use super::Float64;

let f: f64 = Float64 { value: 1.0 }.into();
assert_eq!(f, 1.0);
}

#[test]
fn test_float64_neg() {
use super::Float64;

let f = Float64 { value: 1.0 };
assert_eq!(-f, Float64 { value: -1.0 });
}

#[test]
fn test_float64_add() {
use super::Float64;

let f1 = Float64 { value: 1.0 };
let f2 = Float64 { value: 2.0 };
assert_eq!(f1 + f2, Float64 { value: 3.0 });
}

#[test]
fn test_float64_sub() {
use super::Float64;

let f1 = Float64 { value: 1.0 };
let f2 = Float64 { value: 2.0 };
assert_eq!(f1 - f2, Float64 { value: -1.0 });
}

#[test]
fn test_float64_mul() {
use super::Float64;

let f1 = Float64 { value: 1.0 };
let f2 = Float64 { value: 2.0 };
assert_eq!(f1 * f2, Float64 { value: 2.0 });
}

#[test]
fn test_float64_div() {
use super::Float64;

let f1 = Float64 { value: 1.0 };
let f2 = Float64 { value: 2.0 };
assert_eq!(f1 / f2, Float64 { value: 0.5 });
}

#[test]
fn test_float64_eq() {
use super::Float64;

let f1 = Float64 { value: 1.0 };
let f2 = Float64 { value: 1.0 };
assert_eq!(PartialEq::eq(&f1, &f2), true);
}

#[test]
fn test_float64_partial_cmp() {
use super::Float64;

let f1 = Float64 { value: 1.0 };
let f2 = Float64 { value: 2.0 };
assert_eq!(
PartialOrd::partial_cmp(&f1, &f2),
Some(std::cmp::Ordering::Less)
);

let f1 = Float64 { value: 1.0 };
let f2 = Float64 { value: 1.0 };
assert_eq!(
PartialOrd::partial_cmp(&f1, &f2),
Some(std::cmp::Ordering::Equal)
);

let f1 = Float64 { value: 2.0 };
let f2 = Float64 { value: 1.0 };
assert_eq!(
PartialOrd::partial_cmp(&f1, &f2),
Some(std::cmp::Ordering::Greater)
);
}

#[test]
fn test_float64_cmp() {
use super::Float64;

let f1 = Float64 { value: 1.0 };
let f2 = Float64 { value: 2.0 };
assert_eq!(Ord::cmp(&f1, &f2), std::cmp::Ordering::Less);

let f1 = Float64 { value: 1.0 };
let f2 = Float64 { value: 1.0 };
assert_eq!(Ord::cmp(&f1, &f2), std::cmp::Ordering::Equal);

let f1 = Float64 { value: 2.0 };
let f2 = Float64 { value: 1.0 };
assert_eq!(Ord::cmp(&f1, &f2), std::cmp::Ordering::Greater);

let f1 = Float64 { value: f64::NAN };
let f2 = Float64 { value: f64::NAN };
assert_eq!(Ord::cmp(&f1, &f2), std::cmp::Ordering::Less);
}
}
14 changes: 14 additions & 0 deletions src/utils/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,17 @@ pub fn get_profile_path() -> String {

profile_path
}

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

#[test]
fn test_get_profile_path() {
let username = whoami::username();
let user_path = format!("/Users/{}", username);
let profile_path = format!("{}/.zshenv", user_path);

assert_eq!(get_profile_path(), profile_path);
}
}

0 comments on commit 32dec33

Please sign in to comment.