|
| 1 | +// Copyright 2022 Datafuse Labs. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use std::collections::VecDeque; |
| 16 | +use std::io::Cursor; |
| 17 | + |
| 18 | +use aho_corasick::AhoCorasick; |
| 19 | +use common_exception::Result; |
| 20 | +use common_io::cursor_ext::*; |
| 21 | + |
| 22 | +#[test] |
| 23 | +fn test_positions() -> Result<()> { |
| 24 | + let cases = vec![ |
| 25 | + (r#"abc"#.to_string(), vec![]), |
| 26 | + (r#"'abcdefg'"#.to_string(), vec![0, 8]), |
| 27 | + (r#"'abc\d'e'"#.to_string(), vec![0, 4, 6, 8]), |
| 28 | + (r#"'abc','d\'ef','g\\\'hi'"#.to_string(), vec![ |
| 29 | + 0, 4, 6, 8, 9, 12, 14, 16, 17, 18, 19, 22, |
| 30 | + ]), |
| 31 | + ]; |
| 32 | + |
| 33 | + let patterns = &["'", "\\"]; |
| 34 | + let ac = AhoCorasick::new(patterns); |
| 35 | + for (data, expect) in cases { |
| 36 | + let mut positions = VecDeque::new(); |
| 37 | + for mat in ac.find_iter(&data) { |
| 38 | + let pos = mat.start(); |
| 39 | + positions.push_back(pos); |
| 40 | + } |
| 41 | + assert_eq!(positions, expect) |
| 42 | + } |
| 43 | + Ok(()) |
| 44 | +} |
| 45 | + |
| 46 | +#[test] |
| 47 | +fn test_fast_read_text() -> Result<()> { |
| 48 | + let data = r#"'abc','d\'ef','g\\\'hi'"#.to_string(); |
| 49 | + let patterns = &["'", "\\"]; |
| 50 | + let ac = AhoCorasick::new(patterns); |
| 51 | + let mut positions = VecDeque::new(); |
| 52 | + for mat in ac.find_iter(&data) { |
| 53 | + let pos = mat.start(); |
| 54 | + positions.push_back(pos); |
| 55 | + } |
| 56 | + |
| 57 | + let mut reader = Cursor::new(data.as_bytes()); |
| 58 | + let expected = vec![ |
| 59 | + "abc".as_bytes().to_vec(), |
| 60 | + "d'ef".as_bytes().to_vec(), |
| 61 | + "g\\'hi".as_bytes().to_vec(), |
| 62 | + ]; |
| 63 | + let mut res = vec![]; |
| 64 | + for i in 0..expected.len() { |
| 65 | + if i > 0 { |
| 66 | + assert!(reader.ignore_byte(b',')); |
| 67 | + } |
| 68 | + let mut buf = vec![]; |
| 69 | + reader.fast_read_quoted_text(&mut buf, &mut positions)?; |
| 70 | + res.push(buf); |
| 71 | + } |
| 72 | + assert_eq!(res, expected); |
| 73 | + Ok(()) |
| 74 | +} |
0 commit comments