Skip to content

Commit fbf8aca

Browse files
authored
Add tests 3,4,6,7,8,9,11 (#105) (#106)
* Add tests 3,4,6,7,8,9,11
1 parent e68b4c9 commit fbf8aca

7 files changed

+379
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
3+
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter10.Listing10_03.Tests
4+
{
5+
[TestClass]
6+
public class ProgramTests
7+
{
8+
[TestMethod]
9+
public void Main_GivenSerialNumbers_EqualityOverrideUsed()
10+
{
11+
const string expected =
12+
@"serialNumber1 reference equals serialNumber2
13+
serialNumber1 equals serialNumber2
14+
serialNumber1 equals serialNumber3";
15+
16+
IntelliTect.TestTools.Console.ConsoleAssert.Expect(
17+
expected, Program.Main);
18+
}
19+
}
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
3+
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter10.Listing10_04.Tests
4+
{
5+
[TestClass]
6+
public class ProgramTests
7+
{
8+
[TestMethod]
9+
public void Main_ReferenceEqualsOnValueType_ReferencesNotEqual()
10+
{
11+
const string expected = "coordinate1 does NOT reference equal itself";
12+
13+
IntelliTect.TestTools.Console.ConsoleAssert.Expect(
14+
expected, Program.Main);
15+
}
16+
}
17+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
3+
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter10.Listing10_06.Tests
4+
{
5+
[TestClass]
6+
public class ProductSerialNumberTests
7+
{
8+
[TestMethod]
9+
public void Equals_GivenEqualObjects_Equal()
10+
{
11+
ProductSerialNumber productSerialNumber1 = new ProductSerialNumber("12", 11, 11001);
12+
ProductSerialNumber productSerialNumber2 = new ProductSerialNumber("12", 11, 11001);
13+
14+
Assert.IsTrue(productSerialNumber1.Equals(productSerialNumber2));
15+
}
16+
17+
[TestMethod]
18+
[DataRow("12", 11, 11001, "14", 11, 11001)]
19+
[DataRow("12", 12, 11001, "12", 11, 11001)]
20+
[DataRow("12", 12, 11005, "12", 11, 11001)]
21+
public void NotEquals_GivenDifferentObjects_NotEqual(string productSeries1, int model1, int id1,
22+
string productSeries2 ,int model2, long id2)
23+
{
24+
ProductSerialNumber productSerialNumber1 = new ProductSerialNumber(productSeries1, model1, id1);
25+
ProductSerialNumber productSerialNumber2 = new ProductSerialNumber(productSeries2, model2, id2);
26+
27+
Assert.IsTrue(productSerialNumber1 != productSerialNumber2);
28+
}
29+
30+
[TestMethod]
31+
public void Equals_GivenNull_NotEqual()
32+
{
33+
ProductSerialNumber productSerialNumber1 = new ProductSerialNumber("12", 11, 11001);
34+
ProductSerialNumber productSerialNumber2 = null;
35+
36+
37+
Assert.IsFalse(productSerialNumber1.Equals(productSerialNumber2));
38+
}
39+
40+
[TestMethod]
41+
public void Equals_GivenSameReference_Equal()
42+
{
43+
ProductSerialNumber productSerialNumber1 = new ProductSerialNumber("12", 11, 11001);
44+
ProductSerialNumber productSerialNumber2 = productSerialNumber1;
45+
46+
Assert.IsTrue(productSerialNumber1 == productSerialNumber2);
47+
}
48+
49+
[TestMethod]
50+
public void Equals_GivenDifferentTypes_NotEqual()
51+
{
52+
ProductSerialNumber productSerialNumber1 = new ProductSerialNumber("12", 11, 11001);
53+
int otherObj = 12;
54+
55+
Assert.IsFalse(productSerialNumber1.Equals(otherObj));
56+
}
57+
}
58+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
3+
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter10.Listing10_07.Tests
4+
{
5+
[TestClass]
6+
public class CoordinateTests
7+
{
8+
[TestMethod]
9+
public void CoordinateEquals_GivenEqualObjects_AreEqual()
10+
{
11+
Coordinate coordinate1 = new Coordinate(new Longitude(48, 52),
12+
new Latitude(-2, -20));
13+
Coordinate coordinate2 = new Coordinate(new Longitude(48, 52),
14+
new Latitude(-2, -20));
15+
16+
Assert.IsTrue(coordinate1.Equals(coordinate2));
17+
}
18+
19+
[TestMethod]
20+
[DataRow(48, 52, -2, -20, 49, 52, -2, -20)]
21+
[DataRow(48, 52, -2, -20, 48, 53, -2, -20)]
22+
[DataRow(48, 52, -2, -20, 48, 52, 0, -20)]
23+
[DataRow(48, 52, -2, -20, 48, 52, -2, -10)]
24+
public void CoordinateNotEquals_GivenDifferentObjects_NotEqual(int longDegrees1, int longMinutes1,
25+
int latDegrees1, int latMinutes1, int longDegrees2, int longMinutes2, int latDegrees2, int latMinutes2)
26+
{
27+
Coordinate coordinate1 = new Coordinate(new Longitude(longDegrees1, longMinutes1),
28+
new Latitude(latDegrees1, latMinutes1));
29+
Coordinate coordinate2 = new Coordinate(new Longitude(longDegrees2, longMinutes2),
30+
new Latitude(latDegrees2, latMinutes2));
31+
32+
Assert.IsTrue(coordinate1 != coordinate2);
33+
}
34+
35+
[TestMethod]
36+
public void CoordinateEquals_GivenNull_NotEqual()
37+
{
38+
Coordinate coordinate1 = new Coordinate(new Longitude(48, 52),
39+
new Latitude(-2, -20));
40+
Coordinate? coordinate2 = null;
41+
42+
Assert.IsFalse(coordinate1.Equals(coordinate2));
43+
}
44+
45+
[TestMethod]
46+
public void CoordinateEquals_GivenSameReference_Equal()
47+
{
48+
Coordinate coordinate1 = new Coordinate(new Longitude(48, 52),
49+
new Latitude(-2, -20));
50+
Coordinate coordinate2 = coordinate1;
51+
52+
Assert.IsTrue(coordinate1 == coordinate2);
53+
}
54+
55+
[TestMethod]
56+
public void CoordinateEquals_GivenDifferentTypes_NotEqual()
57+
{
58+
Coordinate coordinate1 = new Coordinate(new Longitude(48, 52),
59+
new Latitude(-2, -20));
60+
int otherObj = 12;
61+
62+
Assert.IsFalse(coordinate1.Equals(otherObj));
63+
}
64+
65+
66+
[TestMethod]
67+
public void CoordinateAdd_GivenArc_CoordinateOffset()
68+
{
69+
Coordinate coordinate = new Coordinate(new Longitude(48, 52),
70+
new Latitude(-2, -20));
71+
Arc arc = new Arc(new Longitude(3), new Latitude(1));
72+
coordinate += arc;
73+
74+
Assert.AreEqual(51, coordinate.Longitude.Degrees);
75+
Assert.AreEqual(52, coordinate.Longitude.Minutes);
76+
Assert.AreEqual(-1, coordinate.Latitude.Degrees);
77+
Assert.AreEqual(-20, coordinate.Latitude.Minutes);
78+
}
79+
80+
[TestMethod]
81+
public void CoordinateSubtract_GivenArc_CoordinateOffset()
82+
{
83+
Coordinate coordinate = new Coordinate(new Longitude(48, 52),
84+
new Latitude(-2, -20));
85+
Arc arc = new Arc(new Longitude(3), new Latitude(1));
86+
coordinate -= arc;
87+
88+
Assert.AreEqual(45, coordinate.Longitude.Degrees);
89+
Assert.AreEqual(52, coordinate.Longitude.Minutes);
90+
Assert.AreEqual(-3, coordinate.Latitude.Degrees);
91+
Assert.AreEqual(-20, coordinate.Latitude.Minutes);
92+
}
93+
94+
[TestMethod]
95+
public void CoordinateToString_ValidateCoordinate()
96+
{
97+
Coordinate coordinate = new Coordinate(new Longitude(48, 52),
98+
new Latitude(-2, -20));
99+
100+
string expected = @"48° 52' 0 E -2° -20' 0 N";
101+
102+
Assert.AreEqual(expected, coordinate.ToString());
103+
}
104+
}
105+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
3+
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter10.Listing10_08.Tests
4+
{
5+
[TestClass]
6+
public class ProgramTests
7+
{
8+
[TestMethod]
9+
public void Main_CoordinateAdditionSubtraction()
10+
{
11+
const string expected =
12+
@"51° 52' 0 E -1° -20' 0 N
13+
48° 52' 0 E -2° -20' 0 N
14+
51° 52' 0 E -1° -20' 0 N";
15+
16+
IntelliTect.TestTools.Console.ConsoleAssert.Expect(
17+
expected, Program.Main);
18+
}
19+
}
20+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
3+
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter10.Listing10_09.Tests
4+
{
5+
[TestClass]
6+
public class ArcTests
7+
{
8+
[TestMethod]
9+
public void ArcUnaryMinus_InvertSignOfArc()
10+
{
11+
Arc arc = new Arc(new Longitude(48), new Latitude(12));
12+
arc = -arc;
13+
14+
Assert.AreEqual(-48, arc.LongitudeDifference.Degrees);
15+
Assert.AreEqual(-12, arc.LatitudeDifference.Degrees);
16+
}
17+
}
18+
19+
[TestClass]
20+
public class CoordinateTests
21+
{
22+
[TestMethod]
23+
public void ArcUnaryPlus_KeepSignOfArc()
24+
{
25+
Arc arc = new Arc(new Longitude(48), new Latitude(12));
26+
arc = +arc;
27+
28+
Assert.AreEqual(48, arc.LongitudeDifference.Degrees);
29+
Assert.AreEqual(12, arc.LatitudeDifference.Degrees);
30+
}
31+
32+
33+
[TestMethod]
34+
public void CoordinateEquals_GivenEqualObjects_AreEqual()
35+
{
36+
Coordinate coordinate1 = new Coordinate(new Longitude(48, 52),
37+
new Latitude(-2, -20));
38+
Coordinate coordinate2 = new Coordinate(new Longitude(48, 52),
39+
new Latitude(-2, -20));
40+
41+
Assert.IsTrue(coordinate1.Equals(coordinate2));
42+
}
43+
44+
[TestMethod]
45+
[DataRow(48, 52, -2, -20, 49, 52, -2, -20)]
46+
[DataRow(48, 52, -2, -20, 48, 53, -2, -20)]
47+
[DataRow(48, 52, -2, -20, 48, 52, 0, -20)]
48+
[DataRow(48, 52, -2, -20, 48, 52, -2, -10)]
49+
public void CoordinateNotEquals_GivenDifferentObjects_NotEqual(int longDegrees1, int longMinutes1,
50+
int latDegrees1, int latMinutes1, int longDegrees2, int longMinutes2, int latDegrees2, int latMinutes2)
51+
{
52+
Coordinate coordinate1 = new Coordinate(new Longitude(longDegrees1, longMinutes1),
53+
new Latitude(latDegrees1, latMinutes1));
54+
Coordinate coordinate2 = new Coordinate(new Longitude(longDegrees2, longMinutes2),
55+
new Latitude(latDegrees2, latMinutes2));
56+
57+
Assert.IsTrue(coordinate1 != coordinate2);
58+
}
59+
60+
[TestMethod]
61+
public void CoordinateEquals_GivenNull_NotEqual()
62+
{
63+
Coordinate coordinate1 = new Coordinate(new Longitude(48, 52),
64+
new Latitude(-2, -20));
65+
Coordinate? coordinate2 = null;
66+
67+
Assert.IsFalse(coordinate1.Equals(coordinate2));
68+
}
69+
70+
[TestMethod]
71+
public void CoordinateEquals_GivenSameReference_Equal()
72+
{
73+
Coordinate coordinate1 = new Coordinate(new Longitude(48, 52),
74+
new Latitude(-2, -20));
75+
Coordinate coordinate2 = coordinate1;
76+
77+
Assert.IsTrue(coordinate1 == coordinate2);
78+
}
79+
80+
[TestMethod]
81+
public void CoordinateEquals_GivenDifferentTypes_NotEqual()
82+
{
83+
Coordinate coordinate1 = new Coordinate(new Longitude(48, 52),
84+
new Latitude(-2, -20));
85+
int otherObj = 12;
86+
87+
Assert.IsFalse(coordinate1.Equals(otherObj));
88+
}
89+
90+
91+
[TestMethod]
92+
public void CoordinateAdd_GivenArc_CoordinateOffset()
93+
{
94+
Coordinate coordinate = new Coordinate(new Longitude(48, 52),
95+
new Latitude(-2, -20));
96+
Arc arc = new Arc(new Longitude(3), new Latitude(1));
97+
coordinate += arc;
98+
99+
Assert.AreEqual(51, coordinate.Longitude.Degrees);
100+
Assert.AreEqual(52, coordinate.Longitude.Minutes);
101+
Assert.AreEqual(-1, coordinate.Latitude.Degrees);
102+
Assert.AreEqual(-20, coordinate.Latitude.Minutes);
103+
}
104+
105+
[TestMethod]
106+
public void CoordinateSubtract_GivenArc_CoordinateOffset()
107+
{
108+
Coordinate coordinate = new Coordinate(new Longitude(48, 52),
109+
new Latitude(-2, -20));
110+
Arc arc = new Arc(new Longitude(3), new Latitude(1));
111+
coordinate -= arc;
112+
113+
Assert.AreEqual(45, coordinate.Longitude.Degrees);
114+
Assert.AreEqual(52, coordinate.Longitude.Minutes);
115+
Assert.AreEqual(-3, coordinate.Latitude.Degrees);
116+
Assert.AreEqual(-20, coordinate.Latitude.Minutes);
117+
}
118+
119+
[TestMethod]
120+
public void CoordinateToString_ValidateCoordinate()
121+
{
122+
Coordinate coordinate = new Coordinate(new Longitude(48, 52),
123+
new Latitude(-2, -20));
124+
125+
string expected = @"48° 52' 0 E -2° -20' 0 N";
126+
127+
Assert.AreEqual(expected, coordinate.ToString());
128+
}
129+
}
130+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
4+
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter10.Listing10_11.Tests
5+
{
6+
[TestClass]
7+
public class LatitudeTests
8+
{
9+
[TestMethod]
10+
public void LatitudeDoubleCast_GivenValidLatitude_CastDegreesToDouble()
11+
{
12+
Latitude latitude = new Latitude(12);
13+
14+
double castTo = (Double) latitude;
15+
16+
Assert.AreEqual(12.00, castTo);
17+
}
18+
19+
[TestMethod]
20+
public void LatitudeCast_GivenValidLatitude_CastDoubleToLatitude()
21+
{
22+
double d = 12.00;
23+
24+
Latitude castTo = (Latitude) d;
25+
26+
Assert.AreEqual(12.00, castTo.DecimalDegrees);
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)