diff --git a/apps/openmw_test_suite/mwscript/test_scripts.cpp b/apps/openmw_test_suite/mwscript/test_scripts.cpp index 90c7fb53c2..b2e534a401 100644 --- a/apps/openmw_test_suite/mwscript/test_scripts.cpp +++ b/apps/openmw_test_suite/mwscript/test_scripts.cpp @@ -93,6 +93,21 @@ End)mwscript"; AddTopic "OpenMW Unit Test" +End)mwscript"; + + const std::string sScript3 = R"mwscript(Begin math + +short a +short b +short c +short d +short e + +set b to ( a + 1 ) +set c to ( a - 1 ) +set d to ( b * c ) +set e to ( d / a ) + End)mwscript"; TEST_F(MWScriptTest, mwscript_test_invalid) @@ -139,4 +154,49 @@ End)mwscript"; FAIL(); } } + + TEST_F(MWScriptTest, mwscript_test_math) + { + if(auto script = compile(sScript3)) + { + struct Algorithm + { + int a; + int b; + int c; + int d; + int e; + + void run(int input) + { + a = input; + b = a + 1; + c = a - 1; + d = b * c; + e = d / a; + } + + void test(const TestInterpreterContext& context) const + { + EXPECT_EQ(a, context.getLocalShort(0)); + EXPECT_EQ(b, context.getLocalShort(1)); + EXPECT_EQ(c, context.getLocalShort(2)); + EXPECT_EQ(d, context.getLocalShort(3)); + EXPECT_EQ(e, context.getLocalShort(4)); + } + } algorithm; + TestInterpreterContext context; + for(int i = 1; i < 1000; ++i) + { + context.setLocalShort(0, i); + run(*script, context); + algorithm.run(i); + algorithm.test(context); + } + } + else + { + FAIL(); + } + } } \ No newline at end of file diff --git a/apps/openmw_test_suite/mwscript/test_utils.hpp b/apps/openmw_test_suite/mwscript/test_utils.hpp index 58218b7000..b1f4e04262 100644 --- a/apps/openmw_test_suite/mwscript/test_utils.hpp +++ b/apps/openmw_test_suite/mwscript/test_utils.hpp @@ -64,7 +64,7 @@ namespace std::vector mFloats; template - T getLocal(int index, const std::vector& vector) const + T getLocal(std::size_t index, const std::vector& vector) const { if(index < vector.size()) return vector[index]; @@ -72,7 +72,7 @@ namespace } template - void setLocal(T value, int index, std::vector& vector) + void setLocal(T value, std::size_t index, std::vector& vector) { if(index >= vector.size()) vector.resize(index + 1); @@ -86,17 +86,17 @@ namespace mFloats.clear(); } - int getShort(int index) const { return getLocal(index, mShorts); }; + int getShort(std::size_t index) const { return getLocal(index, mShorts); }; - int getLong(int index) const { return getLocal(index, mLongs); }; + int getLong(std::size_t index) const { return getLocal(index, mLongs); }; - float getFloat(int index) const { return getLocal(index, mFloats); }; + float getFloat(std::size_t index) const { return getLocal(index, mFloats); }; - void setShort(int index, int value) { setLocal(value, index, mShorts); }; + void setShort(std::size_t index, int value) { setLocal(value, index, mShorts); }; - void setLong(int index, int value) { setLocal(value, index, mLongs); }; + void setLong(std::size_t index, int value) { setLocal(value, index, mLongs); }; - void setFloat(int index, float value) { setLocal(value, index, mFloats); }; + void setFloat(std::size_t index, float value) { setLocal(value, index, mFloats); }; }; class GlobalVariables