Skip to content

Instantly share code, notes, and snippets.

@lucasheriques
Last active February 11, 2019 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucasheriques/e4a2815fb56f3e891b83115b3638c409 to your computer and use it in GitHub Desktop.
Save lucasheriques/e4a2815fb56f3e891b83115b3638c409 to your computer and use it in GitHub Desktop.
using System;
using System.Threading.Tasks;
using Xunit;
namespace UnitTest {
public class Triangle {
[Fact]
public void CalculateArea_WithValidTriangle_ShouldReturnCorrectArea () {
Assert.Equal (24, CalculateArea (6, 8, 10));
}
[Fact]
public void CalculateArea_WithAnySidesEquallingZero_ShouldReturnException () {
Assert.Throws<ArgumentException> (() => CalculateArea (0, 4, 5));
Assert.Throws<ArgumentException> (() => CalculateArea (3, 0, 5));
Assert.Throws<ArgumentException> (() => CalculateArea (3, 4, 0));
}
[Fact]
public void CalculateArea_WithAnySidesLowerThanZero_ShouldReturnException () {
Assert.Throws<ArgumentException> (() => CalculateArea (-1, 4, 5));
Assert.Throws<ArgumentException> (() => CalculateArea (3, -1, 5));
Assert.Throws<ArgumentException> (() => CalculateArea (3, 4, -1));
}
[Fact]
public void CalculateArea_WithoutValidTriangle_ShouldReturnException () {
Assert.Throws<ArgumentException> (() => CalculateArea (1, 4, 5));
Assert.Throws<ArgumentException> (() => CalculateArea (10, 20, 50));
Assert.Throws<ArgumentException> (() => CalculateArea (37, 42, 101));
}
[Fact]
public void CalculatePerimeter_WithValidTriangle_ShouldReturnCorrectPerimeter () {
Assert.Equal (12, CalculatePerimeter (3, 4, 5));
}
[Fact]
public void CalculatePerimeter_WithAnySidesEquallingZero_ShouldReturnException () {
Assert.Throws<ArgumentException> (() => CalculatePerimeter (0, 4, 5));
Assert.Throws<ArgumentException> (() => CalculatePerimeter (3, 0, 5));
Assert.Throws<ArgumentException> (() => CalculatePerimeter (3, 4, 0));
}
[Fact]
public void CalculatePerimeter_WithAnySidesLowerThanZero_ShouldReturnException () {
Assert.Throws<ArgumentException> (() => CalculatePerimeter (-1, 4, 5));
Assert.Throws<ArgumentException> (() => CalculatePerimeter (3, -1, 5));
Assert.Throws<ArgumentException> (() => CalculatePerimeter (3, 4, -1));
}
[Fact]
public void CalculatePerimeter_WithoutValidTriangle_ShouldReturnException () {
Assert.Throws<ArgumentException> (() => CalculatePerimeter (1, 4, 5));
Assert.Throws<ArgumentException> (() => CalculatePerimeter (10, 20, 50));
Assert.Throws<ArgumentException> (() => CalculatePerimeter (37, 42, 101));
}
double CalculateArea (int a, int b, int c) {
if (a == 0 || b == 0 || c == 0)
throw new ArgumentException ("Sides should not be zero");
if (a < 0 || b < 0 || c < 0)
throw new ArgumentException ("Sides should not be negatives");
if (a + b <= c || a + c <= b || b + c <= a)
throw new ArgumentException ("This is not a valid triangle");
return (0.25 * Math.Sqrt ((a + b + c) * (-a + b + c) * (a - b + c) * (a + b - c)));
}
double CalculatePerimeter (int a, int b, int c) {
if (a == 0 || b == 0 || c == 0)
throw new ArgumentException ("Sides should not be zero");
if (a < 0 || b < 0 || c < 0)
throw new ArgumentException ("Sides should not be negatives");
if (a + b <= c || a + c <= b || b + c <= a)
throw new ArgumentException ("This is not a valid triangle");
return a + b + c;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment