diff --git a/ballerina/natives.bal b/ballerina/natives.bal index ba26355..354f96e 100644 --- a/ballerina/natives.bal +++ b/ballerina/natives.bal @@ -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 (lcg() / m * ((endRange - 1) - startRange) + startRange); diff --git a/ballerina/tests/random.bal b/ballerina/tests/random.bal index c7e0a32..32a27be 100644 --- a/ballerina/tests/random.bal +++ b/ballerina/tests/random.bal @@ -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); @@ -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); diff --git a/changelog.md b/changelog.md index 9d875ad..f77f24e 100644 --- a/changelog.md +++ b/changelog.md @@ -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