<commit_before>// The MIT License (MIT)
//
// Copyright (c) 2015-2020 <NAME> (github.com/kean).

import XCTest
@testable import Nuke

#if !os(macOS)
    import UIKit
#endif

class ImageProcessorsResizeTests: XCTestCase {

    func testThatImageIsResizedToFill() throws {
        // Given
        let processor = ImageProcessors.Resize(size: CGSize(width: 400, height: 400), unit: .pixels, contentMode: .aspectFill)

        // When
        let output = try XCTUnwrap(processor.process(Test.image), "Failed to process an image")

        // Then
        XCTAssertEqual(output.sizeInPixels, CGSize(width: 533, height: 400))
    }

    func testThatImageIsntUpscaledByDefault() throws {
        // Given
        let processor = ImageProcessors.Resize(size: CGSize(width: 960, height: 960), unit: .pixels, contentMode: .aspectFill)

        // When
        let output = try XCTUnwrap(processor.process(Test.image), "Failed to process an image")

        // Then
        XCTAssertEqual(output.sizeInPixels, CGSize(width: 640, height: 480))
    }

    func testResizeToFitHeight() throws {
        // Given
        let processor = ImageProcessors.Resize(height: 300, unit: .pixels)

        // When
        let output = try XCTUnwrap(processor.process(Test.image), "Failed to process an image")

        // Then
        XCTAssertEqual(output.sizeInPixels, CGSize(width: 400, height: 300))
    }

    func testResizeToFitWidth() throws {
        // Given
        let processor = ImageProcessors.Resize(width: 400, unit: .pixels)

        // When
        let output = try XCTUnwrap(processor.process(Test.image), "Failed to process an image")

        // Then
        XCTAssertEqual(output.sizeInPixels, CGSize(width: 400, height: 300))
    }

    func testThatImageIsUpscaledIfOptionIsEnabled() throws {
        // Given
        let processor = ImageProcessors.Resize(size: CGSize(width: 960, height: 960), unit: .pixels, contentMode: .aspectFill, upscale: true)

        // When
        let output = try XCTUnwrap(processor.process(Test.image), "Failed to process an image")

        // Then
        XCTAssertEqual(output.sizeInPixels, CGSize(width: 1280, height: 960))
    }

    func testThatContentModeCanBeChangeToAspectFit() throws {
        // Given
        let processor = ImageProcessors.Resize(size: CGSize(width: 480, height: 480), unit: .pixels, contentMode: .aspectFit)

        // When
        let output = try XCTUnwrap(processor.process(Test.image), "Failed to process an image")

        // Then
        XCTAssertEqual(output.sizeInPixels, CGSize(width: 480, height: 360))
    }

    func testThatImageIsCropped() throws {
        // Given
        let processor = ImageProcessors.Resize(size: CGSize(width: 400, height: 400), unit: .pixels, crop: true)

        // When
        let output = try XCTUnwrap(processor.process(Test.image), "Failed to process an image")

        // Then
        XCTAssertEqual(output.sizeInPixels, CGSize(width: 400, height: 400))
    }

    func testThatImageIsntCroppedWithAspectFitMode() throws {
        // Given
        let processor = ImageProcessors.Resize(size: CGSize(width: 480, height: 480), unit: .pixels, contentMode: .aspectFit, crop: true)

        // When
        let output = try XCTUnwrap(processor.process(Test.image), "Failed to process an image")

        // Then image is resized but isn't cropped
        XCTAssertEqual(output.sizeInPixels, CGSize(width: 480, height: 360))
    }

    func testExtendedColorSpaceSupport() throws {
        // Given
        let processor = ImageProcessors.Resize(size: CGSize(width: 480, height: 480), unit: .pixels, contentMode: .aspectFit, crop: true)

        // When
        let output = try XCTUnwrap(processor.process(Test.image(named: "image-p3", extension: "jpg")), "Failed to process an image")

        // Then image is resized but isn't cropped
        XCTAssertEqual(output.sizeInPixels, CGSize(width: 480, height: 320))
        let colorSpace = try XCTUnwrap(output.cgImage?.colorSpace)
        XCTAssertTrue(colorSpace.isWideGamutRGB)
    }

    #if os(macOS)
    func testResizeImageWithOrientationLeft() throws {
        // Given an image with `left` orientation. From the user perspective,
        // the image a landscape image with s size 640x480px. The raw pixel
        // data, on the other hand, is 480x640px. macOS, however, automatically
        // changes image orientaiton to `up` so that you don't have to worry about it
        let input = try XCTUnwrap(Test.image(named: "left-orientation.jpeg"))

        // When we resize the image to fit 320x480px frame, we expect the processor
        // to take image orientation into the account and produce a 320x240px.
        let processor = ImageProcessors.Resize(size: CGSize(width: 320, height: 1000), unit: .pixels, contentMode: .aspectFit)
        let output = try XCTUnwrap(processor.process(input), "Failed to process an image")

        // Then the image orientation is still `.left`
        XCTAssertEqual(output.sizeInPixels, CGSize(width: 320, height: 240))

        // Then the image is resized according to orientation
        XCTAssertEqual(output.size, CGSize(width: 320 / Screen.scale, height: 240 / Screen.scale))
    }
    #endif

    #if os(iOS) || os(tvOS) || os(watchOS)
    func testResizeImageWithOrientationLeft() throws {
        // Given an image with `left` orientation. From the user perspective,
        // the image a landscape image with s size 640x480px. The raw pixel
        // data, on the other hand, is 480x640px.
        let input = try XCTUnwrap(Test.image(named: "left-orientation.jpeg"))
        XCTAssertEqual(input.imageOrientation, .right)

        // When we resize the image to fit 320x480px frame, we expect the processor
        // to take image orientation into the account and produce a 320x240px.
        let processor = ImageProcessors.Resize(size: CGSize(width: 320, height: 1000), unit: .pixels, contentMode: .aspectFit)
        let output = try XCTUnwrap(processor.process(input), "Failed to process an image")

        // Then the image orientation is still `.left`
        XCTAssertEqual(output.sizeInPixels, CGSize(width: 240, height: 320))
        XCTAssertEqual(output.imageOrientation, .right)
        // Then the image is resized according to orientation
        XCTAssertEqual(output.size, CGSize(width: 320 / Screen.scale, height: 240 / Screen.scale))
    }
    #endif

    #if os(macOS)

    #endif<commit_msg>Add resize and crop test<commit_after>// The MIT License (MIT)
//
// Copyright (c) 2015-2020 <NAME> (github.com/kean).

import XCTest
@testable import Nuke

#if !os(macOS)
    import UIKit
#endif

class ImageProcessorsResizeTests: XCTestCase {

    func testThatImageIsResizedToFill() throws {
        // Given
        let processor = ImageProcessors.Resize(size: CGSize(width: 400, height: 400), unit: .pixels, contentMode: .aspectFill)

        // When
        let output = try XCTUnwrap(processor.process(Test.image), "Failed to process an image")

        // Then
        XCTAssertEqual(output.sizeInPixels, CGSize(width: 533, height: 400))
    }

    func testThatImageIsntUpscaledByDefault() throws {
        // Given
        let processor = ImageProcessors.Resize(size: CGSize(width: 960, height: 960), unit: .pixels, contentMode: .aspectFill)

        // When
        let output = try XCTUnwrap(processor.process(Test.image), "Failed to process an image")

        // Then
        XCTAssertEqual(output.sizeInPixels, CGSize(width: 640, height: 480))
    }

    func testResizeToFitHeight() throws {
        // Given
        let processor = ImageProcessors.Resize(height: 300, unit: .pixels)

        // When
        let output = try XCTUnwrap(processor.process(Test.image), "Failed to process an image")

        // Then
        XCTAssertEqual(output.sizeInPixels, CGSize(width: 400, height: 300))
    }

    func testResizeToFitWidth() throws {
        // Given
        let processor = ImageProcessors.Resize(width: 400, unit: .pixels)

        // When
        let output = try XCTUnwrap(processor.process(Test.image), "Failed to process an image")

        // Then
        XCTAssertEqual(output.sizeInPixels, CGSize(width: 400, height: 300))
    }

    func testThatImageIsUpscaledIfOptionIsEnabled() throws {
        // Given
        let processor = ImageProcessors.Resize(size: CGSize(width: 960, height: 960), unit: .pixels, contentMode: .aspectFill, upscale: true)

        // When
        let output = try XCTUnwrap(processor.process(Test.image), "Failed to process an image")

        // Then
        XCTAssertEqual(output.sizeInPixels, CGSize(width: 1280, height: 960))
    }

    func testThatContentModeCanBeChangeToAspectFit() throws {
        // Given
        let processor = ImageProcessors.Resize(size: CGSize(width: 480, height: 480), unit: .pixels, contentMode: .aspectFit)

        // When
        let output = try XCTUnwrap(processor.process(Test.image), "Failed to process an image")

        // Then
        XCTAssertEqual(output.sizeInPixels, CGSize(width: 480, height: 360))
    }

    func testThatImageIsCropped() throws {
        // Given
        let processor = ImageProcessors.Resize(size: CGSize(width: 400, height: 400), unit: .pixels, crop: true)

        // When
        let output = try XCTUnwrap(processor.process(Test.image), "Failed to process an image")

        // Then
        XCTAssertEqual(output.sizeInPixels, CGSize(width: 400, height: 400))
    }

    func testThatImageIsntCroppedWithAspectFitMode() throws {
        // Given
        let processor = ImageProcessors.Resize(size: CGSize(width: 480, height: 480), unit: .pixels, contentMode: .aspectFit, crop: true)

        // When
        let output = try XCTUnwrap(processor.process(Test.image), "Failed to process an image")

        // Then image is resized but isn't cropped
        XCTAssertEqual(output.sizeInPixels, CGSize(width: 480, height: 360))
    }

    func testExtendedColorSpaceSupport() throws {
        // Given
        let processor = ImageProcessors.Resize(size: CGSize(width: 480, height: 480), unit: .pixels, contentMode: .aspectFit, crop: true)

        // When
        let output = try XCTUnwrap(processor.process(Test.image(named: "image-p3", extension: "jpg")), "Failed to process an image")

        // Then image is resized but isn't cropped
        XCTAssertEqual(output.sizeInPixels, CGSize(width: 480, height: 320))
        let colorSpace = try XCTUnwrap(output.cgImage?.colorSpace)
        XCTAssertTrue(colorSpace.isWideGamutRGB)
    }

    #if os(macOS)
    func testResizeImageWithOrientationLeft() throws {
        // Given an image with `left` orientation. From the user perspective,
        // the image a landscape image with s size 640x480px. The raw pixel
        // data, on the other hand, is 480x640px. macOS, however, automatically
        // changes image orientaiton to `up` so that you don't have to worry about it
        let input = try XCTUnwrap(Test.image(named: "left-orientation.jpeg"))

        // When we resize the image to fit 320x480px frame, we expect the processor
        // to take image orientation into the account and produce a 320x240px.
        let processor = ImageProcessors.Resize(size: CGSize(width: 320, height: 1000), unit: .pixels, contentMode: .aspectFit)
        let output = try XCTUnwrap(processor.process(input), "Failed to process an image")

        // Then the image orientation is still `.left`
        XCTAssertEqual(output.sizeInPixels, CGSize(width: 320, height: 240))

        // Then the image is resized according to orientation
        XCTAssertEqual(output.size, CGSize(width: 320 / Screen.scale, height: 240 / Screen.scale))
    }
    #endif

    #if os(iOS) || os(tvOS) || os(watchOS)
    func testResizeImageWithOrientationLeft() throws {
        // Given an image with `left` orientation. From the user perspective,
        // the image a landscape image with s size 640x480px. The raw pixel
        // data, on the other hand, is 480x640px.
        let input = try XCTUnwrap(Test.image(named: "left-orientation.jpeg"))
        XCTAssertEqual(input.imageOrientation, .right)

        // When we resize the image to fit 320x480px frame, we expect the processor
        // to take image orientation into the account and produce a 320x240px.
        let processor = ImageProcessors.Resize(size: CGSize(width: 320, height: 1000), unit: .pixels, contentMode: .aspectFit)
        let output = try XCTUnwrap(processor.process(input), "Failed to process an image")

        // Then the image orientation is still `.left`
        XCTAssertEqual(output.sizeInPixels, CGSize(width: 240, height: 320))
        XCTAssertEqual(output.imageOrientation, .right)
        // Then the image is resized according to orientation
        XCTAssertEqual(output.size, CGSize(width: 320 / Screen.scale, height: 240 / Screen.scale))
    }

    func testResizeAndCropWithOrientationLeft() throws {
        // Given an image with `left` orientation. From the user perspective,
        // the image a landscape image with s size 640x480px. The raw pixel
        // data, on the other hand, is 480x640px.
        let input = try XCTUnwrap(Test.image(named: "left-orientation.jpeg"))
        XCTAssertEqual(input.imageOrientation, .right)

        // When we resize the image to fit 320x480px frame, we expect the processor
        // to take image orientation into the account and produce a 320x240px.
        let processor = ImageProcessors.Resize(size: CGSize(width: 320, height: 320), unit: .pixels, contentMode: .aspectFill, crop: true)
        let output = try XCTUnwrap(processor.process(input), "Failed to process an image")

        // Then the image orientation is still `.left`
        XCTAssertEqual(output.sizeInPixels, CGSize(width: 320, height: 320))
        XCTAssertEqual(output.imageOrientation, .right)
        // Then the image is resized according to orientation
        XCTAssertEqual(output.size, CGSize(width: 320 / Screen.scale, height: 320 / Screen.scale))
    }
    #endif

    #if os(macOS)

    #endif
{'fraction_non_alphanumeric': 0.10106837606837607, 'fraction_numerical': 0.031196581196581197, 'mean_word_length': 2.86378646119978, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '403389', 'n_tokens_mistral': 4591, 'n_tokens_neox': 4041, 'n_words': 1249}