@@ -208,6 +208,47 @@ final class BitmapTests: XCTestCase {
208208 )
209209 }
210210
211+ func testTransposed( ) {
212+ // Test square bitmap
213+ let squareBitmap = Bitmap ( width: 3 , height: 3 , data: [
214+ 1 , 2 , 3 , 255 , 4 , 5 , 6 , 255 , 7 , 8 , 9 , 255 ,
215+ 10 , 11 , 12 , 255 , 13 , 14 , 15 , 255 , 16 , 17 , 18 , 255 ,
216+ 19 , 20 , 21 , 255 , 22 , 23 , 24 , 255 , 25 , 26 , 27 , 255
217+ ] )
218+
219+ let squareTransposed = squareBitmap. transposed ( )
220+ let expectedSquare = Bitmap ( width: 3 , height: 3 , data: [
221+ 1 , 2 , 3 , 255 , 10 , 11 , 12 , 255 , 19 , 20 , 21 , 255 ,
222+ 4 , 5 , 6 , 255 , 13 , 14 , 15 , 255 , 22 , 23 , 24 , 255 ,
223+ 7 , 8 , 9 , 255 , 16 , 17 , 18 , 255 , 25 , 26 , 27 , 255
224+ ] )
225+ XCTAssertEqual ( squareTransposed, expectedSquare)
226+
227+ // Test rectangular bitmap
228+ let rectBitmap = Bitmap ( width: 2 , height: 3 , data: [
229+ 1 , 2 , 3 , 255 , 4 , 5 , 6 , 255 ,
230+ 7 , 8 , 9 , 255 , 10 , 11 , 12 , 255 ,
231+ 13 , 14 , 15 , 255 , 16 , 17 , 18 , 255
232+ ] )
233+
234+ let rectTransposed = rectBitmap. transposed ( )
235+ XCTAssertEqual ( rectTransposed. width, 3 )
236+ XCTAssertEqual ( rectTransposed. height, 2 )
237+ let expectedRect = Bitmap ( width: 3 , height: 2 , data: [
238+ 1 , 2 , 3 , 255 , 7 , 8 , 9 , 255 , 13 , 14 , 15 , 255 ,
239+ 4 , 5 , 6 , 255 , 10 , 11 , 12 , 255 , 16 , 17 , 18 , 255
240+ ] )
241+ XCTAssertEqual ( rectTransposed, expectedRect)
242+
243+ // Test that original bitmap is unchanged
244+ XCTAssertEqual ( rectBitmap. width, 2 )
245+ XCTAssertEqual ( rectBitmap. height, 3 )
246+
247+ // Test double transpose returns original
248+ let doubleTransposed = rectTransposed. transposed ( )
249+ XCTAssertEqual ( doubleTransposed, rectBitmap)
250+ }
251+
211252 func testSwap( ) {
212253 let width = 500 , height = 500
213254 let xRange = 0 ... width - 1 , yRange = 0 ... height - 1
0 commit comments