import Testing @testable import ParcelCore private func interval(origin: Double, extent: Double, gap: Double, count: Int, index: Int) -> (Double, Double) { let length = (extent - gap * Double(count - 1)) / Double(count) let start = origin + Double(index) * (length + gap) return (start, start + length) } private func expectedFrame(layout: Layout, count: Int, index: Int, area: Rect, gap: Double, ratio: Double) -> Rect { var horizontal = (area.minX, area.maxX) var vertical = (area.minY, area.maxY) switch layout { case .columns: horizontal = interval(origin: area.minX, extent: area.size.width, gap: gap, count: count, index: index) case .rows: vertical = interval(origin: area.minY, extent: area.size.height, gap: gap, count: count, index: index) case .side: if count > 1 { let main = (area.size.width - gap) * ratio if index == 0 { horizontal = (area.minX, area.minX + main) } else { horizontal = (area.minX + main + gap, area.maxX) vertical = interval( origin: area.minY, extent: area.size.height, gap: gap, count: count - 1, index: index - 1) } } case .grid: let columns = Int(Double(count).squareRoot().rounded(.up)) let rows = (count + columns - 1) / columns let row = index / columns let rowCount = min(columns, count - row * columns) horizontal = interval( origin: area.minX, extent: area.size.width, gap: gap, count: rowCount, index: index % columns) vertical = interval(origin: area.minY, extent: area.size.height, gap: gap, count: rows, index: row) case .tree, .bsp, .accordion: preconditionFailure("No expected pixel formula for \(layout)") } let left = horizontal.0.rounded() let top = vertical.0.rounded() return Rect(x: left, y: top, width: horizontal.1.rounded() - left, height: vertical.1.rounded() - top) } @Test(arguments: [Layout.grid, .columns, .rows, .side], 1...8) func exactPixelsWithGapsAndReservedEdges(layout: Layout, count: Int) { var space = layoutTree(count, layout: layout, gaps: Gaps(inner: 12, outer: Insets(8))) space.reservedEdges = Insets(top: 24, left: 10, bottom: 6, right: 20) let screen = Rect(x: -1200, y: -800, width: 1200, height: 800) space.adapt(to: screen) let result = LayoutEngine.arrange(space, in: screen) let area = Rect(x: -1182, y: -768, width: 1154, height: 754) let frames = result.frames for index in 0..= area.minX && frame.maxX <= area.maxX) #expect(frame.minY >= area.minY && frame.maxY <= area.maxY) #expect(frame.minX == frame.minX.rounded()) #expect(frame.size.width >= 0 && frame.size.height >= 0) } if count == 1 { #expect(frames[windowID(1)] == area) } if layout == .columns && count > 1 { for number in 1..