Skip to content

Fix bugs in rnd#70

Merged
WhyPenguins merged 5 commits intothoth-tech:t2-2024from
matt439:fix-rand
Oct 1, 2024
Merged

Fix bugs in rnd#70
WhyPenguins merged 5 commits intothoth-tech:t2-2024from
matt439:fix-rand

Conversation

@matt439
Copy link

@matt439 matt439 commented Sep 2, 2024

Description

The rnd(int ubound) and rnd(int min, int max) overloads of rand generate arithmetic exceptions when a divide by zero occurs through a modulo operator. rnd(0) will reliably generate the exception. When min = max, the divide by zero will occur in rnd(int min, int max) as ... % (max - min) is used in the function.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

I added calls to rnd(0) and rnd(10, 10) to test_graphics.cpp to test the edge cases. While it may seem out of place to use rnd in test_graphics.cpp, #include "random.h" was already present in the file with no associated references. Now the #include is justified.

Testing Checklist

  • Tested with sktest

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code in hard-to-understand areas
  • My changes generate no new warnings

Copy link

@Liquidscroll Liquidscroll left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work! A few issues:

  • What if the user passes in a value for min that is greater than max?
  • I'm not a fan of the mixing of functionality in the graphics test, this might be better in its own unit test.

EDIT: I just saw PR#68 adds the unit tests for rnd()


int rnd(int min, int max)
{
if (min == max) return min;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if min is greater than max? For example running rnd(50, 10) returned 56. Should this print an error if this occurs?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a check for this edge case. I see three potential actions: throw an exception, return 0, or return rnd(max, min). I believe the third option is the best as most people who call rnd(20, 10) are expecting it to behave as rnd(10, 20) anyway. I added an output to the console whenever the function is used improperly. Throwing an exception seems extreme in this case and returning 0 could confuse users.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps adding some form of clarification to this string would be good. Something to let the users know that we'll be swapping the values they've input into the function.

I don't think this should block this from the next review, so I'll approve this.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The correct answer imo would be to throw an error anytime the function is being used incorrectly. However as splashkit is scared of throwing exceptions as they're too scary for people new to programming, I think this is an acceptable way of handling the error.

save_bitmap(user_image, "1");
fill_rectangle_on_bitmap (user_image, COLOR_BLACK, 0, 0, 10, 10);
// rnd(0) should return 0, and rnd(10, 10) should return 10
fill_rectangle_on_bitmap (user_image, COLOR_BLACK, rnd(0), 0, rnd(10, 10), 10);
Copy link

@Liquidscroll Liquidscroll Sep 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is a great place for this; this should be testing graphics only. The rnd functions seem like they'd be a good for unit testing.

EDIT: Ignore this, I see you've added exactly this in PR#68. IMO this should be removed from the graphics test.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with this. I was concerned that this specific pull request would be deemed insufficient if there was no testing included. I reverted the changes to test_graphics.cpp.

Copy link

@Liquidscroll Liquidscroll left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job! Left a comment about a clarification that could be made; but I don't think it should block this from the next review.


int rnd(int min, int max)
{
if (min == max) return min;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps adding some form of clarification to this string would be good. Something to let the users know that we'll be swapping the values they've input into the function.

I don't think this should block this from the next review, so I'll approve this.

Copy link

@hugglesfox hugglesfox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm


int rnd(int min, int max)
{
if (min == max) return min;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The correct answer imo would be to throw an error anytime the function is being used incorrectly. However as splashkit is scared of throwing exceptions as they're too scary for people new to programming, I think this is an acceptable way of handling the error.

Copy link

@WhyPenguins WhyPenguins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Just would be better to make the rnd error be output via the in-built logging system - after that this should be good to merge.

{
if (min > max)
{
std::cout << "Error: min value is greater than max value when calling rnd" << std::endl;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for consistency this should probably be output via LOG(WARNING) rather than cout

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have altered the output to use LOG(WARNING). It required me to include easylogging++.h in the file.

@WhyPenguins WhyPenguins changed the base branch from develop to t2-2024 October 1, 2024 10:19
@WhyPenguins WhyPenguins merged commit 740aa70 into thoth-tech:t2-2024 Oct 1, 2024
@matt439 matt439 deleted the fix-rand branch November 1, 2024 01:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants