comparison Stocks/Stocks.Common.Tests.Unit/Models/PriceTests.cs @ 0:e5d46bb6cdb0

Initial commit
author adminSH stevenhollidge@hotmail.com
date Mon, 20 Feb 2012 13:52:35 +0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e5d46bb6cdb0
1 using Stocks.Common.Models;
2 using Xunit;
3 using System.Collections.Generic;
4
5 namespace Stocks.Common.Tests.Unit.Models
6 {
7 public class PriceTests
8 {
9 #region GetHashCode tests
10 [Fact]
11 public void GetHashCode_success()
12 {
13 var lhs = new Price() { Symbol = "GOOG", CurrentPrice = (decimal) 123.45, PreviousPrice = 0 };
14 var rhs = new Price();
15 rhs.Symbol = "GOOG";
16 rhs.CurrentPrice = (decimal) 123.45;
17 rhs.PreviousPrice = 0;
18 var expected = lhs.GetHashCode();
19 var actual = rhs.GetHashCode();
20 Assert.Equal(expected, actual);
21 }
22
23 [Fact]
24 public void GetHashCode_should_be_different()
25 {
26 var lhs = new Price() { Symbol = "GOOG", CurrentPrice = (decimal) 123.45, PreviousPrice = 0 };
27 var rhs = new Price();
28 rhs.Symbol = "GOOG";
29 rhs.CurrentPrice = 0;
30 rhs.PreviousPrice = (decimal)123.45;
31 var expected = lhs.GetHashCode();
32 var actual = rhs.GetHashCode();
33 Assert.NotEqual(expected, actual);
34 }
35
36 [Fact]
37 public void GetHashCode_should_be_different_and_not_overflow()
38 {
39 var lhs = new Price()
40 {
41 Symbol = "GOOG",
42 CurrentPrice = (decimal) 999999.99,
43 PreviousPrice = (decimal) 999999.99
44 };
45 var rhs = new Price();
46 rhs.Symbol = "GOOG";
47 rhs.CurrentPrice = (decimal) 999999.99;
48 rhs.PreviousPrice = 0;
49 var expected = lhs.GetHashCode();
50 var actual = rhs.GetHashCode();
51 Assert.NotEqual(expected, actual);
52 }
53 #endregion
54
55 #region Equals tests
56 [Fact]
57 public void Equals_success()
58 {
59 var lhs = new Price() { Symbol = "GOOG", CurrentPrice = (decimal)123.45, PreviousPrice = 0 };
60 var rhs = new Price();
61 rhs.Symbol = "GOOG";
62 rhs.CurrentPrice = (decimal)123.45;
63 rhs.PreviousPrice = 0;
64 var expected = true;
65
66 var actual = lhs.Equals(rhs);
67 Assert.Equal(expected, actual);
68
69 actual = rhs.Equals(lhs);
70 Assert.Equal(expected, actual);
71 }
72
73 [Fact]
74 public void Equals_expected_to_fail()
75 {
76 var lhs = new Price() { Symbol = "GOOG", CurrentPrice = (decimal)123.45, PreviousPrice = 0 };
77 var rhs = new Price();
78 rhs.Symbol = "GOOG";
79 rhs.CurrentPrice = (decimal)123.44;
80 rhs.PreviousPrice = 0;
81 var expected = true;
82
83 var actual = lhs.Equals(rhs);
84 Assert.NotEqual(expected, actual);
85
86 actual = rhs.Equals(lhs);
87 Assert.NotEqual(expected, actual);
88 }
89
90 [Fact]
91 public void Equals_success_using_object()
92 {
93 var lhs = new Price() { Symbol = "GOOG", CurrentPrice = (decimal)123.45, PreviousPrice = 0 };
94 var rhs = new Price();
95 rhs.Symbol = "GOOG";
96 rhs.CurrentPrice = (decimal)123.45;
97 rhs.PreviousPrice = 0;
98
99 object obj = rhs;
100 var expected = true;
101
102 var actual = lhs.Equals(obj);
103 Assert.Equal(expected, actual);
104 }
105
106 [Fact]
107 public void Equals_expected_to_fail_using_object()
108 {
109 var lhs = new Price() { Symbol = "GOOG", CurrentPrice = (decimal)123.45, PreviousPrice = 0 };
110 var rhs = new Price();
111 rhs.Symbol = "GOOG";
112 rhs.CurrentPrice = (decimal)123.46;
113 rhs.PreviousPrice = 0;
114
115 object obj = rhs;
116 var expected = true;
117
118 var actual = lhs.Equals(obj);
119 Assert.NotEqual(expected, actual);
120 }
121 #endregion
122
123 #region CompareTo tests
124
125 public void CompareTo_the_same_by_symbol_implicit_comparer()
126 {
127 var lhs = new Price() { Symbol = "GOOG", CurrentPrice = (decimal)123.45, PreviousPrice = 0 };
128
129 var rhs = new Price();
130 rhs.Symbol = "GOOG";
131 rhs.CurrentPrice = (decimal)999;
132 rhs.PreviousPrice = 0;
133
134 var expected = 0;
135 var actual = lhs.CompareTo(rhs);
136
137 Assert.Equal(expected, actual);
138 }
139
140 public void CompareTo_different_by_symbol_implicit_comparer()
141 {
142 var lhs = new Price() { Symbol = "GOOG", CurrentPrice = (decimal)123.45, PreviousPrice = 0 };
143
144 var rhs = new Price();
145 rhs.Symbol = "AAPL";
146 rhs.CurrentPrice = (decimal)123.45;
147 rhs.PreviousPrice = 0;
148
149 var expected = 1;
150 var actual = lhs.CompareTo(rhs);
151
152 Assert.Equal(expected, actual);
153
154 expected = -1;
155 actual = rhs.CompareTo(lhs);
156
157 Assert.Equal(expected, actual);
158 }
159
160 public void CompareTo_same_by_current_price()
161 {
162 var lhs = new Price() { Symbol = "GOOG", CurrentPrice = (decimal)123.45, PreviousPrice = 0 };
163
164 var rhs = new Price();
165 rhs.Symbol = "MSFT";
166 rhs.CurrentPrice = (decimal)123.45;
167 rhs.PreviousPrice = (decimal)999;
168
169 var expected = 0;
170 var actual = rhs.CompareTo(lhs, PriceComparisonType.CurrentPrice);
171
172 Assert.Equal(expected, actual);
173 }
174
175 public void CompareTo_different_by_current_price()
176 {
177 var lhs = new Price() { Symbol = "GOOG", CurrentPrice = (decimal)123.45, PreviousPrice = 0 };
178
179 var rhs = new Price();
180 rhs.Symbol = "MSFT";
181 rhs.CurrentPrice = (decimal)9;
182 rhs.PreviousPrice = (decimal)999;
183
184 var expected = -1;
185 var actual = lhs.CompareTo(rhs, PriceComparisonType.CurrentPrice);
186
187 Assert.Equal(expected, actual);
188
189 expected = 1;
190 actual = rhs.CompareTo(lhs, PriceComparisonType.CurrentPrice);
191 Assert.Equal(expected, actual);
192 }
193
194 public void CompareTo_same_by_previous_price()
195 {
196 var lhs = new Price() { Symbol = "GOOG", CurrentPrice = (decimal)44, PreviousPrice = 999 };
197
198 var rhs = new Price();
199 rhs.Symbol = "MSFT";
200 rhs.CurrentPrice = (decimal)123.45;
201 rhs.PreviousPrice = (decimal)999;
202
203 var expected = 0;
204 var actual = rhs.CompareTo(lhs, PriceComparisonType.PreviousPrice);
205
206 Assert.Equal(expected, actual);
207 }
208
209 public void CompareTo_different_by_previous_price()
210 {
211 var lhs = new Price() { Symbol = "GOOG", CurrentPrice = (decimal)123.45, PreviousPrice = 0 };
212
213 var rhs = new Price();
214 rhs.Symbol = "MSFT";
215 rhs.CurrentPrice = (decimal)9;
216 rhs.PreviousPrice = (decimal)999;
217
218 var expected = -1;
219 var actual = lhs.CompareTo(rhs, PriceComparisonType.PreviousPrice);
220
221 Assert.Equal(expected, actual);
222
223 expected = 1;
224 actual = rhs.CompareTo(lhs, PriceComparisonType.PreviousPrice);
225 Assert.Equal(expected, actual);
226 }
227 #endregion
228
229 #region == tests
230
231 #endregion
232
233 #region != tests
234
235 #endregion
236
237 #region List tests
238
239 [Fact]
240 public void Sorting_a_list()
241 {
242 var actual = getListOfPrices();
243
244 var expected = new List<Price>(5)
245 {
246 new Price() { Symbol = "AAPL", CurrentPrice = (decimal)123.45, PreviousPrice = (decimal) 123.40 },
247 new Price() { Symbol = "DIZ", CurrentPrice = (decimal)96.45, PreviousPrice = (decimal) 87.20 },
248 new Price() { Symbol = "GOOG", CurrentPrice = (decimal)89.23, PreviousPrice = (decimal) 90.86 },
249 new Price() { Symbol = "MSFT", CurrentPrice = (decimal)67.45, PreviousPrice = (decimal) 92.23 },
250 new Price() { Symbol = "ZZZ", CurrentPrice = (decimal)3123.45, PreviousPrice = (decimal) 3123.45 }
251 };
252
253 actual.Sort();
254 Assert.Equal(expected, actual);
255 }
256
257 [Fact]
258 public void Sorting_a_list_by_currentPrice()
259 {
260 var actual = getListOfPrices();
261
262 var expected = new List<Price>(5)
263 {
264 new Price() { Symbol = "MSFT", CurrentPrice = (decimal)67.45, PreviousPrice = (decimal) 92.23 },
265 new Price() { Symbol = "GOOG", CurrentPrice = (decimal)89.23, PreviousPrice = (decimal) 90.86 },
266 new Price() { Symbol = "DIZ", CurrentPrice = (decimal)96.45, PreviousPrice = (decimal) 87.20 },
267 new Price() { Symbol = "AAPL", CurrentPrice = (decimal)123.45, PreviousPrice = (decimal) 123.40 },
268 new Price() { Symbol = "ZZZ", CurrentPrice = (decimal)3123.45, PreviousPrice = (decimal) 3123.45 }
269 };
270
271 var comparer = new Price.PriceComparer();
272 comparer.ComparisonMethod = PriceComparisonType.CurrentPrice;
273 actual.Sort(comparer);
274 Assert.Equal(expected, actual);
275 }
276
277 [Fact]
278 public void Sorting_a_list_by_previousPrice()
279 {
280 var actual = getListOfPrices();
281
282 var expected = new List<Price>(5)
283 {
284 new Price() { Symbol = "DIZ", CurrentPrice = (decimal)96.45, PreviousPrice = (decimal) 87.20 },
285 new Price() { Symbol = "GOOG", CurrentPrice = (decimal)89.23, PreviousPrice = (decimal) 90.86 },
286 new Price() { Symbol = "MSFT", CurrentPrice = (decimal)67.45, PreviousPrice = (decimal) 92.23 },
287 new Price() { Symbol = "AAPL", CurrentPrice = (decimal)123.45, PreviousPrice = (decimal) 123.40 },
288 new Price() { Symbol = "ZZZ", CurrentPrice = (decimal)3123.45, PreviousPrice = (decimal) 3123.45 }
289 };
290
291 var comparer = new Price.PriceComparer();
292 comparer.ComparisonMethod = PriceComparisonType.PreviousPrice;
293 actual.Sort(comparer);
294 Assert.Equal(expected, actual);
295 }
296
297 private List<Price> getListOfPrices()
298 {
299 return new List<Price>(5)
300 {
301 new Price() { Symbol = "GOOG", CurrentPrice = (decimal)89.23, PreviousPrice = (decimal) 90.86 },
302 new Price() { Symbol = "AAPL", CurrentPrice = (decimal)123.45, PreviousPrice = (decimal) 123.40 },
303 new Price() { Symbol = "MSFT", CurrentPrice = (decimal)67.45, PreviousPrice = (decimal) 92.23 },
304 new Price() { Symbol = "ZZZ", CurrentPrice = (decimal)3123.45, PreviousPrice = (decimal) 3123.45 },
305 new Price() { Symbol = "DIZ", CurrentPrice = (decimal)96.45, PreviousPrice = (decimal) 87.20 }
306 };
307 }
308
309 #endregion
310
311 // need to test for null and other object types being passed in as null
312 }
313 }