diff options
author | Bernardo <[email protected]> | 2018-12-24 16:01:25 +0000 |
---|---|---|
committer | Bernardo <[email protected]> | 2018-12-25 19:14:27 +0000 |
commit | 863ed19946d6f707ce09dd77bf26b26be73e097c (patch) | |
tree | 32cecbe803073ef30127c764199678bf2d618601 /crates/ra_editor | |
parent | 6b2da4e5474ec064e44b7cf19523de1bab72ba27 (diff) |
remove benchmark and simplify tests
Diffstat (limited to 'crates/ra_editor')
-rw-r--r-- | crates/ra_editor/Cargo.toml | 12 | ||||
-rw-r--r-- | crates/ra_editor/benches/translate_offset_with_edit_benchmark.rs | 87 | ||||
-rw-r--r-- | crates/ra_editor/src/line_index.rs | 23 | ||||
-rw-r--r-- | crates/ra_editor/src/line_index_utils.rs | 63 |
4 files changed, 41 insertions, 144 deletions
diff --git a/crates/ra_editor/Cargo.toml b/crates/ra_editor/Cargo.toml index 7ed7526ec..1ad99af28 100644 --- a/crates/ra_editor/Cargo.toml +++ b/crates/ra_editor/Cargo.toml | |||
@@ -18,15 +18,3 @@ proptest = "0.8.7" | |||
18 | 18 | ||
19 | [dev-dependencies] | 19 | [dev-dependencies] |
20 | test_utils = { path = "../test_utils" } | 20 | test_utils = { path = "../test_utils" } |
21 | criterion = "0.2" | ||
22 | rand = "*" | ||
23 | rand_xorshift = "*" | ||
24 | lazy_static = "*" | ||
25 | |||
26 | [lib] | ||
27 | # so that criterion arguments work, see: https://bheisler.github.io/criterion.rs/book/faq.html#cargo-bench-gives-unrecognized-option-errors-for-valid-command-line-options | ||
28 | bench = false | ||
29 | |||
30 | [[bench]] | ||
31 | name = "translate_offset_with_edit_benchmark" | ||
32 | harness = false \ No newline at end of file | ||
diff --git a/crates/ra_editor/benches/translate_offset_with_edit_benchmark.rs b/crates/ra_editor/benches/translate_offset_with_edit_benchmark.rs deleted file mode 100644 index 0f550fd39..000000000 --- a/crates/ra_editor/benches/translate_offset_with_edit_benchmark.rs +++ /dev/null | |||
@@ -1,87 +0,0 @@ | |||
1 | use criterion::{criterion_group, criterion_main}; | ||
2 | use criterion::Criterion; | ||
3 | use criterion::Fun; | ||
4 | use ra_text_edit::AtomTextEdit; | ||
5 | use ra_text_edit::test_utils::{arb_edits_custom, arb_offset}; | ||
6 | use ra_editor::line_index_utils; | ||
7 | use ra_editor::LineIndex; | ||
8 | use ra_syntax::TextUnit; | ||
9 | use proptest::test_runner; | ||
10 | use proptest::string::string_regex; | ||
11 | use proptest::strategy::{Strategy, ValueTree}; | ||
12 | use rand_xorshift::XorShiftRng; | ||
13 | use rand::SeedableRng; | ||
14 | use lazy_static::lazy_static; | ||
15 | |||
16 | #[derive(Debug)] | ||
17 | struct Data { | ||
18 | text: String, | ||
19 | line_index: LineIndex, | ||
20 | edits: Vec<AtomTextEdit>, | ||
21 | offset: TextUnit, | ||
22 | } | ||
23 | |||
24 | fn setup_data() -> Data { | ||
25 | let mut runner = test_runner::TestRunner::default(); | ||
26 | { | ||
27 | struct TestRng { | ||
28 | rng: XorShiftRng, | ||
29 | } | ||
30 | // HACK to be able to manually seed the TestRunner | ||
31 | let rng: &mut TestRng = unsafe { std::mem::transmute(runner.rng()) }; | ||
32 | rng.rng = XorShiftRng::seed_from_u64(0); | ||
33 | } | ||
34 | |||
35 | let text = { | ||
36 | let arb = string_regex("([a-zA-Z_0-9]{10,50}.{1,5}\n){100,500}").unwrap(); | ||
37 | let tree = arb.new_tree(&mut runner).unwrap(); | ||
38 | tree.current() | ||
39 | }; | ||
40 | |||
41 | let edits = { | ||
42 | let arb = arb_edits_custom(&text, 99, 100); | ||
43 | let tree = arb.new_tree(&mut runner).unwrap(); | ||
44 | tree.current() | ||
45 | }; | ||
46 | |||
47 | let offset = { | ||
48 | let arb = arb_offset(&text); | ||
49 | let tree = arb.new_tree(&mut runner).unwrap(); | ||
50 | tree.current() | ||
51 | }; | ||
52 | |||
53 | let line_index = LineIndex::new(&text); | ||
54 | |||
55 | Data { | ||
56 | text, | ||
57 | line_index, | ||
58 | edits, | ||
59 | offset, | ||
60 | } | ||
61 | } | ||
62 | |||
63 | lazy_static! { | ||
64 | static ref DATA: Data = setup_data(); | ||
65 | } | ||
66 | |||
67 | fn compare_translates(c: &mut Criterion) { | ||
68 | let functions = vec![ | ||
69 | Fun::new("translate_after_edit", |b, _| { | ||
70 | b.iter(|| { | ||
71 | let d = &*DATA; | ||
72 | line_index_utils::translate_after_edit(&d.text, d.offset, d.edits.clone()); | ||
73 | }) | ||
74 | }), | ||
75 | Fun::new("translate_offset_with_edit", |b, _| { | ||
76 | b.iter(|| { | ||
77 | let d = &*DATA; | ||
78 | line_index_utils::translate_offset_with_edit(&d.line_index, d.offset, &d.edits); | ||
79 | }) | ||
80 | }), | ||
81 | ]; | ||
82 | |||
83 | c.bench_functions("translate", functions, ()); | ||
84 | } | ||
85 | |||
86 | criterion_group!(benches, compare_translates); | ||
87 | criterion_main!(benches); | ||
diff --git a/crates/ra_editor/src/line_index.rs b/crates/ra_editor/src/line_index.rs index 5304fbcf6..898fee7e0 100644 --- a/crates/ra_editor/src/line_index.rs +++ b/crates/ra_editor/src/line_index.rs | |||
@@ -128,8 +128,8 @@ impl LineIndex { | |||
128 | } | 128 | } |
129 | } | 129 | } |
130 | 130 | ||
131 | #[cfg(test)] | ||
131 | /// Simple reference implementation to use in proptests | 132 | /// Simple reference implementation to use in proptests |
132 | /// and benchmarks as baseline | ||
133 | pub fn to_line_col(text: &str, offset: TextUnit) -> LineCol { | 133 | pub fn to_line_col(text: &str, offset: TextUnit) -> LineCol { |
134 | let mut res = LineCol { | 134 | let mut res = LineCol { |
135 | line: 0, | 135 | line: 0, |
@@ -270,6 +270,27 @@ mod test_line_index { | |||
270 | .boxed() | 270 | .boxed() |
271 | } | 271 | } |
272 | 272 | ||
273 | fn to_line_col(text: &str, offset: TextUnit) -> LineCol { | ||
274 | let mut res = LineCol { | ||
275 | line: 0, | ||
276 | col_utf16: 0, | ||
277 | }; | ||
278 | for (i, c) in text.char_indices() { | ||
279 | if i + c.len_utf8() > offset.to_usize() { | ||
280 | // if it's an invalid offset, inside a multibyte char | ||
281 | // return as if it was at the start of the char | ||
282 | break; | ||
283 | } | ||
284 | if c == '\n' { | ||
285 | res.line += 1; | ||
286 | res.col_utf16 = 0; | ||
287 | } else { | ||
288 | res.col_utf16 += 1; | ||
289 | } | ||
290 | } | ||
291 | res | ||
292 | } | ||
293 | |||
273 | proptest! { | 294 | proptest! { |
274 | #[test] | 295 | #[test] |
275 | fn test_line_index_proptest((offset, text) in arb_text_with_offset()) { | 296 | fn test_line_index_proptest((offset, text) in arb_text_with_offset()) { |
diff --git a/crates/ra_editor/src/line_index_utils.rs b/crates/ra_editor/src/line_index_utils.rs index ba3ac8aeb..b8b149442 100644 --- a/crates/ra_editor/src/line_index_utils.rs +++ b/crates/ra_editor/src/line_index_utils.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use ra_text_edit::AtomTextEdit; | 1 | use ra_text_edit::AtomTextEdit; |
2 | use ra_syntax::{TextUnit, TextRange}; | 2 | use ra_syntax::{TextUnit, TextRange}; |
3 | use crate::{LineIndex, LineCol, line_index::{self, Utf16Char}}; | 3 | use crate::{LineIndex, LineCol, line_index::Utf16Char}; |
4 | use superslice::Ext; | 4 | use superslice::Ext; |
5 | 5 | ||
6 | #[derive(Debug, Clone)] | 6 | #[derive(Debug, Clone)] |
@@ -325,59 +325,34 @@ pub fn translate_offset_with_edit( | |||
325 | res.to_line_col(offset) | 325 | res.to_line_col(offset) |
326 | } | 326 | } |
327 | 327 | ||
328 | /// Simplest implementation to use as reference in proptest and benchmarks | ||
329 | pub fn translate_after_edit( | ||
330 | pre_edit_text: &str, | ||
331 | offset: TextUnit, | ||
332 | edits: Vec<AtomTextEdit>, | ||
333 | ) -> LineCol { | ||
334 | let text = edit_text(pre_edit_text, edits); | ||
335 | line_index::to_line_col(&text, offset) | ||
336 | } | ||
337 | |||
338 | fn edit_text(pre_edit_text: &str, mut edits: Vec<AtomTextEdit>) -> String { | ||
339 | // apply edits ordered from last to first | ||
340 | // since they should not overlap we can just use start() | ||
341 | edits.sort_by_key(|x| -(x.delete.start().to_usize() as isize)); | ||
342 | |||
343 | let mut text = pre_edit_text.to_owned(); | ||
344 | |||
345 | for edit in &edits { | ||
346 | let range = edit.delete.start().to_usize()..edit.delete.end().to_usize(); | ||
347 | text.replace_range(range, &edit.insert); | ||
348 | } | ||
349 | |||
350 | text | ||
351 | } | ||
352 | |||
353 | #[cfg(test)] | 328 | #[cfg(test)] |
354 | mod test { | 329 | mod test { |
355 | use super::*; | 330 | use super::*; |
356 | use proptest::{prelude::*, proptest, proptest_helper}; | 331 | use proptest::{prelude::*, proptest, proptest_helper}; |
357 | use ra_text_edit::test_utils::{arb_text, arb_offset, arb_edits}; | 332 | use crate::line_index; |
333 | use ra_text_edit::test_utils::{arb_offset, arb_text_with_edits}; | ||
334 | use ra_text_edit::TextEdit; | ||
358 | 335 | ||
359 | #[derive(Debug)] | 336 | #[derive(Debug)] |
360 | struct ArbTextWithOffsetAndEdits { | 337 | struct ArbTextWithOffsetAndEdits { |
361 | text: String, | 338 | text: String, |
339 | edits: TextEdit, | ||
362 | edited_text: String, | 340 | edited_text: String, |
363 | offset: TextUnit, | 341 | offset: TextUnit, |
364 | edits: Vec<AtomTextEdit>, | ||
365 | } | 342 | } |
366 | 343 | ||
367 | fn arb_text_with_offset_and_edits() -> BoxedStrategy<ArbTextWithOffsetAndEdits> { | 344 | fn arb_text_with_edits_and_offset() -> BoxedStrategy<ArbTextWithOffsetAndEdits> { |
368 | arb_text() | 345 | arb_text_with_edits() |
369 | .prop_flat_map(|text| { | 346 | .prop_flat_map(|x| { |
370 | (arb_edits(&text), Just(text)).prop_flat_map(|(edits, text)| { | 347 | let edited_text = x.edits.apply(&x.text); |
371 | let edited_text = edit_text(&text, edits.clone()); | 348 | let arb_offset = arb_offset(&edited_text); |
372 | let arb_offset = arb_offset(&edited_text); | 349 | (Just(x), Just(edited_text), arb_offset).prop_map(|(x, edited_text, offset)| { |
373 | (Just(text), Just(edited_text), Just(edits), arb_offset).prop_map( | 350 | ArbTextWithOffsetAndEdits { |
374 | |(text, edited_text, edits, offset)| ArbTextWithOffsetAndEdits { | 351 | text: x.text, |
375 | text, | 352 | edits: x.edits, |
376 | edits, | 353 | edited_text, |
377 | edited_text, | 354 | offset, |
378 | offset, | 355 | } |
379 | }, | ||
380 | ) | ||
381 | }) | 356 | }) |
382 | }) | 357 | }) |
383 | .boxed() | 358 | .boxed() |
@@ -385,10 +360,10 @@ mod test { | |||
385 | 360 | ||
386 | proptest! { | 361 | proptest! { |
387 | #[test] | 362 | #[test] |
388 | fn test_translate_offset_with_edit(x in arb_text_with_offset_and_edits()) { | 363 | fn test_translate_offset_with_edit(x in arb_text_with_edits_and_offset()) { |
389 | let expected = line_index::to_line_col(&x.edited_text, x.offset); | 364 | let expected = line_index::to_line_col(&x.edited_text, x.offset); |
390 | let line_index = LineIndex::new(&x.text); | 365 | let line_index = LineIndex::new(&x.text); |
391 | let actual = translate_offset_with_edit(&line_index, x.offset, &x.edits); | 366 | let actual = translate_offset_with_edit(&line_index, x.offset, x.edits.as_atoms()); |
392 | 367 | ||
393 | assert_eq!(actual, expected); | 368 | assert_eq!(actual, expected); |
394 | } | 369 | } |