rlucene 0.1.0-alpha.3

A Rust port of Apache Lucene, a full-text search engine library
Documentation
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

use crate::core::analysis::character_utils::CharacterUtils;
use crate::core::analysis::reusable_string_reader::ReusableStringReader;
use crate::core::util::error::lucene_error::{LuceneError, LuceneErrorKind, Result};
use crate::test_framework::core::util::lucene_test_case::random;
use crate::test_framework::core::util::test_util::TestUtil;
#[allow(dead_code)] // for quick search
struct TestCharacterUtils;
#[test]
fn test_lower_upper() -> Result<()> {
  let mut reader = ReusableStringReader::new();
  reader.set_value("Abc");
  let mut buffer = CharacterUtils::new_character_buffer(3)?;
  assert!(CharacterUtils::fill_with_num(&mut buffer, &mut reader, 3)?);
  assert_eq!(buffer.length, 3);
  CharacterUtils::convert_to_lower_case(&mut buffer.buffer, 1, 3);
  assert_eq!(buffer.buffer.as_slice(), &['A', 'b', 'c']);
  CharacterUtils::get_upper_case(&mut buffer.buffer, 1, 3);
  assert_eq!(buffer.buffer.as_slice(), &['A', 'B', 'C']);
  Ok(())
}
#[test]
fn test_conversions() -> Result<()> {
  let mut random = random();
  let orig: Vec<char> = TestUtil::random_unicode_string(&mut random)
    .chars()
    .collect();
  let mut buf = [0i32; 20];
  let buf = &mut buf[..orig.len()];
  let mut restored = ['\0'; 20];
  let restored = &mut restored[..buf.len()];
  let o1 = TestUtil::next_usize(&mut random, 0, orig.len().min(5));
  let o2 = TestUtil::next_usize(&mut random, 0, o1);
  let o3 = TestUtil::next_usize(&mut random, 0, o1);
  let code_point_count = CharacterUtils::get_code_points(&orig, o1, orig.len() - o1, buf, o2)?;
  let char_count = CharacterUtils::get_chars(buf, o2, code_point_count, restored, o3)?;
  assert_eq!(orig.len() - o1, char_count);
  let orig_sub = &orig[o1..o1 + char_count];
  let restored_sub = &restored[o3..o3 + char_count];
  assert_eq!(orig_sub, restored_sub);
  Ok(())
}
#[test]
fn test_new_character_buffer() -> Result<()> {
  let cb1 = CharacterUtils::new_character_buffer(1024)?;
  assert_eq!(cb1.buffer.len(), 1024);
  assert_eq!(cb1.offset, 0);
  assert_eq!(cb1.length, 0);

  let cb2 = CharacterUtils::new_character_buffer(2)?;
  assert_eq!(cb2.buffer.len(), 2);
  assert_eq!(cb2.offset, 0);
  assert_eq!(cb2.length, 0);

  let result = CharacterUtils::new_character_buffer(0);
  assert!(matches!(
    result.as_ref().map_err(LuceneError::kind),
    Err(LuceneErrorKind::IllegalArgument(_))
  ));
  Ok(())
}
#[test]
fn test_fill_no_high_surrogate() -> Result<()> {
  let mut reader = ReusableStringReader::new();
  reader.set_value("helloworld");
  let mut buffer = CharacterUtils::new_character_buffer(6)?;
  assert!(CharacterUtils::fill_with_num(&mut buffer, &mut reader, 6)?);
  assert_eq!(buffer.offset, 0);
  assert_eq!(buffer.length, 6);
  assert_eq!(buffer.get_buffer(), &['h', 'e', 'l', 'l', 'o', 'w']);
  assert!(!CharacterUtils::fill_with_num(&mut buffer, &mut reader, 6)?);
  assert_eq!(buffer.offset, 0);
  assert_eq!(buffer.length, 4);
  assert_eq!(
    &buffer.buffer[buffer.offset..buffer.offset + buffer.length],
    &['o', 'r', 'l', 'd']
  );
  assert!(!CharacterUtils::fill(&mut buffer, &mut reader)?);

  Ok(())
}
#[test]
fn test_fill() -> Result<()> {
  let mut reader = ReusableStringReader::new();
  reader.set_value("1234𐐜789123?𐐜?");
  let mut buffer = CharacterUtils::new_character_buffer(5)?;

  assert!(CharacterUtils::fill(&mut buffer, &mut reader)?);
  assert_eq!(buffer.length, 5);
  assert_eq!(buffer.to_string(), "1234𐐜");

  assert!(CharacterUtils::fill(&mut buffer, &mut reader)?);
  assert_eq!(buffer.length, 5);
  assert_eq!(buffer.to_string(), "78912");

  assert!(!CharacterUtils::fill(&mut buffer, &mut reader)?);
  assert_eq!(buffer.length, 4);
  assert_eq!(buffer.to_string(), "3?𐐜?");

  assert!(!CharacterUtils::fill(&mut buffer, &mut reader)?);
  assert_eq!(buffer.length, 0);

  Ok(())
}