Skip to content

Commit 7e1d013

Browse files
committed
Update buffer get behavior
1 parent 6384a1b commit 7e1d013

File tree

6 files changed

+236
-15
lines changed

6 files changed

+236
-15
lines changed

kgl/src/commonTest/kotlin/ByteBufferTest.kt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,54 @@ class ByteBufferTest {
5353

5454
assertEquals(111, byteBuffer[5])
5555
}
56+
57+
@Test
58+
fun byteBufferArrayGet() {
59+
val byteBuffer = ByteBuffer(byteArrayOf(10, 20, 30, 40))
60+
61+
byteBuffer.get()
62+
63+
val byteArray = ByteArray(2)
64+
byteBuffer.get(byteArray)
65+
66+
assertEquals(20, byteArray[0])
67+
assertEquals(30, byteArray[1])
68+
}
69+
70+
@Test
71+
fun byteBufferSingleGetChangesPosition() {
72+
val byteBuffer = ByteBuffer(byteArrayOf(10, 20, 30))
73+
74+
val firstRead = byteBuffer.get()
75+
assertEquals(10, firstRead)
76+
77+
val secondRead = byteBuffer.get()
78+
assertEquals(20, secondRead)
79+
80+
assertEquals(2, byteBuffer.position)
81+
}
82+
83+
@Test
84+
fun byteBufferArrayGetChangesPosition() {
85+
val byteBuffer = ByteBuffer(byteArrayOf(10, 20, 30))
86+
87+
val byteArray = ByteArray(2)
88+
byteBuffer.get(byteArray)
89+
90+
assertEquals(10, byteArray[0])
91+
assertEquals(20, byteArray[1])
92+
93+
assertEquals(30, byteBuffer.get())
94+
}
95+
96+
@Test
97+
fun byteBufferRandomGetDoesntChangePosition() {
98+
val byteBuffer = ByteBuffer(byteArrayOf(10, 20, 30))
99+
100+
val getValue0 = byteBuffer[0]
101+
val getValue1 = byteBuffer[1]
102+
103+
assertEquals(0, byteBuffer.position)
104+
assertEquals(byteBuffer.get(), 10)
105+
}
56106
}

kgl/src/commonTest/kotlin/FloatBufferTest.kt

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import com.danielgergely.kgl.FloatBuffer
2+
import kotlin.math.abs
23
import kotlin.test.Test
34
import kotlin.test.assertEquals
5+
import kotlin.test.assertTrue
46

57
class FloatBufferTest {
68

@@ -53,4 +55,61 @@ class FloatBufferTest {
5355

5456
assertEquals(111f, floatBuffer[5])
5557
}
58+
59+
@Test
60+
fun floatBufferArrayGet() {
61+
val floatBuffer = FloatBuffer(floatArrayOf(10.1f, 20.2f, 30.3f, 40.4f))
62+
63+
floatBuffer.get()
64+
65+
val floatArray = FloatArray(2)
66+
floatBuffer.get(floatArray)
67+
68+
assertEqualsWithTolerance(20.2f, floatArray[0])
69+
assertEqualsWithTolerance(30.3f, floatArray[1])
70+
}
71+
72+
@Test
73+
fun floatBufferSingleGetChangesPosition() {
74+
val floatBuffer = FloatBuffer(floatArrayOf(10.1f, 20.2f, 30.3f))
75+
76+
val firstRead = floatBuffer.get()
77+
assertEqualsWithTolerance(10.1f, firstRead)
78+
79+
val secondRead = floatBuffer.get()
80+
assertEqualsWithTolerance(20.2f, secondRead)
81+
82+
assertEquals(2, floatBuffer.position)
83+
}
84+
85+
@Test
86+
fun floatBufferArrayGetChangesPosition() {
87+
val floatBuffer = FloatBuffer(floatArrayOf(10.1f, 20.2f, 30.3f))
88+
89+
val floatArray = FloatArray(2)
90+
floatBuffer.get(floatArray)
91+
92+
assertEqualsWithTolerance(10.1f, floatArray[0])
93+
assertEqualsWithTolerance(20.2f, floatArray[1])
94+
95+
assertEqualsWithTolerance(30.3f, floatBuffer.get())
96+
}
97+
98+
@Test
99+
fun floatBufferRandomGetDoesntChangePosition() {
100+
val floatBuffer = FloatBuffer(floatArrayOf(10.1f, 20.2f, 30.3f))
101+
102+
val getValue0 = floatBuffer[0]
103+
val getValue1 = floatBuffer[1]
104+
105+
assertEquals(0, floatBuffer.position)
106+
assertEqualsWithTolerance(floatBuffer.get(), 10.1f)
107+
}
108+
109+
private fun assertEqualsWithTolerance(
110+
expected: Float,
111+
actual: Float,
112+
) {
113+
assertTrue(abs(expected - actual) < 0.0001f)
114+
}
56115
}

kgl/src/commonTest/kotlin/IntBufferTest.kt

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,54 @@ class IntBufferTest {
5353

5454
assertEquals(111, intBuffer[5])
5555
}
56-
}
56+
57+
@Test
58+
fun intBufferArrayGet() {
59+
val intBuffer = IntBuffer(intArrayOf(10, 20, 30, 40))
60+
61+
intBuffer.get()
62+
63+
val intArray = IntArray(2)
64+
intBuffer.get(intArray)
65+
66+
assertEquals(20, intArray[0])
67+
assertEquals(30, intArray[1])
68+
}
69+
70+
@Test
71+
fun intBufferSingleGetChangesPosition() {
72+
val intBuffer = IntBuffer(intArrayOf(10, 20, 30))
73+
74+
val firstRead = intBuffer.get()
75+
assertEquals(10, firstRead)
76+
77+
val secondRead = intBuffer.get()
78+
assertEquals(20, secondRead)
79+
80+
assertEquals(2, intBuffer.position)
81+
}
82+
83+
@Test
84+
fun intBufferArrayGetChangesPosition() {
85+
val intBuffer = IntBuffer(intArrayOf(10, 20, 30))
86+
87+
val intArray = IntArray(2)
88+
intBuffer.get(intArray)
89+
90+
assertEquals(10, intArray[0])
91+
assertEquals(20, intArray[1])
92+
93+
assertEquals(30, intBuffer.get())
94+
}
95+
96+
@Test
97+
fun intBufferRandomGetDoesntChangePosition() {
98+
val intBuffer = IntBuffer(intArrayOf(10, 20, 30))
99+
100+
val getValue0 = intBuffer[0]
101+
val getValue1 = intBuffer[1]
102+
103+
assertEquals(0, intBuffer.position)
104+
assertEquals(intBuffer.get(), 10)
105+
}
106+
}

kgl/src/commonTest/kotlin/ShortBufferTest.kt

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,54 @@ class ShortBufferTest {
5353

5454
assertEquals(111, shortBuffer[5])
5555
}
56-
}
56+
57+
@Test
58+
fun shortBufferArrayGet() {
59+
val shortBuffer = ShortBuffer(shortArrayOf(10, 20, 30, 40))
60+
61+
shortBuffer.get()
62+
63+
val shortArray = ShortArray(2)
64+
shortBuffer.get(shortArray)
65+
66+
assertEquals(20, shortArray[0])
67+
assertEquals(30, shortArray[1])
68+
}
69+
70+
@Test
71+
fun shortBufferSingleGetChangesPosition() {
72+
val shortBuffer = ShortBuffer(shortArrayOf(10, 20, 30))
73+
74+
val firstRead = shortBuffer.get()
75+
assertEquals(10, firstRead)
76+
77+
val secondRead = shortBuffer.get()
78+
assertEquals(20, secondRead)
79+
80+
assertEquals(2, shortBuffer.position)
81+
}
82+
83+
@Test
84+
fun shortBufferArrayGetChangesPosition() {
85+
val shortBuffer = ShortBuffer(shortArrayOf(10, 20, 30))
86+
87+
val shortArray = ShortArray(2)
88+
shortBuffer.get(shortArray)
89+
90+
assertEquals(10, shortArray[0])
91+
assertEquals(20, shortArray[1])
92+
93+
assertEquals(30, shortBuffer.get())
94+
}
95+
96+
@Test
97+
fun shortBufferRandomGetDoesntChangePosition() {
98+
val shortBuffer = ShortBuffer(shortArrayOf(10, 20, 30))
99+
100+
val getValue0 = shortBuffer[0]
101+
val getValue1 = shortBuffer[1]
102+
103+
assertEquals(0, shortBuffer.position)
104+
assertEquals(shortBuffer.get(), 10)
105+
}
106+
}

kgl/src/jsMain/kotlin/com/danielgergely/kgl/Buffer.kt

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import org.khronos.webgl.*
55
public actual sealed class Buffer
66

77
public fun Buffer.getJsBufferWithOffset(): ArrayBufferView {
8-
return when(this) {
8+
return when (this) {
99
is ByteBuffer -> byteBuffer.asDynamic().subarray(position)
1010
is FloatBuffer -> buffer.asDynamic().subarray(position)
1111
is IntBuffer -> buffer.asDynamic().subarray(position)
@@ -39,15 +39,17 @@ public actual class FloatBuffer(internal val buffer: Float32Array) : Buffer() {
3939
buffer[pos] = f
4040
}
4141

42-
public actual fun get(): Float = buffer[position]
42+
public actual fun get(): Float = buffer[position++]
4343

4444
public actual fun get(floatArray: FloatArray) {
4545
get(floatArray, 0, floatArray.size)
4646
}
4747

4848
public actual fun get(floatArray: FloatArray, offset: Int, length: Int) {
4949
val dest = floatArray.unsafeCast<Float32Array>()
50-
dest.subarray(offset, length).set(buffer, position)
50+
val destSubarray = dest.subarray(offset, length)
51+
destSubarray.set(buffer.subarray(position, position + length))
52+
position += length
5153
}
5254

5355
public actual operator fun get(pos: Int): Float = buffer[pos]
@@ -79,7 +81,7 @@ public actual class ByteBuffer(internal val byteBuffer: Uint8Array) : Buffer() {
7981
}
8082

8183
public actual fun get(): Byte {
82-
return byteBuffer[position].toUByte().toByte()
84+
return byteBuffer[position++].toUByte().toByte()
8385
}
8486

8587
public actual fun get(byteArray: ByteArray) {
@@ -88,7 +90,9 @@ public actual class ByteBuffer(internal val byteBuffer: Uint8Array) : Buffer() {
8890

8991
public actual fun get(byteArray: ByteArray, offset: Int, length: Int) {
9092
val dest = byteArray.unsafeCast<Uint8Array>()
91-
dest.subarray(offset, length).set(byteBuffer, position)
93+
val destSubarray = dest.subarray(offset, length)
94+
destSubarray.set(byteBuffer.subarray(position, position + length))
95+
position += length
9296
}
9397

9498
public actual operator fun get(pos: Int): Byte {
@@ -121,15 +125,17 @@ public actual class IntBuffer(internal val buffer: Int32Array) : Buffer() {
121125
buffer[pos] = i
122126
}
123127

124-
public actual fun get(): Int = buffer[position]
128+
public actual fun get(): Int = buffer[position++]
125129

126130
public actual fun get(intArray: IntArray) {
127131
get(intArray, 0, intArray.size)
128132
}
129133

130134
public actual fun get(intArray: IntArray, offset: Int, length: Int) {
131135
val dest = intArray.unsafeCast<Int32Array>()
132-
dest.subarray(offset, length).set(buffer, position)
136+
val destSubarray = dest.subarray(offset, length)
137+
destSubarray.set(buffer.subarray(position, position + length))
138+
position += length
133139
}
134140

135141
public actual operator fun get(pos: Int): Int = buffer[pos]
@@ -160,15 +166,17 @@ public actual class ShortBuffer(internal val buffer: Int16Array) : Buffer() {
160166
buffer[pos] = s
161167
}
162168

163-
public actual fun get(): Short = buffer[position]
169+
public actual fun get(): Short = buffer[position++]
164170

165171
public actual fun get(shortArray: ShortArray) {
166172
get(shortArray, 0, shortArray.size)
167173
}
168174

169175
public actual fun get(shortArray: ShortArray, offset: Int, length: Int) {
170176
val dest = shortArray.unsafeCast<Int16Array>()
171-
dest.subarray(offset, length).set(buffer, position)
177+
val destSubarray = dest.subarray(offset, length)
178+
destSubarray.set(buffer.subarray(position, position + length))
179+
position += length
172180
}
173181

174182
public actual operator fun get(pos: Int): Short = buffer[pos]

kgl/src/nativeMain/kotlin/com/danielgergely/kgl/Buffer.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public actual class FloatBuffer : Buffer {
5252
}
5353

5454
public actual fun get(): Float {
55-
return buffer[position]
55+
return buffer[position++]
5656
}
5757

5858
public actual fun get(floatArray: FloatArray) {
@@ -61,6 +61,7 @@ public actual class FloatBuffer : Buffer {
6161

6262
public actual fun get(floatArray: FloatArray, offset: Int, length: Int) {
6363
buffer.copyInto(floatArray, offset, position, position + length)
64+
position += length
6465
}
6566

6667
public actual operator fun get(pos: Int): Float {
@@ -105,7 +106,7 @@ public actual class ByteBuffer : Buffer {
105106
}
106107

107108
public actual fun get(): Byte {
108-
return buffer[position]
109+
return buffer[position++]
109110
}
110111

111112
public actual fun get(byteArray: ByteArray) {
@@ -114,6 +115,7 @@ public actual class ByteBuffer : Buffer {
114115

115116
public actual fun get(byteArray: ByteArray, offset: Int, length: Int) {
116117
buffer.copyInto(byteArray, offset, position, position + length)
118+
position += length
117119
}
118120

119121
public actual operator fun get(pos: Int): Byte {
@@ -157,7 +159,7 @@ public actual class IntBuffer : Buffer {
157159
}
158160

159161
public actual fun get(): Int {
160-
return buffer[position]
162+
return buffer[position++]
161163
}
162164

163165
public actual fun get(intArray: IntArray) {
@@ -166,6 +168,7 @@ public actual class IntBuffer : Buffer {
166168

167169
public actual fun get(intArray: IntArray, offset: Int, length: Int) {
168170
buffer.copyInto(intArray, offset, position, position + length)
171+
position += length
169172
}
170173

171174
public actual operator fun get(pos: Int): Int {
@@ -210,7 +213,7 @@ public actual class ShortBuffer : Buffer {
210213
}
211214

212215
public actual fun get(): Short {
213-
return buffer[position]
216+
return buffer[position++]
214217
}
215218

216219
public actual fun get(shortArray: ShortArray) {
@@ -219,6 +222,7 @@ public actual class ShortBuffer : Buffer {
219222

220223
public actual fun get(shortArray: ShortArray, offset: Int, length: Int) {
221224
buffer.copyInto(shortArray, offset, position, position + length)
225+
position += length
222226
}
223227

224228
public actual operator fun get(pos: Int): Short {

0 commit comments

Comments
 (0)