Skip to content

Commit 83accf0

Browse files
committed
chore: Increased coverage and disabled coverage for error strings
1 parent 93cad1a commit 83accf0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+279
-30
lines changed

src/data/partition.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ pub enum PartitionError {
7777

7878
impl std::error::Error for PartitionError {}
7979

80+
#[cfg_attr(coverage_nightly, coverage(off))]
8081
impl fmt::Display for PartitionError {
8182
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
8283
match self {

src/dist/bernoulli.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ impl Variance<f64> for Bernoulli {
369369

370370
impl std::error::Error for BernoulliError {}
371371

372+
#[cfg_attr(coverage_nightly, coverage(off))]
372373
impl fmt::Display for BernoulliError {
373374
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
374375
match self {

src/dist/beta.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ impl_traits!(f64);
450450

451451
impl std::error::Error for BetaError {}
452452

453+
#[cfg_attr(coverage_nightly, coverage(off))]
453454
impl fmt::Display for BetaError {
454455
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
455456
match self {

src/dist/beta_binom.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ impl_int_traits!(i64);
427427

428428
impl std::error::Error for BetaBinomialError {}
429429

430+
#[cfg_attr(coverage_nightly, coverage(off))]
430431
impl fmt::Display for BetaBinomialError {
431432
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
432433
match self {

src/dist/betaprime.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ impl Default for BetaPrime {
250250
}
251251
}
252252

253+
#[cfg_attr(coverage_nightly, coverage(off))]
253254
impl From<&BetaPrime> for String {
254255
fn from(bp: &BetaPrime) -> String {
255256
format!("BetaPrime(α: {}, β: {})", bp.alpha, bp.beta)
@@ -358,6 +359,7 @@ impl Variance<f64> for BetaPrime {
358359

359360
impl std::error::Error for BetaPrimeError {}
360361

362+
#[cfg_attr(coverage_nightly, coverage(off))]
361363
impl fmt::Display for BetaPrimeError {
362364
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
363365
match self {
@@ -448,11 +450,14 @@ mod tests {
448450
use rand_xoshiro::Xoshiro256Plus;
449451

450452
use super::*;
451-
use crate::test_basic_impls;
453+
use crate::{
454+
misc::{KsAlternative, KsMode, ks_two_sample},
455+
test_basic_impls,
456+
};
452457

453458
const TOL: f64 = 1E-12;
454459

455-
test_basic_impls!(f64, BetaPrime, BetaPrime::new(1.0, 1.0).unwrap());
460+
test_basic_impls!(f64, BetaPrime, BetaPrime::default());
456461

457462
#[test]
458463
fn new() {
@@ -564,6 +569,29 @@ mod tests {
564569
}
565570
}
566571

572+
#[test]
573+
fn draw_and_sample_are_consistent() {
574+
let mut rng = rand::rng();
575+
let bp = BetaPrime::new(2.0, 3.0).unwrap();
576+
577+
let draw_sample: Vec<f64> =
578+
(0..1000).map(|_| bp.draw(&mut rng)).collect();
579+
let sample_sample: Vec<f64> = bp.sample(1000, &mut rng);
580+
581+
let (stat, p) = ks_two_sample(
582+
&draw_sample,
583+
&sample_sample,
584+
KsMode::Auto,
585+
KsAlternative::TwoSided,
586+
)
587+
.unwrap();
588+
589+
dbg!(&p);
590+
dbg!(stat);
591+
592+
assert!(p > 0.05);
593+
}
594+
567595
#[cfg(feature = "experimental")]
568596
#[test]
569597
fn test_ln_m_cache_consistency() {

src/dist/categorical.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ impl KlDivergence for Categorical {
301301
}
302302
}
303303

304+
#[cfg_attr(coverage_nightly, coverage(off))]
304305
impl fmt::Display for CategoricalError {
305306
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
306307
match self {

src/dist/cauchy.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ impl_traits!(f32);
287287

288288
impl std::error::Error for CauchyError {}
289289

290+
#[cfg_attr(coverage_nightly, coverage(off))]
290291
impl fmt::Display for CauchyError {
291292
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
292293
match self {

src/dist/cdvm.rs

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ use std::fmt;
3535
// }
3636
// }
3737

38-
/// [CDVM distribution](https://arxiv.org/pdf/2009.05437),
38+
/// Conditionalized Discrete von Mises (CDVM)
39+
///
40+
/// This is defined under "Definition 4" in
41+
/// [Families of discrete circular distributions with some novel applications](https://arxiv.org/pdf/2009.05437),
3942
/// A unimodal distribution over x in (0, m-1) where m is the number of categories.
4043
///
4144
/// Note that in while the paper uses μ ∈ [0, 2π), we use μ ∈ [0, m)
@@ -90,6 +93,20 @@ impl Cdvm {
9093
/// * `mu` - mean direction (must be in [0, modulus))
9194
/// * `k` - concentration (must be non-negative)
9295
/// * `modulus` - Number of categories
96+
///
97+
/// # Example
98+
/// ```rust
99+
/// use rv::prelude::*;
100+
/// use rv::dist::{Cdvm, CdvmError};
101+
///
102+
/// assert!(matches!(Cdvm::new(5, f64::INFINITY, 0.0), Err(CdvmError::MuNotFinite { .. })));
103+
/// assert!(matches!(Cdvm::new(5, 0.0, f64::INFINITY), Err(CdvmError::KNotFinite { .. })));
104+
/// assert!(matches!(Cdvm::new(5, 0.0, -1.0), Err(CdvmError::KNegative { .. })));
105+
/// assert!(matches!(Cdvm::new(2, 1.0, 1.0), Err(CdvmError::InvalidCategories { .. })));
106+
///
107+
/// let cdvm = Cdvm::new(3, 1.0, 1.0).expect("valid parameters");
108+
///
109+
/// ```
93110
pub fn new(modulus: usize, mu: f64, k: f64) -> Result<Self, CdvmError> {
94111
// Validate parameters
95112
if !mu.is_finite() {
@@ -110,14 +127,14 @@ impl Cdvm {
110127

111128
// Test that dependent fields are properly set
112129
// This is just for testing purposes
113-
#[must_use]
114-
pub fn is_consistent(&self) -> bool {
130+
#[cfg(test)]
131+
fn is_consistent(&self) -> bool {
115132
let other = Cdvm::new(self.modulus, self.mu, self.k).unwrap();
116-
self.mu == other.mu
117-
&& self.k == other.k
118-
&& self.modulus == other.modulus
119-
&& self.log_norm_const == other.log_norm_const
120-
&& self.twopi_over_m == other.twopi_over_m
133+
self.mu() == other.mu()
134+
&& self.k() == other.k()
135+
&& self.modulus() == other.modulus()
136+
&& self.log_norm_const() == other.log_norm_const()
137+
&& self.twopi_over_m() == other.twopi_over_m()
121138
}
122139

123140
/// Creates a new CDVM without checking whether the parameters are valid.
@@ -258,6 +275,7 @@ impl_display!(Cdvm);
258275

259276
impl std::error::Error for CdvmError {}
260277

278+
#[cfg_attr(coverage_nightly, coverage(off))]
261279
impl fmt::Display for CdvmError {
262280
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
263281
match self {
@@ -322,8 +340,11 @@ impl HasSuffStat<usize> for Cdvm {
322340

323341
#[cfg(test)]
324342
mod tests {
343+
use crate::misc::x2_test;
344+
325345
use super::*;
326346
use proptest::prelude::*;
347+
use rand::{SeedableRng, rngs::SmallRng};
327348

328349
const TOL: f64 = 1E-12;
329350

@@ -486,4 +507,29 @@ mod tests {
486507
"CDVM not consistent after set_mu: m={}, mu1={}, mu2={}, k={}", m, mu1, mu2, k);
487508
}
488509
}
510+
511+
#[test]
512+
fn f_is_probability_measure() {
513+
let dist = Cdvm::new_unchecked(10, 5.0, 0.5);
514+
515+
assert::close((0..10).map(|i| dist.f(&i)).sum::<f64>(), 1.0, 1e-10);
516+
}
517+
518+
#[test]
519+
fn ln_f_agrees_with_draw() {
520+
let mut rng = SmallRng::from_os_rng();
521+
let dist = Cdvm::new_unchecked(10, 5.0, 0.5);
522+
523+
let sample = dist.sample(100_000, &mut rng);
524+
let ps: Vec<f64> = (0..10).map(|i| dist.f(&i)).collect();
525+
526+
let observed_counts =
527+
sample.into_iter().fold(vec![0; 10], |mut acc, x| {
528+
acc[x] += 1;
529+
acc
530+
});
531+
532+
let (_, p) = x2_test(&observed_counts, &ps);
533+
assert!(p > 0.05);
534+
}
489535
}

src/dist/chi_squared.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ impl_traits!(f32);
229229

230230
impl std::error::Error for ChiSquaredError {}
231231

232+
#[cfg_attr(coverage_nightly, coverage(off))]
232233
impl fmt::Display for ChiSquaredError {
233234
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
234235
match self {

src/dist/crp.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ impl Support<Partition> for Crp {
274274

275275
impl std::error::Error for CrpError {}
276276

277+
#[cfg_attr(coverage_nightly, coverage(off))]
277278
impl fmt::Display for CrpError {
278279
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
279280
match self {

0 commit comments

Comments
 (0)