#![forbid(unsafe_code)]
use nix::errno::Errno;
pub const fn kcov_hash64(s: &str) -> u64 {
let bytes = s.as_bytes();
let mut h: u64 = 0xcbf29ce484222325;
let mut i: usize = 0;
while i < bytes.len() {
h ^= bytes[i] as u64;
h = h.wrapping_mul(0x100000001b3);
i += 1;
}
h
}
pub fn record_pc(pc: u64) -> Result<(), Errno> {
crate::kcov::abi::record_pc(pc)
}
#[macro_export]
macro_rules! kcov_edge {
() => {{
const __KCOV_SITE: u64 = $crate::kcov::api::kcov_hash64(concat!(file!(), ":", line!()));
let _ = $crate::kcov::api::record_pc(__KCOV_SITE);
}};
($site:expr) => {{
let _ = $crate::kcov::api::record_pc(($site) as u64);
}};
}
#[macro_export]
macro_rules! kcov_edge_site {
($s:literal) => {{
const __KCOV_SITE: u64 = $crate::kcov::api::kcov_hash64($s);
let _ = $crate::kcov::api::record_pc(__KCOV_SITE);
}};
}
#[macro_export]
macro_rules! kcov_cmp {
($sz:expr, $isconst:expr, $a:expr, $b:expr) => {{
const __KCOV_SITE: u64 = $crate::kcov::api::kcov_hash64(concat!(file!(), ":", line!()));
let _ = $crate::kcov::api::record_cmp(
($sz) as u8,
($isconst),
($a) as u64,
($b) as u64,
__KCOV_SITE,
);
}};
($sz:expr, $isconst:expr, $a:expr, $b:expr, $site:expr) => {{
let _ = $crate::kcov::api::record_cmp(
($sz) as u8,
($isconst),
($a) as u64,
($b) as u64,
($site) as u64,
);
}};
}
#[macro_export]
macro_rules! kcov_cmp_site {
($sz:expr, $isconst:expr, $a:expr, $b:expr, $s:literal) => {{
const __KCOV_SITE: u64 = $crate::kcov::api::kcov_hash64($s);
let _ = $crate::kcov::api::record_cmp(
($sz) as u8,
($isconst),
($a) as u64,
($b) as u64,
__KCOV_SITE,
);
}};
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_kcov_hash64_empty_1() {
let h = kcov_hash64("");
assert_eq!(h, 0xcbf29ce484222325u64);
}
#[test]
fn test_kcov_hash64_deterministic_1() {
assert_eq!(kcov_hash64("foo"), kcov_hash64("foo"));
}
#[test]
fn test_kcov_hash64_different_inputs_1() {
assert_ne!(kcov_hash64("foo"), kcov_hash64("bar"));
}
#[test]
fn test_kcov_hash64_different_inputs_2() {
assert_ne!(kcov_hash64("a"), kcov_hash64("b"));
}
#[test]
fn test_kcov_hash64_const_eval_1() {
const H: u64 = kcov_hash64("syd");
assert_ne!(H, 0);
}
#[test]
fn test_kcov_hash64_known_value_1() {
let h = kcov_hash64("a");
assert_eq!(h, 0xaf63dc4c8601ec8cu64);
}
}