| 1 |
// Tests that ensure weird (but valid) usage behave as expected. |
| 2 |
|
| 3 |
// Miri cannot discover benchmarks. |
| 4 |
#![cfg(not(miri))] |
| 5 |
|
| 6 |
use std::time::Duration; |
| 7 |
|
| 8 |
extern crate codspeed_divan_compat_walltime as divan; |
| 9 |
use divan::{__private::BENCH_ENTRIES, Divan}; |
| 10 |
|
| 11 |
#[divan::bench(bytes_count = 0u8, chars_count = 0u16, cycles_count = 0u32, items_count = 0u64)] |
| 12 |
fn zero_throughput() {} |
| 13 |
|
| 14 |
#[divan::bench(min_time = Duration::ZERO)] |
| 15 |
fn min_min() {} |
| 16 |
|
| 17 |
#[divan::bench(max_time = Duration::MAX)] |
| 18 |
fn max_max() {} |
| 19 |
|
| 20 |
#[divan::bench] |
| 21 |
fn lifetime<'a>() -> &'a str { |
| 22 |
"hello" |
| 23 |
} |
| 24 |
|
| 25 |
#[divan::bench] |
| 26 |
fn embedded() { |
| 27 |
#[divan::bench] |
| 28 |
fn inner() { |
| 29 |
#[divan::bench] |
| 30 |
fn inner() {} |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
#[divan::bench] |
| 35 |
fn r#raw_ident() {} |
| 36 |
|
| 37 |
#[divan::bench(r#name = "raw name ident")] |
| 38 |
fn raw_name_ident() {} |
| 39 |
|
| 40 |
#[divan::bench] |
| 41 |
extern "system" fn extern_abi_1() {} |
| 42 |
|
| 43 |
#[divan::bench] |
| 44 |
#[allow(improper_ctypes_definitions)] |
| 45 |
extern "C" fn extern_abi_2(_: divan::Bencher) {} |
| 46 |
|
| 47 |
#[divan::bench(types = [i32, u8])] |
| 48 |
extern "system" fn extern_abi_3<T>() {} |
| 49 |
|
| 50 |
#[divan::bench(r#types = [i32, u8])] |
| 51 |
#[allow(improper_ctypes_definitions)] |
| 52 |
extern "C" fn extern_abi_4<T>(_: divan::Bencher) {} |
| 53 |
|
| 54 |
#[divan::bench(consts = [0, -1, isize::MAX])] |
| 55 |
extern "system" fn extern_abi_5<const N: isize>() {} |
| 56 |
|
| 57 |
#[divan::bench(consts = [0, -1, isize::MAX])] |
| 58 |
#[allow(improper_ctypes_definitions)] |
| 59 |
extern "C" fn extern_abi_6<const N: isize>(_: divan::Bencher) {} |
| 60 |
|
| 61 |
macro_rules! consts { |
| 62 |
() => { |
| 63 |
[0, -1, isize::MAX] |
| 64 |
}; |
| 65 |
} |
| 66 |
|
| 67 |
#[divan::bench(consts = consts!())] |
| 68 |
fn bench_consts<const N: isize>() {} |
| 69 |
|
| 70 |
#[divan::bench(args = [])] |
| 71 |
fn empty_args(_: usize) {} |
| 72 |
|
| 73 |
#[divan::bench(types = [])] |
| 74 |
#[allow(dead_code)] |
| 75 |
fn empty_types<T>() {} |
| 76 |
|
| 77 |
#[divan::bench(consts = [])] |
| 78 |
#[allow(dead_code)] |
| 79 |
fn empty_consts<const C: usize>() {} |
| 80 |
|
| 81 |
#[divan::bench(args = [], consts = [])] |
| 82 |
#[allow(dead_code)] |
| 83 |
fn empty_args_consts<const C: usize>(_: usize) {} |
| 84 |
|
| 85 |
#[divan::bench(types = [], consts = [])] |
| 86 |
#[allow(dead_code)] |
| 87 |
fn empty_types_consts_1<T, const C: usize>() {} |
| 88 |
|
| 89 |
#[divan::bench(consts = [], types = [])] |
| 90 |
#[allow(dead_code)] |
| 91 |
fn empty_types_consts_2<T, const C: usize>() {} |
| 92 |
|
| 93 |
#[divan::bench(types = [], consts = [])] |
| 94 |
#[allow(dead_code)] |
| 95 |
fn empty_types_consts_3<const C: usize, T>() {} |
| 96 |
|
| 97 |
#[divan::bench(consts = [], types = [])] |
| 98 |
#[allow(dead_code)] |
| 99 |
fn empty_types_consts_4<const C: usize, T>() {} |
| 100 |
|
| 101 |
#[test] |
| 102 |
fn test_fn() { |
| 103 |
Divan::default().test_benches(); |
| 104 |
} |
| 105 |
|
| 106 |
// Test that each function appears the expected number of times. |
| 107 |
#[test] |
| 108 |
fn count() { |
| 109 |
let mut inner_count = 0; |
| 110 |
|
| 111 |
for entry in BENCH_ENTRIES.iter() { |
| 112 |
if entry.meta.raw_name == "inner" { |
| 113 |
inner_count += 1; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
assert_eq!(inner_count, 2); |
| 118 |
} |
| 119 |
|
| 120 |
// Test expected `BenchEntry.path` values. |
| 121 |
#[test] |
| 122 |
fn path() { |
| 123 |
for entry in BENCH_ENTRIES.iter() { |
| 124 |
// Embedded functions do not contain their parent function's name in |
| 125 |
// their `module_path!()`. |
| 126 |
if entry.meta.raw_name == "inner" { |
| 127 |
assert_eq!(entry.meta.module_path, "weird_usage"); |
| 128 |
} |
| 129 |
|
| 130 |
// "r#" is removed from raw identifiers. |
| 131 |
if entry.meta.raw_name.contains("raw_ident") { |
| 132 |
assert_eq!(entry.meta.raw_name, "r#raw_ident"); |
| 133 |
assert_eq!(entry.meta.display_name, "raw_ident"); |
| 134 |
} |
| 135 |
} |
| 136 |
} |