Skip to content
This repository has been archived by the owner on Jun 10, 2024. It is now read-only.

Commit

Permalink
>=/2 infix operator
Browse files Browse the repository at this point in the history
Implemented as `erlang::is_greater_than_or_equal_2`.
  • Loading branch information
KronicDeth committed Apr 17, 2019
1 parent 11aeb0a commit a0c4ca0
Show file tree
Hide file tree
Showing 16 changed files with 2,021 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lumen_runtime/src/otp/erlang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,11 @@ pub fn is_greater_than_2(left: Term, right: Term) -> Term {
left.gt(&right).into()
}

/// `>=/2` infix operator. Floats and integers are converted.
pub fn is_greater_than_or_equal_2(left: Term, right: Term) -> Term {
left.ge(&right).into()
}

pub fn is_integer_1(term: Term) -> Term {
match term.tag() {
SmallInteger => true,
Expand Down
1 change: 1 addition & 0 deletions lumen_runtime/src/otp/erlang/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ mod is_bitstring_1;
mod is_boolean_1;
mod is_float_1;
mod is_greater_than_2;
mod is_greater_than_or_equal_2;
mod is_integer_1;
mod is_less_than_2;
mod is_list_1;
Expand Down
31 changes: 31 additions & 0 deletions lumen_runtime/src/otp/erlang/tests/is_greater_than_or_equal_2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use super::*;

mod with_atom_left;
mod with_big_integer_left;
mod with_empty_list_left;
mod with_external_pid_left;
mod with_float_left;
mod with_heap_binary_left;
mod with_list_left;
mod with_local_pid_left;
mod with_local_reference_left;
mod with_map_left;
mod with_small_integer_left;
mod with_subbinary_left;
mod with_tuple_left;

fn is_greater_than_or_equal<L, R>(left: L, right: R, expected: bool)
where
L: FnOnce(&mut Process) -> Term,
R: FnOnce(Term, &mut Process) -> Term,
{
with_process(|mut process| {
let left = left(&mut process);
let right = right(left, &mut process);

assert_eq!(
erlang::is_greater_than_or_equal_2(left, right),
expected.into()
);
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
use super::*;

#[test]
fn with_small_integer_right_returns_true() {
is_greater_than_or_equal(|_, mut process| 0.into_process(&mut process), true)
}

#[test]
fn with_big_integer_right_returns_true() {
is_greater_than_or_equal(
|_, mut process| (crate::integer::small::MAX + 1).into_process(&mut process),
true,
)
}

#[test]
fn with_float_right_returns_true() {
is_greater_than_or_equal(|_, mut process| 0.0.into_process(&mut process), true)
}

#[test]
fn with_greater_atom_returns_true() {
is_greater_than_or_equal(|_, _| Term::str_to_atom("keft", DoNotCare).unwrap(), true);
}

#[test]
fn with_same_atom_returns_true() {
is_greater_than_or_equal(|left, _| left, true);
}

#[test]
fn with_same_atom_value_returns_true() {
is_greater_than_or_equal(|_, _| Term::str_to_atom("left", DoNotCare).unwrap(), true);
}

#[test]
fn with_greater_atom_returns_false() {
is_greater_than_or_equal(|_, _| Term::str_to_atom("meft", DoNotCare).unwrap(), false);
}

#[test]
fn with_local_reference_right_returns_false() {
is_greater_than_or_equal(|_, mut process| Term::local_reference(&mut process), false);
}

#[test]
fn with_local_pid_right_returns_false() {
is_greater_than_or_equal(|_, _| Term::local_pid(0, 1).unwrap(), false);
}

#[test]
fn with_external_pid_right_returns_false() {
is_greater_than_or_equal(
|_, mut process| Term::external_pid(1, 2, 3, &mut process).unwrap(),
false,
);
}

#[test]
fn with_tuple_right_returns_false() {
is_greater_than_or_equal(
|_, mut process| Term::slice_to_tuple(&[], &mut process),
false,
);
}

#[test]
fn with_map_right_returns_false() {
is_greater_than_or_equal(
|_, mut process| Term::slice_to_map(&[], &mut process),
false,
);
}

#[test]
fn with_empty_list_right_returns_false() {
is_greater_than_or_equal(|_, _| Term::EMPTY_LIST, false);
}

#[test]
fn with_list_right_returns_false() {
is_greater_than_or_equal(
|_, mut process| {
Term::cons(
0.into_process(&mut process),
1.into_process(&mut process),
&mut process,
)
},
false,
);
}

#[test]
fn with_heap_binary_right_returns_false() {
is_greater_than_or_equal(
|_, mut process| Term::slice_to_binary(&[], &mut process),
false,
);
}

#[test]
fn with_subbinary_right_returns_false() {
is_greater_than_or_equal(|_, mut process| bitstring!(1 :: 1, &mut process), false);
}

fn is_greater_than_or_equal<R>(right: R, expected: bool)
where
R: FnOnce(Term, &mut Process) -> Term,
{
super::is_greater_than_or_equal(
|_| Term::str_to_atom("left", DoNotCare).unwrap(),
right,
expected,
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
use super::*;

#[test]
fn with_greater_small_integer_right_returns_true() {
is_greater_than_or_equal(|_, mut process| 0.into_process(&mut process), true)
}

#[test]
fn with_greater_small_integer_right_returns_false() {
super::is_greater_than_or_equal(
|mut process| (crate::integer::small::MIN - 1).into_process(&mut process),
|_, mut process| crate::integer::small::MIN.into_process(&mut process),
false,
);
}

#[test]
fn with_greater_big_integer_right_returns_true() {
is_greater_than_or_equal(
|_, mut process| (crate::integer::small::MIN - 1).into_process(&mut process),
true,
)
}

#[test]
fn with_same_big_integer_right_returns_true() {
is_greater_than_or_equal(|left, _| left, true)
}

#[test]
fn with_same_value_big_integer_right_returns_true() {
is_greater_than_or_equal(
|_, mut process| (crate::integer::small::MAX + 1).into_process(&mut process),
true,
)
}

#[test]
fn with_greater_big_integer_right_returns_false() {
is_greater_than_or_equal(
|_, mut process| (crate::integer::small::MAX + 2).into_process(&mut process),
false,
)
}

#[test]
fn with_greater_float_right_returns_true() {
is_greater_than_or_equal(|_, mut process| 0.0.into_process(&mut process), true)
}

#[test]
fn with_greater_float_right_returns_false() {
super::is_greater_than_or_equal(
|mut process| (crate::integer::small::MIN - 1).into_process(&mut process),
|_, mut process| 0.0.into_process(&mut process),
false,
);
}

#[test]
fn with_atom_right_returns_false() {
is_greater_than_or_equal(|_, _| Term::str_to_atom("right", DoNotCare).unwrap(), false);
}

#[test]
fn with_local_reference_right_returns_false() {
is_greater_than_or_equal(|_, mut process| Term::local_reference(&mut process), false);
}

#[test]
fn with_local_pid_right_returns_false() {
is_greater_than_or_equal(|_, _| Term::local_pid(0, 1).unwrap(), false);
}

#[test]
fn with_external_pid_right_returns_false() {
is_greater_than_or_equal(
|_, mut process| Term::external_pid(1, 2, 3, &mut process).unwrap(),
false,
);
}

#[test]
fn with_tuple_right_returns_false() {
is_greater_than_or_equal(
|_, mut process| Term::slice_to_tuple(&[], &mut process),
false,
);
}

#[test]
fn with_map_right_returns_false() {
is_greater_than_or_equal(
|_, mut process| Term::slice_to_map(&[], &mut process),
false,
);
}

#[test]
fn with_empty_list_right_returns_false() {
is_greater_than_or_equal(|_, _| Term::EMPTY_LIST, false);
}

#[test]
fn with_list_right_returns_false() {
is_greater_than_or_equal(
|_, mut process| {
Term::cons(
0.into_process(&mut process),
1.into_process(&mut process),
&mut process,
)
},
false,
);
}

#[test]
fn with_heap_binary_right_returns_false() {
is_greater_than_or_equal(
|_, mut process| Term::slice_to_binary(&[], &mut process),
false,
);
}

#[test]
fn with_subbinary_right_returns_false() {
is_greater_than_or_equal(|_, mut process| bitstring!(1 :: 1, &mut process), false);
}

fn is_greater_than_or_equal<R>(right: R, expected: bool)
where
R: FnOnce(Term, &mut Process) -> Term,
{
super::is_greater_than_or_equal(
|mut process| (crate::integer::small::MAX + 1).into_process(&mut process),
right,
expected,
);
}

0 comments on commit a0c4ca0

Please sign in to comment.