diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index 137710a..0000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,33 +0,0 @@ -name: Build and Test - -# Run this workflow every time you push to main, or open a pull request targeting main -on: - push: - branches: [ "main" ] - pull_request: - branches: [ "main" ] - -jobs: - build: - # macOS is required to build and test iOS targets in a KMP project - runs-on: macos-latest - - steps: - - name: Checkout Repository - uses: actions/checkout@v4 - - - name: Set up Java 17 - uses: actions/setup-java@v4 - with: - java-version: '17' - distribution: 'zulu' # Zulu is a solid default for Kotlin/Android/KMP - - - name: Setup Gradle - uses: gradle/actions/setup-gradle@v3 - with: - # This caches your gradle dependencies so future builds are much faster - cache-read-only: ${{ github.ref != 'refs/heads/main' }} - - - name: Build and Run Tests - # 'check' runs all unit tests and linting. 'build' does that plus assembles the artifacts. - run: ./gradlew build \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..a7991dd --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,40 @@ +name: CI / Run Tests + +on: + pull_request: + branches: [ "main" ] + push: + branches: [ "main" ] + +jobs: + test: + name: Build and Test + runs-on: macos-latest + + steps: + - name: Checkout Repository + uses: actions/checkout@v4 + + - name: Set up Java 17 + uses: actions/setup-java@v4 + with: + java-version: '17' + distribution: 'zulu' + + - name: Setup Gradle + uses: gradle/actions/setup-gradle@v3 + with: + # Caches your gradle dependencies so future builds are much faster + cache-read-only: ${{ github.ref != 'refs/heads/main' }} + + - name: Run All Tests + run: ./gradlew check --no-configuration-cache + + - name: Generate Summary + if: always() + run: | + if [ "${{ job.status }}" == "success" ]; then + echo "### ✅ All tests passed successfully!" >> $GITHUB_STEP_SUMMARY + else + echo "### ❌ Tests failed. Please check the logs." >> $GITHUB_STEP_SUMMARY + fi \ No newline at end of file diff --git a/kapacity-io/src/androidMain/kotlin/io/github/developrofthings/kapacity/io/Kapacity.android.kt b/kapacity-io/src/androidMain/kotlin/io/github/developrofthings/kapacity/io/Kapacity.android.kt index 9658b5b..77d3959 100644 --- a/kapacity-io/src/androidMain/kotlin/io/github/developrofthings/kapacity/io/Kapacity.android.kt +++ b/kapacity-io/src/androidMain/kotlin/io/github/developrofthings/kapacity/io/Kapacity.android.kt @@ -3,6 +3,7 @@ package io.github.developrofthings.kapacity.io import io.github.developrofthings.kapacity.Kapacity import io.github.developrofthings.kapacity.byte import java.io.File +import java.nio.ByteBuffer import java.nio.file.Path import kotlin.io.path.fileSize @@ -19,4 +20,9 @@ val File.kapacity: Kapacity get() = this.length().byte * * If the file does not exist, throws an exception matching the underlying behavior of [Path.fileSize]. */ -val Path.kapacity: Kapacity get() = this.fileSize().byte \ No newline at end of file +val Path.kapacity: Kapacity get() = this.fileSize().byte + +/** + * Returns the [Kapacity] of this [ByteBuffer], calculated directly from its length on the disk. + */ +val ByteBuffer.kapacity: Kapacity get() = this.capacity().byte \ No newline at end of file diff --git a/kapacity/src/commonMain/kotlin/io/github/developrofthings/kapacity/Kapacity.kt b/kapacity/src/commonMain/kotlin/io/github/developrofthings/kapacity/Kapacity.kt index e5adf22..8cb335b 100644 --- a/kapacity/src/commonMain/kotlin/io/github/developrofthings/kapacity/Kapacity.kt +++ b/kapacity/src/commonMain/kotlin/io/github/developrofthings/kapacity/Kapacity.kt @@ -127,6 +127,26 @@ value class Kapacity private constructor(val rawBytes: Long) : Comparable.kapacity: Kapacity get() = this.size.byte + +/** + * Returns the [Kapacity] of this primitive [ByteArray], calculated directly from its size. + * + * Each byte in the array represents exactly 1 byte of capacity. + */ +val ByteArray.kapacity: Kapacity get() = this.size.byte + +/** + * Returns the [Kapacity] of this boxed [Array], calculated directly from its size. + * + * Each [UByte] element in the array represents exactly 1 byte of capacity. + */ +val Array.kapacity: Kapacity get() = this.size.byte + +/** + * Returns the [Kapacity] of this unsigned [UByteArray], calculated directly from its size. + * + * Each unsigned byte in the array represents exactly 1 byte of capacity. + */ +@OptIn(ExperimentalUnsignedTypes::class) +val UByteArray.kapacity: Kapacity get() = this.size.byte + +/** + * Returns the [Kapacity] of this [Collection] of bytes, calculated directly from its size. + * + * Each [Byte] element in the collection represents exactly 1 byte of capacity. + */ +val Collection.kapacity: Kapacity get() = this.size.byte +//endregion private val Kapacity.rawBytesCoercedToIntRange: Int // Maximum size for an array is limited to the max value of `Int` (≈ 2.147 Gigabytes) @@ -1013,40 +1032,4 @@ fun Kapacity.toByteArray(): ByteArray = ByteArray(size = this.rawBytesCoercedToI * * @return A new primitive [UByteArray] filled with zeros. */ -fun Kapacity.toUByteArray(): UByteArray = UByteArray(size = this.rawBytesCoercedToIntRange) - -/** - * Returns the [Kapacity] of this boxed [Array], calculated directly from its size. - * - * Each [Byte] element in the array represents exactly 1 byte of capacity. - */ -val Array.kapacity: Kapacity get() = this.size.byte - -/** - * Returns the [Kapacity] of this primitive [ByteArray], calculated directly from its size. - * - * Each byte in the array represents exactly 1 byte of capacity. - */ -val ByteArray.kapacity: Kapacity get() = this.size.byte - -/** - * Returns the [Kapacity] of this boxed [Array], calculated directly from its size. - * - * Each [UByte] element in the array represents exactly 1 byte of capacity. - */ -val Array.kapacity: Kapacity get() = this.size.byte - -/** - * Returns the [Kapacity] of this unsigned [UByteArray], calculated directly from its size. - * - * Each unsigned byte in the array represents exactly 1 byte of capacity. - */ -@OptIn(ExperimentalUnsignedTypes::class) -val UByteArray.kapacity: Kapacity get() = this.size.byte - -/** - * Returns the [Kapacity] of this [Collection] of bytes, calculated directly from its size. - * - * Each [Byte] element in the collection represents exactly 1 byte of capacity. - */ -val Collection.kapacity: Kapacity get() = this.size.byte +fun Kapacity.toUByteArray(): UByteArray = UByteArray(size = this.rawBytesCoercedToIntRange) \ No newline at end of file diff --git a/kapacity/src/commonTest/kotlin/io/github/developrofthings/kapacity/KapacityTest.kt b/kapacity/src/commonTest/kotlin/io/github/developrofthings/kapacity/KapacityTest.kt index 2e945e3..9005ba6 100644 --- a/kapacity/src/commonTest/kotlin/io/github/developrofthings/kapacity/KapacityTest.kt +++ b/kapacity/src/commonTest/kotlin/io/github/developrofthings/kapacity/KapacityTest.kt @@ -18,20 +18,20 @@ class KapacityTest { @Test fun `verify binary IEC conversions from Long`() { - assertEquals(1L, 1L.binaryByte.rawBytes) - assertEquals(1_024L, 1L.binaryKilobyte.rawBytes) - assertEquals(1_048_576L, 1L.binaryMegabyte.rawBytes) - assertEquals(1_073_741_824L, 1L.binaryGigabyte.rawBytes) - assertEquals(1_099_511_627_776L, 1L.binaryTerabyte.rawBytes) - assertEquals(1_125_899_906_842_624L, 1L.binaryPetabyte.rawBytes) - assertEquals(1_152_921_504_606_846_976L, 1L.binaryExabyte.rawBytes) + assertEquals(1L, 1L.byte.rawBytes) + assertEquals(1_024L, 1L.kibibyte.rawBytes) + assertEquals(1_048_576L, 1L.mebibyte.rawBytes) + assertEquals(1_073_741_824L, 1L.gibibyte.rawBytes) + assertEquals(1_099_511_627_776L, 1L.tebibyte.rawBytes) + assertEquals(1_125_899_906_842_624L, 1L.pebibyte.rawBytes) + assertEquals(1_152_921_504_606_846_976L, 1L.exbibyte.rawBytes) } @Test fun `verify conversions from Int`() { // Just checking a few to ensure the Int extensions route correctly assertEquals(5_000_000L, 5.megabyte.rawBytes) - assertEquals(5_242_880L, 5.binaryMegabyte.rawBytes) + assertEquals(5_242_880L, 5.mebibyte.rawBytes) } @Test @@ -77,7 +77,7 @@ class KapacityTest { @Test fun `verify comparison operators`() { assertTrue(1.gigabyte > 900.megabyte) - assertTrue(1.binaryKilobyte > 1.kilobyte) // 1024 > 1000 + assertTrue(1.kibibyte > 1.kilobyte) // 1024 > 1000 assertTrue(500.megabyte < 1.gigabyte) // Ensure sorting works @@ -104,10 +104,10 @@ class KapacityTest { @Test fun testDoubleBinaryConversions() { // 1.5 MiB = 1.5 * 1,048,576 = 1,572,864 bytes - assertEquals(1_572_864L, 1.5.binaryMegabyte.rawBytes) + assertEquals(1_572_864L, 1.5.mebibyte.rawBytes) // 0.5 GiB = 0.5 * 1,073,741,824 = 536,870,912 bytes - assertEquals(536_870_912L, 0.5.binaryGigabyte.rawBytes) + assertEquals(536_870_912L, 0.5.gibibyte.rawBytes) } @Test @@ -116,6 +116,6 @@ class KapacityTest { assertEquals(1_500_000L, 1.5f.megabyte.rawBytes) // 1.5f MiB = 1,572,864 bytes - assertEquals(1_572_864L, 1.5f.binaryMegabyte.rawBytes) + assertEquals(1_572_864L, 1.5f.mebibyte.rawBytes) } }