34
34
#![ cfg_attr( not( stage0) , deny( warnings) ) ]
35
35
36
36
#![ feature( asm) ]
37
- #![ feature( box_syntax) ]
38
- #![ feature( fnbox) ]
39
37
#![ feature( libc) ]
40
38
#![ feature( rustc_private) ]
41
39
#![ feature( set_stdio) ]
@@ -56,8 +54,7 @@ use self::TestEvent::*;
56
54
use self :: NamePadding :: * ;
57
55
use self :: OutputLocation :: * ;
58
56
59
- use std:: boxed:: FnBox ;
60
-
57
+ use std:: panic:: { catch_unwind, AssertUnwindSafe } ;
61
58
use std:: any:: Any ;
62
59
use std:: cmp;
63
60
use std:: collections:: BTreeMap ;
@@ -135,6 +132,16 @@ pub trait TDynBenchFn: Send {
135
132
fn run ( & self , harness : & mut Bencher ) ;
136
133
}
137
134
135
+ pub trait FnBox < T > : Send + ' static {
136
+ fn call_box ( self : Box < Self > , t : T ) ;
137
+ }
138
+
139
+ impl < T , F : FnOnce ( T ) + Send + ' static > FnBox < T > for F {
140
+ fn call_box ( self : Box < F > , t : T ) {
141
+ ( * self ) ( t)
142
+ }
143
+ }
144
+
138
145
// A function that runs a test. If the function returns successfully,
139
146
// the test succeeds; if the function panics then the test fails. We
140
147
// may need to come up with a more clever definition of test in order
@@ -143,8 +150,8 @@ pub enum TestFn {
143
150
StaticTestFn ( fn ( ) ) ,
144
151
StaticBenchFn ( fn ( & mut Bencher ) ) ,
145
152
StaticMetricFn ( fn ( & mut MetricMap ) ) ,
146
- DynTestFn ( Box < FnBox ( ) + Send > ) ,
147
- DynMetricFn ( Box < FnBox ( & mut MetricMap ) + Send > ) ,
153
+ DynTestFn ( Box < FnBox < ( ) > > ) ,
154
+ DynMetricFn ( Box <for < ' a > FnBox < & ' a mut MetricMap > >) ,
148
155
DynBenchFn ( Box < TDynBenchFn + ' static > ) ,
149
156
}
150
157
@@ -1137,23 +1144,25 @@ pub fn filter_tests(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> Vec<TestDescA
1137
1144
1138
1145
pub fn convert_benchmarks_to_tests ( tests : Vec < TestDescAndFn > ) -> Vec < TestDescAndFn > {
1139
1146
// convert benchmarks to tests, if we're not benchmarking them
1140
- tests. into_iter ( )
1141
- . map ( |x| {
1142
- let testfn = match x. testfn {
1143
- DynBenchFn ( bench) => {
1144
- DynTestFn ( Box :: new ( move || bench:: run_once ( |b| bench. run ( b) ) ) )
1145
- }
1146
- StaticBenchFn ( benchfn) => {
1147
- DynTestFn ( Box :: new ( move || bench:: run_once ( |b| benchfn ( b) ) ) )
1148
- }
1149
- f => f,
1150
- } ;
1151
- TestDescAndFn {
1152
- desc : x. desc ,
1153
- testfn : testfn,
1154
- }
1155
- } )
1156
- . collect ( )
1147
+ tests. into_iter ( ) . map ( |x| {
1148
+ let testfn = match x. testfn {
1149
+ DynBenchFn ( bench) => {
1150
+ DynTestFn ( Box :: new ( move |( ) | {
1151
+ bench:: run_once ( |b| bench. run ( b) )
1152
+ } ) )
1153
+ }
1154
+ StaticBenchFn ( benchfn) => {
1155
+ DynTestFn ( Box :: new ( move |( ) | {
1156
+ bench:: run_once ( |b| benchfn ( b) )
1157
+ } ) )
1158
+ }
1159
+ f => f,
1160
+ } ;
1161
+ TestDescAndFn {
1162
+ desc : x. desc ,
1163
+ testfn : testfn,
1164
+ }
1165
+ } ) . collect ( )
1157
1166
}
1158
1167
1159
1168
pub fn run_test ( opts : & TestOpts ,
@@ -1171,7 +1180,7 @@ pub fn run_test(opts: &TestOpts,
1171
1180
fn run_test_inner ( desc : TestDesc ,
1172
1181
monitor_ch : Sender < MonitorMsg > ,
1173
1182
nocapture : bool ,
1174
- testfn : Box < FnBox ( ) + Send > ) {
1183
+ testfn : Box < FnBox < ( ) > > ) {
1175
1184
struct Sink ( Arc < Mutex < Vec < u8 > > > ) ;
1176
1185
impl Write for Sink {
1177
1186
fn write ( & mut self , data : & [ u8 ] ) -> io:: Result < usize > {
@@ -1182,48 +1191,23 @@ pub fn run_test(opts: &TestOpts,
1182
1191
}
1183
1192
}
1184
1193
1185
- // If the platform is single-threaded we're just going to run
1186
- // the test synchronously, regardless of the concurrency
1187
- // level.
1188
- let supports_threads = !cfg ! ( target_os = "emscripten" ) ;
1189
-
1190
1194
// Buffer for capturing standard I/O
1191
1195
let data = Arc :: new ( Mutex :: new ( Vec :: new ( ) ) ) ;
1192
1196
let data2 = data. clone ( ) ;
1193
1197
1194
- if supports_threads {
1195
- thread:: spawn ( move || {
1196
- let cfg = thread:: Builder :: new ( ) . name ( match desc. name {
1197
- DynTestName ( ref name) => name. clone ( ) ,
1198
- StaticTestName ( name) => name. to_owned ( ) ,
1199
- } ) ;
1200
-
1201
- let result_guard = cfg. spawn ( move || {
1202
- if !nocapture {
1203
- io:: set_print ( Some ( box Sink ( data2. clone ( ) ) ) ) ;
1204
- io:: set_panic ( Some ( box Sink ( data2) ) ) ;
1205
- }
1206
- testfn ( )
1207
- } )
1208
- . unwrap ( ) ;
1209
- let test_result = calc_result ( & desc, result_guard. join ( ) ) ;
1210
- let stdout = data. lock ( ) . unwrap ( ) . to_vec ( ) ;
1211
- monitor_ch. send ( ( desc. clone ( ) , test_result, stdout) ) . unwrap ( ) ;
1212
- } ) ;
1213
- } else {
1198
+ let name = desc. name . clone ( ) ;
1199
+ let runtest = move || {
1214
1200
let oldio = if !nocapture {
1215
1201
Some ( (
1216
- io:: set_print ( Some ( box Sink ( data2. clone ( ) ) ) ) ,
1217
- io:: set_panic ( Some ( box Sink ( data2) ) )
1202
+ io:: set_print ( Some ( Box :: new ( Sink ( data2. clone ( ) ) ) ) ) ,
1203
+ io:: set_panic ( Some ( Box :: new ( Sink ( data2) ) ) )
1218
1204
) )
1219
1205
} else {
1220
1206
None
1221
1207
} ;
1222
1208
1223
- use std:: panic:: { catch_unwind, AssertUnwindSafe } ;
1224
-
1225
1209
let result = catch_unwind ( AssertUnwindSafe ( || {
1226
- testfn ( )
1210
+ testfn. call_box ( ( ) )
1227
1211
} ) ) ;
1228
1212
1229
1213
if let Some ( ( printio, panicio) ) = oldio {
@@ -1234,6 +1218,21 @@ pub fn run_test(opts: &TestOpts,
1234
1218
let test_result = calc_result ( & desc, result) ;
1235
1219
let stdout = data. lock ( ) . unwrap ( ) . to_vec ( ) ;
1236
1220
monitor_ch. send ( ( desc. clone ( ) , test_result, stdout) ) . unwrap ( ) ;
1221
+ } ;
1222
+
1223
+
1224
+ // If the platform is single-threaded we're just going to run
1225
+ // the test synchronously, regardless of the concurrency
1226
+ // level.
1227
+ let supports_threads = !cfg ! ( target_os = "emscripten" ) ;
1228
+ if supports_threads {
1229
+ let cfg = thread:: Builder :: new ( ) . name ( match name {
1230
+ DynTestName ( ref name) => name. clone ( ) ,
1231
+ StaticTestName ( name) => name. to_owned ( ) ,
1232
+ } ) ;
1233
+ cfg. spawn ( runtest) . unwrap ( ) ;
1234
+ } else {
1235
+ runtest ( ) ;
1237
1236
}
1238
1237
}
1239
1238
@@ -1250,7 +1249,7 @@ pub fn run_test(opts: &TestOpts,
1250
1249
}
1251
1250
DynMetricFn ( f) => {
1252
1251
let mut mm = MetricMap :: new ( ) ;
1253
- f. call_box ( ( & mut mm, ) ) ;
1252
+ f. call_box ( & mut mm) ;
1254
1253
monitor_ch. send ( ( desc, TrMetrics ( mm) , Vec :: new ( ) ) ) . unwrap ( ) ;
1255
1254
return ;
1256
1255
}
@@ -1261,7 +1260,8 @@ pub fn run_test(opts: &TestOpts,
1261
1260
return ;
1262
1261
}
1263
1262
DynTestFn ( f) => run_test_inner ( desc, monitor_ch, opts. nocapture , f) ,
1264
- StaticTestFn ( f) => run_test_inner ( desc, monitor_ch, opts. nocapture , Box :: new ( f) ) ,
1263
+ StaticTestFn ( f) => run_test_inner ( desc, monitor_ch, opts. nocapture ,
1264
+ Box :: new ( move |( ) | f ( ) ) ) ,
1265
1265
}
1266
1266
}
1267
1267
@@ -1496,7 +1496,7 @@ mod tests {
1496
1496
ignore : true ,
1497
1497
should_panic : ShouldPanic :: No ,
1498
1498
} ,
1499
- testfn : DynTestFn ( Box :: new ( move || f ( ) ) ) ,
1499
+ testfn : DynTestFn ( Box :: new ( move |( ) | f ( ) ) ) ,
1500
1500
} ;
1501
1501
let ( tx, rx) = channel ( ) ;
1502
1502
run_test ( & TestOpts :: new ( ) , false , desc, tx) ;
@@ -1513,7 +1513,7 @@ mod tests {
1513
1513
ignore : true ,
1514
1514
should_panic : ShouldPanic :: No ,
1515
1515
} ,
1516
- testfn : DynTestFn ( Box :: new ( move || f ( ) ) ) ,
1516
+ testfn : DynTestFn ( Box :: new ( move |( ) | f ( ) ) ) ,
1517
1517
} ;
1518
1518
let ( tx, rx) = channel ( ) ;
1519
1519
run_test ( & TestOpts :: new ( ) , false , desc, tx) ;
@@ -1532,7 +1532,7 @@ mod tests {
1532
1532
ignore : false ,
1533
1533
should_panic : ShouldPanic :: Yes ,
1534
1534
} ,
1535
- testfn : DynTestFn ( Box :: new ( move || f ( ) ) ) ,
1535
+ testfn : DynTestFn ( Box :: new ( move |( ) | f ( ) ) ) ,
1536
1536
} ;
1537
1537
let ( tx, rx) = channel ( ) ;
1538
1538
run_test ( & TestOpts :: new ( ) , false , desc, tx) ;
@@ -1551,7 +1551,7 @@ mod tests {
1551
1551
ignore : false ,
1552
1552
should_panic : ShouldPanic :: YesWithMessage ( "error message" ) ,
1553
1553
} ,
1554
- testfn : DynTestFn ( Box :: new ( move || f ( ) ) ) ,
1554
+ testfn : DynTestFn ( Box :: new ( move |( ) | f ( ) ) ) ,
1555
1555
} ;
1556
1556
let ( tx, rx) = channel ( ) ;
1557
1557
run_test ( & TestOpts :: new ( ) , false , desc, tx) ;
@@ -1570,7 +1570,7 @@ mod tests {
1570
1570
ignore : false ,
1571
1571
should_panic : ShouldPanic :: YesWithMessage ( "foobar" ) ,
1572
1572
} ,
1573
- testfn : DynTestFn ( Box :: new ( move || f ( ) ) ) ,
1573
+ testfn : DynTestFn ( Box :: new ( move |( ) | f ( ) ) ) ,
1574
1574
} ;
1575
1575
let ( tx, rx) = channel ( ) ;
1576
1576
run_test ( & TestOpts :: new ( ) , false , desc, tx) ;
@@ -1587,7 +1587,7 @@ mod tests {
1587
1587
ignore : false ,
1588
1588
should_panic : ShouldPanic :: Yes ,
1589
1589
} ,
1590
- testfn : DynTestFn ( Box :: new ( move || f ( ) ) ) ,
1590
+ testfn : DynTestFn ( Box :: new ( move |( ) | f ( ) ) ) ,
1591
1591
} ;
1592
1592
let ( tx, rx) = channel ( ) ;
1593
1593
run_test ( & TestOpts :: new ( ) , false , desc, tx) ;
@@ -1620,15 +1620,15 @@ mod tests {
1620
1620
ignore: true ,
1621
1621
should_panic: ShouldPanic :: No ,
1622
1622
} ,
1623
- testfn: DynTestFn ( Box :: new( move || { } ) ) ,
1623
+ testfn: DynTestFn ( Box :: new( move |( ) | { } ) ) ,
1624
1624
} ,
1625
1625
TestDescAndFn {
1626
1626
desc: TestDesc {
1627
1627
name: StaticTestName ( "2" ) ,
1628
1628
ignore: false ,
1629
1629
should_panic: ShouldPanic :: No ,
1630
1630
} ,
1631
- testfn: DynTestFn ( Box :: new( move || { } ) ) ,
1631
+ testfn: DynTestFn ( Box :: new( move |( ) | { } ) ) ,
1632
1632
} ] ;
1633
1633
let filtered = filter_tests ( & opts, tests) ;
1634
1634
@@ -1661,7 +1661,7 @@ mod tests {
1661
1661
ignore : false ,
1662
1662
should_panic : ShouldPanic :: No ,
1663
1663
} ,
1664
- testfn : DynTestFn ( Box :: new ( testfn) ) ,
1664
+ testfn : DynTestFn ( Box :: new ( move | ( ) | testfn ( ) ) ) ,
1665
1665
} ;
1666
1666
tests. push ( test) ;
1667
1667
}
0 commit comments