Reputation: 21178
I'm working on a laboratory practical with unit-testing. Below is the application that i'm testing (i'm testing all the methods and the constructor). All my tests are complete accept for one, that is testing a method called "isScalene()", that checks wether the triangle is a scalene (all sides are unique) or not.
You find the test method that fails at the bottom. When I change "equalateral" to True and "scalene" to False, it passes. Something is wrong in the application, but I just can't figure out what it is (probably in "uniqueSides() though).
I would be grateful if someone could help me out!
public class Triangle {
double[] sides;
public Triangle(double a, double b, double c) {
if ((a <= 0) || (b <= 0) || (c <= 0)){
throw new ArgumentOutOfRangeException("Sidorna måste vara större än 0.");
}
sides = new double[] { a, b, c };
}
private int uniqueSides() {
return sides.Distinct<double>().Count();
}
public bool isScalene() {
if (uniqueSides() == 1){
return true;
}
else{
return false;
}
}
public bool isEquilateral() {
if (uniqueSides() == 3){
return true;
}
else{
return false;
}
}
public bool isIsosceles() {
if (uniqueSides() == 2){
return true;
}
else{
return false;
}
}
}
...
[TestMethod()]
public void isScalene3Test3()
{
Triangle triangle = new Triangle(25, 250, 2000);
var isosceles = triangle.isIsosceles();
var equalateral = triangle.isEquilateral();
var scalene = triangle.isScalene();
Assert.IsFalse(isosceles, "Test Isosceles");
Assert.IsFalse(equalateral, "Test Equalateral");
Assert.IsTrue(scalene, "Test Scalene");
}
Upvotes: 0
Views: 237
Reputation: 22379
Don't you want to check if (uniqueSides() == 3)
rather than 1?
Anyway, debugging the code would have helped you find this out very quickly for yourself.
Upvotes: 0
Reputation: 44316
isEquilateral should use 1 unique side, and isScalene should use 3.
public class Triangle {
double[] sides;
public Triangle(double a, double b, double c) {
if ((a <= 0) || (b <= 0) || (c <= 0)){
throw new ArgumentOutOfRangeException("Sidorna måste vara större än 0.");
}
sides = new double[] { a, b, c };
}
private int uniqueSides() {
return sides.Distinct<double>().Count();
}
public bool isScalene() {
if (uniqueSides() == 3){
return true;
}
else{
return false;
}
}
public bool isEquilateral() {
if (uniqueSides() == 1){
return true;
}
else{
return false;
}
}
public bool isIsosceles() {
if (uniqueSides() == 2){
return true;
}
else{
return false;
}
}
}
Upvotes: 5