Добавлена конвертация из десятичных чисел в двоичные.

This commit is contained in:
2026-06-23 08:33:47 +05:00
parent 61ae4f5013
commit b66fa51e85
2 changed files with 245 additions and 0 deletions

View File

@@ -0,0 +1,160 @@
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import ru.thiflict.binaryutils.BinConvert.toBinary
class BinConvertTest {
// ------------------- Long.toBinary() -------------------
@Test
fun `Long toBinary zero`() {
val expected = "0".repeat(64)
assertEquals(expected, 0L.toBinary())
}
@Test
fun `Long toBinary positive one`() {
val expected = "0".repeat(63) + "1"
assertEquals(expected, 1L.toBinary())
}
@Test
fun `Long toBinary negative one`() {
val expected = "1".repeat(64)
assertEquals(expected, (-1L).toBinary())
}
@Test
fun `Long toBinary MAX_VALUE`() {
// Long.MAX_VALUE = 0x7FFFFFFFFFFFFFFF -> первый бит 0, остальные 63 бита 1
val expected = "0" + "1".repeat(63)
assertEquals(expected, Long.MAX_VALUE.toBinary())
}
@Test
fun `Long toBinary MIN_VALUE`() {
// Long.MIN_VALUE = 0x8000000000000000 -> первый бит 1, остальные 63 бита 0
val expected = "1" + "0".repeat(63)
assertEquals(expected, Long.MIN_VALUE.toBinary())
}
@Test
fun `Long toBinary arbitrary value`() {
// 123456789L = 0x75BCD15 -> двоичное представление из 64 бит
val expectedBinaryString = java.lang.Long.toBinaryString(123456789L).padStart(64, '0')
assertEquals(expectedBinaryString, 123456789L.toBinary())
}
// ------------------- Int.toBinary() -------------------
@Test
fun `Int toBinary zero`() {
assertEquals("0".repeat(32), 0.toBinary())
}
@Test
fun `Int toBinary positive one`() {
assertEquals("0".repeat(31) + "1", 1.toBinary())
}
@Test
fun `Int toBinary negative one`() {
assertEquals("1".repeat(32), (-1).toBinary())
}
@Test
fun `Int toBinary MAX_VALUE`() {
// Int.MAX_VALUE = 0x7FFFFFFF -> 0 + 31 единица
assertEquals("0" + "1".repeat(31), Int.MAX_VALUE.toBinary())
}
@Test
fun `Int toBinary MIN_VALUE`() {
// Int.MIN_VALUE = 0x80000000 -> 1 + 31 ноль
assertEquals("1" + "0".repeat(31), Int.MIN_VALUE.toBinary())
}
@Test
fun `Int toBinary arbitrary value`() {
val value = -987654321
val expected = Integer.toBinaryString(value).padStart(32, '0')
assertEquals(expected, value.toBinary())
}
// ------------------- Short.toBinary() -------------------
@Test
fun `Short toBinary zero returns Long`() {
// бинарная строка из 16 нулей -> Long = 0
assertEquals(0L, (0).toShort().toBinary())
}
@Test
fun `Short toBinary positive one`() {
// "0000000000000001" -> 1L
assertEquals(1L, (1).toShort().toBinary())
}
@Test
fun `Short toBinary negative one`() {
// "1111111111111111" -> 1111111111111111L
assertEquals(1111111111111111L, (-1).toShort().toBinary())
}
@Test
fun `Short toBinary MAX_VALUE`() {
// 0x7FFF -> "0111111111111111" -> 111111111111111L (15 единиц)
assertEquals(111111111111111L, Short.MAX_VALUE.toBinary())
}
@Test
fun `Short toBinary MIN_VALUE`() {
// 0x8000 -> "1000000000000000" -> 1000000000000000L
assertEquals(1000000000000000L, Short.MIN_VALUE.toBinary())
}
@Test
fun `Short toBinary arbitrary positive`() {
// 12345 = 0x3039 -> binary: 0011000000111001
val expected = "0011000000111001".toLong() // 11000000111001 -> 11000000111001L? Actually leading zeros are ignored.
// "0011000000111001" -> decimal 11000000111001? Let's compute: 0011000000111001 -> 11000000111001 (14 digits) = 11,000,000,111,001? Wait, the string is 16 chars: 0011000000111001.
// toLong will parse as decimal: 11000000111001? The leading zeros don't change the numeric value.
// The result: 0011000000111001 as decimal is 11000000111001? Let's count: after removing leading zeros, string is "11000000111001" which is 11,000,000,111,001? Actually "11000000111001" is 14 digits: 11 trillion something.
// But it's easier to build directly: val expected = "0011000000111001".toLong() works.
assertEquals("0011000000111001".toLong(), (12345).toShort().toBinary())
}
// ------------------- Byte.toBinary() -------------------
@Test
fun `Byte toBinary zero returns Int`() {
assertEquals(0, (0).toByte().toBinary())
}
@Test
fun `Byte toBinary positive one`() {
// "00000001" -> 1
assertEquals(1, (1).toByte().toBinary())
}
@Test
fun `Byte toBinary negative one`() {
// "11111111" -> 11111111
assertEquals(11111111, (-1).toByte().toBinary())
}
@Test
fun `Byte toBinary MAX_VALUE`() {
// 0x7F -> "01111111" -> 1111111 (7 единиц)
assertEquals(1111111, Byte.MAX_VALUE.toBinary())
}
@Test
fun `Byte toBinary MIN_VALUE`() {
// 0x80 -> "10000000" -> 10000000
assertEquals(10000000, Byte.MIN_VALUE.toBinary())
}
@Test
fun `Byte toBinary arbitrary value`() {
// 42 = 0x2A -> "00101010" -> 101010
assertEquals(101010, (42).toByte().toBinary())
}
}