mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-10 21:43:15 +08:00
Added tests for NumberOfDigits (#5107)
Co-authored-by: Maria Paszkiewicz SCC <maria.paszkiewicz@kit.edu> Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>
This commit is contained in:
@ -3,35 +3,16 @@ package com.thealgorithms.maths;
|
||||
/**
|
||||
* Find the number of digits in a number.
|
||||
*/
|
||||
public class NumberOfDigits {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] numbers = {
|
||||
0,
|
||||
12,
|
||||
123,
|
||||
1234,
|
||||
-12345,
|
||||
123456,
|
||||
1234567,
|
||||
12345678,
|
||||
123456789,
|
||||
};
|
||||
for (int i = 0; i < numbers.length; ++i) {
|
||||
assert numberOfDigits(numbers[i]) == i + 1;
|
||||
assert numberOfDigitsFast(numbers[i]) == i + 1;
|
||||
assert numberOfDigitsFaster(numbers[i]) == i + 1;
|
||||
assert numberOfDigitsRecursion(numbers[i]) == i + 1;
|
||||
}
|
||||
public final class NumberOfDigits {
|
||||
private NumberOfDigits() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the number of digits in a number.
|
||||
*
|
||||
* @param number number to find
|
||||
* @return number of digits of given number
|
||||
*/
|
||||
private static int numberOfDigits(int number) {
|
||||
public static int numberOfDigits(int number) {
|
||||
int digits = 0;
|
||||
do {
|
||||
digits++;
|
||||
@ -46,7 +27,7 @@ public class NumberOfDigits {
|
||||
* @param number number to find
|
||||
* @return number of digits of given number
|
||||
*/
|
||||
private static int numberOfDigitsFast(int number) {
|
||||
public static int numberOfDigitsFast(int number) {
|
||||
return number == 0 ? 1 : (int) Math.floor(Math.log10(Math.abs(number)) + 1);
|
||||
}
|
||||
|
||||
@ -56,7 +37,7 @@ public class NumberOfDigits {
|
||||
* @param number number to find
|
||||
* @return number of digits of given number
|
||||
*/
|
||||
private static int numberOfDigitsFaster(int number) {
|
||||
public static int numberOfDigitsFaster(int number) {
|
||||
return number < 0 ? (-number + "").length() : (number + "").length();
|
||||
}
|
||||
|
||||
@ -66,7 +47,7 @@ public class NumberOfDigits {
|
||||
* @param number number to find
|
||||
* @return number of digits of given number
|
||||
*/
|
||||
private static int numberOfDigitsRecursion(int number) {
|
||||
public static int numberOfDigitsRecursion(int number) {
|
||||
return number / 10 == 0 ? 1 : 1 + numberOfDigitsRecursion(number / 10);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,40 @@
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.function.IntFunction;
|
||||
import java.util.stream.Stream;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
public class NumberOfDigitsTest {
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("testCases")
|
||||
void testNumberOfDigits(final int expected, final int number, final IntFunction<Integer> methodUnderTest) {
|
||||
assertEquals(expected, methodUnderTest.apply(number));
|
||||
assertEquals(expected, methodUnderTest.apply(-number));
|
||||
}
|
||||
|
||||
private static Stream<Arguments> testCases() {
|
||||
final Integer[][] inputs = new Integer[][] {
|
||||
{3, 100},
|
||||
{1, 0},
|
||||
{2, 12},
|
||||
{3, 123},
|
||||
{4, 1234},
|
||||
{5, 12345},
|
||||
{6, 123456},
|
||||
{7, 1234567},
|
||||
{8, 12345678},
|
||||
{9, 123456789},
|
||||
{9, 987654321},
|
||||
};
|
||||
|
||||
final IntFunction<Integer>[] methods = new IntFunction[] {NumberOfDigits::numberOfDigits, NumberOfDigits::numberOfDigitsFast, NumberOfDigits::numberOfDigitsFaster, NumberOfDigits::numberOfDigitsRecursion};
|
||||
|
||||
return Stream.of(inputs).flatMap(input -> Stream.of(methods).map(method -> Arguments.of(input[0], input[1], method)));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user