Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions Sources/Prometheus/MetricTypes/Histogram.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,7 @@ public struct Buckets: ExpressibleByArrayLiteral {
/// - count: Amount of buckets to generate, should be larger than zero. The +Inf bucket is not included in this count.
public static func linear(start: Double, width: Double, count: Int) -> Buckets {
assert(count >= 1, "Bucket.linear needs a count larger than 1")
var arr = [Double]()
var s = start
for x in 0..<count {
arr[x] = s
s += width
}
let arr = (0..<count).map { Double(start) + Double($0) * Double(width) }
return Buckets(arr)
}

Expand All @@ -60,8 +55,8 @@ public struct Buckets: ExpressibleByArrayLiteral {
assert(factor > 1, "Bucket.exponential needs a factor larger than 1")
var arr = [Double]()
var s = start
for x in 0..<count {
arr[x] = s
for _ in 0..<count {
arr.append(s)
s *= factor
}
return Buckets(arr)
Expand Down
13 changes: 13 additions & 0 deletions Tests/SwiftPrometheusTests/BucketsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import XCTest
import Prometheus
class BucketsTests: XCTestCase {
func testExponentialDoesNotThrow() {
let buckets = Buckets.exponential(start: 1, factor: 2, count: 4)
XCTAssertEqual([1.0, 2.0, 4.0, 8.0, Double.greatestFiniteMagnitude], buckets.buckets)
}

func testLinearDoesNotThrow() {
let buckets = Buckets.linear(start: 1, width: 20, count: 4)
XCTAssertEqual([1, 21, 41, 61, Double.greatestFiniteMagnitude], buckets.buckets)
}
}