Skip to content

Commit

Permalink
Merge pull request #3 from XYOracleNetwork/develop
Browse files Browse the repository at this point in the history
add to set
  • Loading branch information
carterharrison authored Nov 20, 2018
2 parents 4037751 + c22566d commit 324848e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ google-services.json
core-android-sample/local.properties
gradle/
/xyo
/out
/out
testingDb/
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,30 @@ object XyoObjectSetCreator {
return XyoObjectCreator.createObject(schema, buffer.array())
}

/**
* Adds to a set
*/
fun addToIterableObject (item : ByteArray, set : ByteArray) : ByteArray {
val setSchema = XyoObjectSchema.createFromHeader(set.copyOfRange(0, 2))
val setValue = XyoObjectCreator.getObjectValue(set)

if (!setSchema.isIterable) throw XyoObjectExceotion("Can no add to non-iterable object.")

if (setSchema.isTyped) {
val schemaOfType = set.copyOfRange(2 + setSchema.sizeIdentifier, 4 + setSchema.sizeIdentifier)

if (!schemaOfType.contentEquals(item.copyOfRange(0, 2))) throw XyoObjectExceotion("Can not add different " +
"type to typed array!")

val buffer = ByteBuffer.allocate(setValue.size + item.size - 2)
buffer.put(setValue)
buffer.put(item.copyOfRange(2, item.size))
return XyoObjectCreator.createObject(setSchema, buffer.array())
}

val buffer = ByteBuffer.allocate(setValue.size + item.size)
buffer.put(setValue)
buffer.put(item)
return XyoObjectCreator.createObject(setSchema, buffer.array())
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package network.xyo.sdkobjectmodelkotlin

import network.xyo.sdkobjectmodelkotlin.objects.XyoObjectCreator
import network.xyo.sdkobjectmodelkotlin.objects.sets.XyoObjectIterator
import network.xyo.sdkobjectmodelkotlin.objects.sets.XyoObjectSetCreator
import network.xyo.sdkobjectmodelkotlin.schema.XyoObjectSchema
import org.junit.Assert
Expand Down Expand Up @@ -61,4 +62,20 @@ class XyoObjectSetCreatorTest {

Assert.assertArrayEquals(expectedSet, createdSet)
}

@Test
fun addToUntypedArray () {
val originalSet = byteArrayOf(0x20, 0x41, 0x09, 0x00, 0x44, 0x02, 0x13, 0x00, 0x42, 0x02, 0x37)
val addedSet = XyoObjectSetCreator.addToIterableObject(originalSet, originalSet)
Assert.assertArrayEquals(originalSet, XyoObjectIterator(addedSet)[2])
}

@Test
fun addToTypedArray () {
val originalSet = byteArrayOf(0x30, 0x41, 0x07, 0x00, 0x44, 0x02, 0x13, 0x02, 0x37)
val itemToAdd = byteArrayOf(0x00, 0x44, 0x02, 0x18)
val addedSet = XyoObjectSetCreator.addToIterableObject(itemToAdd, originalSet)
Assert.assertArrayEquals(byteArrayOf(0x30, 0x41, 0x09, 0x00, 0x44, 0x02, 0x13, 0x02, 0x37, 0x02, 0x18), addedSet)
Assert.assertArrayEquals(itemToAdd, XyoObjectIterator(addedSet)[2])
}
}

0 comments on commit 324848e

Please sign in to comment.