Test lshift of zero#2154
Merged
Merged
Conversation
youknowone
requested changes
Aug 27, 2020
Comment on lines
195
to
216
| if int2.is_negative() { | ||
| return Err(vm.new_value_error("negative shift count".to_owned())); | ||
| } else if int1.is_zero() { | ||
| return Ok(vm.ctx.new_int(0)); | ||
| } else if *int2 > BigInt::from(usize::max_value()) { | ||
| return Err(vm.new_overflow_error("the number is too large to convert to int".to_owned())) | ||
| } else { | ||
| return Ok(vm.ctx.new_int(int1 << int2.to_usize().expect("Failed converting {} to rust usize"))); | ||
| } | ||
| } | ||
|
|
||
| fn inner_rshift(int1: &BigInt, int2: &BigInt, vm: &VirtualMachine) -> PyResult { | ||
| let n_bits = get_shift_amount(int2, vm)?; | ||
| Ok(vm.ctx.new_int(int1 >> n_bits)) | ||
| if int2.is_negative() { | ||
| return Err(vm.new_value_error("negative shift count".to_owned())); | ||
| } else if int1.is_zero() { | ||
| return Ok(vm.ctx.new_int(0)); | ||
| } else if *int2 > BigInt::from(usize::max_value()) { | ||
| return Err(vm.new_overflow_error("the number is too large to convert to int".to_owned())) | ||
| } else { | ||
| return Ok(vm.ctx.new_int(int1 >> int2.to_usize().expect("Failed converting {} to rust usize"))); | ||
| } | ||
| } |
Member
There was a problem hiding this comment.
It seems only shift operation is different and the other parts are the same. If we introduce a closure, they can be combined into a single function
Suggested change
| if int2.is_negative() { | |
| return Err(vm.new_value_error("negative shift count".to_owned())); | |
| } else if int1.is_zero() { | |
| return Ok(vm.ctx.new_int(0)); | |
| } else if *int2 > BigInt::from(usize::max_value()) { | |
| return Err(vm.new_overflow_error("the number is too large to convert to int".to_owned())) | |
| } else { | |
| return Ok(vm.ctx.new_int(int1 << int2.to_usize().expect("Failed converting {} to rust usize"))); | |
| } | |
| } | |
| fn inner_rshift(int1: &BigInt, int2: &BigInt, vm: &VirtualMachine) -> PyResult { | |
| let n_bits = get_shift_amount(int2, vm)?; | |
| Ok(vm.ctx.new_int(int1 >> n_bits)) | |
| if int2.is_negative() { | |
| return Err(vm.new_value_error("negative shift count".to_owned())); | |
| } else if int1.is_zero() { | |
| return Ok(vm.ctx.new_int(0)); | |
| } else if *int2 > BigInt::from(usize::max_value()) { | |
| return Err(vm.new_overflow_error("the number is too large to convert to int".to_owned())) | |
| } else { | |
| return Ok(vm.ctx.new_int(int1 >> int2.to_usize().expect("Failed converting {} to rust usize"))); | |
| } | |
| } | |
| fn inner_shift<F>(int1: &BigInt, int2: &BigInt, shift_op: F, vm: &VirtualMachine) -> PyResult | |
| where | |
| F: Fn(&BitInt, usize) -> BigInt | |
| { | |
| if int2.is_negative() { | |
| Err(vm.new_value_error("negative shift count".to_owned())); | |
| } else if int1.is_zero() { | |
| Ok(vm.ctx.new_int(0)); | |
| } else { | |
| let int2 = int2.to_usize().ok_or_else(|| { | |
| vm.new_overflow_error("the number is too large to convert to int".to_owned())) | |
| })?; | |
| Ok(vm.ctx.new_int(shift_op(int1, int2).expect("Failed converting {} to rust usize"))); | |
| } | |
| } |
youknowone
reviewed
Aug 28, 2020
Comment on lines
+194
to
+200
| fn inner_lshift(int1: &BigInt, int2: &BigInt, vm: &VirtualMachine) -> PyResult { | ||
| let n_bits = get_shift_amount(int2, vm)?; | ||
| Ok(vm.ctx.new_int(int1 << n_bits)) | ||
| inner_shift(int1, int2, |a, b| a << b, vm) | ||
| } | ||
|
|
||
| fn inner_rshift(int1: &BigInt, int2: &BigInt, vm: &VirtualMachine) -> PyResult { | ||
| let n_bits = get_shift_amount(int2, vm)?; | ||
| Ok(vm.ctx.new_int(int1 >> n_bits)) | ||
| inner_shift(int1, int2, |a, b| a >> b, vm) | ||
| } |
Member
There was a problem hiding this comment.
we don't need one line inner_lshift and inner_rshift anymore. caling inner_shift in each shift funciton will be enough.
youknowone
reviewed
Aug 29, 2020
| #[pymethod(name = "__rlshift__")] | ||
| fn rlshift(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { | ||
| self.general_op(other, |a, b| inner_lshift(b, a, vm), vm) | ||
| self.general_op(other, |a, b| inner_shift(a, b, |a, b| a << b, vm), vm) |
youknowone
approved these changes
Sep 5, 2020
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
It seems kinda rough code though, it passes the tests.
I'd like to get advice to improve this code.