|
| 1 | +package com.github.dkoval.leetcode.challenge |
| 2 | + |
| 3 | +import com.github.dkoval.leetcode.challenge.SortIntegersByNumberOf1Bits.SortIntegersByNumberOf1BitsRev1 |
| 4 | +import org.junit.jupiter.api.Assertions.assertArrayEquals |
| 5 | +import org.junit.jupiter.api.Nested |
| 6 | +import org.junit.jupiter.api.extension.ExtensionContext |
| 7 | +import org.junit.jupiter.params.ParameterizedTest |
| 8 | +import org.junit.jupiter.params.provider.Arguments |
| 9 | +import org.junit.jupiter.params.provider.Arguments.arguments |
| 10 | +import org.junit.jupiter.params.provider.ArgumentsProvider |
| 11 | +import org.junit.jupiter.params.provider.ArgumentsSource |
| 12 | +import org.junit.jupiter.params.support.ParameterDeclarations |
| 13 | +import java.util.stream.Stream |
| 14 | + |
| 15 | +internal class SortIntegersByNumberOf1BitsTest { |
| 16 | + |
| 17 | + class InputArgumentsProvider : ArgumentsProvider { |
| 18 | + |
| 19 | + override fun provideArguments( |
| 20 | + parameters: ParameterDeclarations, |
| 21 | + context: ExtensionContext |
| 22 | + ): Stream<out Arguments> = Stream.of( |
| 23 | + arguments( |
| 24 | + intArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8), |
| 25 | + intArrayOf(0, 1, 2, 4, 8, 3, 5, 6, 7) |
| 26 | + ), |
| 27 | + arguments( |
| 28 | + intArrayOf(1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1), |
| 29 | + intArrayOf(1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024) |
| 30 | + ) |
| 31 | + ) |
| 32 | + } |
| 33 | + |
| 34 | + @Nested |
| 35 | + inner class SortIntegersByNumberOf1BitsIterTest { |
| 36 | + |
| 37 | + @ParameterizedTest |
| 38 | + @ArgumentsSource(InputArgumentsProvider::class) |
| 39 | + fun `should sort the array by the number of 1 bits in their binary representation`( |
| 40 | + arr: IntArray, |
| 41 | + expected: IntArray |
| 42 | + ) { |
| 43 | + SortIntegersByNumberOf1BitsRev1().test(arr, expected) |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +private fun SortIntegersByNumberOf1Bits.test(arr: IntArray, expected: IntArray) { |
| 49 | + val actual = sortByBits(arr) |
| 50 | + assertArrayEquals(expected, actual) |
| 51 | +} |
0 commit comments