Skip to content

Commit d6af75d

Browse files
committed
Finished unit tests
1 parent b3e55aa commit d6af75d

File tree

9 files changed

+561
-8
lines changed

9 files changed

+561
-8
lines changed

build.sbt

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ scalaVersion := "2.13.12"
99
// VecMatLib dependency
1010
libraryDependencies += "io.github.scalamath" % "vecmatlib" % "3.0"
1111
// Scala test dependency
12-
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.17" % Test
12+
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.18" % Test
1313
// Junit test dependency
1414
libraryDependencies += "com.github.sbt" % "junit-interface" % "0.13.3" % Test
1515

16-
// Opt out of Scala-version source directory convention
1716
// Needed to run Java Junit tests
18-
crossPaths := false
17+
crossPaths := true
1918

2019
// Show deprecation warnings
2120
scalacOptions ++= Seq("-unchecked", "-deprecation")

src/main/scala/io/github/scalamath/colorlib/Col3f.scala

+28-4
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,13 @@ case class Col3f(r: Float, g: Float, b: Float) extends Color {
7272
* @param c The color to add.
7373
* @return The sum between this color and the given one.
7474
*/
75-
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)
75+
override def +(c: Color): Color = {
76+
if(c.isInstanceOf[Col3f]) {
77+
this + (c.r, c.g, c.b)
78+
} else {
79+
this + (c.r, c.g, c.b, c.a)
80+
}
81+
}
7682

7783
/**
7884
* Subtracts the given values from each component of this color and returns the result.
@@ -107,7 +113,13 @@ case class Col3f(r: Float, g: Float, b: Float) extends Color {
107113
* @param c The color to subtract.
108114
* @return The subtraction between this color and the given one.
109115
*/
110-
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)
116+
override def -(c: Color): Color = {
117+
if(c.isInstanceOf[Col3f]) {
118+
this - (c.r, c.g, c.b)
119+
} else {
120+
this - (c.r, c.g, c.b, c.a)
121+
}
122+
}
111123

112124
/**
113125
* Multiplies each component of this color with the given values and returns the result.
@@ -142,7 +154,13 @@ case class Col3f(r: Float, g: Float, b: Float) extends Color {
142154
* @param c The color to multiply this one by.
143155
* @return The component-wise product between this color and the given one.
144156
*/
145-
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)
157+
override def *(c: Color): Color = {
158+
if(c.isInstanceOf[Col3f]) {
159+
this * (c.r, c.g, c.b)
160+
} else {
161+
this * (c.r, c.g, c.b, c.a)
162+
}
163+
}
146164

147165
/**
148166
* Multiplies each component of this color with the given value and returns the result.
@@ -160,7 +178,13 @@ case class Col3f(r: Float, g: Float, b: Float) extends Color {
160178
* @param c The color to divide this one by.
161179
* @return The component-wise division between this color and the given one.
162180
*/
163-
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)
181+
override def /(c: Color): Color = {
182+
if(c.isInstanceOf[Col3f]) {
183+
this / (c.r, c.g, c.b)
184+
} else {
185+
this / (c.r, c.g, c.b, c.a)
186+
}
187+
}
164188

165189
/**
166190
* Returns this color with its `r`, `g`, and `b` components inverted.

src/main/scala/io/github/scalamath/colorlib/Color.scala

+18
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,24 @@ trait Color {
427427
*/
428428
def divide(r: Float, g: Float, b: Float): Color = this / (r, g, b)
429429

430+
/**
431+
* Divides each component of this color by the given value and returns the result.
432+
*
433+
* @param f The value to divide this color by.
434+
* @return The division between this color and the given value.
435+
*/
436+
def /(f: Float): Color = this * (1.0f / f)
437+
438+
/**
439+
* Divides each component of this color by the given value and returns the result.
440+
*
441+
* This method can be used in place of the `/` operator for better interoperability with Java.
442+
*
443+
* @param f The value to divide this color by.
444+
* @return The division between this color and the given value.
445+
*/
446+
def divide(f: Float): Color = this / f
447+
430448
/**
431449
* Blends this color and the given one and returns the result.
432450
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package io.github.scalamath.colorlib;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class TestCol1i {
7+
8+
@Test
9+
public void testAddFourFloats() {
10+
var color = new Col1i(0.3f, 0.4f, 0.5f, 0.6f);
11+
var res = new Col1i(0.4f, 0.6f, 0.8f, 1.0f);
12+
this.assertEqualsApprox(res, color.add(0.1f, 0.2f, 0.3f, 0.4f));
13+
}
14+
15+
@Test
16+
public void testAddThreeFloats() {
17+
var color = new Col1i(0.3f, 0.4f, 0.5f, 0.8f);
18+
var res = new Col1i(0.4f, 0.6f, 0.8f, 0.8f);
19+
this.assertEqualsApprox(res, color.add(0.1f, 0.2f, 0.3f));
20+
}
21+
22+
@Test
23+
public void testAddTwoColors() {
24+
var c1 = new Col1i(0.3f, 0.4f, 0.5f, 0.6f);
25+
var c2 = new Col1i(0.1f, 0.2f, 0.3f, 0.4f);
26+
var res = new Col1i(0.4f, 0.6f, 0.8f, 1.0f);
27+
this.assertEqualsApprox(res, c1.add(c2));
28+
}
29+
30+
@Test
31+
public void testAddTwoColorsOfDifferentTypes() {
32+
var c1 = new Col1i(0.3f, 0.4f, 0.5f, 0.6f);
33+
var c2 = new Col4f(0.1f, 0.2f, 0.3f, 0.4f);
34+
var res = new Col4f(0.4f, 0.6f, 0.8f, 1.0f);
35+
this.assertEqualsApprox(res, c1.add(c2));
36+
}
37+
38+
@Test
39+
public void testSubtractFourFloats() {
40+
var color = new Col1i(0.4f, 0.6f, 0.8f, 1.0f);
41+
var res = new Col1i(0.3f, 0.4f, 0.5f, 0.6f);
42+
this.assertEqualsApprox(res, color.subtract(0.1f, 0.2f, 0.3f, 0.4f));
43+
}
44+
45+
@Test
46+
public void testSubtractThreeFloats() {
47+
var color = new Col1i(0.4f, 0.6f, 0.8f, 1.0f);
48+
var res = new Col1i(0.3f, 0.4f, 0.5f, 1.0f);
49+
this.assertEqualsApprox(res, color.subtract(0.1f, 0.2f, 0.3f));
50+
}
51+
52+
@Test
53+
public void testSubtractTwoColors() {
54+
var c1 = new Col1i(0.4f, 0.6f, 0.8f, 1.0f);
55+
var c2 = new Col1i(0.3f, 0.4f, 0.5f, 0.6f);
56+
var res = new Col1i(0.1f, 0.2f, 0.3f, 0.4f);
57+
this.assertEqualsApprox(res, c1.subtract(c2));
58+
}
59+
60+
@Test
61+
public void testSubtractTwoColorsOfDifferentTypes() {
62+
var c1 = new Col1i(0.4f, 0.6f, 0.8f, 1.0f);
63+
var c2 = new Col4f(0.3f, 0.4f, 0.5f, 0.6f);
64+
var res = new Col4f(0.1f, 0.2f, 0.3f, 0.4f);
65+
this.assertEqualsApprox(res, c1.subtract(c2));
66+
}
67+
68+
@Test
69+
public void testMultiplyWithFourFloats() {
70+
var color = new Col1i(0.2f, 0.3f, 0.4f, 0.5f);
71+
var res = new Col1i(0.4f, 0.9f, 0.6f, 1.0f);
72+
this.assertEqualsApprox(res, color.multiply(2.0f, 3.0f, 1.5f, 2.0f));
73+
}
74+
75+
@Test
76+
public void testMultiplyWithThreeFloats() {
77+
var color = new Col1i(0.2f, 0.3f, 0.4f, 1.0f);
78+
var res = new Col1i(0.4f, 0.9f, 0.6f);
79+
this.assertEqualsApprox(res, color.multiply(2.0f, 3.0f, 1.5f));
80+
}
81+
82+
@Test
83+
public void testMultiplyTwoColors() {
84+
var c1 = new Col1i(0.5f, 0.4f, 0.6f, 1.0f);
85+
var c2 = new Col1i(0.2f, 0.4f, 0.6f, 1.0f);
86+
var res = new Col1i(0.1f, 0.16f, 0.36f, 1.0f);
87+
this.assertEqualsApprox(res, c1.multiply(c2));
88+
}
89+
90+
@Test
91+
public void testMultiplyTwoColorsOfDifferentTypes() {
92+
var c1 = new Col1i(0.5f, 0.4f, 0.6f, 1.0f);
93+
var c2 = new Col4f(0.2f, 0.4f, 0.6f, 1.0f);
94+
var res = new Col4f(0.1f, 0.16f, 0.36f, 1.0f);
95+
this.assertEqualsApprox(res, c1.multiply(c2));
96+
}
97+
98+
@Test
99+
public void testMultiplyWithAFloat() {
100+
var color = new Col1i(0.2f, 0.4f, 0.6f, 1.0f);
101+
var res = new Col1i(0.3f, 0.6f, 0.9f, 1.0f);
102+
this.assertEqualsApprox(res, color.multiply(1.5f));
103+
}
104+
105+
@Test
106+
public void testDivideByFourFloats() {
107+
var color = new Col1i(1.0f, 0.8f, 0.6f, 1.0f);
108+
var res = new Col1i(0.5f, 0.2f, 0.2f, 0.2f);
109+
this.assertEqualsApprox(res, color.divide(2.0f, 4.0f, 3.0f, 5.0f));
110+
}
111+
112+
@Test
113+
public void testDivideByThreeFloats() {
114+
var color = new Col1i(1.0f, 0.8f, 0.6f, 1.0f);
115+
var res = new Col1i(0.5f, 0.2f, 0.2f, 1.0f);
116+
this.assertEqualsApprox(res, color.divide(2.0f, 4.0f, 3.0f));
117+
}
118+
119+
@Test
120+
public void testDivideTwoColors() {
121+
var c1 = new Col1i(0.1f, 0.2f, 0.1f, 0.4f);
122+
var c2 = new Col1i(0.2f, 0.2f, 0.25f, 0.5f);
123+
var res = new Col1i(0.5f, 1.0f, 0.4f, 0.8f);
124+
this.assertEqualsApprox(res, c1.divide(c2));
125+
}
126+
127+
@Test
128+
public void testDivideTwoColorsOfDifferentTypes() {
129+
var c1 = new Col1i(0.1f, 0.2f, 0.1f, 0.4f);
130+
var c2 = new Col4f(0.2f, 0.2f, 0.25f, 0.5f);
131+
var res = new Col4f(0.5f, 1.0f, 0.4f, 0.8f);
132+
this.assertEqualsApprox(res, c1.divide(c2));
133+
}
134+
135+
@Test
136+
public void testDivideColorByAFloat() {
137+
var color = new Col1i(1.0f, 0.8f, 0.6f, 0.4f);
138+
var res = new Col1i(0.5f, 0.4f, 0.3f, 0.2f);
139+
this.assertEqualsApprox(res, color.divide(2.0f));
140+
}
141+
142+
private void assertEqualsApprox(Color expected, Color res) {
143+
Assert.assertTrue(Math.abs(expected.r() - res.r()) < 0.01f);
144+
Assert.assertTrue(Math.abs(expected.g() - res.g()) < 0.01f);
145+
Assert.assertTrue(Math.abs(expected.b() - res.b()) < 0.01f);
146+
Assert.assertTrue(Math.abs(expected.a() - res.a()) < 0.01f);
147+
}
148+
}

0 commit comments

Comments
 (0)