Skip to content

Commit

Permalink
Add some quick tests for Kryo without registry.
Browse files Browse the repository at this point in the history
It seems to work fine for most common classes, but not EnumMap.
  • Loading branch information
tommyettinger committed Nov 13, 2023
1 parent 1dede05 commit 78efd38
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 1 deletion.
2 changes: 1 addition & 1 deletion kryo-jdkgdxds/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ POM_ARTIFACT_ID=kryo-jdkgdxds
POM_NAME=kryo-jdkgdxds
POM_DESCRIPTION=Kryo 5.x serializers for jdkgdxds types
POM_INCEPTION_YEAR=2022
VERSION_NAME=1.4.4.0
VERSION_NAME=1.4.4.1-SNAPSHOT
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright (c) 2023 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package com.github.tommyettinger.kryo.jdkgdxds;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.github.tommyettinger.ds.IntBag;
import com.github.tommyettinger.ds.LongDeque;
import com.github.tommyettinger.ds.ObjectFloatOrderedMap;
import com.github.tommyettinger.ds.ObjectList;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.objenesis.strategy.StdInstantiatorStrategy;

import java.io.ByteArrayOutputStream;
import java.util.EnumMap;

public class AutoTest {
@Test
public void testAutoLongDeque() {
Kryo kryo = new Kryo();
kryo.setRegistrationRequired(false);

LongDeque data = LongDeque.with(-1234567890L, 0L, 4567890123456789L, 0, 1L, 1, -1, 0);

ByteArrayOutputStream baos = new ByteArrayOutputStream(32);
Output output = new Output(baos);
kryo.writeObject(output, data);
byte[] bytes = output.toBytes();
try (Input input = new Input(bytes)) {
LongDeque data2 = kryo.readObject(input, LongDeque.class);
Assert.assertEquals(data, data2);
}
}

@Test
public void testAutoIntBag() {
Kryo kryo = new Kryo();
kryo.setRegistrationRequired(false);

IntBag data = IntBag.with(-123, 0, 456, 0, 1, -1, 0);

ByteArrayOutputStream baos = new ByteArrayOutputStream(32);
Output output = new Output(baos);
kryo.writeObject(output, data);
byte[] bytes = output.toBytes();
try (Input input = new Input(bytes)) {
IntBag data2 = kryo.readObject(input, IntBag.class);
Assert.assertEquals(data, data2);
}
}

@Test
public void testAutoObjectFloatOrderedMap() {
Kryo kryo = new Kryo();
kryo.setRegistrationRequired(false);

ObjectFloatOrderedMap<ObjectList<String>> data = ObjectFloatOrderedMap.with(ObjectList.with("Cthulhu"),
-123456.789f, ObjectList.with("lies"), 0f, ObjectList.with("deep"),
4.56789f, ObjectList.with("in"), 0.0001f, ObjectList.with("Rl'yeh"), 1f, ObjectList.with("dreaming"), 1,
ObjectList.with("of"), -1, ObjectList.with("waffles"), 0);

ByteArrayOutputStream baos = new ByteArrayOutputStream(32);
Output output = new Output(baos);
kryo.writeObject(output, data);
byte[] bytes = output.toBytes();
try (Input input = new Input(bytes)) {
ObjectFloatOrderedMap<?> data2 = kryo.readObject(input, ObjectFloatOrderedMap.class);
Assert.assertEquals(data, data2);
Assert.assertEquals(data.order(), data2.order());
}
}

/**
* This fails because EnumMap really needs a Class of an enum, and the generics don't provide it.
* There's a private Class variable inside an EnumMap, but it needs reflection to access.
*/
@Test
@Ignore
public void testAutoEnumMap() {
Kryo kryo = new Kryo();
kryo.setRegistrationRequired(false);
kryo.setInstantiatorStrategy(new StdInstantiatorStrategy());
EnumMap<Character.UnicodeScript, String> data = new EnumMap<>(Character.UnicodeScript.class);
data.put(Character.UnicodeScript.EGYPTIAN_HIEROGLYPHS, "Horus");
data.put(Character.UnicodeScript.GREEK, "Zeus");
data.put(Character.UnicodeScript.RUNIC, "Odin");

ByteArrayOutputStream baos = new ByteArrayOutputStream(32);
Output output = new Output(baos);
kryo.writeObject(output, data);
byte[] bytes = output.toBytes();
try (Input input = new Input(bytes)) {
EnumMap<?, ?> data2 = kryo.readObject(input, EnumMap.class);
Assert.assertEquals(data, data2);
}
}

@Test
public void testAutoObjectList() {
Kryo kryo = new Kryo();
kryo.setRegistrationRequired(false);

ObjectList<String> data = ObjectList.with("-123.123", "0", "Four-Fifty Six", "0", "1.0", "-1.0", "0.000001");

ByteArrayOutputStream baos = new ByteArrayOutputStream(32);
Output output = new Output(baos);
kryo.writeObject(output, data);
byte[] bytes = output.toBytes();
try (Input input = new Input(bytes)) {
ObjectList data2 = kryo.readObject(input, ObjectList.class);
Assert.assertEquals(data, data2);
}
}

}

0 comments on commit 78efd38

Please sign in to comment.