[jets] add mod test

This commit is contained in:
Philip Monk 2023-02-14 15:42:42 -07:00
parent fd94db487f
commit 38399d12b0
2 changed files with 36 additions and 18 deletions

View File

@ -33,8 +33,8 @@ pub fn get_jet(jet_name: Noun) -> Result<Jet, ()> {
tas!(b"sub") => Ok(jet_sub),
tas!(b"mul") => Ok(jet_mul),
tas!(b"div") => Ok(jet_div),
tas!(b"dvr") => Ok(jet_dvr),
tas!(b"mod") => Ok(jet_mod),
tas!(b"dvr") => Ok(jet_dvr),
tas!(b"lth") => Ok(jet_lth),
tas!(b"lte") => Ok(jet_lte),
tas!(b"gth") => Ok(jet_gth),

View File

@ -156,6 +156,23 @@ pub fn jet_div(stack: &mut NockStack, subject: Noun) -> Result<Noun, JetErr> {
}
}
pub fn jet_mod(stack: &mut NockStack, subject: Noun) -> Result<Noun, JetErr> {
let arg = raw_slot(subject, 6);
let a = raw_slot(arg, 2).as_atom()?;
let b = raw_slot(arg, 3).as_atom()?;
if unsafe { b.as_noun().raw_equals(D(0)) } {
Err(Deterministic)
} else {
if let (Ok(a), Ok(b)) = (a.as_direct(), b.as_direct()) {
Ok(unsafe { DirectAtom::new_unchecked(a.data() % b.data()) }.as_noun())
} else {
let res = a.as_ubig() % b.as_ubig();
Ok(Atom::from_ubig(stack, &res).as_noun())
}
}
}
pub fn jet_dvr(stack: &mut NockStack, subject: Noun) -> Result<Noun, JetErr> {
let arg = raw_slot(subject, 6);
let a = raw_slot(arg, 2).as_atom()?;
@ -184,23 +201,6 @@ pub fn jet_dvr(stack: &mut NockStack, subject: Noun) -> Result<Noun, JetErr> {
}
}
pub fn jet_mod(stack: &mut NockStack, subject: Noun) -> Result<Noun, JetErr> {
let arg = raw_slot(subject, 6);
let a = raw_slot(arg, 2).as_atom()?;
let b = raw_slot(arg, 3).as_atom()?;
if unsafe { b.as_noun().raw_equals(D(0)) } {
Err(Deterministic)
} else {
if let (Ok(a), Ok(b)) = (a.as_direct(), b.as_direct()) {
Ok(unsafe { DirectAtom::new_unchecked(a.data() % b.data()) }.as_noun())
} else {
let res = a.as_ubig() % b.as_ubig();
Ok(Atom::from_ubig(stack, &res).as_noun())
}
}
}
pub fn jet_lth(_stack: &mut NockStack, subject: Noun) -> Result<Noun, JetErr> {
let arg = raw_slot(subject, 6);
let a = raw_slot(arg, 2).as_atom()?;
@ -650,6 +650,24 @@ mod tests {
assert_math_jet_err(s, jet_div, &[atom_0, atom_0], Deterministic);
}
#[test]
fn test_mod() {
let ref mut s = init();
assert_math_jet(
s,
jet_mod,
&[atom_128, atom_96],
ubig!(0xcb0ce564ec598f658409d170),
);
assert_math_jet(s, jet_mod, &[atom_96, atom_63], ubig!(0x15deadc0e4af946e));
assert_math_jet(s, jet_mod, &[atom_63, atom_96], ubig!(0x7fffffffffffffff));
assert_math_jet(s, jet_mod, &[atom_63, atom_63], ubig!(0));
assert_math_jet(s, jet_mod, &[atom_63, atom_24], ubig!(0x798385));
assert_math_jet(s, jet_mod, &[atom_128, atom_24], ubig!(0x3b2013));
assert_math_jet_err(s, jet_mod, &[atom_63, atom_0], Deterministic);
assert_math_jet_err(s, jet_mod, &[atom_0, atom_0], Deterministic);
}
#[test]
fn test_dvr() {
let ref mut s = init();