disktest_lib/
seed.rs

1// -*- coding: utf-8 -*-
2//
3// disktest - Storage tester
4//
5// Copyright 2020-2024 Michael Büsch <m@bues.ch>
6//
7// Licensed under the Apache License version 2.0
8// or the MIT license, at your option.
9// SPDX-License-Identifier: Apache-2.0 OR MIT
10//
11
12use rand::distributions::Alphanumeric;
13use rand::{thread_rng, Rng};
14
15/// Generate a new alphanumeric truly random seed.
16///
17/// length: The number of ASCII characters to return.
18pub fn gen_seed_string(length: usize) -> String {
19    let rng = thread_rng();
20    rng.sample_iter(Alphanumeric)
21        .take(length)
22        .map(char::from)
23        .collect()
24}
25
26#[cfg(test)]
27mod tests {
28    use super::*;
29
30    #[test]
31    fn test_gen() {
32        // Check returned ASCII string length.
33        let seed = gen_seed_string(42);
34        assert_eq!(seed.len(), 42);
35        assert_eq!(seed.chars().count(), 42);
36    }
37}
38
39// vim: ts=4 sw=4 expandtab