Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial unit tests for LogTable with tooling support #62

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,22 @@ http_archive(
urls = ["https://github.com/google/googletest/archive/e2239ee6043f73722e7aa812a459f54a28552929.zip"],
)

# JUnit5 is used for unit testing in Java. We pull that in via contrib_rules_jvm.
http_archive(
name = "contrib_rules_jvm",
sha256 = "bfb24b0959b98d1f4b2181896a42b0e01a869b7994d53158d48e3ef979aafd89",
strip_prefix = "rules_jvm-0.21.4",
url = "https://github.com/bazel-contrib/rules_jvm/releases/download/v0.21.4/rules_jvm-v0.21.4.tar.gz",
)

load("@contrib_rules_jvm//:repositories.bzl", "contrib_rules_jvm_deps")

contrib_rules_jvm_deps()

load("@contrib_rules_jvm//:setup.bzl", "contrib_rules_jvm_setup")

contrib_rules_jvm_setup()

# This makes WPILib's source repo (allwpilib) available as a repository within our Bazel workspace.
#new_git_repository(
# name = "allwpilib",
Expand Down
7 changes: 5 additions & 2 deletions generate_library_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@
"com.fasterxml.jackson.core:jackson-databind:2.15.2",
"org.ejml:ejml-simple:0.43.1",
"org.ejml:ejml-core:0.43.1",
"junit:junit:4.13.2",
"com.squareup:javapoet:1.13.0",
"us.hebi.quickbuf:quickbuf-runtime:1.3.2"
"us.hebi.quickbuf:quickbuf-runtime:1.3.2",
"org.junit.jupiter:junit-jupiter-api:5.10.1",
"org.junit.jupiter:junit-jupiter-engine:5.10.1",
"org.junit.platform:junit-platform-launcher:1.10.1",
"org.junit.platform:junit-platform-reporting:1.10.1",
]

# WPILib dependencies to pull from frcmaven. If no version is provided, the above version is used.
Expand Down
19 changes: 18 additions & 1 deletion junction/core/BUILD
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
load("//build_tools/repo:java_export.bzl", "java_export")
load("@contrib_rules_jvm//java:defs.bzl", "java_test_suite")

java_library(
name = "core",
srcs = glob(["src/**/*.java"]),
srcs = glob(["src/main/**/*.java"]),
tags = ["ci_build"],
visibility = ["//visibility:public"],
deps = [
Expand All @@ -27,3 +28,19 @@ java_export(
visibility = ["//visibility:public"],
runtime_deps = [":core"],
)

java_test_suite(
name = "core-tests",
srcs = glob(["src/test/**/*.java"]),
runner = "junit5",
runtime_deps = [
"@maven//:org_junit_jupiter_junit_jupiter_engine",
"@maven//:org_junit_platform_junit_platform_launcher",
"@maven//:org_junit_platform_junit_platform_reporting",
],
deps = [
":core-export",
"//third_party/wpilib:wpilibj-compileonly",
"@maven//:org_junit_jupiter_junit_jupiter_api",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import java.util.Objects;
import java.util.Set;

import org.littletonrobotics.junction.LogTable.LogValue;

import edu.wpi.first.units.ImmutableMeasure;
import edu.wpi.first.units.MutableMeasure;
import edu.wpi.first.units.Measure;
Expand Down Expand Up @@ -359,15 +357,7 @@ private void addStructSchema(Struct<?> struct, Set<String> seen) {
public <T> void put(String key, Struct<T> struct, T value) {
if (value == null) return;
if (writeAllowed(key, LoggableType.Raw)) {
addStructSchema(struct, new HashSet<>());
if (!structBuffers.containsKey(struct.getTypeString())) {
structBuffers.put(struct.getTypeString(), StructBuffer.create(struct));
}
StructBuffer<T> buffer = (StructBuffer<T>) structBuffers.get(struct.getTypeString());
ByteBuffer bb = buffer.write(value);
byte[] array = new byte[bb.position()];
bb.position(0);
bb.get(array);
byte[] array = pack(struct, value);
put(key, new LogValue(array, struct.getTypeString()));
}
}
Expand All @@ -380,19 +370,36 @@ public <T> void put(String key, Struct<T> struct, T value) {
public <T> void put(String key, Struct<T> struct, T... value) {
if (value == null) return;
if (writeAllowed(key, LoggableType.Raw)) {
addStructSchema(struct, new HashSet<>());
if (!structBuffers.containsKey(struct.getTypeString())) {
structBuffers.put(struct.getTypeString(), StructBuffer.create(struct));
}
StructBuffer<T> buffer = (StructBuffer<T>) structBuffers.get(struct.getTypeString());
ByteBuffer bb = buffer.writeArray(value);
byte[] array = new byte[bb.position()];
bb.position(0);
bb.get(array);
byte[] array = pack(struct, value);
put(key, new LogValue(array, struct.getTypeString() + "[]"));
}
}

private <T> StructBuffer<T> bufferForStruct(Struct<T> struct) {
addStructSchema(struct, new HashSet<>());
if (!structBuffers.containsKey(struct.getTypeString())) {
structBuffers.put(struct.getTypeString(), StructBuffer.create(struct));
}
return (StructBuffer<T>) structBuffers.get(struct.getTypeString());
}

private <T> byte[] pack(Struct<T> struct, T[] value) {
StructBuffer<T> buffer = bufferForStruct(struct);
return copyBytes(buffer.writeArray(value));
}

private <T> byte[] pack(Struct<T> struct, T value) {
StructBuffer<T> buffer = bufferForStruct(struct);
return copyBytes(buffer.write(value));
}

private static byte[] copyBytes(ByteBuffer bb) {
byte[] array = new byte[bb.position()];
bb.position(0);
bb.get(array);
return array;
}

/**
* Writes a new protobuf value to the table. Skipped if the key already exists
* as a different type.
Expand All @@ -410,10 +417,7 @@ public <T, MessageType extends ProtoMessage<?>> void put(String key, Protobuf<T,
ByteBuffer bb;
try {
bb = buffer.write(value);
byte[] array = new byte[bb.position()];
bb.position(0);
bb.get(array);
put(key, new LogValue(array, proto.getTypeString()));
put(key, new LogValue(copyBytes(bb), proto.getTypeString()));
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.littletonrobotics.junction;

import edu.wpi.first.math.geometry.Rotation2d;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class LogTableTest {
private LogTable table;

@BeforeEach
void setup() {
table = new LogTable(0);
}

@Test
void supportsIntegers() {
table.put("int", 5);
Assertions.assertEquals(5, table.get("int").getInteger());
}

@Test
void supportsIntArrays() {
long[] expected = { 1, 2, 3 };

table.put("int-array", expected);

Assertions.assertArrayEquals(expected, table.get("int-array").getIntegerArray());
}

@Test
void supportsProtobufSerialization() {
Rotation2d expected = new Rotation2d(1, 2);

// We're forcing protobuf based serialization so Struct based doesn't run
table.put("rot", Rotation2d.proto, expected);

Assertions.assertEquals(expected, table.get("rot", Rotation2d.proto, new Rotation2d()));
}

@Test
void protobufIsntWrittenIfKeyAlreadyInUse() {
Rotation2d rot = new Rotation2d(1, 2);

table.put("rot", 5);
table.put("rot", Rotation2d.proto, rot); // This should be skipped

Assertions.assertNotEquals(rot, table.get("rot", Rotation2d.proto, new Rotation2d()));
Assertions.assertEquals(5, table.get("rot").getInteger());
}


@Test
void supportsStructSerialization() {
Rotation2d expected = new Rotation2d(1, 2);

// We're forcing Struct based serialization so Protobuf based doesn't run
table.put("rot", Rotation2d.struct, expected);

Assertions.assertEquals(expected, table.get("rot", Rotation2d.struct, new Rotation2d()));
}

@Test
void supportsStructArraySerialization() {
Rotation2d first = new Rotation2d(1, 2);
Rotation2d second = new Rotation2d(3, 4);

// We're forcing Struct serialization
table.put("rot", Rotation2d.struct, first, second);

Rotation2d[] actual = table.get("rot", Rotation2d.struct, new Rotation2d(0, 0), new Rotation2d(0,0));
Assertions.assertArrayEquals(new Rotation2d[] { first, second }, actual);
}
}
2 changes: 1 addition & 1 deletion library_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file")

MAVEN_ARTIFACTS = ['io.github.classgraph:classgraph:4.8.128', 'com.fasterxml.jackson.core:jackson-annotations:2.15.2', 'com.fasterxml.jackson.core:jackson-core:2.15.2', 'com.fasterxml.jackson.core:jackson-databind:2.15.2', 'org.ejml:ejml-simple:0.43.1', 'org.ejml:ejml-core:0.43.1', 'junit:junit:4.13.2', 'com.squareup:javapoet:1.13.0', 'us.hebi.quickbuf:quickbuf-runtime:1.3.2']
MAVEN_ARTIFACTS = ['io.github.classgraph:classgraph:4.8.128', 'com.fasterxml.jackson.core:jackson-annotations:2.15.2', 'com.fasterxml.jackson.core:jackson-core:2.15.2', 'com.fasterxml.jackson.core:jackson-databind:2.15.2', 'org.ejml:ejml-simple:0.43.1', 'org.ejml:ejml-core:0.43.1', 'com.squareup:javapoet:1.13.0', 'us.hebi.quickbuf:quickbuf-runtime:1.3.2', 'org.junit.jupiter:junit-jupiter-api:5.10.1', 'org.junit.jupiter:junit-jupiter-engine:5.10.1', 'org.junit.platform:junit-platform-launcher:1.10.1', 'org.junit.platform:junit-platform-reporting:1.10.1']

WPILIB_VERSION = "2024.3.1"

Expand Down
Loading