-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed turn_towards #40
Conversation
Hi! I tried out your change locally and it definitely fixes the bug. Thank you for submitting this fix and figuring out what was going on! I am ready to merge this, but if you would like to do a little more, I have two suggestions for additional changes that would make this even better:
Though both of these are probably things I should have done when I wrote the method, I think you must have a pretty good understanding of the method in order to make this fix. That means that you're in a great position to make these additions. If you don't want to do one or both of these things, or don't have time, please let me know. There is no pressure. In that case, I'll merge your changes immediately and add those two items to my own to-do list. 😄 If you're unsure, it may be worth giving it a shot to see if you can do it. I'm happy to help you with any questions you might have. If you want me to, I'll even setup a sample test for you so you can just add additional cases that you think are important. Let me know what you're comfortable with. :) |
Thanks for your assistance! |
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.
Thanks for adding all of that! The comments you added are very informative!
Your test looks good. Could you also add a test to make sure the turtle can turn from any of the 8 cardinal directions to any other cardinal directions? You can do that either as part of your current test or split into a new one. Your current test only checks if the angle is increasing but I would also like to make sure that the method results in the correct heading. In that new test, it would be great if you could assert that the heading actually equals the intended value. Let me know if you want me to give you more of an idea of how to write that test. :)
src/turtle.rs
Outdated
#[test] | ||
fn turn_towards() { | ||
let mut turtle = Turtle::new(); | ||
turtle.set_speed("instant"); |
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.
This line shouldn't be necessary since we disable all animations during tests. Try running the tests with cargo test --features test
and let me know if for some reason keeping this line actually makes a difference.
src/turtle.rs
Outdated
fn turn_towards() { | ||
let mut turtle = Turtle::new(); | ||
turtle.set_speed("instant"); | ||
let mut last_delta_angle = 0.0; |
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.
Please replace this line with the following code to get rid of the warning that occurs when this compiles.
// This variable is used in the assert!(), but Rust still warns us because its value isn't
// used to do anything other than that. We ignore this error since this is okay for tests.
#[allow(unused_assignments)]
let mut last_delta_angle = 0.0;
I have updated the test according to your requests. |
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.
Thank you for updating the PR and working with me to get this feature correct! Your contribution is going to make a huge difference. 😄
It's really good that we're writing these tests because I found a bug that wasn't revealed earlier when I went through and tried this out. Take a look at my review comments to find out more. I wrote you a little test application which should help you see the problem and debug for yourself.
Feel free to let me know if you have any questions!
P.S: setting the turtle speed to instant doesn't seem to change anything. You can see it in the followmouse example, where the turtle does not jump directly to the cursor position, nor turns immediately.
To clarify, examples (like followmouse
) run animations because those are meant to actually be seen by users. Tests do not run examples because they have no UI and are meant to complete as fast as possible. In tests, we're really just trying to make sure we get the right values out of our functions/methods.
src/turtle.rs
Outdated
for n in 0..16 as u32 { | ||
let original_angle = radians::TWO_PI * n as f64 / 16.0; | ||
turtle.turn_towards([original_angle.cos(), original_angle.sin()]); | ||
assert_eq!(turtle.heading().ceil(), original_angle.to_degrees().ceil()); |
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.
Please use round()
instead of ceil()
because we want to make sure that rounding differences are caught by these tests.
@@ -1156,4 +1158,22 @@ mod tests { | |||
assert_eq!(turtle.position()[1].round(), 100.0); | |||
assert_eq!(turtle.heading(), 51.0); | |||
} | |||
|
|||
#[test] | |||
fn turn_towards() { |
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.
Thanks for updating this! I think you need to move lines 1168-1170 into the inner loop. Otherwise you are rotating to original_angle only once, then essentially rotating in a circle without actually going back to the original angle. The goal is to test that you can rotate from any angle to any angle. That means we need to go back to the original angle every time in order to make an exhaustive test.
If you make that change, you'll find that the test actually doesn't pass anymore. In fact, I made a little demo application which reveals that a bug still remains in the implementation of turn_towards
.
Try running this by overwriting the contents of an example like examples/circle.rs
and then running it with cargo run --example circle
(don't commit that change, this is just to help you debug) Click to advance to each rotation. This should be a really helpful tool for you to debug what is going on. Feel free to modify it as you see fit in order to help you figure out the problem.
extern crate turtle;
use turtle::Turtle;
use std::f64::consts::PI;
fn main() {
let mut turtle = Turtle::new();
// Turn from each cardinal direction to each cardinal direction
for n in 0..16 as u32 {
let original_angle = 2.*PI * n as f64 / 16.0;
for i in 0..16 as u32 {
println!("{:?}", (n, i));
turtle.wait_for_click();
turtle.turn_towards([original_angle.cos(), original_angle.sin()]);
//assert_eq!(turtle.heading().ceil(), original_angle.to_degrees().ceil());
turtle.wait(1.0);
let target_angle = 2.*PI * i as f64 / 16.0;
turtle.turn_towards([target_angle.cos(), target_angle.sin()]);
//assert_eq!(turtle.heading().ceil(), target_angle.to_degrees().ceil());
turtle.forward(100.0);
turtle.backward(100.0);
}
turtle.wait_for_click();
turtle.clear();
}
}
Notice that this doesn't produce a full circle of 16 lines. There are "gaps" which are the result of a bug in the calculation that still needs to be fixed. Good thing we're writing these tests! If you hadn't volunteered to do that, we wouldn't have found this problem. Thank you so much!
I have ran your changed example as you suggested and everything works fine. |
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.
Are you sure you are using the new version?
Ah! You're right! That was totally my bad. I am clearly bouncing between way too many branches right now. Thanks for clearing that up!
Using round() instead of ceil() makes it fail by one pesky 202.49999999999997 vs 202.5 rounding difference.
No problem! Everything looks good.
@Piripant Thank you so much for working on this! You did an excellent job fixing the bug, documenting everything, and writing tests. I really appreciated that you went back and resolved all my comments so quickly and without complaint. Thanks to you, this feature will be available in the first 1.0.0 stable release! |
My pleasure! Thanks for assisting me and driving me in the right direction! I am happy to contribute! |
With a small change in the turn_towards function it should now work correctly.
The followmouse example can show it functioning.
Closes #8