Skip to content

Commit 0db265e

Browse files
realDuYuanChaogithub-actions
andauthored
Calculate middle (#109)
* middle index calculate * Formatted with Google Java Formatter Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 68b103c commit 0db265e

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.examplehub.maths;
2+
3+
public class MiddleIndexCalculate {
4+
5+
/**
6+
* Return middle index from left index to right index.
7+
*
8+
* @param left the left index of array.
9+
* @param right the right index of array.
10+
* @return middle index.
11+
*/
12+
public static int middle(int left, int right) {
13+
return (left + right) >>> 1;
14+
}
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.examplehub.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class MiddleIndexCalculateTest {
8+
@Test
9+
void testMiddle() {
10+
assertEquals(0, MiddleIndexCalculate.middle(0, 1));
11+
assertEquals(5, MiddleIndexCalculate.middle(0, 10));
12+
assertEquals(
13+
Integer.MAX_VALUE - 1,
14+
MiddleIndexCalculate.middle(Integer.MAX_VALUE - 1, Integer.MAX_VALUE));
15+
assertEquals(
16+
Integer.MAX_VALUE, MiddleIndexCalculate.middle(Integer.MAX_VALUE, Integer.MAX_VALUE));
17+
}
18+
}

0 commit comments

Comments
 (0)