Skip to content

Commit 7461712

Browse files
committed
refactor: Improve documentation and test descriptions for binary search functions
1 parent 0618c19 commit 7461712

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

src/typings/decoders.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ interface Decoder {
128128
* behaviour.
129129
*
130130
* @param timestamp
131-
* @return The index of the log event L.
131+
* @return The index of the log event L, or null if there are no log events.
132132
*/
133133
findNearestLogEventByTimestamp(timestamp: number): Nullable<number>;
134134
}

src/utils/math.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ const getChunkNum =
2828
* `upperboundValue`. If all elements in the collection are greater than `upperboundValue`, return
2929
* the first index of the collection (i.e., `0`).
3030
*
31-
* @param get
31+
* @param get Function provided to access elements in the collection by index.
3232
* @param lowIdx
3333
* @param highIdx
3434
* @param upperboundValue
35-
* @return
35+
* @return The index of the last element less than or equal to `upperboundValue`,`0` if all elements
36+
* are greater, or `null` if the collection is empty or the indices are invalid.
3637
*/
3738
const upperBoundBinarySearch = <T>(
3839
get: (index: number) => T,

test/utils/math.test.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe("clamp", () => {
2424
});
2525

2626
test(
27-
"returns the upper boundary when the upper boundary is less than the lower boundary",
27+
"returns the upper boundary if the upper boundary is less than the lower boundary",
2828
() => {
2929
expect(clamp(5, 10, 1)).toBe(1);
3030
}
@@ -56,48 +56,48 @@ describe("upperBoundBinarySearch", () => {
5656
const emptyArray: number[] = [];
5757
const singleElementArray = [3];
5858
const array = [-5, -3, 0, 1, 1, 8, 8, 8, 100];
59-
test("returns null for empty collection with fake highIdx", () => {
59+
test("returns null if collection is empty with lowIdx < highIdx", () => {
6060
const result = upperBoundBinarySearch((idx) => emptyArray[idx], 0, 1, 5);
6161
expect(result).toBeNull();
6262
});
63-
test("returns null for empty collection with valid highIdx", () => {
63+
test("returns null if collection is empty with highIdx < lowIdx", () => {
6464
const result = upperBoundBinarySearch((idx) => emptyArray[idx], 0, -1, 5);
6565
expect(result).toBeNull();
6666
});
67-
test("returns the first index for upperboundValue < all elements in array", () => {
67+
test("returns the first index if upperboundValue < all elements in array", () => {
6868
const result = upperBoundBinarySearch((idx) => array[idx], 0, array.length - 1, -10);
6969
expect(result).toBe(0);
7070
});
71-
test("returns the first index for upperboundValue < element in singleElementArray", () => {
71+
test("returns the first index if upperboundValue < element in singleElementArray", () => {
7272
const result = upperBoundBinarySearch((idx) => singleElementArray[idx], 0, 0, -10);
7373
expect(result).toBe(0);
7474
});
75-
test("returns the last index for upperboundValue > all elements in array", () => {
75+
test("returns the last index if upperboundValue > all elements in array", () => {
7676
const result = upperBoundBinarySearch((idx) => array[idx], 0, array.length - 1, 200);
7777
expect(result).toBe(array.length - 1);
7878
});
79-
test("returns the last index for upperboundValue > element in singleElementArray", () => {
79+
test("returns the last index if upperboundValue > element in singleElementArray", () => {
8080
const result = upperBoundBinarySearch((idx) => singleElementArray[idx], 0, 0, 200);
8181
expect(result).toBe(0);
8282
});
83-
test("returns the index for upperboundValue doesn't exactly match any elements", () => {
83+
test("returns the correct index if upperboundValue doesn't match any elements", () => {
8484
const result = upperBoundBinarySearch((idx) => array[idx], 0, array.length - 1, 2);
8585
expect(result).toBe(4);
8686
});
87-
test("returns the index for upperboundValue matches a unique element", () => {
87+
test("returns the correct index if upperboundValue matches a unique element", () => {
8888
const result = upperBoundBinarySearch((idx) => array[idx], 0, array.length - 1, 0);
8989
expect(result).toBe(2);
9090
});
91-
test("returns the index for upperboundValue matches a duplicated element", () => {
91+
test("returns the last matched index if upperboundValue matches a duplicated element", () => {
9292
const result = upperBoundBinarySearch((idx) => array[idx], 0, array.length - 1, 8);
9393
expect(result).toBe(7);
9494
});
95-
test("returns the first element index", () => {
95+
test("returns index 0 if upperboundValue matches the first element", () => {
9696
const result = upperBoundBinarySearch((idx) => array[idx], 0, array.length - 1, -5);
9797
expect(result).toBe(0);
9898
});
99-
test("returns the last element index", () => {
99+
test("returns the last index if upperboundValue matches the last element", () => {
100100
const result = upperBoundBinarySearch((idx) => array[idx], 0, array.length - 1, 100);
101-
expect(result).toBe(8);
101+
expect(result).toBe(array.length - 1);
102102
});
103103
});

0 commit comments

Comments
 (0)