PageRenderTime 53ms CodeModel.GetById 50ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/specs/core/time.ds

http://github.com/wilkie/djehuty
Unknown | 245 lines | 204 code | 41 blank | 0 comment | 0 complexity | 27333944f6634af4b13bf2c20266825a MD5 | raw file
  1module specs.core.time;
  2
  3import testing.support;
  4
  5import core.time;
  6
  7describe time() {
  8	describe creation() {
  9		it should_have_sane_defaults() {
 10			auto t = new Time();
 11			should(t.microseconds == 0);
 12		}
 13
 14		it should_handle_zero_milliseconds() {
 15			auto t = new Time(0);
 16			should(t.microseconds == 0);
 17		}
 18
 19		it should_handle_positive_microseconds() {
 20			auto t = new Time(1234000);
 21			should(t.microseconds == 1234000);
 22		}
 23
 24		it should_handle_negative_microseconds() {
 25			auto t = new Time(-1234000);
 26			should(t.microseconds == -1234000);
 27		}
 28
 29		it should_handle_hours_minutes_seconds() {
 30			auto t = new Time(1, 2, 3);
 31			should(t.microseconds == 3723000000);
 32		}
 33
 34		it should_handle_hours_minutes_seconds_microseconds() {
 35			auto t = new Time(1, 2, 3, 4);
 36			should(t.microseconds == 3723000004);
 37		}
 38
 39		it should_handle_negative_everything() {
 40			auto t = new Time(-1, -2, -3, -4);
 41			should(t.microseconds == -3723000004);
 42		}
 43	}
 44
 45	describe Now() {
 46		it should_return_a_new_time() {
 47			auto n = Time.Now();
 48			shouldNot(cast(Time)n is null);
 49		}
 50	}
 51
 52	describe hours() {
 53		it should_handle_zero_time() {
 54			auto t = new Time(0);
 55			should(t.hours == 0);
 56		}
 57
 58		it should_handle_positive_time() {
 59			auto t = new Time(3L * 60L * 60L * 1000000L);
 60			should(t.hours == 3);
 61		}
 62
 63		it should_handle_negative_time() {
 64			auto t = new Time(-3L * 60L * 60L * 1000000L);
 65			should(t.hours == -3);
 66		}
 67	}
 68
 69	describe minutes() {
 70		it should_handle_zero_time() {
 71			auto t = new Time(0);
 72			should(t.minutes == 0);
 73		}
 74
 75		it should_handle_positive_time() {
 76			auto t = new Time(25L * 60L * 1000000L);
 77			should(t.minutes == 25);
 78		}
 79
 80		it should_handle_negative_time() {
 81			auto t = new Time(-25L * 60L * 1000000L);
 82			should(t.minutes == -25);
 83		}
 84	}
 85
 86	describe seconds() {
 87		it should_handle_zero_time() {
 88			auto t = new Time(0);
 89			should(t.seconds == 0);
 90		}
 91
 92		it should_handle_positive_time() {
 93			auto t = new Time(45L * 1000000L);
 94			should(t.seconds == 45);
 95		}
 96
 97		it should_handle_negative_time() {
 98			auto t = new Time(-45L * 1000000L);
 99			should(t.seconds == -45);
100		}
101	}
102
103	describe milliseconds() {
104		it should_handle_zero_time() {
105			auto t = new Time(0);
106			should(t.milliseconds == 0);
107		}
108
109		it should_handle_positive_time() {
110			auto t = new Time(678L * 1000L);
111			should(t.milliseconds == 678);
112		}
113
114		it should_handle_negative_time() {
115			auto t = new Time(-678L * 1000L);
116			should(t.milliseconds == -678);
117		}
118
119		it should_handle_being_set_to_zero_milliseconds() {
120			auto t = new Time();
121			t.milliseconds = 0;
122			should(t.microseconds == 0);
123		}
124
125		it should_handle_being_set_to_positive_milliseconds() {
126			auto t = new Time();
127			t.milliseconds = 1234;
128			should(t.microseconds == 1234000);
129		}
130
131		it should_handle_being_set_to_negative_milliseconds() {
132			auto t = new Time();
133			t.milliseconds = -1234;
134			should(t.microseconds == -1234000);
135		}
136	}
137
138	describe microseconds() {
139		it should_handle_being_set_to_zero_microseconds() {
140			auto t = new Time();
141			t.microseconds = 0;
142			should(t.microseconds == 0);
143		}
144
145		it should_handle_being_set_to_positive_microseconds() {
146			auto t = new Time();
147			t.microseconds = 1234;
148			should(t.microseconds == 1234);
149		}
150
151		it should_handle_being_set_to_negative_microseconds() {
152			auto t = new Time();
153			t.microseconds(-1234);
154			should(t.microseconds == -1234);
155		}
156	}
157
158	describe comparators() {
159		it should_handle_equal_times() {
160			auto a = new Time(1234);
161			auto b = new Time(1234);
162			shouldNot(a < b);
163			should(a == b);
164			shouldNot(a > b);
165		}
166
167		it should_handle_unequal_times() {
168			auto a = new Time(-1234);
169			auto b = new Time(1234);
170			should(a < b);
171			shouldNot(a == b);
172			shouldNot(a > b);
173		}
174	}
175
176	describe toString() {
177		it should_handle_zero_time() {
178			auto t = new Time(0);
179			should(t.toString() == "00:00:00.000");
180		}
181
182		it should_handle_some_microseconds() {
183			auto t = new Time();
184			t.microseconds = 123456;
185			should(t.toString() == "00:00:00.123");
186		}
187
188		it should_handle_some_milliseconds() {
189			auto t = new Time(123000);
190			should(t.toString() == "00:00:00.123");
191		}
192
193		it should_handle_hours_minutes_seconds() {
194			auto t = new Time(10, 2, 30);
195			should(t.toString() == "10:02:30.000");
196		}
197
198		it should_handle_everything() {
199			auto t = new Time(12345678000);
200			should(t.toString() == "03:25:45.678");
201		}
202
203		it should_handle_negative_time() {
204			auto t = new Time(-12345678000);
205			should(t.toString() == "-03:25:45.678");
206		}
207	}
208
209	describe opAdd() {
210		it should_work() {
211			auto a = new Time(1000000);
212			auto b = new Time(234000);
213			auto c = a + b;
214			should(c.microseconds == 1234000);
215		}
216	}
217
218	describe opSub() {
219		it should_work() {
220			auto a = new Time(234000);
221			auto b = new Time(1234000);
222			auto c = a - b;
223			should(c.microseconds == -1000000);
224		}
225	}
226
227	describe opAddAssign() {
228		it should_work() {
229			auto a = new Time(1000000);
230			auto b = new Time(234000);
231			a += b;
232			should(a.microseconds == 1234000);
233		}
234	}
235
236	describe opSubAssign() {
237		it should_work() {
238			auto a = new Time(234000);
239			auto b = new Time(1234000);
240			a -= b;
241			should(a.microseconds == -1000000);
242		}
243	}
244}
245