Skip to content
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

Test median #3

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
24 changes: 0 additions & 24 deletions .idea/sonarlint/issuestore/index.pb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 0 additions & 24 deletions .idea/sonarlint/securityhotspotstore/index.pb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 24 additions & 1 deletion src/main/java/Median.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
/*
Написать метод, который принимает на вход массив целых чисел и возвращает медиану (серединное значение)

Медианой называется число, стоящее в упорядоченном ряде чисел посередине.
Например, в ряду: 1, 2, 3, 7, 9 — тройка является медианой. Если количество чисел чётное,
то за медиану принимают среднее двух стоящих в центре чисел.
Либо можно выбирать элемент под номером k/2, если k чётное и (k+1)/2, если нечетное.
*/

import java.util.Arrays;

public class Median {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Move this file to a named package

Copy link
Owner

Choose a reason for hiding this comment

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

Done

public int median(int[] numbers){
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Recommended to use descriptive verbs that indicate what the method does
Here are some examples: findMedian, getMedian, calculateMedian

It can also return a double value if the length is even-numbered

Copy link
Owner

Choose a reason for hiding this comment

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

Done

Copy link
Owner

Choose a reason for hiding this comment

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

When I used assertEquals for double was error, that I use deprecated method, so first i tried with int. Now I found solution.

if (numbers == null) {
return Integer.MAX_VALUE;
}
if (numbers.length == 0){
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There no spaces between ){
It's better to format the code: Ctrl+Alt+L

return Integer.MIN_VALUE;
} else {
Arrays.sort(numbers);
int median;
if (numbers.length % 2 == 0) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Variables can be extracted from 'if':

int size = numbers.length;

Copy link
Owner

Choose a reason for hiding this comment

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

Done

int middleIndex = numbers.length / 2;
median = (numbers[middleIndex] + numbers[middleIndex - 1]) /2;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

if the length is even, the code returns an incorrect result
Probably because of the int cast

Copy link
Owner

Choose a reason for hiding this comment

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

fixed

} else {
int middleIndex = numbers.length / 2;
median = numbers[middleIndex];
}
return median;
}

return 1;
}
}
43 changes: 43 additions & 0 deletions src/test/java/DataNumbersProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import org.testng.annotations.DataProvider;

public class DataNumbersProvider {

@DataProvider(name = "validNumberArrayProvider")
public Object[][] validNumberArrayProvider() {
return new Object[][]{
{new int[]{1, 2, 3, 4}, 2.5},
{new int[]{-1, -2, -3, -4}, -2.5},
{new int[]{1, 3, 5}, 3.0},
{new int[]{5, 3, 2}, 3.0},
{new int[]{1, 2, 3, 4, 5, 6}, 3.5},
{new int[]{6, 5, 4, 3, 2, 1}, 3.5},
{new int[]{3, 1, 4, 1, 5, 9}, 3.5},
{new int[]{3, 1, 4, 1, 5}, 3.0},
{new int[]{-1, -2, -3, -4, -5}, -3.0},
{new int[]{2147483647, 2147483647, 2147483647, 2147483647}, 2147483647},
{new int[]{2147483647, 2147483647, 2147483647}, 2147483647},
{new int[]{2147483647}, 2147483647},
{new int[]{20, 18, 16, 14, 12, 10, 8, 6, 4, 2}, 11.0},
{new int[]{1000000, 2000000, 3000001, 4000000, 5000000}, 3000001.0},
};
}

@DataProvider(name = "invalidNumberArrayProvider")
public Object[][] invalidNumberArrayProvider() {
return new Object[][]{
{null, 0x80000000},
{new int[]{}, 0x80000000},
};
}

@DataProvider(name = "singleElementArrayProvider")
public Object[][] singleElementArrayProvider() {
return new Object[][]{
{new int[]{0}, 0.0},
{new int[]{1}, 1.0},
{new int[]{2}, 2.0},
{new int[]{5}, 5.0},
{new int[]{-5}, -5.0},
};
}
}
36 changes: 36 additions & 0 deletions src/test/java/MedianTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,43 @@
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import static org.testng.AssertJUnit.assertEquals;

public class MedianTest {

private Median median;
@BeforeClass
public void beforeClass() {
median = new Median();
}

@Test
public void testMedianHN() {
}

@Test(dataProvider = "validNumberArrayProvider",
dataProviderClass = DataNumbersProvider.class)
public void testFindMedianValidNumberArrayProvider(int[] input, double expected) {
double actual = median.median(input);
assertEquals(expected, actual);
}
@Test(dataProvider = "validNumberArrayProvider",
dataProviderClass = DataNumbersProvider.class)
public void testFindMedianValidNumberArrayProviderQuickSelect(int[] input, double expected) {
double actual = median.median(input);
assertEquals(expected, actual);
}
@Test(dataProvider = "invalidNumberArrayProvider",
dataProviderClass = DataNumbersProvider.class)
public void testCalculateMedianWithInvalidInput(int[] numbers, double expected) {
double actual = median.median(numbers);
assertEquals(expected, actual);
}

@Test(dataProvider = "singleElementArrayProvider",
dataProviderClass = DataNumbersProvider.class)
public void testFindMedianValidNumberArrayProviderWithSingleElementArrayProvider(int[] input, double expected) {
double actual = median.median(input);
assertEquals(actual, expected);
}
}
Loading