1
Fork 0
mirror of https://github.com/redstrate/Physis.git synced 2025-04-25 22:07:44 +00:00
physis/src/common_file_operations.rs

33 lines
879 B
Rust
Raw Normal View History

// SPDX-FileCopyrightText: 2024 Joshua Goins <josh@redstrate.com>
// SPDX-License-Identifier: GPL-3.0-or-later
pub(crate) fn read_bool_from<T: std::convert::From<u8> + std::cmp::PartialEq>(x: T) -> bool {
x == T::from(1u8)
}
pub(crate) fn write_bool_as<T: std::convert::From<u8>>(x: &bool) -> T {
if *x { T::from(1u8) } else { T::from(0u8) }
}
#[cfg(test)]
mod tests {
use std::io::Cursor;
use crate::common_file_operations::{read_bool_from, write_bool_as};
use crate::model::MDL;
const DATA: [u8; 2] = [0u8, 1u8];
// TODO: add tests for u16
#[test]
fn read_bool_u8() {
assert!(!read_bool_from::<u8>(DATA[0]));
assert!(read_bool_from::<u8>(DATA[1]));
}
#[test]
fn write_bool_u8() {
assert_eq!(write_bool_as::<u8>(&false), DATA[0]);
assert_eq!(write_bool_as::<u8>(&true), DATA[1]);
}
}