Files

100 lines
4.8 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2026 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
@testable import IMAPToolLib
import Testing
@Suite
private enum HistogramTests {
@Test
static func emptyDurations() {
#expect(TextHistogram(durations: []) == nil)
}
@Test
static func durationsBars() {
#expect(
TextHistogram(durations: durations)?.textOutputLines == [
"110ms-140ms: █55.9%██████████████████████████████▍ ",
"140ms-170ms: ███████████████████▉ 30.6%",
"170ms-200ms: █████▎ 8.06%",
"200ms-230ms: ██▌ 3.76%",
"230ms-260ms: 0%",
"260ms-290ms: ▊ 1.08%",
]
)
}
@Test
static func durationFormatting() {
#expect(formatDuration(0.00123456) == "1.23ms")
#expect(formatDuration(0.0123456) == "12.3ms")
#expect(formatDuration(0.123456) == "123ms")
#expect(formatDuration(1.23456) == "1,230ms")
#expect(formatDuration(12.3456) == "12.3s")
#expect(formatDuration(123.456) == "123s")
#expect(formatDuration(1234.56) == "1,230s")
}
/// A width too narrow to hold the labels must not trap; every bin still
/// produces a line.
@Test(arguments: [1, 0, -10, 20])
static func narrowWidthDoesNotCrash(width: Int) {
let histogram = TextHistogram(durations: durations, width: width)
#expect(histogram != nil)
#expect(histogram?.textOutputLines.count == 6)
}
}
private let durations: [Double] = [
0.122171192, 0.123395652, 0.123472153, 0.13781253, 0.144689425,
0.158754769, 0.135024804, 0.212556388, 0.119193779, 0.134820369,
0.127815519, 0.124780539, 0.150910074, 0.120827119, 0.150364741,
0.146966822, 0.123306126, 0.140330586, 0.153496039, 0.129829528,
0.274138931, 0.137818234, 0.119060215, 0.125635767, 0.118335712,
0.12496699, 0.126019394, 0.133717599, 0.118377887, 0.123200242,
0.120214432, 0.128805262, 0.141605264, 0.147688018, 0.13665841,
0.118342992, 0.13340833, 0.164123679, 0.143677784, 0.138495189,
0.171459794, 0.185034615, 0.163257631, 0.125976305, 0.135628424,
0.132502991, 0.1505422, 0.160112694, 0.185119389, 0.138207083,
0.126176955, 0.138213057, 0.134598795, 0.177951131, 0.128130949,
0.172570313, 0.124905424, 0.141619087, 0.207915823, 0.131152969,
0.123139228, 0.175255743, 0.15831435, 0.14910411, 0.159537588,
0.193938038, 0.125374246, 0.139930923, 0.140384119, 0.129001231,
0.126245557, 0.131497164, 0.137858935, 0.121978086, 0.138980462,
0.154026367, 0.125193752, 0.176967353, 0.145071349, 0.13641629,
0.131520032, 0.126133394, 0.126545686, 0.13611576, 0.122739261,
0.134888492, 0.179227416, 0.147208528, 0.125008901, 0.12108777,
0.137417394, 0.14340435, 0.167365776, 0.139132545, 0.165451138,
0.166845733, 0.13760426, 0.139901412, 0.140568276, 0.163856659,
0.145537728, 0.152495003, 0.147586944, 0.125992611, 0.119146645,
0.14098255, 0.128252514, 0.12346929, 1.079886135, 0.122693624,
0.122775901, 0.131289177, 0.127224351, 0.128993312, 0.124280839,
0.128965948, 0.17204532, 0.20466414, 0.14229628, 0.131979797,
0.145716555, 0.227100914, 0.143878482, 0.131024928, 0.142743257,
0.155885018, 0.12318903, 0.144658231, 0.126166246, 0.163060934,
0.171971104, 0.14729245, 0.125241788, 0.191618277, 0.12446891,
0.129576867, 0.132809882, 0.147108319, 0.114849054, 0.206822199,
0.1257895, 0.1365188, 0.140638183, 0.123274213, 0.12684512,
0.166034035, 0.146449088, 0.126644715, 0.139560665, 0.138363492,
0.174758821, 0.131317809, 0.123551418, 0.165621598, 0.167744131,
0.11768365, 0.124945645, 0.119563897, 0.134626433, 0.166622316,
0.275837005, 0.143031372, 0.155217997, 0.130167568, 0.136592654,
0.144256404, 0.133093567, 0.193387005, 0.165363057, 0.14475142,
0.162050933, 0.131305375, 0.125124742, 0.136353538, 0.138129362,
0.132879413, 0.20659515, 0.229068123, 0.161760476, 0.128732902,
0.164451069, 0.138786147, 0.164313424, 0.176923405, 0.118360439,
0.163233585,
]