<commit_before>// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.


use std::{cmp, panic};
use std::fs::OpenOptions;
use std::path::{Path, PathBuf};

use serde_json::{Value, from_reader};

use devicemapper::{Bytes, IEC, Sectors};

use super::logger::init_logger;
use super::util::clean_up;

use super::super::device::wipe_sectors;

pub struct RealTestDev {
    path: PathBuf,
}

impl RealTestDev {
    /// Construct a new test device for the given path.
    /// Wipe initial MiB to clear metadata.
    pub fn new(path: &Path) -> RealTestDev {
        wipe_sectors(path, Sectors(0), Bytes(IEC::Mi).sectors()).unwrap();
        RealTestDev { path: PathBuf::from(path) }
    }

    /// Get the device node of the device.
    fn as_path(&self) -> &Path {
        &self.path
    }
}

impl Drop for RealTestDev {
    fn drop(&mut self) {
        wipe_sectors(&self.path, Sectors(0), Bytes(IEC::Mi).sectors()).unwrap();
    }
}

pub enum DeviceLimits {
    Exactly(usize),
    AtLeast(usize),
    #[allow(dead_code)]
    Range(usize, usize), // inclusive
}

/// Get a list of counts of devices to use for tests.
/// None of the counts can be greater than avail.
fn get_device_counts(limits: DeviceLimits, avail: usize) -> Vec<usize> {
    // Convert enum to [lower, Option<upper>) values
    let (lower, maybe_upper) = match limits {
        DeviceLimits::Exactly(num) => (num, Some(num + 1)),
        DeviceLimits::AtLeast(num) => (num, None),
        DeviceLimits::Range(lower, upper) => {
            assert!(lower < upper);
            (lower, Some(upper + 1))
        }
    };

    let mut counts = vec![];

    // Check these values against available blockdevs
    if lower > avail {
        return counts;
    }

    counts.push(lower);

    if lower != avail {
        match maybe_upper {<commit_msg>Remove obsoleted allow(dead-code)<commit_after>// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.


use std::{cmp, panic};
use std::fs::OpenOptions;
use std::path::{Path, PathBuf};

use serde_json::{Value, from_reader};

use devicemapper::{Bytes, IEC, Sectors};

use super::logger::init_logger;
use super::util::clean_up;

use super::super::device::wipe_sectors;

pub struct RealTestDev {
    path: PathBuf,
}

impl RealTestDev {
    /// Construct a new test device for the given path.
    /// Wipe initial MiB to clear metadata.
    pub fn new(path: &Path) -> RealTestDev {
        wipe_sectors(path, Sectors(0), Bytes(IEC::Mi).sectors()).unwrap();
        RealTestDev { path: PathBuf::from(path) }
    }

    /// Get the device node of the device.
    fn as_path(&self) -> &Path {
        &self.path
    }
}

impl Drop for RealTestDev {
    fn drop(&mut self) {
        wipe_sectors(&self.path, Sectors(0), Bytes(IEC::Mi).sectors()).unwrap();
    }
}

pub enum DeviceLimits {
    Exactly(usize),
    AtLeast(usize),
    Range(usize, usize), // inclusive
}

/// Get a list of counts of devices to use for tests.
/// None of the counts can be greater than avail.
fn get_device_counts(limits: DeviceLimits, avail: usize) -> Vec<usize> {
    // Convert enum to [lower, Option<upper>) values
    let (lower, maybe_upper) = match limits {
        DeviceLimits::Exactly(num) => (num, Some(num + 1)),
        DeviceLimits::AtLeast(num) => (num, None),
        DeviceLimits::Range(lower, upper) => {
            assert!(lower < upper);
            (lower, Some(upper + 1))
        }
    };

    let mut counts = vec![];

    // Check these values against available blockdevs
    if lower > avail {
        return counts;
    }

    counts.push(lower);

    if lower != avail {
        match maybe_upper {
{'fraction_non_alphanumeric': 0.13846153846153847, 'fraction_numerical': 0.0041025641025641026, 'mean_word_length': 2.881592039800995, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5270602', 'n_tokens_mistral': 1291, 'n_tokens_neox': 1260, 'n_words': 383}