OXYGENHUB
# d3b270909c78 oxygen / vendor / crc-fast / src / test / enums.rs
2.3 KiB · Rust · last changed Strata: the cloud stores send through ureq, so every release target links 1 month ago source blame history raw
1
// Copyright 2025 Don MacAskill. Licensed under MIT or Apache-2.0.
2
3
#![cfg(test)]
4
#![allow(dead_code)]
5
6
use crate::test::structs::*;
7
use crate::CrcAlgorithm;
8
use crate::CrcParams;
9
use crc::Crc;
10
11
pub enum AnyCrcTestConfig {
12
    CRC16(&'static Crc16TestConfig),
13
    CRC32(&'static Crc32TestConfig),
14
    CRC64(&'static Crc64TestConfig),
15
}
16
17
impl AnyCrcTestConfig {
18
    pub fn get_params(&self) -> &CrcParams {
19
        match self {
20
            AnyCrcTestConfig::CRC16(cfg) => &cfg.params,
21
            AnyCrcTestConfig::CRC32(cfg) => &cfg.params,
22
            AnyCrcTestConfig::CRC64(cfg) => &cfg.params,
23
        }
24
    }
25
26
    pub fn get_width(&self) -> u8 {
27
        self.get_params().width
28
    }
29
30
    pub fn get_poly(&self) -> u64 {
31
        self.get_params().poly
32
    }
33
34
    pub fn get_refin(&self) -> bool {
35
        self.get_params().refin
36
    }
37
38
    pub fn get_algorithm(&self) -> CrcAlgorithm {
39
        self.get_params().algorithm
40
    }
41
42
    pub fn get_init(&self) -> u64 {
43
        self.get_params().init
44
    }
45
46
    pub fn get_init_algorithm(&self) -> u64 {
47
        self.get_params().init_algorithm
48
    }
49
50
    pub fn get_xorout(&self) -> u64 {
51
        self.get_params().xorout
52
    }
53
54
    pub fn get_check(&self) -> u64 {
55
        self.get_params().check
56
    }
57
58
    pub fn get_name(&self) -> &str {
59
        self.get_params().name
60
    }
61
62
    pub fn get_keys(&self) -> [u64; 23] {
63
        self.get_params().keys.to_keys_array_23()
64
    }
65
66
    pub fn checksum_with_reference(&self, data: &[u8]) -> u64 {
67
        match self {
68
            AnyCrcTestConfig::CRC16(cfg) => cfg.reference_impl.checksum(data) as u64,
69
            AnyCrcTestConfig::CRC32(cfg) => cfg.reference_impl.checksum(data) as u64,
70
            AnyCrcTestConfig::CRC64(cfg) => cfg.reference_impl.checksum(data),
71
        }
72
    }
73
74
    pub fn with_reference_impl<F, R>(&self, f: F) -> R
75
    where
76
        F: FnOnce(
77
            Option<&Crc<u16, crc::Table<16>>>,
78
            Option<&Crc<u32, crc::Table<16>>>,
79
            Option<&Crc<u64, crc::Table<16>>>,
80
        ) -> R,
81
    {
82
        match self {
83
            AnyCrcTestConfig::CRC16(cfg) => f(Some(cfg.reference_impl), None, None),
84
            AnyCrcTestConfig::CRC32(cfg) => f(None, Some(cfg.reference_impl), None),
85
            AnyCrcTestConfig::CRC64(cfg) => f(None, None, Some(cfg.reference_impl)),
86
        }
87
    }
88
}