SCRIPT Move java files

This commit is contained in:
PJ Reiniger
2025-11-07 19:55:40 -05:00
committed by Peter Johnson
parent 7ca1be9bae
commit c350c5f112
1486 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.epilogue.logging;
import static org.junit.jupiter.api.Assertions.assertEquals;
import edu.wpi.first.epilogue.Logged;
import java.util.List;
import org.junit.jupiter.api.Test;
class ClassSpecificLoggerTest {
@Logged
record Point2d(double x, double y, int dim) {
static class Logger extends ClassSpecificLogger<Point2d> {
Logger() {
super(Point2d.class);
}
@Override
protected void update(EpilogueBackend backend, Point2d object) {
backend.log("x", object.x);
backend.log("y", object.y);
backend.log("dim", object.dim);
}
}
}
@Test
void testReadPrivate() {
var point = new Point2d(1, 4, 2);
var logger = new Point2d.Logger();
var dataLog = new TestBackend();
logger.update(dataLog.getNested("Point"), point);
assertEquals(
List.of(
new TestBackend.LogEntry<>("Point/x", 1.0),
new TestBackend.LogEntry<>("Point/y", 4.0),
new TestBackend.LogEntry<>("Point/dim", 2)),
dataLog.getEntries());
}
}

View File

@@ -0,0 +1,45 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.epilogue.logging;
import edu.wpi.first.util.struct.Struct;
import edu.wpi.first.util.struct.StructSerializable;
import java.nio.ByteBuffer;
public record CustomStruct(int x) implements StructSerializable {
public static final Serializer struct = new Serializer();
public static final class Serializer implements Struct<CustomStruct> {
@Override
public Class<CustomStruct> getTypeClass() {
return CustomStruct.class;
}
@Override
public String getTypeName() {
return "CustomStruct";
}
@Override
public int getSize() {
return kSizeInt32;
}
@Override
public String getSchema() {
return "int32 x;";
}
@Override
public CustomStruct unpack(ByteBuffer bb) {
return new CustomStruct(bb.getInt());
}
@Override
public void pack(ByteBuffer bb, CustomStruct value) {
bb.putInt(value.x);
}
}
}

View File

@@ -0,0 +1,202 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.epilogue.logging;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import edu.wpi.first.math.geometry.Rotation2d;
import java.util.List;
import org.junit.jupiter.api.Test;
class LazyBackendTest {
@Test
void lazyOfLazyReturnsSelf() {
var lazy = new LazyBackend(new NullBackend());
assertSame(lazy, lazy.lazy());
}
@Test
void lazyInt() {
var backend = new TestBackend();
var lazy = new LazyBackend(backend);
{
// First time logging to "int" should go through
lazy.log("int", 0);
assertEquals(List.of(new TestBackend.LogEntry<>("int", 0)), backend.getEntries());
}
{
// Logging the current value shouldn't go through
lazy.log("int", 0);
assertEquals(List.of(new TestBackend.LogEntry<>("int", 0)), backend.getEntries());
}
{
// Logging a new value should go through
lazy.log("int", 1);
assertEquals(
List.of(new TestBackend.LogEntry<>("int", 0), new TestBackend.LogEntry<>("int", 1)),
backend.getEntries());
}
{
// Logging a previous value should go through
lazy.log("int", 0);
assertEquals(
List.of(
new TestBackend.LogEntry<>("int", 0),
new TestBackend.LogEntry<>("int", 1),
new TestBackend.LogEntry<>("int", 0)),
backend.getEntries());
}
}
@Test
void inPlaceByteArray() {
var backend = new TestBackend();
var lazy = new LazyBackend(backend);
byte[] arr = new byte[] {0};
lazy.log("arr", arr);
arr[0] = 1;
lazy.log("arr", arr);
assertEquals(2, backend.getEntries().size());
assertArrayEquals(new byte[] {0}, (byte[]) backend.getEntries().get(0).value());
assertArrayEquals(new byte[] {1}, (byte[]) backend.getEntries().get(1).value());
}
@Test
void inPlaceIntArray() {
var backend = new TestBackend();
var lazy = new LazyBackend(backend);
int[] arr = new int[] {0};
lazy.log("arr", arr);
arr[0] = 1;
lazy.log("arr", arr);
assertEquals(2, backend.getEntries().size());
assertArrayEquals(new int[] {0}, (int[]) backend.getEntries().get(0).value());
assertArrayEquals(new int[] {1}, (int[]) backend.getEntries().get(1).value());
}
@Test
void inPlaceLongArray() {
var backend = new TestBackend();
var lazy = new LazyBackend(backend);
long[] arr = new long[] {0};
lazy.log("arr", arr);
arr[0] = 1;
lazy.log("arr", arr);
assertEquals(2, backend.getEntries().size());
assertArrayEquals(new long[] {0}, (long[]) backend.getEntries().get(0).value());
assertArrayEquals(new long[] {1}, (long[]) backend.getEntries().get(1).value());
}
@Test
void inPlaceFloatArray() {
var backend = new TestBackend();
var lazy = new LazyBackend(backend);
float[] arr = new float[] {0};
lazy.log("arr", arr);
arr[0] = 1;
lazy.log("arr", arr);
assertEquals(2, backend.getEntries().size());
assertArrayEquals(new float[] {0}, (float[]) backend.getEntries().get(0).value());
assertArrayEquals(new float[] {1}, (float[]) backend.getEntries().get(1).value());
}
@Test
void inPlaceDoubleArray() {
var backend = new TestBackend();
var lazy = new LazyBackend(backend);
double[] arr = new double[] {0};
lazy.log("arr", arr);
arr[0] = 1;
lazy.log("arr", arr);
assertEquals(2, backend.getEntries().size());
assertArrayEquals(new double[] {0}, (double[]) backend.getEntries().get(0).value());
assertArrayEquals(new double[] {1}, (double[]) backend.getEntries().get(1).value());
}
@Test
void inPlaceBooleanArray() {
var backend = new TestBackend();
var lazy = new LazyBackend(backend);
boolean[] arr = new boolean[] {false};
lazy.log("arr", arr);
arr[0] = true;
lazy.log("arr", arr);
assertEquals(2, backend.getEntries().size());
assertArrayEquals(new boolean[] {false}, (boolean[]) backend.getEntries().get(0).value());
assertArrayEquals(new boolean[] {true}, (boolean[]) backend.getEntries().get(1).value());
}
@Test
void inPlaceStringArray() {
var backend = new TestBackend();
var lazy = new LazyBackend(backend);
String[] arr = new String[] {"0"};
lazy.log("arr", arr);
arr[0] = "1";
lazy.log("arr", arr);
assertEquals(2, backend.getEntries().size());
assertArrayEquals(new String[] {"0"}, (String[]) backend.getEntries().get(0).value());
assertArrayEquals(new String[] {"1"}, (String[]) backend.getEntries().get(1).value());
}
@Test
void inPlaceStructArray() {
var backend = new TestBackend();
var lazy = new LazyBackend(backend);
CustomStruct[] arr = new CustomStruct[] {new CustomStruct(0)};
lazy.log("arr", arr, CustomStruct.struct);
arr[0] = new CustomStruct(1);
lazy.log("arr", arr, CustomStruct.struct);
assertEquals(2, backend.getEntries().size());
assertArrayEquals(
new byte[] {0x00, 0x00, 0x00, 0x00}, (byte[]) backend.getEntries().get(0).value());
assertArrayEquals(
new byte[] {0x01, 0x00, 0x00, 0x00}, (byte[]) backend.getEntries().get(1).value());
}
@Test
void lazyProtobuf() {
var backend = new TestBackend();
var lazy = new LazyBackend(backend);
var rotation = Rotation2d.kZero;
lazy.log("rotation", rotation, Rotation2d.proto);
assertEquals(1, backend.getEntries().size());
var entry = backend.getEntries().get(0);
assertEquals("rotation", entry.identifier());
assertArrayEquals(new byte[] {9, 0, 0, 0, 0, 0, 0, 0, 0}, (byte[]) entry.value());
}
}

View File

@@ -0,0 +1,180 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.epilogue.logging;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import org.junit.jupiter.api.Test;
class NestedBackendTest {
@Test
void prefixesAppliedAndNested() {
var root = new TestBackend();
var nested = new NestedBackend("/Robot", root);
nested.log("int", 1);
nested.log("string", "hello");
var arm = nested.getNested("arm");
arm.log("position", 2.0);
arm.log("enabled", true);
assertEquals(4, root.getEntries().size());
assertEquals("/Robot/int", root.getEntries().get(0).identifier());
assertEquals(1, root.getEntries().get(0).value());
assertEquals("/Robot/string", root.getEntries().get(1).identifier());
assertEquals("hello", root.getEntries().get(1).value());
assertEquals("/Robot/arm/position", root.getEntries().get(2).identifier());
assertEquals(2.0, root.getEntries().get(2).value());
assertEquals("/Robot/arm/enabled", root.getEntries().get(3).identifier());
assertEquals(true, root.getEntries().get(3).value());
}
@Test
void handlesTrailingSlashOnPrefix() {
var root = new TestBackend();
var a = new NestedBackend("/Robot", root);
var b = new NestedBackend("/Robot/", root);
a.log("x", 1);
b.log("y", 2);
assertEquals("/Robot/x", root.getEntries().get(0).identifier());
assertEquals("/Robot/y", root.getEntries().get(1).identifier());
}
@Test
void getNestedIsCached() {
var root = new TestBackend();
var nested = new NestedBackend("/Robot", root);
var arm1 = nested.getNested("arm");
var arm2 = nested.getNested("arm");
assertSame(arm1, arm2);
}
@Test
void usesPrefixedIdentifierCacheForSameField() {
var root = new TestBackend();
var nested = new NestedBackend("/Robot", root);
// Same field logged multiple times - identifier object should be the same (cached)
// We use assertSame to check that the references are identical
nested.log("x", 0);
nested.log("x", 1);
String id0 = root.getEntries().get(0).identifier();
String id1 = root.getEntries().get(1).identifier();
assertSame(
id0,
id1,
"Identifier %s (id: %d) was not reused (new id: %d)"
.formatted(id0, System.identityHashCode(id0), System.identityHashCode(id1)));
// Also verify through a nested backend path
var arm = nested.getNested("arm");
arm.log("position", 0.0);
arm.log("position", 1.0);
String id2 = root.getEntries().get(2).identifier();
String id3 = root.getEntries().get(3).identifier();
assertSame(
id2,
id3,
"Identifier %s (id: %d) was not reused (new id: %d)"
.formatted(id2, System.identityHashCode(id2), System.identityHashCode(id3)));
// Sanity check actual full values
assertEquals("/Robot/x", id0);
assertEquals("/Robot/arm/position", id2);
}
@Test
void logsAllOverloads() {
var root = new TestBackend();
var nested = new NestedBackend("/Robot", root);
// Scalars
nested.log("int", 1);
nested.log("long", 2L);
nested.log("float", 3.0f);
nested.log("double", 4.0);
nested.log("boolean", true);
nested.log("string", "hello");
// Arrays
nested.log("bytes", new byte[] {1, 2});
nested.log("ints", new int[] {3, 4});
nested.log("longs", new long[] {5L, 6L});
nested.log("floats", new float[] {7.0f, 8.0f});
nested.log("doubles", new double[] {9.0, 10.0});
nested.log("booleans", new boolean[] {true, false});
nested.log("strings", new String[] {"x", "y"});
// Structs
nested.log("customStruct", new CustomStruct(7), CustomStruct.struct);
nested.log(
"customStructs",
new CustomStruct[] {new CustomStruct(0), new CustomStruct(1)},
CustomStruct.struct);
var entries = root.getEntries();
int idx = 0;
// Scalars
assertEquals(new TestBackend.LogEntry<>("/Robot/int", 1), entries.get(idx++));
assertEquals(new TestBackend.LogEntry<>("/Robot/long", 2L), entries.get(idx++));
assertEquals(new TestBackend.LogEntry<>("/Robot/float", 3.0f), entries.get(idx++));
assertEquals(new TestBackend.LogEntry<>("/Robot/double", 4.0), entries.get(idx++));
assertEquals(new TestBackend.LogEntry<>("/Robot/boolean", true), entries.get(idx++));
assertEquals(new TestBackend.LogEntry<>("/Robot/string", "hello"), entries.get(idx++));
// Arrays
assertEquals("/Robot/bytes", entries.get(idx).identifier());
assertArrayEquals(new byte[] {1, 2}, (byte[]) entries.get(idx++).value());
assertEquals("/Robot/ints", entries.get(idx).identifier());
assertArrayEquals(new int[] {3, 4}, (int[]) entries.get(idx++).value());
assertEquals("/Robot/longs", entries.get(idx).identifier());
assertArrayEquals(new long[] {5L, 6L}, (long[]) entries.get(idx++).value());
assertEquals("/Robot/floats", entries.get(idx).identifier());
assertArrayEquals(new float[] {7.0f, 8.0f}, (float[]) entries.get(idx++).value());
assertEquals("/Robot/doubles", entries.get(idx).identifier());
assertArrayEquals(new double[] {9.0, 10.0}, (double[]) entries.get(idx++).value());
assertEquals("/Robot/booleans", entries.get(idx).identifier());
assertArrayEquals(new boolean[] {true, false}, (boolean[]) entries.get(idx++).value());
assertEquals("/Robot/strings", entries.get(idx).identifier());
assertArrayEquals(new String[] {"x", "y"}, (String[]) entries.get(idx++).value());
// Structs are serialized to bytes
assertEquals("/Robot/customStruct", entries.get(idx).identifier());
assertArrayEquals(new byte[] {0x07, 0x00, 0x00, 0x00}, (byte[]) entries.get(idx++).value());
assertEquals("/Robot/customStructs", entries.get(idx).identifier());
// two int32 values, little-endian
assertArrayEquals(
new byte[] {
0x00, 0x00, 0x00, 0x00, // 0 (first element)
0x01, 0x00, 0x00, 0x00, // 1 (second element)
0x00, 0x00, 0x00, 0x00, // 0 (empty space allocated by StructBuffer)
0x00, 0x00, 0x00, 0x00 // 0 (empty space allocated by StructBuffer)
},
(byte[]) entries.get(idx++).value());
// Ensure we covered all calls
assertEquals(idx, entries.size());
}
}

View File

@@ -0,0 +1,127 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.epilogue.logging;
import edu.wpi.first.util.protobuf.Protobuf;
import edu.wpi.first.util.struct.Struct;
import edu.wpi.first.util.struct.StructBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import us.hebi.quickbuf.ProtoMessage;
@SuppressWarnings("PMD.TestClassWithoutTestCases") // This is not a test class!
public class TestBackend implements EpilogueBackend {
public record LogEntry<T>(String identifier, T value) {}
private final Map<String, NestedBackend> m_nestedBackends = new HashMap<>();
private final List<LogEntry<?>> m_entries = new ArrayList<>();
public List<LogEntry<?>> getEntries() {
return m_entries;
}
@Override
public EpilogueBackend getNested(String path) {
return m_nestedBackends.computeIfAbsent(path, k -> new NestedBackend(k, this));
}
@Override
public void log(String identifier, int value) {
m_entries.add(new LogEntry<>(identifier, value));
}
@Override
public void log(String identifier, long value) {
m_entries.add(new LogEntry<>(identifier, value));
}
@Override
public void log(String identifier, float value) {
m_entries.add(new LogEntry<>(identifier, value));
}
@Override
public void log(String identifier, double value) {
m_entries.add(new LogEntry<>(identifier, value));
}
@Override
public void log(String identifier, boolean value) {
m_entries.add(new LogEntry<>(identifier, value));
}
@Override
public void log(String identifier, byte[] value) {
m_entries.add(new LogEntry<>(identifier, value.clone()));
}
@Override
public void log(String identifier, int[] value) {
m_entries.add(new LogEntry<>(identifier, value.clone()));
}
@Override
public void log(String identifier, long[] value) {
m_entries.add(new LogEntry<>(identifier, value.clone()));
}
@Override
public void log(String identifier, float[] value) {
m_entries.add(new LogEntry<>(identifier, value.clone()));
}
@Override
public void log(String identifier, double[] value) {
m_entries.add(new LogEntry<>(identifier, value.clone()));
}
@Override
public void log(String identifier, boolean[] value) {
m_entries.add(new LogEntry<>(identifier, value.clone()));
}
@Override
public void log(String identifier, String value) {
m_entries.add(new LogEntry<>(identifier, value));
}
@Override
public void log(String identifier, String[] value) {
m_entries.add(new LogEntry<>(identifier, value.clone()));
}
@Override
public <S> void log(String identifier, S value, Struct<S> struct) {
var buffer = StructBuffer.create(struct).write(value).position(0);
var serialized = new byte[buffer.capacity()];
for (int i = 0; i < buffer.capacity(); i++) {
serialized[i] = buffer.get();
}
m_entries.add(new LogEntry<>(identifier, serialized));
}
@Override
public <S> void log(String identifier, S[] value, Struct<S> struct) {
var buffer = StructBuffer.create(struct).writeArray(value).position(0);
var serialized = new byte[buffer.capacity()];
for (int i = 0; i < buffer.capacity(); i++) {
serialized[i] = buffer.get();
}
m_entries.add(new LogEntry<>(identifier, serialized));
}
@Override
public <P, M extends ProtoMessage<M>> void log(String identifier, P value, Protobuf<P, M> proto) {
var msg = proto.createMessage();
proto.pack(msg, value);
var serialized = msg.toByteArray();
m_entries.add(new LogEntry<>(identifier, serialized));
}
}