import XCTest @testable import Hapi final class ImageZoomMathTests: XCTestCase { func testScaleStaysWithinBounds() { XCTAssertEqual(ImageZoomMath.clampedScale(0.2), 1) XCTAssertEqual(ImageZoomMath.clampedScale(3), 3) XCTAssertEqual(ImageZoomMath.clampedScale(99), 8) } func testFittedSizeUsesTheTighterAxis() { let landscape = ImageZoomMath.fittedSize( for: CGSize(width: 400, height: 200), in: CGSize(width: 300, height: 300) ) XCTAssertEqual(landscape.width, 300) XCTAssertEqual(landscape.height, 150) let portrait = ImageZoomMath.fittedSize( for: CGSize(width: 200, height: 400), in: CGSize(width: 300, height: 300) ) XCTAssertEqual(portrait.width, 150) XCTAssertEqual(portrait.height, 300) } func testOffsetIsClampedToTheScaledImageEdges() { let image = CGSize(width: 400, height: 200) let container = CGSize(width: 300, height: 300) // At 1x the image fits horizontally, so horizontal panning is locked. XCTAssertEqual( ImageZoomMath.clampedOffset( CGSize(width: 200, height: 0), scale: 1, imageSize: image, container: container ), .zero ) // At 2x there is 150 pt of horizontal slack on each side. XCTAssertEqual( ImageZoomMath.clampedOffset( CGSize(width: 500, height: 500), scale: 2, imageSize: image, container: container ), CGSize(width: 150, height: 0) ) } func testDegenerateSizesDoNotProduceNaN() { XCTAssertEqual( ImageZoomMath.fittedSize(for: .zero, in: CGSize(width: 300, height: 300)), .zero ) XCTAssertEqual( ImageZoomMath.clampedOffset(.zero, scale: 2, imageSize: .zero, container: .zero), .zero ) } }