aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorBernardo <[email protected]>2018-12-24 16:01:25 +0000
committerBernardo <[email protected]>2018-12-25 19:14:27 +0000
commit863ed19946d6f707ce09dd77bf26b26be73e097c (patch)
tree32cecbe803073ef30127c764199678bf2d618601 /crates
parent6b2da4e5474ec064e44b7cf19523de1bab72ba27 (diff)
remove benchmark and simplify tests
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_editor/Cargo.toml12
-rw-r--r--crates/ra_editor/benches/translate_offset_with_edit_benchmark.rs87
-rw-r--r--crates/ra_editor/src/line_index.rs23
-rw-r--r--crates/ra_editor/src/line_index_utils.rs63
-rw-r--r--crates/ra_text_edit/src/test_utils.rs72
-rw-r--r--crates/ra_text_edit/src/text_edit.rs15
6 files changed, 69 insertions, 203 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]
20test_utils = { path = "../test_utils" } 20test_utils = { path = "../test_utils" }
21criterion = "0.2"
22rand = "*"
23rand_xorshift = "*"
24lazy_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
28bench = false
29
30[[bench]]
31name = "translate_offset_with_edit_benchmark"
32harness = 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 @@
1use criterion::{criterion_group, criterion_main};
2use criterion::Criterion;
3use criterion::Fun;
4use ra_text_edit::AtomTextEdit;
5use ra_text_edit::test_utils::{arb_edits_custom, arb_offset};
6use ra_editor::line_index_utils;
7use ra_editor::LineIndex;
8use ra_syntax::TextUnit;
9use proptest::test_runner;
10use proptest::string::string_regex;
11use proptest::strategy::{Strategy, ValueTree};
12use rand_xorshift::XorShiftRng;
13use rand::SeedableRng;
14use lazy_static::lazy_static;
15
16#[derive(Debug)]
17struct Data {
18 text: String,
19 line_index: LineIndex,
20 edits: Vec<AtomTextEdit>,
21 offset: TextUnit,
22}
23
24fn 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
63lazy_static! {
64 static ref DATA: Data = setup_data();
65}
66
67fn 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
86criterion_group!(benches, compare_translates);
87criterion_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
133pub fn to_line_col(text: &str, offset: TextUnit) -> LineCol { 133pub 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 @@
1use ra_text_edit::AtomTextEdit; 1use ra_text_edit::AtomTextEdit;
2use ra_syntax::{TextUnit, TextRange}; 2use ra_syntax::{TextUnit, TextRange};
3use crate::{LineIndex, LineCol, line_index::{self, Utf16Char}}; 3use crate::{LineIndex, LineCol, line_index::Utf16Char};
4use superslice::Ext; 4use 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
329pub 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
338fn 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)]
354mod test { 329mod 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 }
diff --git a/crates/ra_text_edit/src/test_utils.rs b/crates/ra_text_edit/src/test_utils.rs
index f150288f6..f0b8dfde1 100644
--- a/crates/ra_text_edit/src/test_utils.rs
+++ b/crates/ra_text_edit/src/test_utils.rs
@@ -1,6 +1,6 @@
1use proptest::prelude::*; 1use proptest::prelude::*;
2use text_unit::{TextUnit, TextRange}; 2use text_unit::{TextUnit, TextRange};
3use crate::AtomTextEdit; 3use crate::{AtomTextEdit, TextEdit};
4 4
5pub fn arb_text() -> proptest::string::RegexGeneratorStrategy<String> { 5pub fn arb_text() -> proptest::string::RegexGeneratorStrategy<String> {
6 // generate multiple newlines 6 // generate multiple newlines
@@ -23,11 +23,7 @@ pub fn arb_offset(text: &str) -> BoxedStrategy<TextUnit> {
23 } 23 }
24} 24}
25 25
26pub fn arb_edits(text: &str) -> BoxedStrategy<Vec<AtomTextEdit>> { 26pub fn arb_text_edit(text: &str) -> BoxedStrategy<TextEdit> {
27 arb_edits_custom(&text, 0, 7)
28}
29
30pub fn arb_edits_custom(text: &str, min: usize, max: usize) -> BoxedStrategy<Vec<AtomTextEdit>> {
31 if text.is_empty() { 27 if text.is_empty() {
32 // only valid edits 28 // only valid edits
33 return Just(vec![]) 29 return Just(vec![])
@@ -37,14 +33,14 @@ pub fn arb_edits_custom(text: &str, min: usize, max: usize) -> BoxedStrategy<Vec
37 .prop_map(|text| vec![AtomTextEdit::insert(TextUnit::from(0), text)]) 33 .prop_map(|text| vec![AtomTextEdit::insert(TextUnit::from(0), text)])
38 .boxed(), 34 .boxed(),
39 ) 35 )
36 .prop_map(TextEdit::from_atoms)
40 .boxed(); 37 .boxed();
41 } 38 }
42 39
43 let offsets = text_offsets(text); 40 let offsets = text_offsets(text);
44 let max_cuts = max.min(offsets.len()); 41 let max_cuts = 7.min(offsets.len());
45 let min_cuts = min.min(offsets.len() - 1);
46 42
47 proptest::sample::subsequence(offsets, min_cuts..max_cuts) 43 proptest::sample::subsequence(offsets, 0..max_cuts)
48 .prop_flat_map(|cuts| { 44 .prop_flat_map(|cuts| {
49 let strategies: Vec<_> = cuts 45 let strategies: Vec<_> = cuts
50 .chunks(2) 46 .chunks(2)
@@ -68,52 +64,22 @@ pub fn arb_edits_custom(text: &str, min: usize, max: usize) -> BoxedStrategy<Vec
68 .collect(); 64 .collect();
69 strategies 65 strategies
70 }) 66 })
67 .prop_map(TextEdit::from_atoms)
71 .boxed() 68 .boxed()
72} 69}
73 70
74#[cfg(test)] 71#[derive(Debug, Clone)]
75mod tests { 72pub struct ArbTextWithEdits {
76 use super::*; 73 pub text: String,
77 use proptest::{proptest, proptest_helper}; 74 pub edits: TextEdit,
78 75}
79 fn arb_text_with_edits() -> BoxedStrategy<(String, Vec<AtomTextEdit>)> {
80 let text = arb_text();
81 text.prop_flat_map(|s| {
82 let edits = arb_edits(&s);
83 (Just(s), edits)
84 })
85 .boxed()
86 }
87
88 fn intersect(r1: TextRange, r2: TextRange) -> Option<TextRange> {
89 let start = r1.start().max(r2.start());
90 let end = r1.end().min(r2.end());
91 if start <= end {
92 Some(TextRange::from_to(start, end))
93 } else {
94 None
95 }
96 }
97 proptest! {
98 #[test]
99 fn atom_text_edits_are_valid((text, edits) in arb_text_with_edits()) {
100 proptest_atom_text_edits_are_valid(text, edits)
101 }
102 }
103 76
104 fn proptest_atom_text_edits_are_valid(text: String, edits: Vec<AtomTextEdit>) { 77pub fn arb_text_with_edits() -> BoxedStrategy<ArbTextWithEdits> {
105 // slicing doesn't panic 78 let text = arb_text();
106 for e in &edits { 79 text.prop_flat_map(|s| {
107 let _ = &text[e.delete]; 80 let edits = arb_text_edit(&s);
108 } 81 (Just(s), edits)
109 // ranges do not overlap 82 })
110 for i in 1..edits.len() { 83 .prop_map(|(text, edits)| ArbTextWithEdits { text, edits })
111 let e1 = &edits[i]; 84 .boxed()
112 for e2 in &edits[0..i] {
113 if intersect(e1.delete, e2.delete).is_some() {
114 assert!(false, "Overlapping ranges {} {}", e1.delete, e2.delete);
115 }
116 }
117 }
118 }
119} 85}
diff --git a/crates/ra_text_edit/src/text_edit.rs b/crates/ra_text_edit/src/text_edit.rs
index 392968d63..0881f3e1c 100644
--- a/crates/ra_text_edit/src/text_edit.rs
+++ b/crates/ra_text_edit/src/text_edit.rs
@@ -26,12 +26,7 @@ impl TextEditBuilder {
26 self.atoms.push(AtomTextEdit::insert(offset, text)) 26 self.atoms.push(AtomTextEdit::insert(offset, text))
27 } 27 }
28 pub fn finish(self) -> TextEdit { 28 pub fn finish(self) -> TextEdit {
29 let mut atoms = self.atoms; 29 TextEdit::from_atoms(self.atoms)
30 atoms.sort_by_key(|a| (a.delete.start(), a.delete.end()));
31 for (a1, a2) in atoms.iter().zip(atoms.iter().skip(1)) {
32 assert!(a1.delete.end() <= a2.delete.start())
33 }
34 TextEdit { atoms }
35 } 30 }
36 pub fn invalidates_offset(&self, offset: TextUnit) -> bool { 31 pub fn invalidates_offset(&self, offset: TextUnit) -> bool {
37 self.atoms 32 self.atoms
@@ -41,6 +36,14 @@ impl TextEditBuilder {
41} 36}
42 37
43impl TextEdit { 38impl TextEdit {
39 pub(crate) fn from_atoms(mut atoms: Vec<AtomTextEdit>) -> TextEdit {
40 atoms.sort_by_key(|a| (a.delete.start(), a.delete.end()));
41 for (a1, a2) in atoms.iter().zip(atoms.iter().skip(1)) {
42 assert!(a1.delete.end() <= a2.delete.start())
43 }
44 TextEdit { atoms }
45 }
46
44 pub fn as_atoms(&self) -> &[AtomTextEdit] { 47 pub fn as_atoms(&self) -> &[AtomTextEdit] {
45 &self.atoms 48 &self.atoms
46 } 49 }