From 82c3f81d52bdbe42c2f58c930b81b3132b3fd6e2 Mon Sep 17 00:00:00 2001 From: Nunuhara Cabbage Date: Wed, 16 Oct 2019 20:16:19 -0700 Subject: [PATCH] Add rudimentary unit testing via AIN file Building 'test/test.pje' with the SDK compiler produces a file at 'test/Run/test.ain' which can be run with xsystem4 to test various features. Not the greatest testing method, but it works. --- test/.gitignore | 4 ++ test/Source/System/System.inc | 3 ++ test/Source/System/System42/README.md | 12 +++++ test/Source/System/System42/System40.ini | 42 +++++++++++++++ test/Source/System/System42/System42.inc | 9 ++++ test/Source/arithmetic.jaf | 58 ++++++++++++++++++++ test/Source/main.jaf | 69 ++++++++++++++++++++++++ test/Source/strings.jaf | 69 ++++++++++++++++++++++++ test/Source/test.inc | 6 +++ test/test.pje | 26 +++++++++ 10 files changed, 298 insertions(+) create mode 100644 test/.gitignore create mode 100755 test/Source/System/System.inc create mode 100755 test/Source/System/System42/README.md create mode 100755 test/Source/System/System42/System40.ini create mode 100755 test/Source/System/System42/System42.inc create mode 100755 test/Source/arithmetic.jaf create mode 100755 test/Source/main.jaf create mode 100755 test/Source/strings.jaf create mode 100755 test/Source/test.inc create mode 100755 test/test.pje diff --git a/test/.gitignore b/test/.gitignore new file mode 100644 index 0000000..8464f0b --- /dev/null +++ b/test/.gitignore @@ -0,0 +1,4 @@ +OBJ/ +Run/ +Source/System/System42/System40.exe +Source/System/System42/DLL/Sys42VM.dll \ No newline at end of file diff --git a/test/Source/System/System.inc b/test/Source/System/System.inc new file mode 100755 index 0000000..bf04922 --- /dev/null +++ b/test/Source/System/System.inc @@ -0,0 +1,3 @@ +Source = { + "System42\System42.inc", +} diff --git a/test/Source/System/System42/README.md b/test/Source/System/System42/README.md new file mode 100755 index 0000000..90e11ef --- /dev/null +++ b/test/Source/System/System42/README.md @@ -0,0 +1,12 @@ +Note +---- + +I am not 100% sure about the legality of distributing System40.exe and +Sys42VM.dll here, so I've left them out. They can be found in the System 4 SDK, +which you'll want to have anyways to compile these tests. + +Once you've copied the files, this directory should contain the following: + + System40.exe + System40.ini + DLL\Sys42VM.dll diff --git a/test/Source/System/System42/System40.ini b/test/Source/System/System42/System40.ini new file mode 100755 index 0000000..0bfdf94 --- /dev/null +++ b/test/Source/System/System42/System40.ini @@ -0,0 +1,42 @@ +; ★ System4.0 初期設定ファイル ★ +; このファイルの中身は書き換えないで下さい + +; iniファイルバージョン +IniFileVersion = 1 + +; 初期ビューサイズ +ViewWidth = 800 +ViewHeight = 600 + +; ゲーム名 +GameName = "test" + +; レジストリ名 +RegName = "Software\nunuhara\test" + +; コード名 +CodeName = "test.ain" + +; VM設定 +MainVM = "Sys42VM" + +; セーブフォルダ設定 +SaveFolder = "SaveData" + +; DLLフォルダ設定 +DLL = "DLL" + +; スキップ名設定 +MsgSkipName = "既読スキップ" + +; 音楽設定 +UseCD = false + +; ジョイパッド設定 +UseJoypad = true + +; ボリュームバランサー設定 +VolumeValancer[0] = "BGM" +VolumeValancer[1] = "効果音" + +; end of file diff --git a/test/Source/System/System42/System42.inc b/test/Source/System/System42/System42.inc new file mode 100755 index 0000000..1d95380 --- /dev/null +++ b/test/Source/System/System42/System42.inc @@ -0,0 +1,9 @@ + +CopyToRun = { + "System40.exe", + "System40.ini" +} + +CopyToDLL = { + "DLL\Sys42VM.dll", +} \ No newline at end of file diff --git a/test/Source/arithmetic.jaf b/test/Source/arithmetic.jaf new file mode 100755 index 0000000..90b652b --- /dev/null +++ b/test/Source/arithmetic.jaf @@ -0,0 +1,58 @@ +// -*-mode: C; coding: sjis; -*- + +void test_arithmetic(void) +{ + int n; + int one = 1; + int three = 3; + int five = 5; + int ten = 10; + test_equal("-(5)", -five, -5); + test_equal("-(-5)", -(-five), 5); + test_equal("!5", !five, 0); + test_equal("!!5", !!five, 1); + test_equal("~5", ~five, 0xFFFFFFFA); + test_equal("5 + 10", five + ten, 15); + test_equal("5 - 10", five - ten, -5); + test_equal("10 - 5", ten - five, 5); + test_equal("5 * 10", five * ten, 50); + test_equal("5 / 10", five / ten, 0); + test_equal("10 / 5", ten / five, 2); + test_equal("5 % 10", five % ten, 5); + test_equal("10 % 5", ten % five, 0); + test_equal("5 & 10", five & ten, 0); + test_equal("5 | 10", five | ten, 15); + test_equal("1 ^ 3", one ^ three, 2); + test_equal("1 << 3", one << three, 8); + test_equal("3 >> 1", three >> one, 1); + // comparisons + test_bool("1 < 3", one < three, true); + test_bool("3 < 1", three < one, false); + test_bool("1 < 1", one < one, false); + test_bool("3 > 1", three > one, true); + test_bool("1 > 3", one > three, false); + test_bool("1 > 1", one > one, false); + test_bool("1 <= 3", one <= three, true); + test_bool("3 <= 1", three <= one, false); + test_bool("1 <= 1", one <= one, true); + test_bool("3 >= 1", three >= one, true); + test_bool("1 >= 3", one >= three, false); + test_bool("1 >= 1", one >= one, true); + test_bool("1 != 3", one != three, true); + test_bool("1 != 1", one != one, false); + test_bool("1 == 1", one == one, true); + test_bool("1 == 3", one == three, false); + // assignment + test_equal("++0", (n = 0, ++n), 1); + test_equal("--1", (n = 1, --n), 0); + test_equal("0 += 2", (n = 0, n+=2), 2); + test_equal("3 -= 2", (n = 3, n-=2), 1); + test_equal("2 *= 3", (n = 2, n*=3), 6); + test_equal("6 /= 2", (n = 6, n/=2), 3); + test_equal("4 %= 3", (n = 4, n%=3), 1); + test_equal("3 &= 2", (n = 3, n&=2), 2); + test_equal("2 |= 1", (n = 2, n|=1), 3); + test_equal("3 ^= 1", (n = 3, n^=1), 2); + test_equal("1 <<= 3", (n = 1, n<<=3), 8); + test_equal("3 >>= 1", (n = 3, n>>=1), 1); +} diff --git a/test/Source/main.jaf b/test/Source/main.jaf new file mode 100755 index 0000000..97c04a4 --- /dev/null +++ b/test/Source/main.jaf @@ -0,0 +1,69 @@ +// -*-mode: C; coding: sjis; -*- + +const bool true = 1; +const bool false = 0; + +int tests_failed; +int tests_passed; +int total_failed = 0; +int total_passed = 0; + +void test_start(string name) +{ + tests_failed = 0; + tests_passed = 0; +} + +void test_end(string name) +{ + system.Output(name + ": " + string(tests_failed) + " failed; " + string(tests_passed) + " passed\n"); + total_failed += tests_failed; + total_passed += tests_passed; +} + +void test_equal(string msg, int actual, int expected) +{ + if (actual != expected) { + system.Output("FAIL: " + msg + " (expected " + string(expected) + "; got " + string(actual) + ")\n"); + tests_failed++; + } else { + tests_passed++; + } +} + +void test_bool(string msg, bool actual, bool expected) +{ + if (actual != expected) { + system.Output("FAIL: " + msg + " (expected " + (expected ? "true" : "false") + "; got " + (actual ? "true" : "false") + ")\n"); + tests_failed++; + } else { + tests_passed++; + } +} + +void test_string(string msg, string actual, string expected) +{ + if (actual != expected) { + system.Output("FAIL: " + msg + " (expected \"" + expected + "\"; got \"" + actual + "\")\n"); + tests_failed++; + } else { + tests_passed++; + } +} + +int main(void) +{ + test_start("arithmetic"); + test_arithmetic(); + test_end("arithmetic"); + test_start("strings"); + test_strings(); + test_end("strings"); + if (total_failed > 0) { + system.Output(string(total_failed) + " tests failed.\n"); + } else { + system.Output("All tests passed.\n"); + } + system.Exit(total_failed > 0); + return 0; +} diff --git a/test/Source/strings.jaf b/test/Source/strings.jaf new file mode 100755 index 0000000..a68577a --- /dev/null +++ b/test/Source/strings.jaf @@ -0,0 +1,69 @@ +// -*-mode: C; coding: sjis; -*- + +void test_strings(void) +{ + int i; + float f; + string s; + test_bool("\"a\" == \"a\"", "a" == "a", true); + test_bool("\"a\" != \"b\"", "a" == "b", false); + test_bool("\"a\" < \"b\"", "a" < "b", true); + test_bool("\"a\" < \"a\"", "a" < "a", false); + test_bool("\"b\" < \"a\"", "b" < "a", false); + test_bool("\"a\" > \"b\"", "a" > "b", false); + test_bool("\"a\" > \"a\"", "a" > "a", false); + test_bool("\"b\" > \"a\"", "b" > "a", true); + test_bool("\"a\" <= \"b\"", "a" <= "b", true); + test_bool("\"a\" <= \"a\"", "a" <= "a", true); + test_bool("\"b\" <= \"a\"", "b" <= "a", false); + test_bool("\"a\" >= \"b\"", "a" >= "b", false); + test_bool("\"a\" >= \"a\"", "a" >= "a", true); + test_bool("\"b\" >= \"a\"", "b" >= "a", true); + test_bool("var = \"str\"", (s = "str", s == "str"), true); + // S_ADD + test_string("\"a\" + \"b\"", "a" + "b", "ab"); + // S_LENGTH + test_equal("\"\".Length()", (s = "", s.Length()), 0); + test_equal("\"abc\".Length()", (s = "abc", s.Length()), 3); + test_equal("\"あbc\".Length()", (s = "あbc", s.Length()), 3); + // S_LENGTHBYTE + test_equal("\"\".LengthByte()", (s = "", s.LengthByte()), 0); + test_equal("\"abc\".LengthByte()", (s = "abc", s.LengthByte()), 3); + test_equal("\"あbc\".LengthByte()", (s = "あbc", s.LengthByte()), 4); + // S_EMPTY + test_bool("\"\".Empty()", (s = "", s.Empty()), true); + test_bool("\"a\".Empty()", (s = "a", s.Empty()), false); + // S_FIND + test_equal("\"abあいう\".Find(\"bあ\")", (s = "abあいう", s.Find("bあ")), 1); + test_equal("\"abあいう\".Find(\"bあ\")", (s = "abあいう", s.Find("い")), 3); + test_equal("\"abあいう\".Find(\"うえお\")", (s = "abあいう", s.Find("うえお")), -1); + // S_GETPART + test_string("\"abあいう\".GetPart(0, 0)", (s = "abあいう", s.GetPart(0, 0)), ""); + test_string("\"abあいう\".GetPart(16, 2)", (s = "abあいう", s.GetPart(16, 2)), ""); + test_string("\"abあいう\".GetPart(1, 3)", (s = "abあいう", s.GetPart(1, 3)), "bあい"); + test_string("\"abあいう\".GetPart(-1, 3)", (s = "abあいう", s.GetPart(-1, 3)), "abあ"); + test_string("\"abあいう\".GetPart(2, 16)", (s = "abあいう", s.GetPart(2, 16)), "あいう"); + // S_PUSHBACK2 + test_string("\"\".PushBack('a')", (s = "", s.PushBack('a'), s), "a"); + test_string("\"あ\".PushBack('a')", (s = "あ", s.PushBack('a'), s), "あa"); + // S_POPBACK2 + test_string("\"あa\".PopBack()", (s = "あa", s.PopBack(), s), "あ"); + test_string("\"あ\".PopBack()", (s = "あ", s.PopBack(), s), ""); + // S_ERASE2 + test_string("\"\".Erase(-1)", (s = "", s.Erase(-1), s), ""); + test_string("\"\".Erase(0)", (s = "", s.Erase(0), s), ""); + test_string("\"\".Erase(1)", (s = "", s.Erase(1), s), ""); + test_string("\"a\".Erase(-1)", (s = "a", s.Erase(-1), s), ""); + test_string("\"a\".Erase(0)", (s = "a", s.Erase(0), s), ""); + test_string("\"a\".Erase(1)", (s = "a", s.Erase(1), s), "a"); + test_string("\"あ\".Erase(-1)", (s = "あ", s.Erase(-1), s), ""); + test_string("\"あ\".Erase(0)", (s = "あ", s.Erase(0), s), ""); + test_string("\"あ\".Erase(1)", (s = "あ", s.Erase(1), s), "あ"); + test_string("\"あbc\".Erase(1)", (s = "あbc", s.Erase(1), s), "あc"); + + // I_STRING + test_bool("(42).String()", (i = 42, i.String() == "42"), true); + // FTOS + test_bool("(4.2).String()", (f = 4.2, f.String() == "4.200000"), true); + test_bool("(4.222).String(2)", (f = 4.222, f.String(2) == "4.22"), true); +} diff --git a/test/Source/test.inc b/test/Source/test.inc new file mode 100755 index 0000000..2227848 --- /dev/null +++ b/test/Source/test.inc @@ -0,0 +1,6 @@ +Source = { +"main.jaf", +"arithmetic.jaf", +"strings.jaf" +} + diff --git a/test/test.pje b/test/test.pje new file mode 100755 index 0000000..6a71da0 --- /dev/null +++ b/test/test.pje @@ -0,0 +1,26 @@ +ProjectName = "test" + +Formation "Debug" { + Define = "DEBUG" +} +Formation "Release" { + Define = "RELEASE" +} + +CodeName = "test.ain" + +GameVersion = 100 + +SourceDir = "Source" +HLLDir = "HLL" +ObjDir = "OBJ" +OutputDir = "Run" + +SystemSource = { + "System\System.inc", +} + +Source = { + "test.inc", +} +