• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2016 The vulkano developers
2 // Licensed under the Apache License, Version 2.0
3 // <LICENSE-APACHE or
4 // https://www.apache.org/licenses/LICENSE-2.0> or the MIT
5 // license <LICENSE-MIT or https://opensource.org/licenses/MIT>,
6 // at your option. All files in the project carrying such
7 // notice may not be copied, modified, or distributed except
8 // according to those terms.
9 
10 #![cfg(test)]
11 
12 /// Creates an instance or returns if initialization fails.
13 macro_rules! instance {
14     () => {{
15         use crate::instance;
16         use crate::Version;
17 
18         match instance::Instance::new(
19             None,
20             Version::V1_1,
21             &instance::InstanceExtensions::none(),
22             None,
23         ) {
24             Ok(i) => i,
25             Err(_) => return,
26         }
27     }};
28 }
29 
30 /// Creates a device and a queue for graphics operations.
31 macro_rules! gfx_dev_and_queue {
32     ($($feature:ident),*) => ({
33         use crate::device::physical::PhysicalDevice;
34         use crate::device::Device;
35         use crate::device::DeviceExtensions;
36         use crate::device::Features;
37 
38         let instance = instance!();
39 
40         let physical = match PhysicalDevice::enumerate(&instance).next() {
41             Some(p) => p,
42             None => return
43         };
44 
45         let queue = match physical.queue_families().find(|q| q.supports_graphics()) {
46             Some(q) => q,
47             None => return
48         };
49 
50         let extensions = DeviceExtensions::none();
51 
52         let features = Features {
53             $(
54                 $feature: true,
55             )*
56             .. Features::none()
57         };
58 
59         // If the physical device doesn't support the requested features, just return.
60         if !physical.supported_features().is_superset_of(&features) {
61             return;
62         }
63 
64         let (device, mut queues) = match Device::new(physical, &features,
65                                                      &extensions, [(queue, 0.5)].iter().cloned())
66         {
67             Ok(r) => r,
68             Err(_) => return
69         };
70 
71         (device, queues.next().unwrap())
72     });
73 }
74 
75 macro_rules! assert_should_panic {
76     ($msg:expr, $code:block) => {{
77         let res = ::std::panic::catch_unwind(::std::panic::AssertUnwindSafe(|| $code));
78 
79         match res {
80             Ok(_) => panic!("Test expected to panic but didn't"),
81             Err(err) => {
82                 if let Some(msg) = err.downcast_ref::<String>() {
83                     assert!(msg.contains($msg));
84                 } else if let Some(&msg) = err.downcast_ref::<&str>() {
85                     assert!(msg.contains($msg));
86                 } else {
87                     panic!("Couldn't decipher the panic message of the test")
88                 }
89             }
90         }
91     }};
92 
93     ($code:block) => {{
94         let res = ::std::panic::catch_unwind(::std::panic::AssertUnwindSafe(|| $code));
95 
96         match res {
97             Ok(_) => panic!("Test expected to panic but didn't"),
98             Err(_) => {}
99         }
100     }};
101 }
102