From: Simon McVittie <smcv@collabora.com>
Date: Tue, 7 Feb 2023 21:22:29 +0000
Subject: Fix unit tests on big-endian systems

Manual backport of https://github.com/py-sdl/py-sdl2/pull/232 adjusted
to the 0.9.9 codebase.

Co-authored-by: Austin Hurst
Origin: backport, 0.9.12, commit:38ba051a39b86ccd2c5a95125c8d7cfea3ddb691
Bug: https://github.com/py-sdl/py-sdl2/issues/227
Bug-Debian: https://bugs.debian.org/1030676
---
 sdl2/test/pixels_test.py              | 50 ++++++++++-------------------------
 sdl2/test/sdl2ext_array_test.py       | 20 +++++++++-----
 sdl2/test/sdl2ext_draw_test.py        |  9 ++++---
 sdl2/test/sdl2ext_pixelaccess_test.py |  2 +-
 4 files changed, 34 insertions(+), 47 deletions(-)

diff --git a/sdl2/test/pixels_test.py b/sdl2/test/pixels_test.py
index 4ba014c..72af40e 100644
--- a/sdl2/test/pixels_test.py
+++ b/sdl2/test/pixels_test.py
@@ -160,31 +160,17 @@ class TestSDLPixels(object):
             assert pixels.SDL_GetPixelFormatName(fmt) == name
 
     def test_SDL_MasksToPixelFormatEnum(self):
-        if sys.byteorder == "little":
-            val = pixels.SDL_MasksToPixelFormatEnum(32,
-                                                    0xFF000000,
-                                                    0x00FF0000,
-                                                    0x0000FF00,
-                                                    0x000000FF)
-        else:
-            val = pixels.SDL_MasksToPixelFormatEnum(32,
-                                                   0x000000FF,
-                                                   0x0000FF00,
-                                                   0x00FF0000,
-                                                   0xFF000000)
+        val = pixels.SDL_MasksToPixelFormatEnum(32,
+                                                0xFF000000,
+                                                0x00FF0000,
+                                                0x0000FF00,
+                                                0x000000FF)
         assert val == pixels.SDL_PIXELFORMAT_RGBA8888
-        if sys.byteorder == "little":
-            val = pixels.SDL_MasksToPixelFormatEnum(32,
-                                                   0xFF000000,
-                                                   0x00FF0000,
-                                                   0x0000FF00,
-                                                   0)
-        else:
-            val = pixels.SDL_MasksToPixelFormatEnum(32,
-                                                   0,
-                                                   0x000000FF,
-                                                   0x0000FF00,
-                                                   0x00FF0000)
+        val = pixels.SDL_MasksToPixelFormatEnum(32,
+                                                0xFF000000,
+                                                0x00FF0000,
+                                                0x0000FF00,
+                                                0)
         assert val == pixels.SDL_PIXELFORMAT_RGBX8888
         val = pixels.SDL_MasksToPixelFormatEnum(1, 0, 0, 0, 0)
         assert val == pixels.SDL_PIXELFORMAT_INDEX1MSB  # not LSB
@@ -210,22 +196,14 @@ class TestSDLPixels(object):
         pixels.SDL_PixelFormatEnumToMasks(pixels.SDL_PIXELFORMAT_RGBA8888,
                                           byref(bpp), byref(r), byref(g),
                                           byref(b), byref(a))
-        if sys.byteorder == "little":
-            assert (bpp.value, r.value, g.value, b.value, a.value) == \
-                   (32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF)
-        else:
-            assert (bpp.value, r.value, g.value, b.value, a.value) == \
-                   (32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000)
+        assert (bpp.value, r.value, g.value, b.value, a.value) == \
+               (32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF)
         pixels.SDL_PixelFormatEnumToMasks(pixels.SDL_PIXELFORMAT_RGBX8888,
                                           byref(bpp), byref(r),
                                           byref(g), byref(b),
                                           byref(a))
-        if sys.byteorder == "little":
-            assert (bpp.value, r.value, g.value, b.value, a.value) == \
-                   (32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0)
-        else:
-            assert (bpp.value, r.value, g.value, b.value, a.value) == \
-                   (32, 0, 0x0000FF00, 0x00FF0000, 0xFF000000)
+        assert (bpp.value, r.value, g.value, b.value, a.value) == \
+               (32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0)
         # self.assertRaises(sdl.SDLError, pixels.SDL_PixelFormatEnumToMasks,
         #                   99999)
 
diff --git a/sdl2/test/sdl2ext_array_test.py b/sdl2/test/sdl2ext_array_test.py
index 1c902e3..780c8e7 100644
--- a/sdl2/test/sdl2ext_array_test.py
+++ b/sdl2/test/sdl2ext_array_test.py
@@ -5,6 +5,8 @@ import struct
 import pytest
 from sdl2.ext import array as sdlextarray
 
+BIG_ENDIAN = sys.byteorder == "big"
+
 singlebyteseq = [x for x in range(0x100)]
 doublebyteseq = [x for x in range(0x10000)]
 quadbyteseq = [0x00000000,
@@ -94,7 +96,7 @@ def create_64b(seq, size, offset):
                     seq[offset + 2] << 16 |
                     seq[offset + 3])
         elif size == 4:
-            return (seq[offset] << 32 | seq[offset])
+            return (seq[offset] << 32 | seq[offset + 1])
 
 
 def lobyte16(val):
@@ -191,12 +193,14 @@ class TestSDL2ExtArray(object):
             offset = 0
             cnt = 0
             for val in singlebytes.to_bytes():
+                hi = hibyte16(doublebyteseq[offset])
+                lo = lobyte16(doublebyteseq[offset])
                 if cnt > 0:
-                    assert val == hibyte16(doublebyteseq[offset])
+                    assert val == (lo if BIG_ENDIAN else hi)
                     cnt = 0
                     offset += 1
                 else:
-                    assert val == lobyte16(doublebyteseq[offset])
+                    assert val == (hi if BIG_ENDIAN else lo)
                     cnt += 1
 
             offset = 0
@@ -236,12 +240,14 @@ class TestSDL2ExtArray(object):
             cnt = 0
             offset = 0
             for val in singlebytes.to_uint16():
+                hi = hibytes32(quadbyteseq[offset])
+                lo = lobytes32(quadbyteseq[offset])
                 if cnt > 0:
-                    assert val == hibytes32(quadbyteseq[offset])
+                    assert val == (lo if BIG_ENDIAN else hi)
                     cnt = 0
                     offset += 1
                 else:
-                    assert val == lobytes32(quadbyteseq[offset])
+                    assert val == (hi if BIG_ENDIAN else lo)
                     cnt += 1
 
             offset = 0
@@ -356,9 +362,9 @@ class TestSDL2ExtArray(object):
             if i == 1:
                 assert parr[0] == 0x0
             elif i == 2:
-                assert parr[0] == 0x0100
+                assert parr[0] == struct.unpack("=H", barr[0:2])[0]
             elif i == 4:
-                assert parr[0] == 0x03020100
+                assert parr[0] == struct.unpack("=I", barr[0:4])[0]
         for i in (0, 3, 5, 6, 7, 9, 10, 12, "test", self):
             with pytest.raises(TypeError):
                 sdlextarray.create_array(barr, i)
diff --git a/sdl2/test/sdl2ext_draw_test.py b/sdl2/test/sdl2ext_draw_test.py
index ac11bc8..2fc3304 100644
--- a/sdl2/test/sdl2ext_draw_test.py
+++ b/sdl2/test/sdl2ext_draw_test.py
@@ -1,6 +1,7 @@
 import sys
 import pytest
-from sdl2.surface import SDL_CreateRGBSurface
+import sdl2.pixels
+from sdl2.surface import SDL_CreateRGBSurface, SDL_CreateRGBSurfaceWithFormat
 from sdl2.rect import SDL_Rect
 from sdl2.ext.color import Color, COLOR
 from sdl2.ext.compat import ExperimentalWarning
@@ -31,7 +32,8 @@ class TestSDL2ExtDraw(object):
         # Initialize colour and surface/view
         WHITE = (255, 255, 255)
         BLACK = (0, 0, 0)
-        sf = SDL_CreateRGBSurface(0, 10, 10, 32, 0, 0, 0, 0)
+        sf = SDL_CreateRGBSurfaceWithFormat(0, 10, 10, 32,
+                                            sdl2.pixels.SDL_PIXELFORMAT_RGBA32)
         with pytest.warns(ExperimentalWarning):
             view = sdl2ext.pixels3d(sf.contents, False)
 
@@ -74,7 +76,8 @@ class TestSDL2ExtDraw(object):
         # Initialize colour and surface/view
         WHITE = (255, 255, 255)
         BLACK = (0, 0, 0)
-        sf = SDL_CreateRGBSurface(0, 10, 10, 32, 0, 0, 0, 0)
+        sf = SDL_CreateRGBSurfaceWithFormat(0, 10, 10, 32,
+                                            sdl2.pixels.SDL_PIXELFORMAT_RGBA32)
         with pytest.warns(ExperimentalWarning):
             view = sdl2ext.pixels3d(sf.contents, False)
 
diff --git a/sdl2/test/sdl2ext_pixelaccess_test.py b/sdl2/test/sdl2ext_pixelaccess_test.py
index 7e3dea8..cd15f39 100644
--- a/sdl2/test/sdl2ext_pixelaccess_test.py
+++ b/sdl2/test/sdl2ext_pixelaccess_test.py
@@ -78,7 +78,7 @@ class TestSDL2ExtPixelAccess(object):
             'black': color.Color(0, 0, 0, 255),
             'white': color.Color(255, 255, 255, 255)
         }
-        rgba = pixels.SDL_PIXELFORMAT_ABGR8888
+        rgba = pixels.SDL_PIXELFORMAT_RGBA32
         # Import test image, convert to RGBA, and open pixels3d view
         imgsurf = surface.SDL_LoadBMP(self.testfile.encode("utf-8"))
         imgsurf = surface.SDL_ConvertSurfaceFormat(imgsurf.contents, rgba, 0)
