Skip to content

Commit 987cc1b

Browse files
committed
add test for fast_read_quoted_text
1 parent 6dad005 commit 987cc1b

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/common/io/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ ordered-float = "3.1.0"
2727
serde = { workspace = true }
2828

2929
[dev-dependencies]
30+
aho-corasick = { version = "0.7.20" }
3031
rand = "0.8.5"
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
}

src/common/io/tests/it/cursor_ext/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
mod fast_read_text_ext;
1516
mod read_bytes_ext;
1617
mod read_datetime_ext;
1718
mod read_number_ext;

0 commit comments

Comments
 (0)