Skip to content

Commit

Permalink
Add FlowRandom to kryo-juniper; test it.
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyettinger committed Dec 9, 2023
1 parent d8ed56f commit e4ec170
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ kryoVersion=5.5.0
funderbyVersion=0.1.1
regexodusVersion=0.1.15
digitalVersion=0.4.5
juniperVersion=0.4.2
juniperVersion=0.5.0
jdkgdxdsVersion=1.4.4
simpleGraphsVersion=v5.1.1

Expand Down
2 changes: 1 addition & 1 deletion kryo-juniper/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ compileTestJava {
}

dependencies {
api "com.github.tommyettinger:kryo-digital:0.4.3.0"
api "com.github.tommyettinger:kryo-digital:0.4.5.0"
api "com.esotericsoftware:kryo:$kryoVersion"
api "com.github.tommyettinger:juniper:$juniperVersion"
testImplementation "junit:junit:4.13.2"
Expand Down
2 changes: 1 addition & 1 deletion kryo-juniper/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ POM_ARTIFACT_ID=kryo-juniper
POM_NAME=kryo-juniper
POM_DESCRIPTION=Kryo 5.x serializers for juniper types
POM_INCEPTION_YEAR=2022
VERSION_NAME=0.4.2.0
VERSION_NAME=0.5.0.0-SNAPSHOT
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2022-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.juniper;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.github.tommyettinger.random.FlowRandom;

/**
* Kryo {@link Serializer} for juniper {@link FlowRandom}s.
*/
public class FlowRandomSerializer extends Serializer<FlowRandom> {

public FlowRandomSerializer() {
setImmutable(false);
setAcceptsNull(false);
}

@Override
public void write(final Kryo kryo, final Output output, final FlowRandom data) {
output.writeVarLong(data.getStateA(), false);
output.writeVarLong(data.getStateB(), false);
}

@Override
public FlowRandom read(final Kryo kryo, final Input input, final Class<? extends FlowRandom> dataClass) {
return new FlowRandom(input.readVarLong(false), input.readVarLong(false));
}

@Override
public FlowRandom copy(Kryo kryo, FlowRandom original) {
return original.copy();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void testDistributions() throws NoSuchMethodException, InvocationTargetEx

for (Map.Entry<Class<? extends Distribution>, Class<? extends Serializer<? extends Distribution>>> ent : mapping.entrySet()) {
Kryo kryo = new Kryo();
kryo.register(WhiskerRandom.class, new WhiskerRandomSerializer());
kryo.register(AceRandom.class, new AceRandomSerializer());
kryo.register(ent.getKey(), ent.getValue().getDeclaredConstructor().newInstance());

Distribution data = ent.getKey().getDeclaredConstructor().newInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,26 @@ public void testMizuchiRandom() {
Assert.assertEquals(data, data2);
}
}


@Test
public void testFlowRandom() {
Kryo kryo = new Kryo();
kryo.register(FlowRandom.class, new FlowRandomSerializer());

FlowRandom data = new FlowRandom(-12345L);

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

@Test
public void testRomuTrioRandom() {
Kryo kryo = new Kryo();
Expand Down

0 comments on commit e4ec170

Please sign in to comment.