@@ -21,6 +21,7 @@ use header::TestProps;
21
21
use util:: logv;
22
22
use regex:: Regex ;
23
23
24
+ use std:: collections:: VecDeque ;
24
25
use std:: collections:: HashMap ;
25
26
use std:: collections:: HashSet ;
26
27
use std:: env;
@@ -48,6 +49,88 @@ pub fn dylib_env_var() -> &'static str {
48
49
}
49
50
}
50
51
52
+ #[ derive( Debug , PartialEq ) ]
53
+ pub enum DiffLine {
54
+ Context ( String ) ,
55
+ Expected ( String ) ,
56
+ Resulting ( String ) ,
57
+ }
58
+
59
+ #[ derive( Debug , PartialEq ) ]
60
+ pub struct Mismatch {
61
+ pub line_number : u32 ,
62
+ pub lines : Vec < DiffLine > ,
63
+ }
64
+
65
+ impl Mismatch {
66
+ fn new ( line_number : u32 ) -> Mismatch {
67
+ Mismatch {
68
+ line_number : line_number,
69
+ lines : Vec :: new ( ) ,
70
+ }
71
+ }
72
+ }
73
+
74
+ // Produces a diff between the expected output and actual output.
75
+ pub fn make_diff ( expected : & str , actual : & str , context_size : usize ) -> Vec < Mismatch > {
76
+ let mut line_number = 1 ;
77
+ let mut context_queue: VecDeque < & str > = VecDeque :: with_capacity ( context_size) ;
78
+ let mut lines_since_mismatch = context_size + 1 ;
79
+ let mut results = Vec :: new ( ) ;
80
+ let mut mismatch = Mismatch :: new ( 0 ) ;
81
+
82
+ for result in diff:: lines ( actual, expected) {
83
+ match result {
84
+ diff:: Result :: Left ( str) => {
85
+ if lines_since_mismatch >= context_size && lines_since_mismatch > 0 {
86
+ results. push ( mismatch) ;
87
+ mismatch = Mismatch :: new ( line_number - context_queue. len ( ) as u32 ) ;
88
+ }
89
+
90
+ while let Some ( line) = context_queue. pop_front ( ) {
91
+ mismatch. lines . push ( DiffLine :: Context ( line. to_owned ( ) ) ) ;
92
+ }
93
+
94
+ mismatch. lines . push ( DiffLine :: Resulting ( str. to_owned ( ) ) ) ;
95
+ lines_since_mismatch = 0 ;
96
+ }
97
+ diff:: Result :: Right ( str) => {
98
+ if lines_since_mismatch >= context_size && lines_since_mismatch > 0 {
99
+ results. push ( mismatch) ;
100
+ mismatch = Mismatch :: new ( line_number - context_queue. len ( ) as u32 ) ;
101
+ }
102
+
103
+ while let Some ( line) = context_queue. pop_front ( ) {
104
+ mismatch. lines . push ( DiffLine :: Context ( line. to_owned ( ) ) ) ;
105
+ }
106
+
107
+ mismatch. lines . push ( DiffLine :: Expected ( str. to_owned ( ) ) ) ;
108
+ line_number += 1 ;
109
+ lines_since_mismatch = 0 ;
110
+ }
111
+ diff:: Result :: Both ( str, _) => {
112
+ if context_queue. len ( ) >= context_size {
113
+ let _ = context_queue. pop_front ( ) ;
114
+ }
115
+
116
+ if lines_since_mismatch < context_size {
117
+ mismatch. lines . push ( DiffLine :: Context ( str. to_owned ( ) ) ) ;
118
+ } else if context_size > 0 {
119
+ context_queue. push_back ( str) ;
120
+ }
121
+
122
+ line_number += 1 ;
123
+ lines_since_mismatch += 1 ;
124
+ }
125
+ }
126
+ }
127
+
128
+ results. push ( mismatch) ;
129
+ results. remove ( 0 ) ;
130
+
131
+ results
132
+ }
133
+
51
134
pub fn run ( config : Config , testpaths : & TestPaths ) {
52
135
match & * config. target {
53
136
"arm-linux-androideabi" | "armv7-linux-androideabi" | "aarch64-linux-android" => {
@@ -2712,12 +2795,25 @@ impl<'test> TestCx<'test> {
2712
2795
println ! ( "normalized {}:\n {}\n " , kind, actual) ;
2713
2796
} else {
2714
2797
println ! ( "diff of {}:\n " , kind) ;
2715
- for diff in diff:: lines ( expected, actual) {
2716
- match diff {
2717
- diff:: Result :: Left ( l) => println ! ( "-{}" , l) ,
2718
- diff:: Result :: Right ( r) => println ! ( "+{}" , r) ,
2719
- _ => { } ,
2798
+ let diff_results = make_diff ( expected, actual, 3 ) ;
2799
+ for result in diff_results {
2800
+ let mut line_number = result. line_number ;
2801
+ for line in result. lines {
2802
+ match line {
2803
+ DiffLine :: Expected ( e) => {
2804
+ println ! ( "-\t {}" , e) ;
2805
+ line_number += 1 ;
2806
+ } ,
2807
+ DiffLine :: Context ( c) => {
2808
+ println ! ( "{}\t {}" , line_number, c) ;
2809
+ line_number += 1 ;
2810
+ } ,
2811
+ DiffLine :: Resulting ( r) => {
2812
+ println ! ( "+\t {}" , r) ;
2813
+ } ,
2814
+ }
2720
2815
}
2816
+ println ! ( "" ) ;
2721
2817
}
2722
2818
}
2723
2819
0 commit comments