From 8f68b66bb9974af38a633050ea0fcbce2c52b7c5 Mon Sep 17 00:00:00 2001 From: Tyler Veness Date: Fri, 1 Dec 2023 09:33:54 -0800 Subject: [PATCH] Add tests --- .../src/main/native/include/frc/util/Color.h | 15 +++-- .../main/native/include/frc/util/Color8Bit.h | 15 +++-- .../test/native/cpp/util/Color8BitTest.cpp | 55 +++++++++++++++-- .../src/test/native/cpp/util/ColorTest.cpp | 55 +++++++++++++++-- .../edu/wpi/first/wpilibj/util/Color.java | 7 +++ .../edu/wpi/first/wpilibj/util/Color8Bit.java | 7 +++ .../wpi/first/wpilibj/util/Color8BitTest.java | 51 ++++++++++++++-- .../edu/wpi/first/wpilibj/util/ColorTest.java | 59 +++++++++++++++++-- 8 files changed, 229 insertions(+), 35 deletions(-) diff --git a/wpilibc/src/main/native/include/frc/util/Color.h b/wpilibc/src/main/native/include/frc/util/Color.h index e9d7a53070e..3e4567373d7 100644 --- a/wpilibc/src/main/native/include/frc/util/Color.h +++ b/wpilibc/src/main/native/include/frc/util/Color.h @@ -744,6 +744,9 @@ class Color { */ static const Color kYellowGreen; + /** + * Constructs a default color (black). + */ constexpr Color() = default; /** @@ -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; diff --git a/wpilibc/src/main/native/include/frc/util/Color8Bit.h b/wpilibc/src/main/native/include/frc/util/Color8Bit.h index 5ea92e29806..559559bcd14 100644 --- a/wpilibc/src/main/native/include/frc/util/Color8Bit.h +++ b/wpilibc/src/main/native/include/frc/util/Color8Bit.h @@ -21,6 +21,9 @@ namespace frc { */ class Color8Bit { public: + /** + * Constructs a default color (black). + */ constexpr Color8Bit() = default; /** @@ -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; diff --git a/wpilibc/src/test/native/cpp/util/Color8BitTest.cpp b/wpilibc/src/test/native/cpp/util/Color8BitTest.cpp index 69e36fbe4f0..e0b26dd5790 100644 --- a/wpilibc/src/test/native/cpp/util/Color8BitTest.cpp +++ b/wpilibc/src/test/native/cpp/util/Color8BitTest.cpp @@ -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()); +} diff --git a/wpilibc/src/test/native/cpp/util/ColorTest.cpp b/wpilibc/src/test/native/cpp/util/ColorTest.cpp index 9b9eea7e078..c76c7f1da29 100644 --- a/wpilibc/src/test/native/cpp/util/ColorTest.cpp +++ b/wpilibc/src/test/native/cpp/util/ColorTest.cpp @@ -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()); +} diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/util/Color.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/util/Color.java index c9787325fa8..706795dd701 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/util/Color.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/util/Color.java @@ -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. * diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/util/Color8Bit.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/util/Color8Bit.java index e9e0e8880b3..5d916e1941a 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/util/Color8Bit.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/util/Color8Bit.java @@ -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. * diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/util/Color8BitTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/util/Color8BitTest.java index 42abf020d76..499ecbde80c 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/util/Color8BitTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/util/Color8BitTest.java @@ -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()); + } } diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/util/ColorTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/util/ColorTest.java index fdd8fbd6870..c6af4e35c21 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/util/ColorTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/util/ColorTest.java @@ -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()); + } }