Skip to content

Commit

Permalink
Merge pull request #434 from vil02/4892_prohibit_calling_createIntInR…
Browse files Browse the repository at this point in the history
…ange_with_empty_range

Prohibit calling `createIntInRange` with empty range
  • Loading branch information
daneshk committed Oct 31, 2023
2 parents 8e14b99 + a650d28 commit d67aa74
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ballerina/natives.bal
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public isolated function createDecimal() returns float {
# + endRange - Range end value
# + return - Selected random value or else, a `random:Error` if the start range is greater than the end range
public isolated function createIntInRange(int startRange, int endRange) returns int|Error {
if startRange > endRange {
if startRange >= endRange {
return error Error("End range value must be greater than the start range value");
}
return <int>(lcg() / m * (<decimal>(endRange - 1) - <decimal>startRange) + <decimal>startRange);
Expand Down
22 changes: 22 additions & 0 deletions ballerina/tests/random.bal
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ isolated function createIntInRangeTest() {
}
}

@test:Config {}
isolated function createIntInRangeWithSingleElementTest() {
final int someInt = 123;
int|error result = createIntInRange(someInt, someInt + 1);
if result is int {
test:assertTrue(result == someInt, msg = "createIntInRangeWithSingleElementTest result is not " + someInt.toString());
} else {
test:assertFail("createIntInRangeTest result is not int");
}
}

@test:Config {}
isolated function negativeTestforCreateIntInRangeTest() {
int|error result = createIntInRange(5000, 10);
Expand All @@ -42,6 +53,17 @@ isolated function negativeTestforCreateIntInRangeTest() {
}
}

@test:Config {}
isolated function negativeTestforCreateIntInRange2Test() {
int|error result = createIntInRange(17, 17);
if result is error {
test:assertTrue(result.message().includes("End range value must be greater than the start range value"),
msg = "negativeTestforCreateIntInRange2Test result incorrect");
} else {
test:assertFail("Result is not mismatch");
}
}

@test:Config {}
isolated function doesNotThrowForFullIntRangeTest() {
int|error result = createIntInRange(int:MIN_VALUE, int:MAX_VALUE);
Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This file contains all the notable changes done to the Ballerina TCP package thr

- [Make `createIntInRange` work for full int range ](https://github.com/ballerina-platform/ballerina-standard-library/issues/4744)
- [Remove duplicated codecov badge](https://github.com/ballerina-platform/ballerina-standard-library/issues/4893)
- [Prohibit calling `createIntInRange` with empty range](https://github.com/ballerina-platform/ballerina-standard-library/issues/4892)

## [1.5.0] - 2023-09-15

Expand Down

0 comments on commit d67aa74

Please sign in to comment.