summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Wiederhake <BenWiederhake.GitHub@gmx.de>2023-01-01 16:55:58 +0100
committerTim Flynn <trflynn89@pm.me>2023-01-02 16:19:35 -0500
commit60ee69528715df921b3a37493e38b5783eb16395 (patch)
tree4a0c132337cf55aa957d3e306355aa5177124d69
parente0ab7763da348198894e3cb544079931373fcaf2 (diff)
downloadserenity-60ee69528715df921b3a37493e38b5783eb16395.zip
AK+Tests: Demonstrate slowness of years_to_days_since_epoch
In practice, this function does not take any perceptible amount of time. However, this benchmark demonstrates that for extreme values, the internal for-loop does matter.
-rw-r--r--Tests/AK/TestTime.cpp135
1 files changed, 135 insertions, 0 deletions
diff --git a/Tests/AK/TestTime.cpp b/Tests/AK/TestTime.cpp
index 76bd9ce36d..e124d98052 100644
--- a/Tests/AK/TestTime.cpp
+++ b/Tests/AK/TestTime.cpp
@@ -274,3 +274,138 @@ TEST_CASE(is_negative)
EXPECT_EQ(result.to_nanoseconds(), 5);
EXPECT(!result.is_negative());
}
+
+struct YearAndDays {
+ int year;
+ int days;
+};
+
+TEST_CASE(years_to_days_since_epoch_points)
+{
+ Array<YearAndDays, 22> test_data = { {
+ { 1969, -365 },
+ { 1970, 0 },
+ { 1971, 365 },
+ { 1900, -25567 },
+ { 2023, 19358 },
+ { 1800, -62091 },
+ { 2100, 47482 },
+ { 0, -719528 },
+ { -1, -719893 },
+ { -2, -720258 },
+ { -3, -720623 },
+ { -4, -720989 },
+ { -5, -721354 },
+ { -6, -721719 },
+ { 4000, 741442 },
+ { -10000, -4371953 },
+ { 10000, 2932897 },
+ { -1000000, -365962028 },
+ { 1000000, 364522972 },
+ { -5877640, -2147483456 },
+ { 5881474, 2147444740 },
+ // Very important year: https://github.com/SerenityOS/serenity/pull/16760#issuecomment-1369054745
+ { -999999, -365961662 },
+ } };
+ for (auto entry : test_data) {
+ int year = entry.year;
+ int expected_days = entry.days;
+ int actual_days = years_to_days_since_epoch(year);
+ EXPECT_EQ(actual_days, expected_days);
+ }
+}
+
+BENCHMARK_CASE(years_to_days_since_epoch_benchmark)
+{
+ // This benchmark takes consistently about 295±1 ms on Linux, and roughly 2300 ms on Serenity.
+ // TODO: Computing the amount of days should never take dozens of milliseconds.
+ for (size_t i = 0; i < 100; ++i) {
+ int actual_days = years_to_days_since_epoch(-5877640);
+ (void)actual_days;
+ EXPECT_EQ(actual_days, -2147483456);
+ }
+}
+
+TEST_CASE(years_to_days_since_epoch_span)
+{
+ auto test_data_start_year = 1900;
+ // Data was pre-computed with a slow, but known-correct implementation.
+ // clang-format off
+ auto test_data = Array {
+ -25567, -25202, -24837, -24472, -24107, -23741, -23376, -23011,
+ -22646, -22280, -21915, -21550, -21185, -20819, -20454, -20089,
+ -19724, -19358, -18993, -18628, -18263, -17897, -17532, -17167,
+ -16802, -16436, -16071, -15706, -15341, -14975, -14610, -14245,
+ -13880, -13514, -13149, -12784, -12419, -12053, -11688, -11323,
+ -10958, -10592, -10227, -9862, -9497, -9131, -8766, -8401, -8036,
+ -7670, -7305, -6940, -6575, -6209, -5844, -5479, -5114, -4748, -4383,
+ -4018, -3653, -3287, -2922, -2557, -2192, -1826, -1461, -1096, -731,
+ -365, 0, 365, 730, 1096, 1461, 1826, 2191, 2557, 2922, 3287, 3652,
+ 4018, 4383, 4748, 5113, 5479, 5844, 6209, 6574, 6940, 7305, 7670,
+ 8035, 8401, 8766, 9131, 9496, 9862, 10227, 10592, 10957, 11323, 11688,
+ 12053, 12418, 12784, 13149, 13514, 13879, 14245, 14610, 14975, 15340,
+ 15706, 16071, 16436, 16801, 17167, 17532, 17897, 18262, 18628, 18993,
+ 19358, 19723, 20089, 20454, 20819, 21184, 21550, 21915, 22280, 22645,
+ 23011, 23376, 23741, 24106, 24472, 24837, 25202, 25567, 25933, 26298,
+ 26663, 27028, 27394, 27759, 28124, 28489, 28855, 29220, 29585, 29950,
+ 30316, 30681, 31046, 31411, 31777, 32142, 32507, 32872, 33238, 33603,
+ 33968, 34333, 34699, 35064, 35429, 35794, 36160, 36525, 36890, 37255,
+ 37621, 37986, 38351, 38716, 39082, 39447, 39812, 40177, 40543, 40908,
+ 41273, 41638, 42004, 42369, 42734, 43099, 43465, 43830, 44195, 44560,
+ 44926, 45291, 45656, 46021, 46387, 46752, 47117, 47482, 47847, 48212,
+ 48577, 48942, 49308, 49673, 50038, 50403, 50769, 51134, 51499, 51864,
+ 52230, 52595, 52960, 53325, 53691, 54056, 54421, 54786, 55152, 55517,
+ 55882, 56247, 56613, 56978, 57343, 57708, 58074, 58439, 58804, 59169,
+ 59535, 59900, 60265, 60630, 60996, 61361, 61726, 62091, 62457, 62822,
+ 63187, 63552, 63918, 64283, 64648, 65013, 65379, 65744, 66109, 66474,
+ 66840, 67205, 67570, 67935, 68301, 68666, 69031, 69396, 69762, 70127,
+ 70492, 70857, 71223, 71588, 71953, 72318, 72684, 73049, 73414, 73779,
+ 74145, 74510, 74875, 75240, 75606, 75971, 76336, 76701, 77067, 77432,
+ 77797, 78162, 78528, 78893, 79258, 79623, 79989, 80354, 80719, 81084,
+ 81450, 81815, 82180, 82545, 82911, 83276, 83641, 84006, 84371, 84736,
+ 85101, 85466, 85832, 86197, 86562, 86927, 87293, 87658, 88023, 88388,
+ 88754, 89119, 89484, 89849, 90215, 90580, 90945, 91310, 91676, 92041,
+ 92406, 92771, 93137, 93502, 93867, 94232, 94598, 94963, 95328, 95693,
+ 96059, 96424, 96789, 97154, 97520, 97885, 98250, 98615, 98981, 99346,
+ 99711, 100076, 100442, 100807, 101172, 101537, 101903, 102268, 102633,
+ 102998, 103364, 103729, 104094, 104459, 104825, 105190, 105555,
+ 105920, 106286, 106651, 107016, 107381, 107747, 108112, 108477,
+ 108842, 109208, 109573, 109938, 110303, 110669, 111034, 111399,
+ 111764, 112130, 112495, 112860, 113225, 113591, 113956, 114321,
+ 114686, 115052, 115417, 115782, 116147, 116513, 116878, 117243,
+ 117608, 117974, 118339, 118704, 119069, 119435, 119800, 120165,
+ 120530, 120895, 121260, 121625, 121990, 122356, 122721, 123086,
+ 123451, 123817, 124182, 124547, 124912, 125278, 125643, 126008,
+ 126373, 126739, 127104, 127469, 127834, 128200, 128565, 128930,
+ 129295, 129661, 130026, 130391, 130756, 131122, 131487, 131852,
+ 132217, 132583, 132948, 133313, 133678, 134044, 134409, 134774,
+ 135139, 135505, 135870, 136235, 136600, 136966, 137331, 137696,
+ 138061, 138427, 138792, 139157, 139522, 139888, 140253, 140618,
+ 140983, 141349, 141714, 142079, 142444, 142810, 143175, 143540,
+ 143905, 144271, 144636, 145001, 145366, 145732, 146097, 146462,
+ 146827, 147193, 147558, 147923, 148288, 148654, 149019, 149384,
+ 149749, 150115, 150480, 150845, 151210, 151576, 151941, 152306,
+ 152671, 153037, 153402, 153767, 154132, 154498, 154863, 155228,
+ 155593, 155959, 156324, 156689, 157054, 157420, 157785, 158150,
+ 158515, 158881, 159246, 159611, 159976, 160342, 160707, 161072,
+ 161437, 161803, 162168, 162533, 162898, 163264, 163629, 163994,
+ 164359, 164725, 165090, 165455, 165820, 166186, 166551, 166916,
+ 167281, 167647, 168012, 168377, 168742, 169108, 169473, 169838,
+ 170203, 170569, 170934, 171299, 171664, 172030, 172395, 172760,
+ 173125, 173491, 173856, 174221, 174586, 174952, 175317, 175682,
+ 176047, 176413, 176778, 177143, 177508, 177874, 178239, 178604,
+ 178969, 179335, 179700, 180065, 180430, 180796, 181161, 181526,
+ 181891, 182257, 182622, 182987, 183352, 183718, 184083, 184448,
+ 184813, 185179, 185544, 185909, 186274, 186640, 187005, 187370,
+ 187735, 188101, 188466, 188831, 189196, 189562, 189927, 190292,
+ 190657, 191023, 191388, 191753, 192118, 192484, 192849, 193214,
+ 193579, 193944, 194309, 194674, 195039, 195405,
+ };
+ // clang-format on
+ for (size_t offset = 0; offset < test_data.size(); ++offset) {
+ int year = offset + test_data_start_year;
+ int expected_days = test_data[offset];
+ int actual_days = years_to_days_since_epoch(year);
+ EXPECT_EQ(actual_days, expected_days);
+ }
+}