Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul committed Dec 1, 2023
1 parent fc3cbe3 commit 8f68b66
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 35 deletions.
15 changes: 9 additions & 6 deletions wpilibc/src/main/native/include/frc/util/Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,9 @@ class Color {
*/
static const Color kYellowGreen;

/**
* Constructs a default color (black).
*/
constexpr Color() = default;

/**
Expand Down Expand Up @@ -783,12 +786,12 @@ class Color {
fmt::format("Invalid hex string for Color \"{}\"", hexString));
}

int r = wpi::hexDigitValue(hexString[0]) * 16 +
wpi::hexDigitValue(hexString[1]);
int g = wpi::hexDigitValue(hexString[2]) * 16 +
wpi::hexDigitValue(hexString[3]);
int b = wpi::hexDigitValue(hexString[4]) * 16 +
wpi::hexDigitValue(hexString[5]);
int r = wpi::hexDigitValue(hexString[1]) * 16 +
wpi::hexDigitValue(hexString[2]);
int g = wpi::hexDigitValue(hexString[3]) * 16 +
wpi::hexDigitValue(hexString[4]);
int b = wpi::hexDigitValue(hexString[5]) * 16 +
wpi::hexDigitValue(hexString[6]);

red = r / 255.0;
green = g / 255.0;
Expand Down
15 changes: 9 additions & 6 deletions wpilibc/src/main/native/include/frc/util/Color8Bit.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ namespace frc {
*/
class Color8Bit {
public:
/**
* Constructs a default color (black).
*/
constexpr Color8Bit() = default;

/**
Expand Down Expand Up @@ -60,12 +63,12 @@ class Color8Bit {
fmt::format("Invalid hex string for Color \"{}\"", hexString));
}

red = wpi::hexDigitValue(hexString[0]) * 16 +
wpi::hexDigitValue(hexString[1]);
green = wpi::hexDigitValue(hexString[2]) * 16 +
wpi::hexDigitValue(hexString[3]);
blue = wpi::hexDigitValue(hexString[4]) * 16 +
wpi::hexDigitValue(hexString[5]);
red = wpi::hexDigitValue(hexString[1]) * 16 +
wpi::hexDigitValue(hexString[2]);
green = wpi::hexDigitValue(hexString[3]) * 16 +
wpi::hexDigitValue(hexString[4]);
blue = wpi::hexDigitValue(hexString[5]) * 16 +
wpi::hexDigitValue(hexString[6]);
}

constexpr bool operator==(const Color8Bit&) const = default;
Expand Down
55 changes: 49 additions & 6 deletions wpilibc/src/test/native/cpp/util/Color8BitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,57 @@

#include "frc/util/Color8Bit.h"

TEST(Color8BitTest, ConstructDefault) {}
TEST(Color8BitTest, ConstructDefault) {
constexpr frc::Color8Bit color;

TEST(Color8BitTest, ConstructFromInts) {}
EXPECT_EQ(0, color.red);
EXPECT_EQ(0, color.green);
EXPECT_EQ(0, color.blue);
}

TEST(Color8BitTest, ConstructFromColor) {}
TEST(Color8BitTest, ConstructFromInts) {
constexpr frc::Color8Bit color{255, 128, 64};

TEST(Color8BitTest, ConstructFromHexString) {}
EXPECT_EQ(255, color.red);
EXPECT_EQ(128, color.green);
EXPECT_EQ(64, color.blue);
}

TEST(Color8BitTest, ImplicitConversionToColor) {}
TEST(Color8BitTest, ConstructFromColor) {
constexpr frc::Color8Bit color{frc::Color{255, 128, 64}};

TEST(Color8BitTest, ToHexString) {}
EXPECT_EQ(255, color.red);
EXPECT_EQ(128, color.green);
EXPECT_EQ(64, color.blue);
}

TEST(Color8BitTest, ConstructFromHexString) {
constexpr frc::Color8Bit color{"#FF8040"};

EXPECT_EQ(255, color.red);
EXPECT_EQ(128, color.green);
EXPECT_EQ(64, color.blue);

// No leading #
EXPECT_THROW(frc::Color8Bit{"112233"}, std::invalid_argument);

// Too long
EXPECT_THROW(frc::Color8Bit{"#11223344"}, std::invalid_argument);

// Invalid hex characters
EXPECT_THROW(frc::Color8Bit{"#$$$$$$"}, std::invalid_argument);
}

TEST(Color8BitTest, ImplicitConversionToColor) {
frc::Color color = frc::Color8Bit{255, 128, 64};

EXPECT_NEAR(1.0, color.red, 1e-2);
EXPECT_NEAR(0.5, color.green, 1e-2);
EXPECT_NEAR(0.25, color.blue, 1e-2);
}

TEST(Color8BitTest, ToHexString) {
constexpr frc::Color8Bit color{255, 128, 64};

EXPECT_EQ("#FF8040", color.HexString());
}
55 changes: 49 additions & 6 deletions wpilibc/src/test/native/cpp/util/ColorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,57 @@

#include "frc/util/Color.h"

TEST(ColorTest, ConstructDefault) {}
TEST(ColorTest, ConstructDefault) {
constexpr frc::Color color;

TEST(ColorTest, ConstructFromDoubles) {}
EXPECT_DOUBLE_EQ(0.0, color.red);
EXPECT_DOUBLE_EQ(0.0, color.green);
EXPECT_DOUBLE_EQ(0.0, color.blue);
}

TEST(ColorTest, ConstructFromInts) {}
TEST(ColorTest, ConstructFromDoubles) {
constexpr frc::Color color{1.0, 0.5, 0.25};

TEST(ColorTest, ConstructFromHexString) {}
EXPECT_NEAR(1.0, color.red, 1e-2);
EXPECT_NEAR(0.5, color.green, 1e-2);
EXPECT_NEAR(0.25, color.blue, 1e-2);
}

TEST(ColorTest, FromHSV) {}
TEST(ColorTest, ConstructFromInts) {
constexpr frc::Color color{255, 128, 64};

TEST(ColorTest, ToHexString) {}
EXPECT_NEAR(1.0, color.red, 1e-2);
EXPECT_NEAR(0.5, color.green, 1e-2);
EXPECT_NEAR(0.25, color.blue, 1e-2);
}

TEST(ColorTest, ConstructFromHexString) {
constexpr frc::Color color{"#FF8040"};

EXPECT_NEAR(1.0, color.red, 1e-2);
EXPECT_NEAR(0.5, color.green, 1e-2);
EXPECT_NEAR(0.25, color.blue, 1e-2);

// No leading #
EXPECT_THROW(frc::Color{"112233"}, std::invalid_argument);

// Too long
EXPECT_THROW(frc::Color{"#11223344"}, std::invalid_argument);

// Invalid hex characters
EXPECT_THROW(frc::Color{"#$$$$$$"}, std::invalid_argument);
}

TEST(ColorTest, FromHSV) {
constexpr frc::Color color = frc::Color::FromHSV(90, 128, 64);

EXPECT_DOUBLE_EQ(0.1256103515625, color.red);
EXPECT_DOUBLE_EQ(0.2510986328125, color.green);
EXPECT_DOUBLE_EQ(0.2510986328125, color.blue);
}

TEST(ColorTest, ToHexString) {
constexpr frc::Color color{255, 128, 64};

EXPECT_EQ("#FF8040", color.HexString());
}
7 changes: 7 additions & 0 deletions wpilibj/src/main/java/edu/wpi/first/wpilibj/util/Color.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ public class Color {
public final double blue;
private String m_name;

/** Constructs a default color (black). */
public Color() {
red = 0.0;
green = 0.0;
blue = 0.0;
}

/**
* Constructs a Color from doubles.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ public class Color8Bit {
public final int green;
public final int blue;

/** Constructs a default color (black). */
public Color8Bit() {
red = 0;
green = 0;
blue = 0;
}

/**
* Constructs a Color8Bit.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,62 @@

package edu.wpi.first.wpilibj.util;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;

class Color8BitTest {
@Test
void testConstructDefault() {}
void testConstructDefault() {
var color = new Color8Bit();

assertEquals(0, color.red);
assertEquals(0, color.green);
assertEquals(0, color.blue);
}

@Test
void testConstructFromInts() {}
void testConstructFromInts() {
var color = new Color8Bit(255, 128, 64);

assertEquals(255, color.red);
assertEquals(128, color.green);
assertEquals(64, color.blue);
}

@Test
void testConstructFromColor() {}
void testConstructFromColor() {
var color = new Color8Bit(new Color(255, 128, 64));

assertEquals(255, color.red);
assertEquals(128, color.green);
assertEquals(64, color.blue);
}

@Test
void testConstructFromHexString() {}
void testConstructFromHexString() {
var color = new Color8Bit("#FF8040");

assertEquals(255, color.red);
assertEquals(128, color.green);
assertEquals(64, color.blue);

// No leading #
assertThrows(IllegalArgumentException.class, () -> new Color8Bit("112233"));

// Too long
assertThrows(IllegalArgumentException.class, () -> new Color8Bit("#11223344"));

// Invalid hex characters
assertThrows(IllegalArgumentException.class, () -> new Color8Bit("#$$$$$$"));
}

@Test
void testToHexString() {}
void testToHexString() {
var color = new Color8Bit(255, 128, 64);

assertEquals("#FF8040", color.toHexString());
assertEquals("#FF8040", color.toString());
}
}
59 changes: 53 additions & 6 deletions wpilibj/src/test/java/edu/wpi/first/wpilibj/util/ColorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,71 @@

package edu.wpi.first.wpilibj.util;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;

class ColorTest {
@Test
void testConstructDefault() {}
void testConstructDefault() {
var color = new Color();

assertEquals(0.0, color.red);
assertEquals(0.0, color.green);
assertEquals(0.0, color.blue);
}

@Test
void testConstructFromDoubles() {}
void testConstructFromDoubles() {
var color = new Color(1.0, 0.5, 0.25);

assertEquals(1.0, color.red, 1e-2);
assertEquals(0.5, color.green, 1e-2);
assertEquals(0.25, color.blue, 1e-2);
}

@Test
void testConstructFromInts() {}
void testConstructFromInts() {
var color = new Color(255, 128, 64);

assertEquals(1.0, color.red, 1e-2);
assertEquals(0.5, color.green, 1e-2);
assertEquals(0.25, color.blue, 1e-2);
}

@Test
void testConstructFromHexString() {}
void testConstructFromHexString() {
var color = new Color("#FF8040");

assertEquals(1.0, color.red, 1e-2);
assertEquals(0.5, color.green, 1e-2);
assertEquals(0.25, color.blue, 1e-2);

// No leading #
assertThrows(IllegalArgumentException.class, () -> new Color("112233"));

// Too long
assertThrows(IllegalArgumentException.class, () -> new Color("#11223344"));

// Invalid hex characters
assertThrows(IllegalArgumentException.class, () -> new Color("#$$$$$$"));
}

@Test
void testFromHSV() {}
void testFromHSV() {
var color = Color.fromHSV(90, 128, 64);

assertEquals(0.125732421875, color.red);
assertEquals(0.251220703125, color.green);
assertEquals(0.251220703125, color.blue);
}

@Test
void testToHexString() {}
void testToHexString() {
var color = new Color(255, 128, 64);

assertEquals("#FF8040", color.toHexString());
assertEquals("#FF8040", color.toString());
}
}

0 comments on commit 8f68b66

Please sign in to comment.