rust: more tests, refactors & improvements
- separate tests and constructors into submodules for cells and channels - add missing functions channels_set_bg_palindex & channels_set_fg_palindex - fix bug in channel_palindex_p function - wrap NCSTYLE_* constants - add more missing cell tests - improve many doc comments - fix some typespull/1170/head
parent
535fffa9d5
commit
279f36ec93
@ -0,0 +1,29 @@
|
||||
//! Handy [`NcCell`] constructors
|
||||
|
||||
pub use crate::{NcCell, NcChannels, NcCharBackstop, NcStyleMask};
|
||||
|
||||
impl NcCell {
|
||||
/// [`NcCell`] constructor expecting [`char`], [`NcStyleMask`] and [`NcChannels`]
|
||||
#[inline]
|
||||
pub const fn new(ch: char, stylemask: NcStyleMask, channels: NcChannels) -> Self {
|
||||
NcCell {
|
||||
gcluster: ch as u32,
|
||||
gcluster_backstop: 0 as NcCharBackstop,
|
||||
reserved: 0,
|
||||
stylemask,
|
||||
channels,
|
||||
}
|
||||
}
|
||||
|
||||
/// [`NcCell`] simple constructor just expecting a [`char`]
|
||||
#[inline]
|
||||
pub const fn with_char(ch: char) -> Self {
|
||||
Self::new(ch, 0 as NcStyleMask, 0 as NcChannels)
|
||||
}
|
||||
|
||||
/// [`NcCell`] simple constructor for an empty cell
|
||||
#[inline]
|
||||
pub const fn new_blank() -> Self {
|
||||
Self::with_char(0 as char)
|
||||
}
|
||||
}
|
@ -0,0 +1,130 @@
|
||||
//! [`NcCell`] tests
|
||||
|
||||
use crate::NcCell;
|
||||
|
||||
use serial_test::serial;
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
fn constructors() {
|
||||
let _c1 = NcCell::new_blank();
|
||||
|
||||
let _c2 = NcCell::with_char('C');
|
||||
|
||||
let _c3 = NcCell::new('c', 0, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
fn channels() {
|
||||
let mut c1 = NcCell::new_blank();
|
||||
assert_eq![0, crate::cell_fchannel(&c1)];
|
||||
assert_eq![0, crate::cell_bchannel(&c1)];
|
||||
|
||||
crate::cell_set_fchannel(&mut c1, 0x77112233);
|
||||
assert_eq![0x77112233, crate::cell_fchannel(&c1)];
|
||||
crate::cell_set_bchannel(&mut c1, 0x88445566);
|
||||
assert_eq![0x88445566, crate::cell_bchannel(&c1)];
|
||||
|
||||
let c2 = NcCell::new(' ', 0, 0x0011223300445566);
|
||||
assert_eq![0x112233, crate::cell_fchannel(&c2)];
|
||||
assert_eq![0x445566, crate::cell_bchannel(&c2)];
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
fn rgb() {
|
||||
// rgb
|
||||
|
||||
let mut c1 = NcCell::new_blank();
|
||||
assert_eq![0, crate::cell_fg_rgb(&c1)];
|
||||
assert_eq![0, crate::cell_bg_rgb(&c1)];
|
||||
|
||||
crate::cell_set_fg_rgb(&mut c1, 0x99112233);
|
||||
assert_eq![0x112233, crate::cell_fg_rgb(&c1)];
|
||||
crate::cell_set_bg_rgb(&mut c1, 0x99445566);
|
||||
assert_eq![0x445566, crate::cell_bg_rgb(&c1)];
|
||||
|
||||
// rgb8
|
||||
|
||||
let mut c2 = NcCell::new_blank();
|
||||
let (mut r, mut g, mut b) = (0, 0, 0);
|
||||
|
||||
crate::cell_set_fg_rgb8(&mut c2, 0x11, 0x22, 0x33);
|
||||
let fchannel = crate::cell_fg_rgb8(&c2, &mut r, &mut g, &mut b);
|
||||
assert_eq!((0x11, 0x22, 0x33), (r, g, b));
|
||||
assert_eq![0x112233, fchannel & !crate::NCCELL_BGDEFAULT_MASK];
|
||||
|
||||
crate::cell_set_bg_rgb8(&mut c2, 0x44, 0x55, 0x66);
|
||||
let bchannel = crate::cell_bg_rgb8(&c2, &mut r, &mut g, &mut b);
|
||||
assert_eq!((0x44, 0x55, 0x66), (r, g, b));
|
||||
assert_eq![0x445566, bchannel & !crate::NCCELL_BGDEFAULT_MASK];
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
fn alpha() {
|
||||
let mut c1 = NcCell::new_blank();
|
||||
assert_eq![0, crate::cell_fg_alpha(&c1)];
|
||||
assert_eq![0, crate::cell_bg_alpha(&c1)];
|
||||
|
||||
crate::cell_set_fg_alpha(&mut c1, crate::NCCELL_ALPHA_TRANSPARENT);
|
||||
assert_eq![crate::NCCELL_ALPHA_TRANSPARENT, crate::cell_fg_alpha(&c1)];
|
||||
|
||||
crate::cell_set_bg_alpha(&mut c1, crate::NCCELL_ALPHA_BLEND);
|
||||
assert_eq![crate::NCCELL_ALPHA_BLEND, crate::cell_bg_alpha(&c1)];
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
fn default() {
|
||||
let mut c1 = NcCell::new_blank();
|
||||
assert_eq![true, crate::cell_fg_default_p(&c1)];
|
||||
assert_eq![true, crate::cell_bg_default_p(&c1)];
|
||||
|
||||
// rgb
|
||||
crate::cell_set_fg_rgb(&mut c1, 0x112233);
|
||||
crate::cell_set_bg_rgb(&mut c1, 0x445566);
|
||||
assert_eq![false, crate::cell_fg_default_p(&c1)];
|
||||
assert_eq![false, crate::cell_bg_default_p(&c1)];
|
||||
|
||||
// reset
|
||||
crate::cell_set_fg_default(&mut c1);
|
||||
crate::cell_set_bg_default(&mut c1);
|
||||
assert_eq![true, crate::cell_fg_default_p(&c1)];
|
||||
assert_eq![true, crate::cell_bg_default_p(&c1)];
|
||||
|
||||
// rgb8
|
||||
crate::cell_set_fg_rgb8(&mut c1, 0x11, 0x22, 0x33);
|
||||
crate::cell_set_bg_rgb8(&mut c1, 0x44, 0x55, 0x66);
|
||||
assert_eq![false, crate::cell_fg_default_p(&c1)];
|
||||
assert_eq![false, crate::cell_bg_default_p(&c1)];
|
||||
|
||||
// reset
|
||||
crate::cell_set_fg_default(&mut c1);
|
||||
crate::cell_set_bg_default(&mut c1);
|
||||
|
||||
// palette
|
||||
crate::cell_set_fg_palindex(&mut c1, 5);
|
||||
crate::cell_set_bg_palindex(&mut c1, 6);
|
||||
assert_eq![false, crate::cell_fg_default_p(&c1)];
|
||||
assert_eq![false, crate::cell_bg_default_p(&c1)];
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
fn palette() {
|
||||
let mut c1 = NcCell::new_blank();
|
||||
assert_eq![false, crate::cell_fg_palindex_p(&c1)];
|
||||
assert_eq![false, crate::cell_bg_palindex_p(&c1)];
|
||||
assert_eq![0, crate::cell_fg_palindex(&c1)];
|
||||
assert_eq![0, crate::cell_bg_palindex(&c1)];
|
||||
|
||||
crate::cell_set_fg_palindex(&mut c1, 5);
|
||||
crate::cell_set_bg_palindex(&mut c1, 6);
|
||||
assert_eq![true, crate::cell_fg_palindex_p(&c1)];
|
||||
assert_eq![true, crate::cell_bg_palindex_p(&c1)];
|
||||
|
||||
assert_eq![5, crate::cell_fg_palindex(&c1)];
|
||||
assert_eq![6, crate::cell_bg_palindex(&c1)];
|
||||
}
|
@ -0,0 +1,153 @@
|
||||
//! [`NcChannel`] & [`NcChannels`] tests
|
||||
|
||||
use crate::{
|
||||
NcChannel, NcChannels, NCCELL_ALPHA_BLEND, NCCELL_ALPHA_HIGHCONTRAST, NCCELL_ALPHA_OPAQUE,
|
||||
NCCELL_ALPHA_TRANSPARENT,
|
||||
};
|
||||
|
||||
use serial_test::serial;
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
fn channel_r() {
|
||||
let c: NcChannel = 0x112233;
|
||||
assert_eq!(crate::channel_r(c), 0x11);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
fn channel_g() {
|
||||
let c: NcChannel = 0x112233;
|
||||
assert_eq!(crate::channel_g(c), 0x22);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
fn channel_b() {
|
||||
let c: NcChannel = 0x112233;
|
||||
assert_eq!(crate::channel_b(c), 0x33);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
fn channel_rgb8() {
|
||||
let c: NcChannel = 0x112233;
|
||||
let mut r = 0;
|
||||
let mut g = 0;
|
||||
let mut b = 0;
|
||||
crate::channel_rgb8(c, &mut r, &mut g, &mut b);
|
||||
assert_eq!(r, 0x11);
|
||||
assert_eq!(g, 0x22);
|
||||
assert_eq!(b, 0x33);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
fn channel_set_rgb8() {
|
||||
let mut c: NcChannel = 0x000000;
|
||||
crate::channel_set_rgb8(&mut c, 0x11, 0x22, 0x33);
|
||||
assert_eq!(crate::channel_r(c), 0x11);
|
||||
assert_eq!(crate::channel_g(c), 0x22);
|
||||
assert_eq!(crate::channel_b(c), 0x33);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
fn channel_alpha() {
|
||||
let c: NcChannel = 0x112233 | NCCELL_ALPHA_TRANSPARENT;
|
||||
assert_eq!(crate::channel_alpha(c), NCCELL_ALPHA_TRANSPARENT);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
fn channel_set_alpha() {
|
||||
let mut c: NcChannel = 0x112233;
|
||||
crate::channel_set_alpha(&mut c, NCCELL_ALPHA_HIGHCONTRAST);
|
||||
assert_eq!(NCCELL_ALPHA_HIGHCONTRAST, crate::channel_alpha(c));
|
||||
|
||||
crate::channel_set_alpha(&mut c, NCCELL_ALPHA_TRANSPARENT);
|
||||
assert_eq!(NCCELL_ALPHA_TRANSPARENT, crate::channel_alpha(c));
|
||||
|
||||
crate::channel_set_alpha(&mut c, NCCELL_ALPHA_BLEND);
|
||||
assert_eq!(NCCELL_ALPHA_BLEND, crate::channel_alpha(c));
|
||||
|
||||
crate::channel_set_alpha(&mut c, NCCELL_ALPHA_OPAQUE);
|
||||
assert_eq!(NCCELL_ALPHA_OPAQUE, crate::channel_alpha(c));
|
||||
// TODO: CHECK for NCCELL_BGDEFAULT_MASK
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
fn channel_set_default() {
|
||||
const DEFAULT: NcChannel = 0x112233;
|
||||
|
||||
let mut c: NcChannel = DEFAULT | NCCELL_ALPHA_TRANSPARENT;
|
||||
assert!(c != DEFAULT);
|
||||
|
||||
crate::channel_set_default(&mut c);
|
||||
assert_eq!(c, DEFAULT);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
fn channel_default_p() {
|
||||
let mut c: NcChannel = 0x112233;
|
||||
assert_eq!(true, crate::channel_default_p(c));
|
||||
|
||||
let _ = crate::channel_set_alpha(&mut c, NCCELL_ALPHA_OPAQUE);
|
||||
assert_eq!(true, crate::channel_default_p(c));
|
||||
|
||||
crate::channel_set(&mut c, 0x112233);
|
||||
assert_eq!(false, crate::channel_default_p(c));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
#[allow(non_snake_case)]
|
||||
fn channels_set_fchannel__channels_fchannel() {
|
||||
let fc: NcChannel = 0x112233;
|
||||
let mut cp: NcChannels = 0;
|
||||
crate::channels_set_fchannel(&mut cp, fc);
|
||||
assert_eq!(crate::channels_fchannel(cp), fc);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
#[allow(non_snake_case)]
|
||||
fn channels_set_bchannel__channels_bchannel() {
|
||||
let bc: NcChannel = 0x112233;
|
||||
let mut cp: NcChannels = 0;
|
||||
crate::channels_set_bchannel(&mut cp, bc);
|
||||
assert_eq!(crate::channels_bchannel(cp), bc);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
fn channels_combine() {
|
||||
let bc: NcChannel = 0x112233;
|
||||
let fc: NcChannel = 0x445566;
|
||||
let mut cp1: NcChannels = 0;
|
||||
let mut _cp2: NcChannels = 0;
|
||||
crate::channels_set_bchannel(&mut cp1, bc);
|
||||
crate::channels_set_fchannel(&mut cp1, fc);
|
||||
_cp2 = crate::channels_combine(fc, bc);
|
||||
assert_eq!(cp1, _cp2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
fn channels_palette() {
|
||||
let bc: NcChannel = 0x112233;
|
||||
let fc: NcChannel = 0x445566;
|
||||
assert_eq!(false, crate::channel_palindex_p(bc));
|
||||
assert_eq!(false, crate::channel_palindex_p(fc));
|
||||
|
||||
let mut channels = crate::channels_combine(fc, bc);
|
||||
assert_eq!(false, crate::channels_fg_palindex_p(channels));
|
||||
assert_eq!(false, crate::channels_bg_palindex_p(channels));
|
||||
|
||||
crate::channels_set_fg_palindex(&mut channels, 5);
|
||||
crate::channels_set_bg_palindex(&mut channels, 6);
|
||||
assert_eq!(true, crate::channels_fg_palindex_p(channels));
|
||||
assert_eq!(true, crate::channels_bg_palindex_p(channels));
|
||||
}
|
Loading…
Reference in New Issue