@@ -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" => {
@@ -2720,15 +2803,29 @@ impl<'test> TestCx<'test> {
2720
2803
return 0 ;
2721
2804
}
2722
2805
2723
- println ! ( "normalized {}:\n {}\n " , kind, actual) ;
2724
- println ! ( "expected {}:\n {}\n " , kind, expected) ;
2725
- println ! ( "diff of {}:\n " , kind) ;
2726
-
2727
- for diff in diff:: lines ( expected, actual) {
2728
- match diff {
2729
- diff:: Result :: Left ( l) => println ! ( "-{}" , l) ,
2730
- diff:: Result :: Both ( l, _) => println ! ( " {}" , l) ,
2731
- diff:: Result :: Right ( r) => println ! ( "+{}" , r) ,
2806
+ if expected. is_empty ( ) {
2807
+ println ! ( "normalized {}:\n {}\n " , kind, actual) ;
2808
+ } else {
2809
+ println ! ( "diff of {}:\n " , kind) ;
2810
+ let diff_results = make_diff ( expected, actual, 3 ) ;
2811
+ for result in diff_results {
2812
+ let mut line_number = result. line_number ;
2813
+ for line in result. lines {
2814
+ match line {
2815
+ DiffLine :: Expected ( e) => {
2816
+ println ! ( "-\t {}" , e) ;
2817
+ line_number += 1 ;
2818
+ } ,
2819
+ DiffLine :: Context ( c) => {
2820
+ println ! ( "{}\t {}" , line_number, c) ;
2821
+ line_number += 1 ;
2822
+ } ,
2823
+ DiffLine :: Resulting ( r) => {
2824
+ println ! ( "+\t {}" , r) ;
2825
+ } ,
2826
+ }
2827
+ }
2828
+ println ! ( "" ) ;
2732
2829
}
2733
2830
}
2734
2831
0 commit comments