use crate::core::geo::point::Point;
use crate::core::util::CoreHelper;
use crate::core::util::error::lucene_error::{LuceneError, LuceneErrorKind, Result};
use crate::test_framework::core::geo::geo_test_util::GeoTestUtil;
use crate::test_framework::core::util::lucene_test_case::random;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
#[allow(dead_code)] struct TestPoint;
#[test]
fn test_invalid_lat() {
let err = Point::new(134.14, 45.23);
assert!(matches!(
err.as_ref().map_err(LuceneError::kind),
Err(LuceneErrorKind::IllegalArgument(_))
));
if let Err(e) = err {
assert!(
e.to_string()
.contains("invalid latitude 134.14; must be between -90 and 90")
);
}
}
#[test]
fn test_invalid_lon() {
let err = Point::new(43.5, 180.5);
assert!(matches!(
err.as_ref().map_err(LuceneError::kind),
Err(LuceneErrorKind::IllegalArgument(_))
));
if let Err(e) = err {
assert!(
e.to_string()
.contains("invalid longitude 180.5; must be between -180 and 180")
);
}
}
#[test]
fn test_equals_and_hash_code() -> Result<()> {
let mut rng = random();
let point = GeoTestUtil::next_point(&mut rng)?;
let copy = Point::new(point.get_lat(), point.get_lon())?;
assert_eq!(point, copy);
let mut h1 = DefaultHasher::new();
point.hash(&mut h1);
let mut h2 = DefaultHasher::new();
copy.hash(&mut h2);
assert_eq!(h1.finish(), h2.finish());
let other_point = GeoTestUtil::next_point(&mut rng)?;
if !CoreHelper::compare_f64(point.get_lat(), other_point.get_lat()).is_eq()
|| !CoreHelper::compare_f64(point.get_lon(), other_point.get_lon()).is_eq()
{
assert_ne!(point, other_point);
} else {
assert_eq!(point, other_point);
let mut h3 = DefaultHasher::new();
other_point.hash(&mut h3);
assert_eq!(h1.finish(), h3.finish());
}
Ok(())
}