Skip to content

Commit

Permalink
Finished unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HexagonNico committed Apr 18, 2024
1 parent b3e55aa commit d6af75d
Show file tree
Hide file tree
Showing 9 changed files with 561 additions and 8 deletions.
5 changes: 2 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ scalaVersion := "2.13.12"
// VecMatLib dependency
libraryDependencies += "io.github.scalamath" % "vecmatlib" % "3.0"
// Scala test dependency
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.17" % Test
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.18" % Test
// Junit test dependency
libraryDependencies += "com.github.sbt" % "junit-interface" % "0.13.3" % Test

// Opt out of Scala-version source directory convention
// Needed to run Java Junit tests
crossPaths := false
crossPaths := true

// Show deprecation warnings
scalacOptions ++= Seq("-unchecked", "-deprecation")
32 changes: 28 additions & 4 deletions src/main/scala/io/github/scalamath/colorlib/Col3f.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ case class Col3f(r: Float, g: Float, b: Float) extends Color {
* @param c The color to add.
* @return The sum between this color and the given one.
*/
override def +(c: Color): Color = if(c.isInstanceOf[Col3f]) this + (c.r, c.g, c.b) else this + (c.r, c.g, c.b, c.a)
override def +(c: Color): Color = {
if(c.isInstanceOf[Col3f]) {
this + (c.r, c.g, c.b)
} else {
this + (c.r, c.g, c.b, c.a)
}
}

/**
* Subtracts the given values from each component of this color and returns the result.
Expand Down Expand Up @@ -107,7 +113,13 @@ case class Col3f(r: Float, g: Float, b: Float) extends Color {
* @param c The color to subtract.
* @return The subtraction between this color and the given one.
*/
override def -(c: Color): Color = if(c.isInstanceOf[Col3f]) this - (c.r, c.g, c.b) else this - (c.r, c.g, c.b, c.a)
override def -(c: Color): Color = {
if(c.isInstanceOf[Col3f]) {
this - (c.r, c.g, c.b)
} else {
this - (c.r, c.g, c.b, c.a)
}
}

/**
* Multiplies each component of this color with the given values and returns the result.
Expand Down Expand Up @@ -142,7 +154,13 @@ case class Col3f(r: Float, g: Float, b: Float) extends Color {
* @param c The color to multiply this one by.
* @return The component-wise product between this color and the given one.
*/
override def *(c: Color): Color = if(c.isInstanceOf[Col3f]) this * (c.r, c.g, c.b) else this * (c.r, c.g, c.b, c.a)
override def *(c: Color): Color = {
if(c.isInstanceOf[Col3f]) {
this * (c.r, c.g, c.b)
} else {
this * (c.r, c.g, c.b, c.a)
}
}

/**
* Multiplies each component of this color with the given value and returns the result.
Expand All @@ -160,7 +178,13 @@ case class Col3f(r: Float, g: Float, b: Float) extends Color {
* @param c The color to divide this one by.
* @return The component-wise division between this color and the given one.
*/
override def /(c: Color): Color = if(c.isInstanceOf[Col3f]) this / (c.r, c.g, c.b) else this / (c.r, c.g, c.b, c.a)
override def /(c: Color): Color = {
if(c.isInstanceOf[Col3f]) {
this / (c.r, c.g, c.b)
} else {
this / (c.r, c.g, c.b, c.a)
}
}

/**
* Returns this color with its `r`, `g`, and `b` components inverted.
Expand Down
18 changes: 18 additions & 0 deletions src/main/scala/io/github/scalamath/colorlib/Color.scala
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,24 @@ trait Color {
*/
def divide(r: Float, g: Float, b: Float): Color = this / (r, g, b)

/**
* Divides each component of this color by the given value and returns the result.
*
* @param f The value to divide this color by.
* @return The division between this color and the given value.
*/
def /(f: Float): Color = this * (1.0f / f)

/**
* Divides each component of this color by the given value and returns the result.
*
* This method can be used in place of the `/` operator for better interoperability with Java.
*
* @param f The value to divide this color by.
* @return The division between this color and the given value.
*/
def divide(f: Float): Color = this / f

/**
* Blends this color and the given one and returns the result.
*
Expand Down
148 changes: 148 additions & 0 deletions src/test/java/io/github/scalamath/colorlib/TestCol1i.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package io.github.scalamath.colorlib;

import org.junit.Assert;
import org.junit.Test;

public class TestCol1i {

@Test
public void testAddFourFloats() {
var color = new Col1i(0.3f, 0.4f, 0.5f, 0.6f);
var res = new Col1i(0.4f, 0.6f, 0.8f, 1.0f);
this.assertEqualsApprox(res, color.add(0.1f, 0.2f, 0.3f, 0.4f));
}

@Test
public void testAddThreeFloats() {
var color = new Col1i(0.3f, 0.4f, 0.5f, 0.8f);
var res = new Col1i(0.4f, 0.6f, 0.8f, 0.8f);
this.assertEqualsApprox(res, color.add(0.1f, 0.2f, 0.3f));
}

@Test
public void testAddTwoColors() {
var c1 = new Col1i(0.3f, 0.4f, 0.5f, 0.6f);
var c2 = new Col1i(0.1f, 0.2f, 0.3f, 0.4f);
var res = new Col1i(0.4f, 0.6f, 0.8f, 1.0f);
this.assertEqualsApprox(res, c1.add(c2));
}

@Test
public void testAddTwoColorsOfDifferentTypes() {
var c1 = new Col1i(0.3f, 0.4f, 0.5f, 0.6f);
var c2 = new Col4f(0.1f, 0.2f, 0.3f, 0.4f);
var res = new Col4f(0.4f, 0.6f, 0.8f, 1.0f);
this.assertEqualsApprox(res, c1.add(c2));
}

@Test
public void testSubtractFourFloats() {
var color = new Col1i(0.4f, 0.6f, 0.8f, 1.0f);
var res = new Col1i(0.3f, 0.4f, 0.5f, 0.6f);
this.assertEqualsApprox(res, color.subtract(0.1f, 0.2f, 0.3f, 0.4f));
}

@Test
public void testSubtractThreeFloats() {
var color = new Col1i(0.4f, 0.6f, 0.8f, 1.0f);
var res = new Col1i(0.3f, 0.4f, 0.5f, 1.0f);
this.assertEqualsApprox(res, color.subtract(0.1f, 0.2f, 0.3f));
}

@Test
public void testSubtractTwoColors() {
var c1 = new Col1i(0.4f, 0.6f, 0.8f, 1.0f);
var c2 = new Col1i(0.3f, 0.4f, 0.5f, 0.6f);
var res = new Col1i(0.1f, 0.2f, 0.3f, 0.4f);
this.assertEqualsApprox(res, c1.subtract(c2));
}

@Test
public void testSubtractTwoColorsOfDifferentTypes() {
var c1 = new Col1i(0.4f, 0.6f, 0.8f, 1.0f);
var c2 = new Col4f(0.3f, 0.4f, 0.5f, 0.6f);
var res = new Col4f(0.1f, 0.2f, 0.3f, 0.4f);
this.assertEqualsApprox(res, c1.subtract(c2));
}

@Test
public void testMultiplyWithFourFloats() {
var color = new Col1i(0.2f, 0.3f, 0.4f, 0.5f);
var res = new Col1i(0.4f, 0.9f, 0.6f, 1.0f);
this.assertEqualsApprox(res, color.multiply(2.0f, 3.0f, 1.5f, 2.0f));
}

@Test
public void testMultiplyWithThreeFloats() {
var color = new Col1i(0.2f, 0.3f, 0.4f, 1.0f);
var res = new Col1i(0.4f, 0.9f, 0.6f);
this.assertEqualsApprox(res, color.multiply(2.0f, 3.0f, 1.5f));
}

@Test
public void testMultiplyTwoColors() {
var c1 = new Col1i(0.5f, 0.4f, 0.6f, 1.0f);
var c2 = new Col1i(0.2f, 0.4f, 0.6f, 1.0f);
var res = new Col1i(0.1f, 0.16f, 0.36f, 1.0f);
this.assertEqualsApprox(res, c1.multiply(c2));
}

@Test
public void testMultiplyTwoColorsOfDifferentTypes() {
var c1 = new Col1i(0.5f, 0.4f, 0.6f, 1.0f);
var c2 = new Col4f(0.2f, 0.4f, 0.6f, 1.0f);
var res = new Col4f(0.1f, 0.16f, 0.36f, 1.0f);
this.assertEqualsApprox(res, c1.multiply(c2));
}

@Test
public void testMultiplyWithAFloat() {
var color = new Col1i(0.2f, 0.4f, 0.6f, 1.0f);
var res = new Col1i(0.3f, 0.6f, 0.9f, 1.0f);
this.assertEqualsApprox(res, color.multiply(1.5f));
}

@Test
public void testDivideByFourFloats() {
var color = new Col1i(1.0f, 0.8f, 0.6f, 1.0f);
var res = new Col1i(0.5f, 0.2f, 0.2f, 0.2f);
this.assertEqualsApprox(res, color.divide(2.0f, 4.0f, 3.0f, 5.0f));
}

@Test
public void testDivideByThreeFloats() {
var color = new Col1i(1.0f, 0.8f, 0.6f, 1.0f);
var res = new Col1i(0.5f, 0.2f, 0.2f, 1.0f);
this.assertEqualsApprox(res, color.divide(2.0f, 4.0f, 3.0f));
}

@Test
public void testDivideTwoColors() {
var c1 = new Col1i(0.1f, 0.2f, 0.1f, 0.4f);
var c2 = new Col1i(0.2f, 0.2f, 0.25f, 0.5f);
var res = new Col1i(0.5f, 1.0f, 0.4f, 0.8f);
this.assertEqualsApprox(res, c1.divide(c2));
}

@Test
public void testDivideTwoColorsOfDifferentTypes() {
var c1 = new Col1i(0.1f, 0.2f, 0.1f, 0.4f);
var c2 = new Col4f(0.2f, 0.2f, 0.25f, 0.5f);
var res = new Col4f(0.5f, 1.0f, 0.4f, 0.8f);
this.assertEqualsApprox(res, c1.divide(c2));
}

@Test
public void testDivideColorByAFloat() {
var color = new Col1i(1.0f, 0.8f, 0.6f, 0.4f);
var res = new Col1i(0.5f, 0.4f, 0.3f, 0.2f);
this.assertEqualsApprox(res, color.divide(2.0f));
}

private void assertEqualsApprox(Color expected, Color res) {
Assert.assertTrue(Math.abs(expected.r() - res.r()) < 0.01f);
Assert.assertTrue(Math.abs(expected.g() - res.g()) < 0.01f);
Assert.assertTrue(Math.abs(expected.b() - res.b()) < 0.01f);
Assert.assertTrue(Math.abs(expected.a() - res.a()) < 0.01f);
}
}
Loading

0 comments on commit d6af75d

Please sign in to comment.