use crate::test_framework::core::util::lucene_test_case::random;
use crate::core::index::BytesRef;
use crate::core::util::error::lucene_error::{LuceneError, LuceneErrorKind, Result};
use crate::test_framework::core::util::test_util::TestUtil;
#[allow(dead_code)] struct TestBytesRef;
#[test]
fn test_empty() {
let b: BytesRef<Vec<u8>> = BytesRef::new();
assert_eq!(b.bytes.len(), 0);
assert_eq!(b.length, 0);
assert_eq!(b.offset, 0);
}
#[test]
fn test_from_bytes() -> Result<()> {
let bytes: Vec<u8> = "abcd".as_bytes().to_vec();
let b = BytesRef::from_bytes(bytes);
assert_eq!(b"abcd", b.bytes.as_slice());
assert_eq!(b.length, 4);
assert_eq!(b.offset, 0);
let bytes = "abcd".as_bytes().to_vec();
let b2 = BytesRef::from_slice(bytes, 1, 3);
let b2_value = b2.utf8_to_string()?;
assert_eq!("bcd", b2_value);
assert!(!b.eq(&b2));
Ok(())
}
#[test]
fn test_from_chars() -> Result<()> {
let mut random = random();
for _i in 0..100 {
let s = TestUtil::random_unicode_string(&mut random);
let s2: String = BytesRef::<Vec<u8>>::from_string(&s).utf8_to_string()?;
assert_eq!(s, s2);
}
assert_eq!(
"\u{ffff}",
BytesRef::<Vec<u8>>::from_string("\u{ffff}").utf8_to_string()?
);
Ok(())
}
#[test]
fn test_invalid_deep_copy() -> Result<()> {
let mut from = BytesRef::from_bytes(vec![1, 2]);
from.offset += 1;
let result = BytesRef::deep_copy_of(&from);
assert!(matches!(
result.as_ref().map_err(LuceneError::kind),
Err(LuceneErrorKind::ArrayIndexOutOfBounds(_))
));
Ok(())
}