1
+ extern crate rand;
2
+
3
+ use self :: rand:: Rng ;
4
+
1
5
use std:: { iter, time} ;
2
6
use std:: ops:: Add ;
3
7
@@ -72,7 +76,12 @@ fn write_comment_control_characters_still_parseable() {
72
76
assert_eq ! ( & expected, str :: from_utf8( & buf[ ..] ) . unwrap( ) ) ;
73
77
74
78
let mut i = IntervalLogIterator :: new ( & buf) ;
75
- assert_eq ! ( Some ( Ok ( LogEntry :: StartTime ( 123.456 ) ) ) , i. next( ) ) ;
79
+ assert_eq ! (
80
+ Some ( Ok (
81
+ LogEntry :: StartTime ( time:: Duration :: new( 123 , 456_000_000 ) )
82
+ ) ) ,
83
+ i. next( )
84
+ ) ;
76
85
assert_eq ! ( None , i. next( ) ) ;
77
86
}
78
87
@@ -214,13 +223,73 @@ fn write_base_time() {
214
223
) ;
215
224
}
216
225
226
+ #[ test]
227
+ fn parse_duration_full_ns ( ) {
228
+ let ( rest, dur) = fract_sec_duration ( b"123456.789012345foo" ) . unwrap ( ) ;
229
+
230
+ assert_eq ! ( time:: Duration :: new( 123456 , 789_012_345 ) , dur) ;
231
+ assert_eq ! ( b"foo" , rest) ;
232
+ }
233
+
234
+ #[ test]
235
+ fn parse_duration_scale_ns ( ) {
236
+ let ( rest, dur) = fract_sec_duration ( b"123456.789012foo" ) . unwrap ( ) ;
237
+
238
+ assert_eq ! ( time:: Duration :: new( 123456 , 789_012_000 ) , dur) ;
239
+ assert_eq ! ( b"foo" , rest) ;
240
+ }
241
+
242
+ #[ test]
243
+ fn parse_duration_too_many_ns ( ) {
244
+ let ( rest, dur) = fract_sec_duration ( b"123456.7890123456foo" ) . unwrap ( ) ;
245
+
246
+ // consumes all the numbers, but only parses the first 9
247
+ assert_eq ! ( time:: Duration :: new( 123456 , 789_012_345 ) , dur) ;
248
+ assert_eq ! ( b"foo" , rest) ;
249
+ }
250
+
251
+ #[ test]
252
+ fn duration_fp_roundtrip_accuracy ( ) {
253
+ let mut rng = rand:: thread_rng ( ) ;
254
+
255
+ let mut buf = String :: new ( ) ;
256
+ let mut errors = Vec :: new ( ) ;
257
+ for _ in 0 ..100_000 {
258
+ buf. clear ( ) ;
259
+
260
+ // pick seconds
261
+ let secs = rng. gen_range ( 0 , 2_000_000_000 ) ;
262
+ // pick nsecs that only has ms accuracy
263
+ let nsecs = rng. gen_range ( 0 , 1000 ) * 1000_000 ;
264
+
265
+ let dur = time:: Duration :: new ( secs, nsecs) ;
266
+ let fp_secs = duration_as_fp_seconds ( dur) ;
267
+
268
+ write ! ( & mut buf, "{:.3}" , fp_secs) . unwrap ( ) ;
269
+
270
+ let ( _, dur2) = fract_sec_duration ( buf. as_bytes ( ) ) . unwrap ( ) ;
271
+
272
+ if dur != dur2 {
273
+ errors. push ( ( dur, dur2) ) ;
274
+ }
275
+ }
276
+
277
+ if !errors. is_empty ( ) {
278
+ for & ( dur, dur2) in & errors {
279
+ println ! ( "{:?} -> {:?}" , dur, dur2) ;
280
+ }
281
+ }
282
+
283
+ assert_eq ! ( 0 , errors. len( ) ) ;
284
+ }
285
+
217
286
#[ test]
218
287
fn parse_start_time_with_human_date ( ) {
219
288
let ( rest, e) = start_time (
220
289
b"#[StartTime: 1441812279.474 (seconds since epoch), Wed Sep 09 08:24:39 PDT 2015]\n foo" ,
221
290
) . unwrap ( ) ;
222
291
223
- let expected = LogEntry :: StartTime ( 1441812279.474 ) ;
292
+ let expected = LogEntry :: StartTime ( time :: Duration :: new ( 1441812279 , 474_000_000 ) ) ;
224
293
225
294
assert_eq ! ( expected, e) ;
226
295
assert_eq ! ( b"foo" , rest) ;
@@ -233,7 +302,7 @@ fn parse_start_time_without_human_date() {
233
302
// Also, BaseTime doesn't have a human-formatted time.
234
303
let ( rest, e) = start_time ( b"#[StartTime: 1441812279.474 (seconds since epoch)]\n foo" ) . unwrap ( ) ;
235
304
236
- let expected = LogEntry :: StartTime ( 1441812279.474 ) ;
305
+ let expected = LogEntry :: StartTime ( time :: Duration :: new ( 1441812279 , 474_000_000 ) ) ;
237
306
238
307
assert_eq ! ( expected, e) ;
239
308
assert_eq ! ( b"foo" , rest) ;
@@ -243,7 +312,7 @@ fn parse_start_time_without_human_date() {
243
312
fn parse_base_time ( ) {
244
313
let ( rest, e) = base_time ( b"#[BaseTime: 1441812279.474 (seconds since epoch)]\n foo" ) . unwrap ( ) ;
245
314
246
- let expected = LogEntry :: BaseTime ( 1441812279.474 ) ;
315
+ let expected = LogEntry :: BaseTime ( time :: Duration :: new ( 1441812279 , 474_000_000 ) ) ;
247
316
248
317
assert_eq ! ( expected, e) ;
249
318
assert_eq ! ( b"foo" , rest) ;
@@ -271,8 +340,8 @@ fn parse_interval_hist_no_tag() {
271
340
272
341
let expected = LogEntry :: Interval ( IntervalLogHistogram {
273
342
tag : None ,
274
- start_timestamp : 0.127 ,
275
- duration : 1.007 ,
343
+ start_timestamp : time :: Duration :: new ( 0 , 127_000_000 ) ,
344
+ duration : time :: Duration :: new ( 1 , 7_000_000 ) ,
276
345
max : 2.769 ,
277
346
encoded_histogram : "couldBeBase64" ,
278
347
} ) ;
@@ -287,8 +356,8 @@ fn parse_interval_hist_with_tag() {
287
356
288
357
let expected = LogEntry :: Interval ( IntervalLogHistogram {
289
358
tag : Some ( Tag ( "t" ) ) ,
290
- start_timestamp : 0.127 ,
291
- duration : 1.007 ,
359
+ start_timestamp : time :: Duration :: new ( 0 , 127_000_000 ) ,
360
+ duration : time :: Duration :: new ( 1 , 7_000_000 ) ,
292
361
max : 2.769 ,
293
362
encoded_histogram : "couldBeBase64" ,
294
363
} ) ;
@@ -311,13 +380,13 @@ fn iter_with_ignored_prefix() {
311
380
312
381
let expected0 = LogEntry :: Interval ( IntervalLogHistogram {
313
382
tag : Some ( Tag ( "t" ) ) ,
314
- start_timestamp : 0.127 ,
315
- duration : 1.007 ,
383
+ start_timestamp : time :: Duration :: new ( 0 , 127_000_000 ) ,
384
+ duration : time :: Duration :: new ( 1 , 7_000_000 ) ,
316
385
max : 2.769 ,
317
386
encoded_histogram : "couldBeBase64" ,
318
387
} ) ;
319
388
320
- let expected1 = LogEntry :: StartTime ( 1441812279.474 ) ;
389
+ let expected1 = LogEntry :: StartTime ( time :: Duration :: new ( 1441812279 , 474_000_000 ) ) ;
321
390
322
391
assert_eq ! ( vec![ expected0, expected1] , entries)
323
392
}
@@ -334,13 +403,13 @@ fn iter_without_ignored_prefix() {
334
403
335
404
let expected0 = LogEntry :: Interval ( IntervalLogHistogram {
336
405
tag : Some ( Tag ( "t" ) ) ,
337
- start_timestamp : 0.127 ,
338
- duration : 1.007 ,
406
+ start_timestamp : time :: Duration :: new ( 0 , 127_000_000 ) ,
407
+ duration : time :: Duration :: new ( 1 , 7_000_000 ) ,
339
408
max : 2.769 ,
340
409
encoded_histogram : "couldBeBase64" ,
341
410
} ) ;
342
411
343
- let expected1 = LogEntry :: StartTime ( 1441812279.474 ) ;
412
+ let expected1 = LogEntry :: StartTime ( time :: Duration :: new ( 1441812279 , 474_000_000 ) ) ;
344
413
345
414
assert_eq ! ( vec![ expected0, expected1] , entries)
346
415
}
@@ -363,14 +432,14 @@ fn iter_multiple_entrties_with_interleaved_ignored() {
363
432
364
433
let expected0 = LogEntry :: Interval ( IntervalLogHistogram {
365
434
tag : Some ( Tag ( "t" ) ) ,
366
- start_timestamp : 0.127 ,
367
- duration : 1.007 ,
435
+ start_timestamp : time :: Duration :: new ( 0 , 127_000_000 ) ,
436
+ duration : time :: Duration :: new ( 1 , 7_000_000 ) ,
368
437
max : 2.769 ,
369
438
encoded_histogram : "couldBeBase64" ,
370
439
} ) ;
371
440
372
- let expected1 = LogEntry :: StartTime ( 1441812279.474 ) ;
373
- let expected2 = LogEntry :: BaseTime ( 1441812279.474 ) ;
441
+ let expected1 = LogEntry :: StartTime ( time :: Duration :: new ( 1441812279 , 474_000_000 ) ) ;
442
+ let expected2 = LogEntry :: BaseTime ( time :: Duration :: new ( 1441812279 , 474_000_000 ) ) ;
374
443
375
444
assert_eq ! ( vec![ expected0, expected1, expected2] , entries)
376
445
}
0 commit comments