]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/atomic-print.rs
New upstream version 1.28.0~beta.14+dfsg1
[rustc.git] / src / test / run-pass / atomic-print.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // ignore-cloudabi no process support
12 // ignore-emscripten no threads support
13
14 use std::{env, fmt, process, sync, thread};
15
16 struct SlowFmt(u32);
17 impl fmt::Debug for SlowFmt {
18 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
19 thread::sleep_ms(3);
20 self.0.fmt(f)
21 }
22 }
23
24 fn do_print(x: u32) {
25 let x = SlowFmt(x);
26 println!("{:?}{:?}{:?}{:?}{:?}", x, x, x, x, x);
27 }
28
29 fn main(){
30 if env::args().count() == 2 {
31 let barrier = sync::Arc::new(sync::Barrier::new(2));
32 let tbarrier = barrier.clone();
33 let t = thread::spawn(move || {
34 tbarrier.wait();
35 do_print(1);
36 });
37 barrier.wait();
38 do_print(2);
39 t.join();
40 } else {
41 let this = env::args().next().unwrap();
42 let output = process::Command::new(this).arg("-").output().unwrap();
43 for line in String::from_utf8(output.stdout).unwrap().lines() {
44 match line.chars().next().unwrap() {
45 '1' => assert_eq!(line, "11111"),
46 '2' => assert_eq!(line, "22222"),
47 chr => panic!("unexpected character {:?}", chr)
48 }
49 }
50 }
51 }