Skip to content

Ensure K value in reverseK is at least 1 #266

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

Merged
merged 1 commit into from
Mar 16, 2025

Conversation

EricccTaiwan
Copy link
Contributor

@EricccTaiwan EricccTaiwan commented Mar 13, 2025

Passing 0 or a negative value for K does not cause errors, as the input is an integer. However, this behavior may be confusing for users. To improve clarity, enforce K to be an integer greater than or equal to 1 and return an error if the input is invalid.

Change-Id: Ic68c1acbbd45153da8c3d7682d9251eec9ce597f

Before

$ ./qtest
cmd> new
l = []
cmd> it a
l = [a]
cmd> it b
l = [a b]
cmd> reverseK 2
l = [b a]
cmd> reverseK 0
l = [b a]
cmd> reverseK -1
l = [b a]
cmd> free
l = NULL
cmd> quit
Freeing queue
$

After

$ ./qtest
cmd> new
l = []
cmd> it a
l = [a]
cmd> it b
l = [a b]
cmd> reverseK 2
l = [b a]
cmd> reverseK 0
Invalid number of K = '0'
cmd> reverseK -1
Invalid number of K = '-1'
cmd> free
l = NULL
cmd> quit
Freeing queue
$ 

@EricccTaiwan EricccTaiwan mentioned this pull request Mar 13, 2025
qtest.c Outdated
if (!get_int(argv[1], &k)) {
report(1, "Invalid number of K");
if (!get_int(argv[1], &k) || k < 1) {
report(1, "Invalid number of K = '%s'", argv[1]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Refine the error message, addressing the valid range (at least 1).

qtest.c Outdated
if (!get_int(argv[1], &k)) {
report(1, "Invalid number of K");
if (!get_int(argv[1], &k) || k < 1) {
report(1, "Invalid number of K = '%s' (at least 1)", argv[1]);
Copy link
Contributor

Choose a reason for hiding this comment

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

It is not necessary to print the value when K < 1. Improve the wording.

qtest.c Outdated
if (!get_int(argv[1], &k)) {
report(1, "Invalid number of K");
if (!get_int(argv[1], &k) || k < 1) {
report(1, "Invalid number of K (at least 1)", argv[1]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Check format string.

Passing 0 or a negative value for K does not cause errors, as the input
is an integer. However, this behavior may be confusing for users. To
improve clarity, enforce K to be an integer greater than or equal to 1
and return an error if the input is invalid.

Co-authored-by: Po-Ying Chiu <charlie910417@gmail.com>
Change-Id: I6741888f888523d00f83cd554fc1442e99120a64
@jserv jserv merged commit 1a8b5f4 into sysprog21:master Mar 16, 2025
1 of 2 checks passed
@jserv
Copy link
Contributor

jserv commented Mar 16, 2025

Thank @EricccTaiwan for contributing!

@EricccTaiwan EricccTaiwan deleted the fix-reverseK branch March 16, 2025 14:29
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.

2 participants