Documentation
/*
==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--

Debt64

Copyright (C) 2018-2019, 2021-2023  Anonymous

There are several releases over multiple years,
they are listed as ranges, such as: "2018-2019".

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--
*/

use {
    core::{
        str::FromStr,
        time::Duration,
    },
    std::{
        env,
        time::Instant,
    },
    debt64::{Debt64, Result},
};

/// # Gets test duration
fn duration() -> Duration {
    match env::var("TEST_DURATION").map(|s| u8::from_str(&s)) {
        Ok(Ok(duration)) => Duration::from_secs(duration.max(1) as u64),
        _ => Duration::from_secs(10),
    }
}

#[test]
#[ignore]
fn encode() {
    let duration = duration();
    let start_time = Instant::now();
    let bytes = debt64::ID.as_bytes();
    let mut count: usize = 0;
    loop {
        let standard = Debt64::Standard.encode(&bytes);
        let imap = Debt64::IMAP.encode(&standard.as_bytes()[..bytes.len()]);
        let mime = Debt64::MIME.encode(&imap.as_bytes()[..bytes.len()]);
        let url = Debt64::URL.encode(&mime.as_bytes()[..bytes.len()]);
        assert!(Debt64::FreenetURL.encode(&url.as_bytes()[..bytes.len()]).is_empty() == false);
        match Instant::now().duration_since(start_time) <= duration {
            true => count = match count.checked_add(5) {
                Some(count) => count,
                None => break,
            },
            false => break,
        };
    }
    println!("{:?}, count: {} >>> {} << encoded/second >>", duration, count, count as f64 / duration.as_secs() as f64);
}

#[test]
#[ignore]
fn decode() -> Result<()> {
    let bytes = debt64::ID.as_bytes();
    let standard = Debt64::Standard.encode(bytes);
    let imap = Debt64::IMAP.encode(bytes);
    let mime = Debt64::MIME.encode(bytes);
    let url = Debt64::URL.encode(bytes);
    let freenet_url = Debt64::FreenetURL.encode(bytes);

    let duration = duration();
    let start_time = Instant::now();
    let mut count: usize = 0;
    loop {
        Debt64::Standard.decode(&standard)?;
        Debt64::IMAP.decode(&imap)?;
        Debt64::MIME.decode(&mime)?;
        Debt64::URL.decode(&url)?;
        Debt64::FreenetURL.decode(&freenet_url)?;
        match Instant::now().duration_since(start_time) <= duration {
            true => count = match count.checked_add(5) {
                Some(count) => count,
                None => break,
            },
            false => break,
        };
    }
    println!("{:?}, count: {} --> {} [[ decoded/second ]]", duration, count, count as f64 / duration.as_secs() as f64);

    Ok(())
}