Calculate expected value according to the platform endianness: layout of bytes is different for Big Endian mips32.

This commit is contained in:
Pavel Punegov
2020-06-17 17:25:44 +03:00
committed by Pavel Punegov
parent 825a02cfca
commit 126d1caba1
+13 -2
View File
@@ -1,5 +1,6 @@
import cunion.* import cunion.*
import kotlinx.cinterop.* import kotlinx.cinterop.*
import kotlin.native.*
import kotlin.test.* import kotlin.test.*
fun main() { fun main() {
@@ -7,7 +8,12 @@ fun main() {
val basicUnion = alloc<BasicUnion>() val basicUnion = alloc<BasicUnion>()
for (value in Short.MIN_VALUE..Short.MAX_VALUE) { for (value in Short.MIN_VALUE..Short.MAX_VALUE) {
basicUnion.ll = value.toLong() basicUnion.ll = value.toLong()
assertEquals(value.toShort(), basicUnion.s) val expected = if (Platform.isLittleEndian) {
value
} else {
value.toLong() ushr (Long.SIZE_BITS - Short.SIZE_BITS)
}
assertEquals(expected.toShort(), basicUnion.s)
} }
} }
memScoped { memScoped {
@@ -18,7 +24,12 @@ fun main() {
memScoped { memScoped {
val union = alloc<Packed>() val union = alloc<Packed>()
union.b = 1u union.b = 1u
assertEquals(1u, union.i) var expected = if (Platform.isLittleEndian) {
1u
} else {
1u shl (Int.SIZE_BITS - Byte.SIZE_BITS)
}
assertEquals(expected, union.i)
union.i = 0u union.i = 0u
assertEquals(0u, union.b) assertEquals(0u, union.b)
} }