foxygit / fake-08-xbox360 Log in
commit 459957e646624b615914fedd8b698e1188798d52
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Mon Sep 14 11:19:36 2020 -0600
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Tue Sep 15 17:09:09 2020 -0600

    WIP to add screenshot verification tests
---
 source/picoluaapi.cpp                      |   9 ++
 source/picoluaapi.h                        |   3 +
 source/vm.cpp                              |   3 +
 test/Makefile                              |  18 ++-
 test/carts/cliptest.p8                     |  32 ++++++
 test/carts/pset00-test.p8                  |  18 +++
 test/carts/pset3pix.p8                     |  20 ++++
 test/carts/psetall.p8                      |  21 ++++
 test/carts/screenshots/cliptest_f01.png    | Bin 0 -> 539 bytes
 test/carts/screenshots/pset00-test_f01.png | Bin 0 -> 292 bytes
 test/carts/screenshots/pset3pix_f01.png    | Bin 0 -> 307 bytes
 test/carts/screenshots/psetall_f01.png     | Bin 0 -> 698 bytes
 test/endtoendtests.cpp                     | 172 +++++++++++++++++++++++++++++
 13 files changed, 291 insertions(+), 5 deletions(-)

diff --git a/source/picoluaapi.cpp b/source/picoluaapi.cpp
index 21d654d..a227b3d 100644
--- a/source/picoluaapi.cpp
+++ b/source/picoluaapi.cpp
@@ -625,6 +625,15 @@ int dset(lua_State *L) {
     return noop("dset");
 }

+int printh(lua_State *L) {
+    if (lua_isstring(L, 1)){
+        const char * str = "";
+        str = lua_tolstring(L, 1, nullptr);
+        printf(str);
+    }
+    return 0;
+}
+
 int listcarts(lua_State *L) {
     //get cart list from VM (who should get it from host)
     vector<string> carts = _vmForLuaApi->GetCartList();
diff --git a/source/picoluaapi.h b/source/picoluaapi.h
index 80d15f1..6c76ccf 100644
--- a/source/picoluaapi.h
+++ b/source/picoluaapi.h
@@ -80,6 +80,9 @@ int cartdata(lua_State *L);
 int dget(lua_State *L);
 int dset(lua_State *L);

+//
+int printh(lua_State *L);
+
 //file system/vm functions
 int listcarts(lua_State *L);
 int loadcart(lua_State *L);
diff --git a/source/vm.cpp b/source/vm.cpp
index da25672..2fac65d 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -178,6 +178,9 @@ bool Vm::loadCart(Cart* cart) {
     lua_register(_luaState, "dget", dget);
     lua_register(_luaState, "dset", dset);

+    //
+    lua_register(_luaState, "printh", printh);
+
     //system
     lua_register(_luaState, "__listcarts", listcarts);
     lua_register(_luaState, "__loadcart", loadcart);
diff --git a/test/Makefile b/test/Makefile
index cf65f6a..f0dbc75 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -8,7 +8,7 @@ all: runtests

 testrunner.a: testroot.o \
 	fontdatatests.o graphicstests.o audiotests.o cartpatchertests.o \
-	carttests.o
+	carttests.o endtoendtests.o
 	g++ -I$(CURDIR)/include -L$(CURDIR)/lib \
 	testroot.o \
 	fontdata.o fontdatatests.o \
@@ -16,6 +16,7 @@ testrunner.a: testroot.o \
 	Audio.o synth.o audiotests.o \
 	cartPatcher.o cartpatchertests.o \
 	cart.o carttests.o nooplogger.o lodepng.o emojiconversion.o filehelpers.o utf8-util.o \
+	vm.o endtoendtests.o picoluaapi.o Input.o \
 	-llua -ldl \
 	-o testrunner.a

@@ -25,7 +26,7 @@ testroot.o: testroot.cpp doctest.h
 fontdatatests.o: fontdatatests.cpp ../source/fontdata.cpp ../source/fontdata.h
 	g++ $(CFLAGS) fontdatatests.cpp ../source/fontdata.cpp

-graphicstests.o: graphicstests.cpp ../source/graphics.cpp ../source/graphics.h \
+graphicstests.o: fontdatatests.o graphicstests.cpp ../source/graphics.cpp ../source/graphics.h \
 	../source/fontdata.cpp ../source/fontdata.h \
 	../source/PicoRam.h ../source/hostVmShared.h \
 	../source/stringToDataHelpers.h ../source/stringToDataHelpers.cpp
@@ -38,10 +39,8 @@ audiotests.o: audiotests.cpp ../source/Audio.cpp ../source/Audio.h \

 cartpatchertests.o: liblua.a cartpatchertests.cpp ../source/cartPatcher.cpp ../source/cartPatcher.h
 	g++ $(CFLAGS) -I$(CURDIR)/include -L$(CURDIR)/lib \
-	cartpatchertests.cpp ../source/cartPatcher.cpp \
-	-llua -ldl
+	cartpatchertests.cpp ../source/cartPatcher.cpp

-
 liblua.a:
 	@echo 'starting lua build\n' &&\
 	cd ../libs/lua-5.3.2 && make linux && \
@@ -66,6 +65,15 @@ carttests.o: carttests.cpp ../source/cart.cpp ../source/cart.h \
 	nooplogger.cpp ../source/emojiconversion.cpp ../source/filehelpers.cpp \
 	../libs/utf8-util/utf8-util.cpp

+endtoendtests.o: cartpatchertests.o carttests.o graphicstests.o audiotests.o \
+	endtoendtests.cpp \
+	../source/vm.cpp ../source/vm.h \
+	../source/Input.cpp ../source/Input.h \
+	../source/picoluaapi.cpp ../source/picoluaapi.h
+	g++ $(CFLAGS) -I$(CURDIR)/include -L$(CURDIR)/lib \
+	endtoendtests.cpp ../source/vm.cpp  ../source/picoluaapi.cpp ../source/Input.cpp
+
+

 runtests: testrunner.a
 	./testrunner.a
diff --git a/test/carts/cliptest.p8 b/test/carts/cliptest.p8
new file mode 100644
index 0000000..e473602
--- /dev/null
+++ b/test/carts/cliptest.p8
@@ -0,0 +1,32 @@
+pico-8 cartridge // http://www.pico-8.com
+version 29
+__lua__
+function _update()
+
+end
+
+function _draw()
+ cls()
+ printh("made it to lua draw call\n")
+ camera(-64, 0)
+ clip(0, 8, 128, 112)
+
+ -- these circles are clipped to (0-127,8-119)
+circfill(10, 10, 8, 8)
+circfill(10, 118, 8, 8)
+
+-- reset the clipping region to full screen
+clip()
+
+-- these circles are not clipped
+circfill(40, 10, 8, 7)
+circfill(40, 118, 8, 7)
+end
+
+__gfx__
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
diff --git a/test/carts/pset00-test.p8 b/test/carts/pset00-test.p8
new file mode 100644
index 0000000..1ee15f3
--- /dev/null
+++ b/test/carts/pset00-test.p8
@@ -0,0 +1,18 @@
+pico-8 cartridge // http://www.pico-8.com
+version 29
+__lua__
+function _update()
+
+end
+
+function _draw()
+	cls()
+	pset(0,0,9)
+end
+__gfx__
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
diff --git a/test/carts/pset3pix.p8 b/test/carts/pset3pix.p8
new file mode 100644
index 0000000..b623bfb
--- /dev/null
+++ b/test/carts/pset3pix.p8
@@ -0,0 +1,20 @@
+pico-8 cartridge // http://www.pico-8.com
+version 29
+__lua__
+function _update()
+
+end
+
+function _draw()
+	cls()
+	pset(0,0,9)
+	pset(0,1,10)
+	pset(1,0,11)
+end
+__gfx__
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
diff --git a/test/carts/psetall.p8 b/test/carts/psetall.p8
new file mode 100644
index 0000000..2ce170d
--- /dev/null
+++ b/test/carts/psetall.p8
@@ -0,0 +1,21 @@
+pico-8 cartridge // http://www.pico-8.com
+version 29
+__lua__
+function _update()
+
+end
+
+function _draw()
+	for x=0,128 do
+	 for y=0,128 do
+	  pset(x, y, (x+y)%16)
+	 end
+	end
+end
+__gfx__
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
diff --git a/test/carts/screenshots/cliptest_f01.png b/test/carts/screenshots/cliptest_f01.png
new file mode 100644
index 0000000..20e0adc
Binary files /dev/null and b/test/carts/screenshots/cliptest_f01.png differ
diff --git a/test/carts/screenshots/pset00-test_f01.png b/test/carts/screenshots/pset00-test_f01.png
new file mode 100644
index 0000000..d4c4606
Binary files /dev/null and b/test/carts/screenshots/pset00-test_f01.png differ
diff --git a/test/carts/screenshots/pset3pix_f01.png b/test/carts/screenshots/pset3pix_f01.png
new file mode 100644
index 0000000..e49b1c3
Binary files /dev/null and b/test/carts/screenshots/pset3pix_f01.png differ
diff --git a/test/carts/screenshots/psetall_f01.png b/test/carts/screenshots/psetall_f01.png
new file mode 100644
index 0000000..a77b2b9
Binary files /dev/null and b/test/carts/screenshots/psetall_f01.png differ
diff --git a/test/endtoendtests.cpp b/test/endtoendtests.cpp
new file mode 100644
index 0000000..1552dad
--- /dev/null
+++ b/test/endtoendtests.cpp
@@ -0,0 +1,172 @@
+#include <string>
+#include <vector>
+
+#include "doctest.h"
+#include "../libs/lodepng/lodepng.h"
+
+#include "../source/vm.h"
+#include "../source/hostVmShared.h"
+
+
+bool verifyScreenshot(Vm* vm, std::string screenshotFilename) {
+    std::vector<unsigned char> png;
+    std::vector<unsigned char> image; //the raw pixels
+    unsigned width, height;
+
+    //load and decode
+    unsigned error = lodepng::load_file(png, screenshotFilename);
+    if(!error) {
+        error = lodepng::decode(image, width, height, png);
+    }
+    if (error) {
+        CHECK_MESSAGE(error == 0, "Unable to decode screenshot png");
+        return false;
+    }
+
+    bool pixelsMatch = true;
+
+    uint8_t* picoFb = vm->GetPicoInteralFb();
+	uint8_t* screenPaletteMap = vm->GetScreenPaletteMap();
+	Color* paletteColors = vm->GetPaletteColors();
+
+    size_t imageBytes = image.size();
+
+    printf("Dimensions: %dx%d, size: %d\n", width, height, (int)imageBytes);
+
+    for(size_t i = 0; i < imageBytes; i += 4) {
+        //get argb values
+        uint8_t r = image[i];
+        uint8_t g = image[i + 1];
+        uint8_t b = image[i + 2];
+        //ignore alpha
+        //uint8_t a = image[i + 3];
+
+        //pico 8 screenshots don't have pure black for some reason?
+        bool black = r == 2 && g == 4 && b == 8;
+
+        if (black) {
+            continue;
+        }
+
+        /*
+        if (!black && i < 128 * 4) {
+            int x = (i / 4) % 128;
+            int y = (i / 4) / 128;
+            printf("Non-black pixel at idx %d (%d,%d): rgb: %d,%d,%d\n", i, x, y, r, g, b);
+        }
+        */
+        uint8_t pixIdx = i / 4;
+        int x = pixIdx % 128;
+        int y = pixIdx / 128;
+        uint8_t c = picoFb[(x * 128) + y];
+        Color col = paletteColors[screenPaletteMap[c]];
+
+        bool pixelMatches = r == col.Red && g == col.Green && b == col.Blue;
+
+        if (!pixelMatches && pixIdx < 512) {
+
+            printf("Non-matching pixel at idx %d (%d,%d): rgb: %d,%d,%d\n", pixIdx, x, y, r, g, b);
+
+            printf("fake 08 color is %d rgb: %d,%d,%d\n", c, col.Red, col.Green, col.Blue);
+        }
+
+        pixelsMatch &= pixelMatches;
+    }
+
+    return pixelsMatch;
+
+}
+
+TEST_CASE("Loading and running carts") {
+    Vm* vm = new Vm();
+
+    SUBCASE("Load simple cart"){
+        vm->LoadCart("carts/cartparsetest.p8");
+
+        SUBCASE("No error reported"){
+            CHECK(vm->GetBiosError() == "");
+        }
+        SUBCASE("Frame count updated with each UpdateAndDrawCall()")
+        {
+            vm->UpdateAndDraw(0, 0);
+            vm->UpdateAndDraw(0, 0);
+            vm->UpdateAndDraw(0, 0);
+
+            CHECK(vm->GetFrameCount() == 3);
+        }
+
+        vm->CloseCart();
+    }
+    SUBCASE("pset 0,0 test cart renders correctly"){
+        vm->LoadCart("carts/pset00-test.p8");
+
+        SUBCASE("No error reported"){
+            CHECK(vm->GetBiosError() == "");
+        }
+        SUBCASE("sceen matches screenshot")
+        {
+            printf("e2e test calling update and draw\n");
+            vm->UpdateAndDraw(0, 0);
+            printf("done calling update and draw\n");
+
+            CHECK(verifyScreenshot(vm, "carts/screenshots/pset00-test_f01.png"));
+        }
+
+        vm->CloseCart();
+    }
+    SUBCASE("pset 3 pix top left test cart renders correctly"){
+        vm->LoadCart("carts/pset3pix.p8");
+
+        SUBCASE("No error reported"){
+            CHECK(vm->GetBiosError() == "");
+        }
+        SUBCASE("sceen matches screenshot")
+        {
+            printf("e2e test calling update and draw\n");
+            vm->UpdateAndDraw(0, 0);
+            printf("done calling update and draw\n");
+
+            CHECK(verifyScreenshot(vm, "carts/screenshots/pset3pix_f01.png"));
+        }
+
+        vm->CloseCart();
+    }
+    /*
+    SUBCASE("pset all pixels test cart renders correctly"){
+        vm->LoadCart("carts/psetall.p8");
+
+        SUBCASE("No error reported"){
+            CHECK(vm->GetBiosError() == "");
+        }
+        SUBCASE("sceen matches screenshot")
+        {
+            printf("e2e test calling update and draw\n");
+            vm->UpdateAndDraw(0, 0);
+            printf("done calling update and draw\n");
+
+            CHECK(verifyScreenshot(vm, "carts/screenshots/psetall_f01.png"));
+        }
+
+        vm->CloseCart();
+    }
+    SUBCASE("Clip test cart renders correctly"){
+        vm->LoadCart("carts/cliptest.p8");
+
+        SUBCASE("No error reported"){
+            CHECK(vm->GetBiosError() == "");
+        }
+        SUBCASE("sceen matches screenshot")
+        {
+            printf("e2e test calling update and draw\n");
+            vm->UpdateAndDraw(0, 0);
+            printf("done calling update and draw\n");
+
+            CHECK(verifyScreenshot(vm, "carts/screenshots/cliptest_f01.png"));
+        }
+
+        vm->CloseCart();
+    }
+    */
+
+    delete vm;
+}
\ No newline at end of file