rustics 1.0.3

simple statistic library for performance analysis
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
//
//  Copyright 2024 Jonathan L Bertoni
//
//  This code is available under the Berkeley 2-Clause, Berkeley 3-clause,
//  and MIT licenses.
//

//! Rustics provides a simple interface for recording sample and event streams and printing
//! statistics.
//!
//! Many of the module comments contain examples of usage.  The main.rs program contains
//! a brief example of how to use two of the statistics types.
//!
//! ## Types
//!
//! * Statistics for Integer Samples
//!     * Integer statistics provide summary parameters, like the mean, and a pseudo-log histogram.
//!
//!     * Samples are of type i64.
//!
//!     * For the pseudo-log histogram, the log of a positive number is defined as the logarithm
//!       to the base 2, rounding up any fractional part.  Therefore, the pseudo-log of 5 is 3.
//!       The pseudo-log of a negative number n is defines as -pseudo-log(-n), and the pseudo-log
//!       of 0 is defined as 0.
//!
//! * Basic Integer Statistics Types
//!     * RunningInteger
//!         * RunningInteger implements running statistics for a series of sample values.
//!
//!         * It also provides a pseudo-log histogram of the samples.
//!
//!     * IntegerWindow
//!         * IntegerWindow implements a fixed-size window of the last n samples recorded.  Summary
//!           statistics of the window samples are computed on demand.
//!
//!         * Like RunningInteger, it also provides a pseudo-log histogram.  The histogram counts
//!           all samples seen, not just the current window.
//!
//!     * Counter
//!         * This type implements a simple counter that generates no further statistics.  It can be
//!           used for counting events, for example.
//!
//! * Basic Time Statistics Types
//!     * RunningTime
//!         * This type uses the RunningInteger code to handle time intervals.  Values are printed
//!           using units of time when appropriate.
//!
//!     * TimeWindow
//!         * This type uses the IntegerWindow code to handle time intervals.  As with the
//!           RunningTime type, values are printed in units of time.
//!
//! * Basic Floating Point Statistics Types
//!     * Floating point samples currently are supported only for machines that use IEEE f64 format.
//!
//!     * RunningFloat
//!         * This type keeps running statistics, like RunningInteger.  It uses a coarser pseudo-log
//!           function than the integer statistics.  See FloatHistogram for details.
//!
//!     * FloatWindow
//!         * FloatWindow keeps a fixed-size window of samples, like IntegerWindow.  It creates a
//!           histogram using FloatHistogram.
//!
//! * Hierarchical Statistics:  The Hier Type
//!     * A Hier instance uses multiple Rustics instances to maintain statistical information.  This
//!       approach can reduce accuracy loss over long sample periods and provide historical data.
//!
//!     * Samples are collected into a single Rustics instance.  When this instance has collected
//!       a configurable number of samples, it is pushed onto a list of historical data, and a
//!       new Rustics instance is created to collect the next batch of samples.  This process
//!       repeats indefinitely.
//!
//!     * When this list has reached a configurable size limit, the oldest entry is discarded every
//!       time a new entry is pushed onto it.
//!
//!     * This list forms level 0 of the hierarchy.
//!
//!     * When a configurable number of level 0 instances have been collected, a summary instance
//!       of those instances is created and pushed onto level 1.  Like level 0, level 1 forms a
//!       list with a configurable size limit.
//!
//!     * This process is performed recursively for a user-specified number of levels.  The summed
//!       instances thus form a hierarchy somewhat like a tree or a forest of trees.
//!
//!     * Users can query any member of the hierarchy to look into the past.
//!
//!     * A Hier instance also can maintain an optional window of the last N samples collected to
//!       serve as the values for Rustics queries of the hierarchy as a whole.  If the user does not
//!       configure a window, the current level 0 instance (the one receiving samples) is used.
//!
//!     * In addition to pushing a new level 0 instances after a fixed number of samples, the user
//!       instead can choose to push a new level zero instance by calling the advance() method,
//!       allowing for more application-specific control.
//!
//! * Hierarchical Statistics Types
//!     * Hier
//!         * The Hier struct provides framework code for hierarchical statistics.  After creating
//!           a Hier instance, most users will use this interface or the Rustics interface to
//!           interact with this type.  For example, data is recorded into a Hier instance by
//!           invoking Rustics methods directly on the Hier instance itself.
//!
//!         * The HierGenerator trait provides an interface for Hier to use a basic Rustics type
//!           like RunningInteger or RunningTime.  Most users will not use this type directly.
//!
//!     * IntegerHier
//!         * This struct extends the RunningInteger type to support the Hier code.  See
//!           IntegerHier::new_hier() for a simple interface to create a Hier instance using
//!           RunningInteger instances for statistics collection.  The integer_hier and hier
//!           test modules also contains sample_usage() and make_hier() functions as examples.
//!
//!     * TimeHier
//!         * TimeHier implements Hier for the RunningTime type.  TimeHier::new_hier() will make a
//!           Hier instance that uses RunningTime as the Rustics type.
//!
//!     * FloatHier
//!         * This struct extends the RunningFloat type to support the Hier code.  See
//!           FloatHier::new_hier() for an interface to create a Hier instance.  This type is very
//!           similar to IntegerHier.
//!
//! * Creating Sets
//!     * The "arc_sets" and "rc_sets" modules implement sets that accept Rustics instances and
//!       other sets as members.  Sets can be printed and cleared recursively by invoking a method
//!       on the topmost set.
//!
//!     * ArcSet
//!         * This type provides an Arc-based implementation that is thread-safe.
//!
//!     * RcSet
//!         * This type implements an Rc-based version of sets.  These sets are faster than
//!           Arc-based sets, but are not thread-safe.
//!
//! * Timers
//!     *  Timer
//!         * This trait defines the basic abstract timer.  A timer has a frequency and returns
//!           an integer duration in units of that frequency.  The Timer interface provides
//!           start() and finish() methods to measure clock intervals.
//!
//!     *  DurationTimer
//!         * This type is an implementation of Timer that uses the Rust "Duration" struct, which
//!           measures wall clock time.
//!
//!     *  SimpleClock
//!         * This trait defines the interface used to query a user-defined clock, which can be
//!           wrapped using the ClockTimer type, q.v.
//!
//!         * Clock values must be returned as a monotonically non-decreasing integer tick count.
//!
//!         * The interface requires a hz() member to provide the clock frequency to the ClockTimer
//!           layer.
//!
//!     *  ClockTimer
//!         * This Timer implementation is a wrapper for instances of trait SimpleClock.  For
//!           example, a cycle counter like rdtsc on Intel could be wrapped to implement a
//!           ClockTimer.
//!
//! * Printing
//!     *  Printer
//!         * This trait defines the interface for printing Rustics instances, so it can be used
//!           to implement custom printers.
//!
//!         * See StdioPrinter for a sample implementation.  This type is used as the default
//!           printer to send output to stdout.
//!
//!
//!     *  Printable
//!         * The Printable type provides standard formatting for printing data and some support
//!           functions for more readable output, like time values scaled to human-understandable
//!           forms and integers with commas.  It is of interest mostly to developers creating new
//!           Rustics implementations.
//!

use std::any::Any;
use std::cell::RefCell;
use std::rc::Rc;
use std::default::Default;

pub mod running_integer;
pub mod integer_window;
pub mod integer_hier;

pub mod running_time;
pub mod time_window;
pub mod time_hier;

pub mod running_float;
pub mod float_window;
pub mod float_hier;

pub mod counter;
pub mod arc_sets;
pub mod rc_sets;
pub mod hier;
pub mod window;
pub mod time;
pub mod merge;
pub mod sum;
pub mod log_histogram;
pub mod float_histogram;

pub mod printable;

use hier::Hier;
use hier::HierDescriptor;
use hier::HierConfig;
use hier::HierGenerator;
use hier::HierMember;
use hier::HierExporter;
use hier::ExporterRc;
use hier::MemberRc;
use log_histogram::LogHistogram;
use float_histogram::FloatHistogram;
use float_histogram::HistoOpts;
use printable::Printable;
use time::Timer;

pub type PrinterBox         = Rc<RefCell<dyn Printer>>;
// pub type PrinterBox         = Arc<Mutex<dyn Printer>>;
pub type PrinterOption      = Option<PrinterBox>;
pub type TitleOption        = Option<String>;
pub type UnitsOption        = Option<Units>;
pub type HistoOption        = Option<HistoOpts>;
pub type TimerBox           = Rc<RefCell<dyn Timer>>;
pub type PrintOption        = Option<PrintOpts>;
pub type LogHistogramBox    = Rc<RefCell<LogHistogram>>;
pub type FloatHistogramBox  = Rc<RefCell<FloatHistogram>>;

/// Extracts the mantissa from an f64.

pub fn to_mantissa(input: f64) -> i64 {
    let mantissa_size = 52;

    let bits = input.to_bits();
    let mask = (1_u64 << mantissa_size) - 1;

    (bits & mask) as i64
}

pub fn max_exponent() -> isize {
    1023
}

pub fn max_biased_exponent() -> isize {
    max_exponent() + exponent_bias()
}

pub fn min_exponent() -> isize {
    -1022
}

/// Returns the IEEE f64 exponent bias.

pub fn exponent_bias() -> isize {
    1023
}

/// Extracts the sign from an f64 value.

pub fn sign(input: f64) -> isize {
    if input.to_bits() & (1_u64 << 63) != 0 {
        -1
    } else {
        1
    }
}

pub fn is_zero(input: f64) -> bool {
    input == 0.0    // -0.0 == 0 per IEEE definition
}

/// Extracts the raw exponent from an IEEE f64 value.

pub fn biased_exponent(input: f64) -> isize {
    if input.is_nan() {
        return 0;
    }

    if input.is_infinite() {
        return max_exponent();
    }

    if input.is_subnormal() {
        return min_exponent() + exponent_bias();
    }

    let mantissa_size = 52;
    let exponent_size = 11;

    let bits     = input.to_bits();
    let bits     = bits >> mantissa_size;
    let mask     = (1_u64 << exponent_size) - 1;
    let exponent = (bits & mask) as i64;

    exponent as isize
}
/// Computes the minimum of two f64 values, being careful
/// about NaNs.

pub fn min_f64(a: f64, b: f64) -> f64 {
    if a.is_nan() || b.is_nan() {
        f64::NAN
    } else if a < b {
        a
    } else {
        b
    }
}

/// Computes the maximum of two f64 values, being careful
/// about NaNs.

pub fn max_f64(a: f64, b: f64) -> f64 {
    if a.is_nan() || b.is_nan() {
        f64::NAN
    } else if a > b {
        a
    } else {
        b
    }
}

/// Returns the frequency of a timer in a box.

pub fn timer_box_hz(timer:  &TimerBox) -> u128 {
    timer!(*timer).hz()
}

/// Creates a PrinterBox instance that sends output to stdout.
/// This is the default printer for all Rustics types.

pub fn stdout_printer() -> PrinterBox {
    let printer = StdioPrinter::new(StreamKind::Stdout);

    printer_box!(printer)
}

/// Provides the data for estimating the second and fourth moments
/// about the mean, as well as the mean itself.

pub struct StatisticsData {
    pub n:        f64,
    pub sum:      f64,
    pub squares:  f64,
    pub cubes:    f64,
    pub quads:    f64,
}

/// Contains the return data for compute_statistics.

pub struct Statistics {
    pub mean:     f64,
    pub moment_2: f64,
    pub moment_4: f64,
}

/// Computes the second and fourth moments about the mean
/// given the values in StatisticsData.  This is used when
/// merging multiple Rustics instances for upper-level
/// HierMember instances.
///
/// The formulae are derived by applying the binomial
/// theorem to the formulae for the various moments about
/// the mean.

pub fn compute_statistics(data: StatisticsData) -> Statistics {
    let n       = data.n;
    let sum     = data.sum;
    let squares = data.squares;
    let cubes   = data.cubes;
    let quads   = data.quads;

    let mean    = data.sum / n;
    let mean_1  = mean;
    let mean_2  = mean.powi(2);
    let mean_3  = mean.powi(3);
    let mean_4  = mean.powi(4);

    let moment_2 =
                       squares
      - 2.0 * mean_1 * sum
      +       mean_2 * n;

    let moment_4 =
                       quads
      - 4.0 * mean_1 * cubes
      + 6.0 * mean_2 * squares
      - 4.0 * mean_3 * sum
      +       mean_4 * n;

    Statistics { mean, moment_2, moment_4 }
}

/// Provides the data required to try to recover the sum of
/// the squares and the sum of the fourth power of each of
/// the data samples.

pub struct RecoverData {
    pub n:          f64,
    pub mean:       f64,
    pub moment_2:   f64,
    pub cubes:      f64,
    pub moment_4:   f64,
}

/// This routine converts the data in RecoverData into estimators
/// of the sum of the squares and 4th power of each sample.  This
/// is used when merging Rustics instances for use in a Hier instance.
///
/// The formulae are derived by applying the binomial theorem to
/// the definition of the various moments about the mean.

pub fn recover(data: RecoverData) -> (f64, f64) {
    let n        = data.n;
    let mean     = data.mean;
    let moment_2 = data.moment_2;
    let cubes    = data.cubes;
    let moment_4 = data.moment_4;

    let sum      = n * mean;
    let squares  = moment_2 + 2.0 * mean * sum - n * mean.powi(2);

    let mean_1   = mean;
    let mean_2   = mean.powi(2);
    let mean_3   = mean.powi(3);
    let mean_4   = mean.powi(4);

    let quads =
                   moment_4
          + (4.0 * cubes    * mean_1)
          - (6.0 * squares  * mean_2)
          + (4.0 * sum      * mean_3)
          - (      n        * mean_4);

    (squares, quads)
}

/// Provides the data required to try to estimate the
/// third moment about the mean.

pub struct EstimateData {
    pub n:          f64,
    pub mean:       f64,
    pub moment_2:   f64,
    pub cubes:      f64,
}

/// Estimates moment 3 about the mean.
///
/// I know of no good way to keep a running estimate of
/// the 3rd moment about the mean, so this is the best
/// I can do.  The equations for the squares and the
/// third moment about the mean are from the binomial
/// theorem applied to the definition of those moments.

pub fn estimate_moment_3(data: EstimateData) -> f64 {
    let n         = data.n;
    let mean      = data.mean;
    let cubes     = data.cubes;
    let moment_2  = data.moment_2;
    let sum       = n * mean;

    // Estimate the sums of the squares of each sample.

    let squares =
        moment_2
      + 2.0 * sum * mean
      -       n   * mean.powi(2);

    // Now estimate the third moment about the mean.

    cubes - (3.0 * squares * mean) + 3.0 * (sum * mean.powi(2)) - n * mean.powi(3)
}

/// Computes a variance estimator.

pub fn compute_variance(count: u64, moment_2: f64) -> f64 {
    if count < 2 {
        return 0.0;
    }

    let n = count as f64;

    moment_2 / (n - 1.0)
}

/// Computes the sample skewness.
///
/// This formula is from brownmath.com.

pub fn compute_skewness(count: u64, moment_2: f64, moment_3: f64) -> f64 {
    if count < 3 || moment_2 == 0.0 {
        return 0.0;
    }

    // Deal with floating point non-finite values.

    // For debugging new Rustics types.
    //
    //assert!(moment_2 > 0.0);

    if moment_2 <= 0.0 {
        return 0.0;
    }

    let n          = count as f64;
    let m3         = moment_3 / n;
    let m2         = moment_2 / n;
    let skewness   = m3 / m2.powf(1.5);
    let correction = (n * (n - 1.0)).sqrt() / (n - 2.0);

    skewness * correction
}

/// Computes the sample kurtosis estimator.
///
/// This formula is from brownmath.com.

pub fn compute_kurtosis(count: u64, moment_2: f64, moment_4: f64) -> f64 {
    if count < 4 || moment_2 == 0.0 {
        return 0.0;
    }

    // Deal with floating point non-finite values.

    // For debugging new Rustics types.
    //
    // assert!(moment_2 > 0.0 && moment_4 >= 0.0);

    if moment_2 <= 0.0 || moment_4 <= 0.0 {
        return 0.0;
    }

    let n               = count as f64;
    let kurtosis        = moment_4 / (moment_2.powf(2.0) / n) - 3.0;
    let correction      = (n - 1.0) / ((n - 2.0) * (n - 3.0));
    let kurtosis_factor = (n + 1.0) * kurtosis + 6.0;

    correction * kurtosis_factor
}

/// The make_title() function concatenates two strings, inserting the
/// "=>" marker for set hierarchy specification.  It is probably of
/// interest only to implementors of new Rustics types.  It does
/// omit the "=>" if the title prefix is empty.

pub fn make_title(title_prefix: &str, title: &str) -> String {
    if title_prefix.is_empty() {
        title.to_string()
    } else {
        let mut full_title = String::from(title_prefix);
        full_title.push_str(" ==> ");
        full_title.push_str(title);
        full_title
    }
}

/// Defines printable strings for a value's units.

#[derive(Clone)]
pub struct Units {
    pub singular:   String,
    pub plural:     String,
}

impl Units {
    /// Return a Units struct with empty strings.  This is used
    /// internally when printing without units.

    pub fn empty() -> Units {
        let singular = "".to_string();
        let plural   = "".to_string();

        Units { singular, plural }
    }

    pub fn new(singular: &str, plural: &str) -> Units {
        let singular = singular.to_string();
        let plural   = plural  .to_string();

        Units { singular, plural }
    }
}

impl Default for Units {
    fn default() -> Self {
        Units::empty()
    }
}

/// Defines the options available for printing.

#[derive(Clone)]
pub struct PrintOpts {
    pub printer:     PrinterOption,
    pub title:       TitleOption,
    pub units:       UnitsOption,
    pub histo_opts:  HistoOption,
}

/// The Printer trait allows users to create custom output functions to
/// match their I/O needs.
///
/// An instance of this type is invoked for each line to be printed.
/// The print() member is responsible for adding the newline.

pub trait Printer {
    /// Prints a line of output.  The print method itself must append
    /// the newline.

    fn print(&mut self, output: &str);

    fn as_any        (&self    ) -> &dyn Any;
    fn as_any_mut    (&mut self) -> &mut dyn Any;
}

/// Converts a Printer instance into the shareable
/// form.

#[macro_export]
macro_rules! printer_box { ($x:expr) => { Rc::from(RefCell::new($x)) } }

/// Converts a PrinterBox into a mutable Printer reference.

#[macro_export]
macro_rules! printer_mut { ($x:expr) => { &mut *$x.borrow_mut() } }

/// Converts a PrinterBox into a Printer reference.

#[macro_export]
macro_rules! printer { ($x:expr) => { &*$x.borrow() } }

// These macros work with Arc and Mutex
//
// #[macro_export]
// macro_rules! printer { ($x:expr) => { &mut *$x.lock().unwrap() } }
//
// #[macro_export]
// macro_rules! printer_box { ($x:expr) => { Arc::from(Mutex::new($x)) } }

/// Extracts a printer from a PrintOption instance or provides
/// a stdout_printer() if no printer was specified.

pub fn parse_printer(print_opts: &PrintOption) -> PrinterBox {
    match print_opts {
        Some(print_opts) => {
            match &print_opts.printer {
                Some(printer) => { printer.clone()  }
                None          => { stdout_printer() }
            }
        }

        None => { stdout_printer() }
    }
}

/// Extracts the title in a PrintOption instance, if present, or
/// creates a default title using the name parameter, if no title
/// was specified.

pub fn parse_title(print_opts: &PrintOption, name: &str) -> String {
    match print_opts {
        Some(print_opts) => {
            match &print_opts.title {
                Some(title) => { title.clone()    }
                None        => { name.to_string() }
            }
        }

        None => { name.to_string() }
    }
}

/// Returns the float histogram options in a PrintOption instance,
/// if present, or creates a set of defaults if no histogram options
/// were specified.

pub fn parse_histo_opts(print_opts: &PrintOption) -> HistoOpts {
    match print_opts {
        Some(print_opts) => {
            match &print_opts.histo_opts {
                Some(histo_opts) => { *histo_opts          }
                None             => { HistoOpts::default() }
            }
        }

        None => { HistoOpts::default()  }
    }
}

/// Returns the units in a PrintOption instance, if given, or
/// returns the defaults if no units were specified.

pub fn parse_units(print_opts: &PrintOption) -> Units {
    match print_opts {
        Some(print_opts) => {
            match &print_opts.units {
                Some(units) => { units.clone()    }
                None        => { Units::default() }
            }
        }

        None => { Units::default()  }
    }
}

/// Extracts the options in a PrintOption instance, providing
/// defaults for options not specified.

pub fn parse_print_opts(print_opts: &PrintOption, name: &str)
        -> (PrinterBox, String, Units, HistoOpts) {
    let printer;
    let title;
    let units;
    let histo_opts;

    match print_opts {
        Some(print_opts) => {
            printer =
                match &print_opts.printer {
                    Some(printer) => { printer.clone()  }
                    None          => { stdout_printer() }
                };

            title =
                match &print_opts.title {
                    Some(title) => { title.clone() }
                    None        => { name.to_string()   }
                };

            units =
                match &print_opts.units {
                    Some(units) => { units.clone() }

                    None => { Units::default() }
                };

            histo_opts =
                match &print_opts.histo_opts {
                    Some(histo_opts) => { *histo_opts          }
                    None             => { HistoOpts::default() }
                }
        }

        None => {
            printer    = stdout_printer();
            title      = name.to_string();
            units      = Units::default();
            histo_opts = HistoOpts::default();
        }
    }

    let title = title.to_string();

    (printer, title, units, histo_opts)
}

// Define a printer that will send output to Stdout or Stderr, as
// configured.

/// The StdioPrinter struct is used as the default printer by Rustics.
/// It serves as an example of a simple Printer implementation.

#[derive(Clone)]
pub struct StdioPrinter {
    which: StreamKind,
}

#[derive(Clone)]
pub enum StreamKind {
    Stdout,
    Stderr,
}

impl StdioPrinter {
    pub fn new(which: StreamKind) -> StdioPrinter {
        StdioPrinter { which }
    }
}

impl Printer for StdioPrinter {
    fn print(&mut self, output: &str) {
        match self.which {
            StreamKind::Stdout => println! ("{}", output),
            StreamKind::Stderr => eprintln!("{}", output),
        }
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

/// The Rustics trait is the main interface for collecting
/// and querying statistics.

pub trait Rustics {
    /// Records an i64 sample, if allowed by the implementation.
    /// Time-based statistics do not support this method.

    fn record_i64(&mut self, sample: i64);

    /// Records an f64 value.

    fn record_f64(&mut self, sample: f64);

    /// Records an event.  This method is implementation-specific
    /// in meaning.  For the Counter type, it is is equivalent to
    /// record_i64(1).
    ///
    /// The other integer types, e.g., RunningInteger, do not support this call.
    ///
    /// For time statistics, it reads the internal timer (provided by the user
    /// to the constructer) for the instance to determine the time interval in
    /// ticks.  The timer is restarted for the next record_event call.
    ///
    /// The record_event_report is the same as record_event, but it
    /// returns the value that is recorded.  This is used by the Hier
    /// code to implement its window function.

    fn record_event       (&mut self);         // implementation-specific record
    fn record_event_report(&mut self) -> i64;  // implementation-specific record

    /// Records a time in ticks.  This method will panic if the
    /// underlying type is not a time statistic.

    fn record_time(&mut self, sample: i64);  // add a time sample

    /// Records a time interval by reading the given TimerBox instance.
    /// This method will panic if the underlying type is not a
    /// time statistic.

    fn record_interval(&mut self, timer: &mut TimerBox);
                                             // Add a time sample ending now

    /// Returns the name passed on instance creation.

    fn name(&self) -> String;

    /// Returns the default title used for printing.  The Rc and ArcSet
    /// implementation create hierarchical titles for members of the set.
    /// This function can be used to retrieve them.

    fn title(&self)-> String;

    /// Returns the class of the statistic.  Currently, "integer", "counter",
    /// "float", and "time" classes exist.

    fn class(&self) -> &str;

    /// Returns the count of samples used to create the summary statistics
    /// like the mean.

    fn count(&self) -> u64;

    /// Returns the most common pseudo-log seen in the data samples.  This
    /// method is supported only for integer and time types.

    fn log_mode(&self) -> isize;

    /// Returns the mean of the samples in the instance.

    fn mean(&self) -> f64;

    /// Returns the standard deviation of the samples in the instance.

    fn standard_deviation(&self) -> f64;

    /// Returns the variance of the samples in the instance.

    fn variance(&self) -> f64;

    /// Returns the skewness of the samples in the instance.

    fn skewness(&self) -> f64;

    /// Returns the kurtosis of the samples in the instance.

    fn kurtosis(&self) -> f64;

    /// Returns a boolean indicating whether the underlying type supports
    /// the min_i64() and max_i64() methods.

    fn int_extremes(&self) -> bool;

    /// Returns a boolean indicating whether the underlying type supports
    /// the min_f64() and max_f64() methods.

    fn float_extremes(&self) -> bool;

    /// Returns the minimum of the sample space for an integer
    /// or time type.  Time statistics return a value in ticks.

    fn min_i64(&self) -> i64;

    /// Returns the minimum of the sample space for an f64 type.

    fn min_f64(&self) -> f64;

    /// Returns the maximum of the sample space for an integer
    /// or time type.  Time statistics return a value in ticks.

    fn max_i64(&self) -> i64;

    /// Returns the maximum of the sample space for an f64 type.

    fn max_f64(&self) -> f64;

    /// Precomputes the summary data of the samples.  This is
    /// useful when implementing custom print functions or querying
    /// multiple summary statistics like the mean or skewness.
    /// The window-based Rustics types will cache the result of
    /// data analysis so it need not be redone each time a summary
    /// statistic is retrieved.

    fn precompute(&mut self);

    /// Clears the data in the instance.

    fn clear(&mut self);

    /// Returns the statistics for the sample stream.

    fn export_stats(&self) -> ExportStats;

    /// Prints the statistics with the default options.

    fn print     (&self);

    /// Prints the statistics with the printer and title as given.

    fn print_opts(&self, printer: PrinterOption, title: Option<&str>);

    fn set_title (&mut self, title: &str);

    /// Returns a LogHistogramBox for the histogram if possible.

    fn log_histogram  (&self) -> Option<LogHistogramBox>;

    /// Returns a FloatHistogramBox for the histogram if possible.

    fn float_histogram(&self) -> Option<FloatHistogramBox>;

    // For internal use.

    fn set_id (&mut self, id: usize      );
    fn id     (&self                     ) -> usize;
    fn equals (&self, other: &dyn Rustics) -> bool;
    fn generic(&self                     ) -> &dyn Any;
}

/// Defines the data available from the Rustics export_stats()
/// member, which returns bulk data.

pub struct ExportStats {
    pub printable:          Printable,
    pub log_histogram:      Option<LogHistogramBox>,
    pub float_histogram:    Option<FloatHistogramBox>,
}

/// The Histogram trait defines an interface for using a
/// LogHistogram or FloatHistogram instance.

pub trait Histogram {

    /// Prints the histogram on the given Printer instance.

    fn print_histogram(&self, printer: &mut dyn Printer);

    /// Clears the histogram data.

    fn clear_histogram(&mut self);

    /// Returns the corresponding LogHistogramBox if possible.

    fn to_log_histogram(&self) -> Option<LogHistogramBox>;

    /// Returns the corresponding FloatHistogramBox if possible.

    fn to_float_histogram(&self) -> Option<FloatHistogramBox>;
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::running_time::RunningTime;
    use crate::running_integer::RunningInteger;
    use crate::integer_window::IntegerWindow;
    use crate::time_window::TimeWindow;
    use crate::printable::Printable;
    use crate::log_histogram::pseudo_log_index;

    // This function is shared.

    pub fn bytes() -> Option<Units> {
        let singular = "byte".to_string();
        let plural   = "bytes".to_string();

        Some(Units { singular, plural })
    }

    pub fn compute_sum(histogram: &LogHistogram) -> i64 {
        let mut sum = 0;

        for sample in histogram.positive.iter() {
            sum += *sample;
        }

        for sample in histogram.negative.iter() {
            sum += *sample;
        }

        sum as i64
    }

    pub struct CheckPrinter {
        expected:         Vec<String>,
        current:          usize,
        fail_on_overage:  bool,
        verbose:          bool,
    }

    impl CheckPrinter {
        pub fn new(expected: &[&str], fail_on_overage: bool, verbose: bool) -> CheckPrinter {
            let expected: Vec<String> = expected.iter().map(|x| (*x).into()).collect();
            let current  = 0;

            CheckPrinter { expected, current, fail_on_overage, verbose }
        }

        // Check whether the line count was an exact match.

        fn count_match(&self) -> bool {
            self.current == self.expected.len()
        }

        // Get the current count of lines seen and the total expected.

        fn counters(&self) -> (usize, usize) {
            (self.current, self.expected.len())
        }
    }

    // Create a CheckPrinter and put it into a PrinterBox.

    pub fn check_printer_box(expected: &[&str], fail_on_overage: bool, verbose: bool) -> PrinterBox {
        let printer = CheckPrinter::new(expected, fail_on_overage, verbose);

        printer_box!(printer)
    }

    // Check that the number of lines seen and the number of lines
    // expected are a match.

    pub fn check_printer_count_match(printer_box: PrinterBox) -> bool {
        let printer      = printer!(printer_box);
        let printer_any  = printer.as_any();
        let printer_impl = printer_any.downcast_ref::<CheckPrinter>().unwrap();

        printer_impl.count_match()
    }

    pub fn check_printer_counters(printer_box: PrinterBox) -> (usize, usize) {
        let printer      = printer!(printer_box);
        let printer_any  = printer.as_any();
        let printer_impl = printer_any.downcast_ref::<CheckPrinter>().unwrap();

        printer_impl.counters()
    }

    impl Printer for CheckPrinter {
        fn print(&mut self, output: &str) {
            if self.current < self.expected.len() {
                let expected = self.expected[self.current].clone();
                let pass     = expected == *output;

                if !pass || self.verbose {
                    println!("CheckPrinter:");
                    println!("    got      \"{}\"", output);
                    println!("    expected \"{}\"", expected);
                }

                assert!(pass);
            } else if self.fail_on_overage {
                panic!("CheckPrinter:  too many lines");
            } else {
                if self.verbose {
                    println!(" *** CheckPrinter: ignoring extra lines");
                }
            }

            // Count all lines seen, including the excess.

            self.current += 1;

            println!("{}", output);
        }

        fn as_any(&self) -> &dyn Any {
            self
        }

        fn as_any_mut(&mut self) -> &mut dyn Any {
            self
        }
    }

    // Define a testing clock that allows us to define the
    // intervals that the clock returns.  We do this through
    // a global variable, although creating a list might be
    // more rust-like.

    #[derive(Clone, Copy)]
    pub struct TestTimer {
        started: bool,
        ticks:   i64,
        hz:      u128,
    }

    impl TestTimer {
        pub fn new(hz: u128) -> TestTimer {
            let started = false;
            let ticks   = 0;

            TestTimer { started, ticks, hz }
        }

        pub fn new_box(hz: u128) -> Rc<RefCell<TestTimer>> {
            let timer = TestTimer::new(hz);

            timer_box!(timer)
        }

        pub fn setup_elapsed_time(&mut self, ticks: i64) {
            assert!(ticks >= 0);

            self.started = true;
            self.ticks   = ticks;
        }
    }

    impl Timer for TestTimer {
        fn start(&mut self) {
            assert!(self.started);
        }

        fn finish(&mut self) -> i64 {
            assert!(self.started);
            assert!(self.ticks >= 0);

            self.started = false;
            self.ticks
        }

        fn hz(&self) -> u128 {
            self.hz
        }
    }

    // We need dynamic conversion.

    pub trait TestTimerTrait {
        fn setup(&mut self, ticks: i64);
    }

    impl TestTimerTrait for TestTimer {
        fn setup(&mut self, ticks: i64) {
            self.setup_elapsed_time(ticks);
        }
    }

    pub trait ConverterTrait : Timer + TestTimerTrait {
        fn as_timer(it: Rc<RefCell<Self>>) -> Rc<RefCell<dyn Timer>>;
        fn as_test_timer(it: Rc<RefCell<Self>>) -> Rc<RefCell<dyn TestTimerTrait>>;
    }

    impl ConverterTrait for TestTimer {
        fn as_timer(it: Rc<RefCell<Self>>) -> Rc<RefCell<dyn Timer>> {
            it
        }

        fn as_test_timer(it: Rc<RefCell<Self>>) -> Rc<RefCell<dyn TestTimerTrait>> {
            it
        }
    }

    fn test_test_timer() {
        let hz    = 1_000_000_000;
        let both  = TestTimer::new_box(hz);
        let setup = ConverterTrait::as_test_timer(both.clone());
        let value = ConverterTrait::as_timer(both.clone());

        for i in 1..=100 {
            timer_mut!(setup).setup(i);
            assert!(timer_mut!(value).finish() == i);
        }
    }

    // Define a simple timer for testing that just starts at 1000 ticks
    // and then counts up by 1000 ticks for each event interval.  This
    // type is used by other test modules.

    pub fn continuing_timer_increment() -> i64 {
        1000
    }

    pub fn continuing_box() -> TimerBox {
        let hz = 1_000_000_000;

        Rc::from(RefCell::new(ContinuingTimer::new(hz)))
    }

    pub struct ContinuingTimer {
        time:       i64,
        increment:  i64,
        hz:         u128,
    }

    impl ContinuingTimer {
        pub fn new(hz: u128) -> ContinuingTimer {
            let time      = 0;
            let increment = continuing_timer_increment();

            ContinuingTimer { time, increment, hz }
        }
    }

    impl Timer for ContinuingTimer {
        fn start(&mut self) {
            self.time = 0;
        }

        fn finish(&mut self) -> i64 {
            self.time += self.increment;
            self.time
        }

        fn hz(&self) -> u128 {
            self.hz
        }
    }

    pub fn setup_elapsed_time(timer: &mut Rc<RefCell<TestTimer>>, ticks: i64) {
        let timer = timer_mut!(timer);

        timer.setup_elapsed_time(ticks);
    }

    // Set up the next interval to be returned.

    pub fn test_running_time() {
        println!("Testing running time statistics.");

        let     hz         = 1_000_000_000;
        let     both       = TestTimer::new_box(hz);
        let     test_timer = ConverterTrait::as_test_timer(both.clone());
        let mut stat_timer = ConverterTrait::as_timer(both.clone());
        let mut time_stat  = RunningTime::new("Test Running Time 1", stat_timer.clone(), &None);

        timer_mut!(test_timer).setup(i64::MAX);
        time_stat.record_event();

        assert!(time_stat.min_i64() == i64::MAX);
        assert!(time_stat.max_i64() == i64::MAX);

        timer_mut!(test_timer).setup(0);
        time_stat.record_event();

        assert!(time_stat.min_i64() == 0);
        assert!(time_stat.max_i64() == i64::MAX);

        // Try some small integers.

        time_stat.clear();

        for i in 0..=100 {
            timer_mut!(test_timer).setup(i);
            time_stat.record_event();
            assert!(time_stat.max_i64() == i as i64);
        }

        println!("test_running_time:  first stats added.");
        time_stat.print();

        println!("test_running_time:  first print done.");

        // Okay, use a more restricted range of times.

        let mut time_stat = RunningTime::new("Test Running Time 2", stat_timer.clone(), &None);

        let limit = 99;

        for i in 0..=limit {
            let interval = i * i * i;
            timer_mut!(test_timer).setup(interval);

            // Test both record_event and record_interval.

            if i & 1 != 0 {
                time_stat.record_event();
            } else {
                time_stat.record_interval(&mut stat_timer);
            }
        }

        assert!(time_stat.min_i64() == 0);
        assert!(time_stat.max_i64() == limit * limit * limit);

        time_stat.print();

        // Get a sample with easily calculated summary statistics.

        let mut time_stat = RunningTime::new("Test Time => 1..100", stat_timer.clone(), &None);

        for i in 1..=100 {
            timer_mut!(test_timer).setup(i);
            time_stat.record_event();

            assert!(time_stat.max_i64() == i);
        }

        time_stat.print();

        // Cover all the scales.

        let mut time_stat = RunningTime::new("Time => Scale", stat_timer.clone(), &None);

        let mut time    = 1;
        let     printer = stdout_printer();
        let     printer = printer_mut!(printer);


        for i in 1..=16 {
            let elapsed = i * 100;

            timer_mut!(test_timer).setup(elapsed);

            if i & 1 != 0 {
                time_stat.record_event();
            } else {
                time_stat.record_interval(&mut stat_timer);
            }

            assert!(time_stat.max_i64() == elapsed);

            let header = format!("{}", Printable::commas_i64(time));
            Printable::print_time(&header, time as f64, hz as i64, printer);

            time *= 10;
        }

        time_stat.print();
    }

    fn test_time_printing() {
        let hz = 1_000_000_000;

        let ns     =    1_u64;
        let us     = 1000 * ns;
        let ms     = 1000 * us;
        let second = 1000 * ms;
        let minute =   60 * second;
        let hour   =   60 * minute;
        let day    =   24 * hour;

        let values =
            [
                  1 * ns,
                 10 * ns,
                100 * ns,
                  1 * us,
                 10 * us,
                100 * us,
                  1 * ms,
                 10 * ms,
                100 * ms,
                  1 * second,
                 10 * second,
                  1 * minute,
                 16 * minute,
                  2 * hour,
                  1 * day,
                  2 * day,

               1175 * day    / 1000,
               1001 * second / 1000
            ];

        let expected_output =
            [
                  "    >                   1.000 nanosecond",
                  "    >                  10.000 nanoseconds",
                  "    >                 100.000 nanoseconds",
                  "    >                   1.000 microsecond",
                  "    >                  10.000 microseconds",
                  "    >                 100.000 microseconds",
                  "    >                   1.000 millisecond",
                  "    >                  10.000 milliseconds",
                  "    >                 100.000 milliseconds",
                  "    >                   1.000 second",
                  "    >                  10.000 seconds",
                  "    >                   1.000 minute",
                  "    >                  16.000 minutes",
                  "    >                   2.000 hours",
                  "    >                   1.000 day",
                  "    >                   2.000 days",
                  "    >                   1.175 days",
                  "    >                   1.001 seconds"
            ];

        let mut check_printer = CheckPrinter::new(&expected_output, true, false);

        for i in 0..values.len() {
            println!("test_time_printing:  value {}, expect {}", values[i], expected_output[i]);
            Printable::print_time(">", values[i] as f64, hz, &mut check_printer);
        }
    }

    fn test_time_window() {
        println!("Testing time windows.");

        let     hz        = 1_000_000_000;
        let     both       = TestTimer::new_box(hz);
        let     test_timer = ConverterTrait::as_test_timer(both.clone());
        let     stat_timer = ConverterTrait::as_timer(both.clone());

        let mut time_stat =
            TimeWindow::new("Test Time Window 1", 50, stat_timer.clone(), &None);

        assert!(time_stat.class() == "time");

        timer_mut!(test_timer).setup(i64::MAX);
        time_stat.record_event();

        assert!(time_stat.min_i64() == i64::MAX);
        assert!(time_stat.max_i64() == i64::MAX);

        timer_mut!(test_timer).setup(0);
        time_stat.record_event();

        assert!(time_stat.min_i64() == 0);
        assert!(time_stat.max_i64() == i64::MAX);

        // Try some fairly small integers.

        let multiplier = 100;

        time_stat.clear();

        for i in 1..100 {
            timer_mut!(test_timer).setup(i as i64 * multiplier);
            time_stat.record_event();
            assert!(time_stat.max_i64() == i * multiplier);
        }

        time_stat.print();

        // Okay, use a more restricted range of times.

        let mut time_stat = RunningTime::new("Test Time Window 2", stat_timer.clone(), &None);

        assert!(time_stat.class() == "time");

        let limit = 99;

        for i in 0..=limit {
            let interval = i * i * i;

            timer_mut!(test_timer).setup(interval);
            time_stat.record_event();
        }

        assert!(time_stat.min_i64() == 0);
        assert!(time_stat.max_i64() == limit * limit * limit);

        time_stat.print();

        // Get a sample with easily calculated summary statistics.

        let mut time_stat = RunningTime::new("Time Window => 1..=count", stat_timer.clone(), &None);

        let count = 100;

        for i in 1..=count {
            timer_mut!(test_timer).setup(i);
            time_stat.record_event();

            assert!(time_stat.max_i64() == i);
        }

        let float_count = time_stat.count() as f64;
        let sum         = float_count * (float_count + 1.0) / 2.0;
        let mean        = sum / float_count;

        assert!(time_stat.count() == count as u64);
        assert!(time_stat.mean () == mean        );

        time_stat.print();

        // Cover all the scales.

        let mut timer     = TestTimer::new_box(hz);
        let mut time_stat = RunningTime::new("Time => Scale", timer.clone(), &None);

        let mut time    = 1;
        let     printer = &mut StdioPrinter::new(StreamKind::Stdout);

        for _i in 1..16 {
            setup_elapsed_time(&mut timer, time);
            time_stat.record_event();
            let header = format!("{} => ", Printable::commas_i64(time));
            Printable::print_time(&header, time as f64, hz as i64, printer);

            time *= 10;
        }

        time_stat.print();
    }

    fn run_all_histo_tests() {
        let     timer           = continuing_box();
        let mut running_integer = RunningInteger::new("RunningInteger",                     &None);
        let mut running_time    = RunningTime::new   ("RunningTime",    timer.clone(),      &None);
        let mut integer_window  = IntegerWindow::new ("IntegerWindow",  100,                &None);
        let mut time_window     = TimeWindow::new    ("TimeWindow",     100, timer.clone(), &None);

        run_histogram_tests(&mut running_integer);
        run_histogram_tests(&mut running_time   );
        run_histogram_tests(&mut integer_window );
        run_histogram_tests(&mut time_window    );
    }

    // This test is used by other modules.

    pub fn run_histogram_tests(rustics: &mut dyn Rustics) {
        let values = [ -300, 0, 300, 1300, 2300, i64::MIN, i64::MAX ];

        for value in values {
            test_histogram(rustics, value);
        }
    }

    // Put some samples into the instance, then check the
    // contents.

    fn test_histogram(rustics: &mut dyn Rustics, mut value: i64) {
        rustics.clear();

        assert!(rustics.count() == 0);

        // Check the histogram...  We need to lose the borrow before
        // calling the record_* functions.

        {
            let histogram = rustics.log_histogram().unwrap();
            let histogram = histogram.borrow();

            for item in histogram.negative {
                assert!(item == 0);
            }

            for item in histogram.positive {
                assert!(item == 0);
            }
        }

        // Time instances only get positive values...  Avoid overflow
        // when negating and adding.  Consider MAX and MIN...

        if rustics.class() == "time" && value <= 0 {
            value = (value + 2).abs() + 1;
        }

        // Record the values and count the events.

        let mut events = 0;

        for _i in 0..100 {
            if rustics.class() == "time" {
                rustics.record_time(value);
            } else {
                rustics.record_i64(value);
            }

            events += 1;
        }

        // Check that the data seems sane.

        assert!(rustics.standard_deviation() == 0.0);

        assert!(rustics.mean()     == value as f64);
        assert!(rustics.variance() == 0.0);
        assert!(rustics.skewness() == 0.0);
        assert!(rustics.kurtosis() == 0.0);

        // Check that the histogram matches expectation.
        let histogram       = rustics.log_histogram().unwrap();
        let histogram       = histogram.borrow();
        let log_mode_index  = pseudo_log_index(value);

        for i in 0..histogram.negative.len() {
            if value < 0 && i == log_mode_index {
                assert!(histogram.negative[i] == events);
            } else {
                assert!(histogram.negative[i] == 0);
            }
        }

        for i in 0..histogram.positive.len() {
            if value >= 0 && i == log_mode_index {
                assert!(histogram.positive[i] == events);
            } else {
                assert!(histogram.positive[i] == 0);
            }
        }
    }

    fn test_float_functions() {
        let value = 1.0;

        let mantissa = to_mantissa(value);
        assert!(mantissa == 0);

        let value = max_biased_exponent();
        assert!(value == 2046);

        let value = -0.0;
        assert!(is_zero(value));

        let value =  0.0;
        assert!(is_zero(value));

        // Test NaN arguments.

        let value  = f64::NAN;
        let value2 = 1.0;

        assert!(biased_exponent(value) == 0);

        let result = min_f64(value, value2);
        assert!(result.is_nan());

        let result = max_f64(value, value2);
        assert!(result.is_nan());

        let value = f64::INFINITY;
        assert!(biased_exponent(value) == max_exponent());

        let value = f64::from_bits(56 as u64);
        assert!(value.is_subnormal());
        assert!(biased_exponent(value) == min_exponent() + exponent_bias());
    }

    fn test_make_title() {
        let title  = "";
        let name   = "hello";
        let result = make_title(title, name);

        assert!(result == name);

        let title  = "say";
        let result = make_title(title, name);

        assert!(result == "say ==> hello");
    }

    fn test_units() {
        let singular = "byte";
        let plural   = "bytes";
        let result   = Units::new(singular, plural);

        assert!(result.singular == "byte" );
        assert!(result.plural   == "bytes");
    }

    fn test_parsing() {
        let printer      = Some(stdout_printer());
        let title        = Some("Title".to_string());
        let merge_min    = 24;
        let merge_max    = 28;
        let no_zero_rows = true;
        let histo_opts   = Some(HistoOpts { merge_min, merge_max, no_zero_rows });
        let units        = bytes();

        let print_opts = Some(PrintOpts { printer, title, histo_opts, units });

        let _     = parse_printer   (&print_opts);
        let title = parse_title     (&print_opts, "default");
        let histo = parse_histo_opts(&print_opts);
        let units = parse_units     (&print_opts);

        assert!( histo.no_zero_rows          );
        assert!( histo.merge_min == merge_min);
        assert!( histo.merge_max == merge_max);
        assert!( units.singular  == "byte"   );
        assert!( units.plural    == "bytes"  );
        assert!( title           == "Title"  );

        let printer    = None;
        let title      = None;
        let histo_opts = None;
        let units      = None;
        let print_opts = Some(PrintOpts { printer, title, histo_opts, units });

        let _          = parse_printer   (&print_opts);
        let title      = parse_title     (&print_opts, "default");
        let histo      = parse_histo_opts(&print_opts);
        let units      = parse_units     (&print_opts);

        assert!(!histo.no_zero_rows          );
        assert!( histo.merge_min == 0        );
        assert!( histo.merge_max == 0        );
        assert!( units.singular  == ""       );
        assert!( units.plural    == ""       );
        assert!( title           == "default");
    }

    fn test_stdio_printer() {
        let mut printer = StdioPrinter::new(StreamKind::Stderr);

        printer.print("test_stdio_printer:  performed output");
    }

    fn test_check_printer_forgive() {
        let expected_output = [ "test_check_printer:  output" ];

        let mut printer = CheckPrinter::new(&expected_output, false, false);

        printer.print(expected_output[0]);
        printer.print(expected_output[0]);
    }

    #[test]
    #[should_panic]
    fn test_check_printer() {
        let expected_output = [ "test_check_printer:  output" ];

        let mut printer = CheckPrinter::new(&expected_output, true, false);

        printer.print(expected_output[0]);
        printer.print(expected_output[0]);
    }

    #[test]
    #[should_panic]
    fn test_timer_start() {
        let hz    = 1_000;
        let timer = TestTimer::new_box(hz);

        timer_mut!(timer).start();
    }

    fn test_verbose_check_printer() {
        let     expected = [ "Line 1", "Line 2" ];
        let mut printer  = CheckPrinter::new(&expected, false, true);

        printer.print("Line 1");
        printer.print("Line 2");
        printer.print("Line 3");
    }

    fn test_test_timer_setup () {
        let hz    = 1_000;
        let timer = TestTimer::new_box(hz);

        timer_mut!(timer).setup_elapsed_time(hz as i64);
        timer_mut!(timer).start();

        assert!(timer_mut!(timer).finish() == hz as i64);
    }

    fn test_math() {
        assert!(compute_kurtosis(4,  0.0, 0.0) == 0.0);
        assert!(compute_kurtosis(4,  1.0, 0.0) == 0.0);
        assert!(compute_kurtosis(4, -1.0, 0.0) == 0.0);
        assert!(compute_skewness(4,  0.0, 0.0) == 0.0);
        assert!(compute_skewness(4, -1.0, 0.0) == 0.0);
    }

    fn test_printers() {
        let stdout_box = stdout_printer();
        let stdout     = printer_mut!(stdout_box);

        {
            let stdout_any = stdout.as_any();
            let _          = stdout_any.downcast_ref::<StdioPrinter>().unwrap();
        }

        {
            let stdout_any_mut = stdout.as_any_mut();
            let _              = stdout_any_mut.downcast_mut::<StdioPrinter>().unwrap();
        }

        let expect = [ ];

        let check_box = check_printer_box(&expect, true, true);
        let check     = printer_mut!(check_box);

        {
            let check_any = check.as_any();
            let _          = check_any.downcast_ref::<CheckPrinter>().unwrap();
        }

        {
            let check_any_mut = check.as_any_mut();
            let _             = check_any_mut.downcast_mut::<CheckPrinter>().unwrap();
        }

        let check_box = check_printer_box(&expect, true, true);
        let check     = printer_mut!(check_box);

        {
            let check_any = check.as_any();
            let _          = check_any.downcast_ref::<CheckPrinter>().unwrap();
        }

        {
            let check_any_mut = check.as_any_mut();
            let _             = check_any_mut.downcast_mut::<CheckPrinter>().unwrap();
        }

    }

    #[test]
    pub fn run_lib_tests() {
        test_time_printing        ();
        test_time_window          ();
        test_running_time         ();
        run_all_histo_tests       ();
        test_test_timer           ();
        test_test_timer_setup     ();
        test_float_functions      ();
        test_make_title           ();
        test_units                ();
        test_parsing              ();
        test_stdio_printer        ();
        test_check_printer_forgive();
        test_math                 ();
        test_verbose_check_printer();
        test_printers             ();
    }
}