This repository has been archived by the owner on Jun 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 106
encode_unsigned_1 #676
Open
mlwilkerson
wants to merge
17
commits into
GetFirefly:develop
Choose a base branch
from
mlwilkerson:encode_unsigned
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
encode_unsigned_1 #676
Changes from 4 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d28fed9
Initial impl and test of encode_unsigned_1
mlwilkerson ed869b0
update handling for non-integer arg and negative proptest
mlwilkerson b42e063
handle negative integers as bardarg and add proptests
mlwilkerson d70d7aa
auto-formatting
mlwilkerson 665cde9
Fix handling of leading/trailing zeros
mlwilkerson 4b8dfde
auto-formatting
mlwilkerson 1b1abdb
Fix bug with BigInt with trailing zeros
mlwilkerson 3576682
small refactor
mlwilkerson 184951d
add doctest as integration test
mlwilkerson ebff6bc
move to module binary
mlwilkerson 32e0433
move integration test to binary module
mlwilkerson b807dcc
hook in the binary integration test module
mlwilkerson b31bdf0
auto-formatting
mlwilkerson edab51e
migrate integration test with_smallest_big_int
mlwilkerson 112820f
migrate more integration tests
mlwilkerson 25d0dd4
implement remaining tests as integration tests
mlwilkerson d7515e1
Merge branch 'develop' into encode_unsigned
KronicDeth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#[cfg(all(not(target_arch = "wasm32"), test))] | ||
mod test; | ||
|
||
use crate::runtime::context::{term_is_not_integer, term_is_not_non_negative_integer}; | ||
use anyhow::*; | ||
use liblumen_alloc::erts::exception; | ||
use liblumen_alloc::erts::process::Process; | ||
use liblumen_alloc::erts::term::prelude::*; | ||
use num_bigint::Sign; | ||
|
||
/// Returns the smallest possible representation in a binary digit representation for the given big | ||
/// endian unsigned integer. | ||
#[native_implemented::function(erlang:encode_unsigned/1)] | ||
pub fn result(process: &Process, term: Term) -> exception::Result<Term> { | ||
match term.decode().unwrap() { | ||
TypedTerm::SmallInteger(small_integer) => { | ||
let signed: isize = small_integer.into(); | ||
if signed < 0 { | ||
return Err(TryIntoIntegerError::Type) | ||
.context(term_is_not_non_negative_integer("encoded_unsigned", term)) | ||
.map_err(From::from); | ||
} | ||
let mut bytes: Vec<u8> = small_integer | ||
.to_le_bytes() | ||
.iter() | ||
.filter_map(|&b| match b { | ||
0 => None, | ||
b => Some(b), | ||
}) | ||
.collect(); | ||
|
||
bytes.reverse(); | ||
|
||
Ok(process.binary_from_bytes(&bytes)) | ||
} | ||
TypedTerm::BigInteger(big_integer) => { | ||
if Sign::Minus == big_integer.sign() { | ||
return Err(TryIntoIntegerError::Type) | ||
.context(term_is_not_non_negative_integer("encoded_unsigned", term)) | ||
.map_err(From::from); | ||
} | ||
|
||
let bytes: Vec<u8> = big_integer | ||
.to_signed_bytes_be() | ||
.iter() | ||
.filter_map(|&b| match b { | ||
0 => None, | ||
b => Some(b), | ||
}) | ||
.collect(); | ||
|
||
Ok(process.binary_from_bytes(&bytes)) | ||
} | ||
_ => Err(TryIntoIntegerError::Type) | ||
.context(term_is_not_integer("encoded_unsigned", term)) | ||
.map_err(From::from), | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
native_implemented/otp/src/erlang/encode_unsigned_1/test.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use crate::erlang::encode_unsigned_1::result; | ||
use crate::test::with_process; | ||
use crate::test::*; | ||
use liblumen_alloc::erts::term::prelude::*; | ||
use num_bigint::BigInt; | ||
use proptest::strategy::Just; | ||
|
||
// 1> binary:encode_unsigned(11111111). | ||
// <<169,138,199>> | ||
#[test] | ||
fn otp_doctest() { | ||
with_process(|process| { | ||
assert_eq!( | ||
result(process, process.integer(11111111)), | ||
Ok(process.binary_from_bytes(&[169, 138, 199])) | ||
) | ||
}); | ||
} | ||
|
||
#[test] | ||
fn smallest_big_int() { | ||
let largest_small_int_as_big_int: BigInt = SmallInteger::MAX_VALUE.into(); | ||
let smallest_big_int: BigInt = largest_small_int_as_big_int + 1; | ||
|
||
// 1> binary:encode_unsigned(70368744177664). | ||
// <<64,0,0,0,0,0>> | ||
|
||
with_process(|process| { | ||
assert_eq!( | ||
result(process, process.integer(smallest_big_int)), | ||
Ok(process.binary_from_bytes(&[64])) | ||
) | ||
}); | ||
} | ||
|
||
#[test] | ||
fn negative_integer() { | ||
run!( | ||
|arc_process| { | ||
( | ||
Just(arc_process.clone()), | ||
strategy::term::integer::negative(arc_process.clone()), | ||
) | ||
}, | ||
|(arc_process, non_int)| { | ||
prop_assert_badarg!(result(&arc_process, non_int), "invalid integer conversion"); | ||
Ok(()) | ||
}, | ||
); | ||
} | ||
|
||
#[test] | ||
fn not_integer() { | ||
run!( | ||
|arc_process| { | ||
( | ||
Just(arc_process.clone()), | ||
strategy::term::is_not_integer(arc_process.clone()), | ||
) | ||
}, | ||
|(arc_process, non_int)| { | ||
prop_assert_badarg!(result(&arc_process, non_int), "invalid integer conversion"); | ||
Ok(()) | ||
}, | ||
); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're moving away from unit tests and preferring writing integration tests in Erlang now under
native_implemented/otp/tests/internal/lib
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, does that also mean that we're moving away from proptest as well?
How do I run these integration tests in local dev?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Never mind, for now. I think I figured out how to wire up and run the erlang integration tests.