use crate::core::index::BytesRef;
use crate::core::util::StringHelper;
use crate::core::util::error::lucene_error::{LuceneError, LuceneErrorKind, Result};
use crate::test_framework::core::util::lucene_test_case::{new_bytes_ref_from_string, random};
#[allow(dead_code)] pub struct TestStringHelper;
#[test]
fn test_bytes_difference() -> Result<()> {
let mut random = random();
let left: BytesRef<Vec<u8>> = new_bytes_ref_from_string(&mut random, "foobar")?;
let right: BytesRef<Vec<u8>> = new_bytes_ref_from_string(&mut random, "foozo")?;
assert_eq!(StringHelper::bytes_difference(&left, &right)?, 3);
assert_eq!(
StringHelper::bytes_difference::<Vec<u8>, Vec<u8>>(
&new_bytes_ref_from_string(&mut random, "foo")?,
&new_bytes_ref_from_string(&mut random, "for")?
)?,
2
);
assert_eq!(
StringHelper::bytes_difference::<Vec<u8>, Vec<u8>>(
&new_bytes_ref_from_string(&mut random, "foo1234")?,
&new_bytes_ref_from_string(&mut random, "for1234")?
)?,
2
);
assert_eq!(
StringHelper::bytes_difference::<Vec<u8>, Vec<u8>>(
&new_bytes_ref_from_string(&mut random, "foo")?,
&new_bytes_ref_from_string(&mut random, "fz")?
)?,
1
);
assert_eq!(
StringHelper::bytes_difference::<Vec<u8>, Vec<u8>>(
&new_bytes_ref_from_string(&mut random, "foo")?,
&new_bytes_ref_from_string(&mut random, "g")?
)?,
0
);
assert_eq!(
StringHelper::bytes_difference::<Vec<u8>, Vec<u8>>(
&new_bytes_ref_from_string(&mut random, "foo")?,
&new_bytes_ref_from_string(&mut random, "food")?
)?,
3
);
let result = StringHelper::bytes_difference::<Vec<u8>, Vec<u8>>(
&new_bytes_ref_from_string(&mut random, "ab")?,
&new_bytes_ref_from_string(&mut random, "ab")?,
);
assert!(matches!(
result.as_ref().map_err(LuceneError::kind),
Err(LuceneErrorKind::IllegalArgument(_))
));
Ok(())
}
#[test]
fn test_starts_with() -> Result<()> {
let mut random = random();
let ref_bytes: BytesRef<Vec<u8>> = new_bytes_ref_from_string(&mut random, "foobar")?;
let slice: BytesRef<Vec<u8>> = new_bytes_ref_from_string(&mut random, "foo")?;
assert!(StringHelper::starts_with_byte_ref(&ref_bytes, &slice));
Ok(())
}
#[test]
fn test_ends_with() -> Result<()> {
let mut random = random();
let ref_bytes: BytesRef<Vec<u8>> = new_bytes_ref_from_string(&mut random, "foobar")?;
let slice: BytesRef<Vec<u8>> = new_bytes_ref_from_string(&mut random, "bar")?;
assert!(StringHelper::ends_with(&ref_bytes, &slice));
Ok(())
}
#[test]
fn test_starts_with_whole() -> Result<()> {
let mut random = random();
let ref_bytes: BytesRef<Vec<u8>> = new_bytes_ref_from_string(&mut random, "foobar")?;
let slice: BytesRef<Vec<u8>> = new_bytes_ref_from_string(&mut random, "foobar")?;
assert!(StringHelper::starts_with_byte_ref(&ref_bytes, &slice));
Ok(())
}
#[test]
fn test_ends_with_whole() -> Result<()> {
let mut random = random();
let ref_bytes: BytesRef<Vec<u8>> = new_bytes_ref_from_string(&mut random, "foobar")?;
let slice: BytesRef<Vec<u8>> = new_bytes_ref_from_string(&mut random, "foobar")?;
assert!(StringHelper::ends_with(&ref_bytes, &slice));
Ok(())
}
#[test]
fn test_murmur_hash3() -> Result<()> {
let mut random = random();
assert_eq!(
StringHelper::murmurhash3_x86_32::<Vec<u8>>(&new_bytes_ref_from_string(&mut random, "foo")?, 0),
0xf6a5c420u32 as i32
);
assert_eq!(
StringHelper::murmurhash3_x86_32::<Vec<u8>>(
&new_bytes_ref_from_string(&mut random, "foo")?,
16
),
0xcd018ef6u32 as i32
);
assert_eq!(
StringHelper::murmurhash3_x86_32::<Vec<u8>>(
&new_bytes_ref_from_string(
&mut random,
"You want weapons? We're in a library! Books! The best weapons in the world!"
)?,
0
),
0x111e7435
);
assert_eq!(
StringHelper::murmurhash3_x86_32::<Vec<u8>>(
&new_bytes_ref_from_string(
&mut random,
"You want weapons? We're in a library! Books! The best weapons in the world!"
)?,
3476
),
0x2c628cd0
);
Ok(())
}
#[test]
fn test_sort_key_length() -> Result<()> {
let mut random = random();
assert_eq!(
StringHelper::sort_key_length::<Vec<u8>, Vec<u8>>(
&new_bytes_ref_from_string(&mut random, "foo")?,
&new_bytes_ref_from_string(&mut random, "for")?
)?,
3
);
assert_eq!(
StringHelper::sort_key_length::<Vec<u8>, Vec<u8>>(
&new_bytes_ref_from_string(&mut random, "foo1234")?,
&new_bytes_ref_from_string(&mut random, "for1234")?
)?,
3
);
assert_eq!(
StringHelper::sort_key_length::<Vec<u8>, Vec<u8>>(
&new_bytes_ref_from_string(&mut random, "foo")?,
&new_bytes_ref_from_string(&mut random, "fz")?
)?,
2
);
assert_eq!(
StringHelper::sort_key_length::<Vec<u8>, Vec<u8>>(
&new_bytes_ref_from_string(&mut random, "foo")?,
&new_bytes_ref_from_string(&mut random, "g")?
)?,
1
);
assert_eq!(
StringHelper::sort_key_length::<Vec<u8>, Vec<u8>>(
&new_bytes_ref_from_string(&mut random, "foo")?,
&new_bytes_ref_from_string(&mut random, "food")?
)?,
4
);
let result = StringHelper::sort_key_length::<Vec<u8>, Vec<u8>>(
&new_bytes_ref_from_string(&mut random, "ab")?,
&new_bytes_ref_from_string(&mut random, "ab")?,
);
assert!(
result.is_err(),
"Expected an error when the terms are equal"
);
Ok(())
}