Validate integer math

pull/3210/head
Evil Eye 3 years ago
parent f027acf575
commit 3dada0796a

@ -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();
}
}
}

@ -64,7 +64,7 @@ namespace
std::vector<float> mFloats;
template<class T>
T getLocal(int index, const std::vector<T>& vector) const
T getLocal(std::size_t index, const std::vector<T>& vector) const
{
if(index < vector.size())
return vector[index];
@ -72,7 +72,7 @@ namespace
}
template<class T>
void setLocal(T value, int index, std::vector<T>& vector)
void setLocal(T value, std::size_t index, std::vector<T>& 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

Loading…
Cancel
Save