Skip to content

Commit

Permalink
Fix some UBO stuff as it seems to be broken...
Browse files Browse the repository at this point in the history
  • Loading branch information
XyperCode committed Mar 21, 2024
1 parent 9ec6d0f commit f018f57
Show file tree
Hide file tree
Showing 11 changed files with 330 additions and 18 deletions.
7 changes: 1 addition & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,4 @@ bin/

### Mac OS ###
.DS_Store
/map-compressed.ubo
/map-normal.ubo
/map.uso
/list-compressed.ubo
/list-normal.ubo
/list.uso
/*.u[sb]o
109 changes: 105 additions & 4 deletions .idea/inspectionProfiles/Project_Default.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/main/java/com/ultreon/data/types/BigDecType.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public BigDecType(BigDecimal obj) {
this.obj = obj;
}

public BigDecType(String number) {
this.obj = new BigDecimal(number);
}

@Override
public BigDecimal getValue() {
return obj;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/ultreon/data/types/BigIntType.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public BigIntType(BigInteger obj) {
this.obj = obj;
}

public BigIntType(String number) {
this.obj = new BigInteger(number);
}

@Override
public BigInteger getValue() {
return obj;
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/com/ultreon/data/types/BitSetType.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ public BitSetType(BitSet obj) {
this.obj = obj;
}

public BitSetType(String bits) {
this.obj = new BitSet(bits.length());
for (int i = 0; i < bits.length(); i++) {
this.obj.set(i, bits.charAt(i) == '1');
}
}

@Override
public BitSet getValue() {
return obj;
Expand Down Expand Up @@ -83,7 +90,7 @@ public String writeUso() {
builder.append(obj.get(i) ? "1" : "0");
}

return builder.toString() + ";";
return builder + ";";
}

public void setBit(int index, boolean value) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/ultreon/data/types/ByteType.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public ByteType(byte obj) {
this.obj = obj;
}

public ByteType(int obj) {
this.obj = (byte) obj;
}

@Override
public Byte getValue() {
return obj;
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/com/ultreon/data/types/ListType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.ultreon.data.TypeRegistry;
import com.ultreon.data.Types;
import org.jetbrains.annotations.Contract;

import java.io.DataInputStream;
import java.io.DataOutputStream;
Expand Down Expand Up @@ -112,10 +113,11 @@ public boolean hasNext() {

@Override
public T next() {
if (index++ >= list.size())
int oldIdx = index++;
if (oldIdx >= list.size())
throw new NoSuchElementException("No more elements in list");

return list.get(index);
return list.get(oldIdx);
}
};
}
Expand All @@ -141,7 +143,14 @@ public T get(int index) {
}

public boolean remove(int index) {
return obj.remove(get(index));
if (index >= obj.size())
throw new IndexOutOfBoundsException("Index out of bounds: " + index);
if (index < 0)
index = obj.size() + index;
if (index < 0)
throw new IndexOutOfBoundsException("Index out of bounds: " + index);
obj.remove(index);
return true;
}

public T pop(int index) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/ultreon/data/types/ShortType.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public ShortType(short obj) {
this.obj = obj;
}

public ShortType(int obj) {
this.obj = (short) obj;
}

@Override
public Short getValue() {
return obj;
Expand Down
136 changes: 136 additions & 0 deletions src/test/java/com/ultreon/tests/data/TypeTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package com.ultreon.tests.data;

import com.ultreon.data.types.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.BitSet;
import java.util.Iterator;
import java.util.UUID;

public class TypeTests {
@Test
@DisplayName("ListTypes")
void listTypes() {
ListType<StringType> list = new ListType<>();
list.add(new StringType("Apple"));
list.add(new StringType("Banana"));
list.add(new StringType("Pear"));
list.add(new StringType("Orange"));
list.add(new StringType("Watermelon"));
Assertions.assertEquals(list.get(0), new StringType("Apple"));
Assertions.assertEquals(list.get(1), new StringType("Banana"));
Assertions.assertEquals(list.get(2), new StringType("Pear"));
Assertions.assertEquals(list.get(3), new StringType("Orange"));
Assertions.assertEquals(list.get(4), new StringType("Watermelon"));

Assertions.assertEquals(list.size(), 5);

list.remove(0);

Assertions.assertEquals(list.size(), 4);

Iterator<StringType> iterator = list.iterator();

Assertions.assertEquals(iterator.next(), new StringType("Banana"));
Assertions.assertEquals(iterator.next(), new StringType("Pear"));
Assertions.assertEquals(iterator.next(), new StringType("Orange"));
Assertions.assertEquals(iterator.next(), new StringType("Watermelon"));

Assertions.assertFalse(iterator.hasNext());
}

@Test
@DisplayName("MapTypes")
void mapTypes() {
MapType map = new MapType();
map.put("String", new StringType("Apple"));
map.put("Integer", new IntType(5));
map.put("List", new ListType<StringType>(new StringType("Banana")));
map.putInt("Integer2", 6);
map.putString("String2", "Banana");
map.putBigInt("Integer3", new BigInteger("1234567890123456789012345678901234567890"));
map.putBigDec("Integer4", new BigDecimal("1234567890123456789012345678901234567890.9876543210123456789012345678901234567890"));
map.putByteArray("ByteArray", new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15});
map.putBoolean("Boolean", true);

Assertions.assertEquals(map.get("String"), new StringType("Apple"));
Assertions.assertEquals(map.get("Integer"), new IntType(5));
Assertions.assertEquals(map.get("List"), new ListType<StringType>(new StringType("Banana")));
Assertions.assertEquals(map.getInt("Integer2"), 6);
Assertions.assertEquals(map.getString("String2"), "Banana");
Assertions.assertEquals(map.getBigInt("Integer3"), new BigInteger("1234567890123456789012345678901234567890"));
Assertions.assertEquals(map.getBigDec("Integer4"), new BigDecimal("1234567890123456789012345678901234567890.9876543210123456789012345678901234567890"));
Assertions.assertArrayEquals(map.getByteArray("ByteArray"), new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15});
Assertions.assertEquals(map.getBoolean("Boolean"), true);

Assertions.assertEquals(map.size(), 9);

map.remove("Integer2");

Assertions.assertEquals(map.size(), 8);
}

@Test
@DisplayName("PrimitiveTypes")
void primitiveTypes() {
Assertions.assertEquals(new IntType(5).getValue(), 5);
Assertions.assertEquals(new StringType("Apple").getValue(), "Apple");
Assertions.assertEquals(new BooleanType(true).getValue(), true);
Assertions.assertArrayEquals(new ByteArrayType(new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}).getValue(), new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15});
Assertions.assertEquals(new BigIntType(new BigInteger("1234567890123456789012345678901234567890")).getValue(), new BigInteger("1234567890123456789012345678901234567890"));

Assertions.assertEquals(new IntType(5).toString(), "5i");
Assertions.assertEquals(new StringType("Apple").toString(), "\"Apple\"");
Assertions.assertEquals(new BooleanType(true).toString(), "true");
Assertions.assertEquals(new ByteArrayType(new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}).toString(), "(b;0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)");
Assertions.assertEquals(new BigIntType(new BigInteger("1234567890123456789012345678901234567890")).toString(), "1234567890123456789012345678901234567890I");
Assertions.assertEquals(new ByteType(5).toString(), "5b");
Assertions.assertEquals(new ShortType(5).toString(), "5s");
Assertions.assertEquals(new IntType(5).toString(), "5i");
Assertions.assertEquals(new LongType(5).toString(), "5l");
Assertions.assertEquals(new FloatType(5.5f).toString(), "5.5f");
Assertions.assertEquals(new DoubleType(5.5).toString(), "5.5d");
Assertions.assertEquals(new BigDecType(new BigDecimal("1234567890123456789012345678901234567890.9876543210123456789012345678901234567890")).toString(), "1234567890123456789012345678901234567890.9876543210123456789012345678901234567890D");
Assertions.assertEquals(new UUIDType(UUID.fromString("00000000-0000-0000-0000-000000000000")).toString(), "<00000000-0000-0000-0000-000000000000>");
Assertions.assertEquals(new BitSetType(new BitSet()).toString(), "x;");
BitSet obj = new BitSet(5);
obj.set(0);
obj.set(3);
obj.set(4);
obj.set(9);
Assertions.assertEquals(new BitSetType(obj).toString(), "x1001100001;");

Assertions.assertEquals(new IntType(5).copy(), new IntType(5));
Assertions.assertEquals(new StringType("Apple").copy(), new StringType("Apple"));
Assertions.assertEquals(new BooleanType(true).copy(), new BooleanType(true));
Assertions.assertEquals(new ByteArrayType(new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}).copy(), new ByteArrayType(new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}));
Assertions.assertEquals(new BigIntType(new BigInteger("1234567890123456789012345678901234567890")).copy(), new BigIntType(new BigInteger("1234567890123456789012345678901234567890")));
Assertions.assertEquals(new ByteType(5).copy(), new ByteType(5));
Assertions.assertEquals(new ShortType(5).copy(), new ShortType(5));
Assertions.assertEquals(new IntType(5).copy(), new IntType(5));
Assertions.assertEquals(new LongType(5).copy(), new LongType(5));
Assertions.assertEquals(new FloatType(5.5f).copy(), new FloatType(5.5f));
Assertions.assertEquals(new DoubleType(5.5).copy(), new DoubleType(5.5));
Assertions.assertEquals(new BigDecType("1234567890123456789012345678901234567890.9876543210123456789012345678901234567890").copy(), new BigDecType("1234567890123456789012345678901234567890.9876543210123456789012345678901234567890"));
Assertions.assertEquals(new UUIDType(UUID.fromString("00000000-0000-0000-0000-000000000000")).copy(), new UUIDType(UUID.fromString("00000000-0000-0000-0000-000000000000")));
Assertions.assertEquals(new BitSetType(new BitSet()).copy(), new BitSetType(new BitSet()));
Assertions.assertEquals(new BitSetType(new BitSet(5)).copy(), new BitSetType(new BitSet(5)));
BitSet obj2 = new BitSet(5);
obj2.set(0);
obj2.set(3);
obj2.set(4);
obj2.set(9);

BitSet obj3 = new BitSet(5);
obj3.set(0);
obj3.set(3);
obj3.set(4);
obj3.set(9);

Assertions.assertEquals(new BitSetType(obj2).copy(), new BitSetType(obj3));
}
}
54 changes: 51 additions & 3 deletions src/test/java/com/ultreon/tests/data/UboReadWriteTests.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package com.ultreon.tests.data;

import com.sun.jdi.IntegerType;
import com.ultreon.data.DataIo;
import com.ultreon.data.types.ListType;
import com.ultreon.data.types.MapType;
import com.ultreon.data.types.StringType;
import com.ultreon.data.types.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.BitSet;
import java.util.Map;
import java.util.UUID;
import java.util.function.Supplier;

class UboReadWriteTests {
@Test
Expand Down Expand Up @@ -92,4 +97,47 @@ void readWriteList() {
}
Assertions.assertEquals(readCompressedList, list);
}

@Test
@DisplayName("PrimitiveTypes")
void readWritePrimitive() {
try {
readWriteTest(() -> new StringType("Apple"), new File("string.ubo"));
readWriteTest(() -> new ByteType((byte) 1), new File("byte.ubo"));
readWriteTest(() -> new ShortType((short) 1), new File("short.ubo"));
readWriteTest(() -> new IntType(1), new File("int.ubo"));
readWriteTest(() -> new LongType(1L), new File("long.ubo"));
readWriteTest(() -> new FloatType(1f), new File("float.ubo"));
readWriteTest(() -> new DoubleType(1d), new File("double.ubo"));
readWriteTest(() -> new BigIntType(new BigInteger("1")), new File("bigint.ubo"));
readWriteTest(() -> new BigDecType(new BigDecimal("1")), new File("bigdec.ubo"));
readWriteTest(() -> new BooleanType(true), new File("boolean.ubo"));
readWriteTest(() -> new UUIDType(UUID.fromString("00000000-0000-0000-0000-000000000000")), new File("uuid.ubo"));
readWriteTest(() -> new BitSetType(new BitSet()), new File("integer.ubo"));
readWriteTest(() -> new ByteArrayType(new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}), new File("bytearray.ubo"));
readWriteTest(() -> new ShortArrayType(new short[]{0, 32, 128, 512, 2048, 8192, 32767}), new File("shortarray.ubo"));
readWriteTest(() -> new IntArrayType(new int[]{0, 128, 512, 2048, 8192, 32767, 262140, 1048576, 4194304, 16777216, 67108864, 268435456, 1073741824, 2147483647}), new File("intarray.ubo"));
readWriteTest(() -> new LongArrayType(new long[]{0, 1024, 1048576, 1073741824, 2147483647, 4294967295L, 4398046510080L, 4503599626321920L, 4611686017353646080L, 9223372036854775807L}), new File("longarray.ubo"));
readWriteTest(() -> new FloatArrayType(new float[]{0.0f, 0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f, 0.7f, 0.8f, 0.9f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 15.0f}), new File("floatarray.ubo"));
readWriteTest(() -> new DoubleArrayType(new double[]{0.000000001, 0.0000001, 0.000001, 0.00001, 0.0001, 0.001, 0.01, 0.1, 1.0, 10.0, 100.0, 1000.0}), new File("doublearray.ubo"));
readWriteTest(() -> new ListType<>(new StringType("Apple"), new StringType("Banana"), new StringType("Pear")), new File("list.ubo"));
readWriteTest(() -> new MapType(Map.of("Apple", new StringType("Apple"), "Banana", new StringType("Banana"), "Pear", new StringType("Pear"))), new File("map.ubo"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static <T extends IType<?>> void readWriteTest(Supplier<T> supplier, File file, T... typeGetter) throws IOException {
System.out.println("Writing primitive data for " + file.getName() + "...");
DataIo.write(supplier.get(), file);

System.out.println("Reading primitive data for " + file.getName() + "...");
Assertions.assertEquals(DataIo.read(file, typeGetter), supplier.get());

System.out.println("Writing compressed primitive data for " + file.getName() + "...");
DataIo.writeCompressed(supplier.get(), file);

System.out.println("Reading compressed primitive data for " + file.getName() + "...");
Assertions.assertEquals(DataIo.readCompressed(file, typeGetter), supplier.get());
}
}

0 comments on commit f018f57

Please sign in to comment.