Update tests.

This commit is contained in:
chriseth 2019-02-28 23:25:24 +01:00
parent 851dd635eb
commit 43850d55f3
2 changed files with 228 additions and 127 deletions

View File

@ -168,20 +168,35 @@ BOOST_AUTO_TEST_CASE(location_test)
auto codegenCharStream = make_shared<CharStream>("", "--CODEGEN--"); auto codegenCharStream = make_shared<CharStream>("", "--CODEGEN--");
vector<SourceLocation> locations = vector<SourceLocation> locations;
vector<SourceLocation>(4, SourceLocation{2, 82, sourceCode}) + if (dev::test::Options::get().optimize)
vector<SourceLocation>(1, SourceLocation{8, 17, codegenCharStream}) + locations =
vector<SourceLocation>(3, SourceLocation{5, 7, codegenCharStream}) + vector<SourceLocation>(4, SourceLocation{2, 82, sourceCode}) +
vector<SourceLocation>(1, SourceLocation{30, 31, codegenCharStream}) + vector<SourceLocation>(1, SourceLocation{8, 17, codegenCharStream}) +
vector<SourceLocation>(1, SourceLocation{27, 28, codegenCharStream}) + vector<SourceLocation>(3, SourceLocation{5, 7, codegenCharStream}) +
vector<SourceLocation>(1, SourceLocation{20, 32, codegenCharStream}) + vector<SourceLocation>(1, SourceLocation{30, 31, codegenCharStream}) +
vector<SourceLocation>(1, SourceLocation{5, 7, codegenCharStream}) + vector<SourceLocation>(1, SourceLocation{27, 28, codegenCharStream}) +
vector<SourceLocation>(hasShifts ? 19 : 20, SourceLocation{2, 82, sourceCode}) + vector<SourceLocation>(1, SourceLocation{20, 32, codegenCharStream}) +
vector<SourceLocation>(24, SourceLocation{20, 79, sourceCode}) + vector<SourceLocation>(1, SourceLocation{5, 7, codegenCharStream}) +
vector<SourceLocation>(1, SourceLocation{49, 58, sourceCode}) + vector<SourceLocation>(19, SourceLocation{2, 82, sourceCode}) +
vector<SourceLocation>(1, SourceLocation{72, 74, sourceCode}) + vector<SourceLocation>(21, SourceLocation{20, 79, sourceCode}) +
vector<SourceLocation>(2, SourceLocation{65, 74, sourceCode}) + vector<SourceLocation>(1, SourceLocation{72, 74, sourceCode}) +
vector<SourceLocation>(2, SourceLocation{20, 79, sourceCode}); vector<SourceLocation>(2, SourceLocation{20, 79, sourceCode});
else
locations =
vector<SourceLocation>(4, SourceLocation{2, 82, sourceCode}) +
vector<SourceLocation>(1, SourceLocation{8, 17, codegenCharStream}) +
vector<SourceLocation>(3, SourceLocation{5, 7, codegenCharStream}) +
vector<SourceLocation>(1, SourceLocation{30, 31, codegenCharStream}) +
vector<SourceLocation>(1, SourceLocation{27, 28, codegenCharStream}) +
vector<SourceLocation>(1, SourceLocation{20, 32, codegenCharStream}) +
vector<SourceLocation>(1, SourceLocation{5, 7, codegenCharStream}) +
vector<SourceLocation>(hasShifts ? 19 : 20, SourceLocation{2, 82, sourceCode}) +
vector<SourceLocation>(24, SourceLocation{20, 79, sourceCode}) +
vector<SourceLocation>(1, SourceLocation{49, 58, sourceCode}) +
vector<SourceLocation>(1, SourceLocation{72, 74, sourceCode}) +
vector<SourceLocation>(2, SourceLocation{65, 74, sourceCode}) +
vector<SourceLocation>(2, SourceLocation{20, 79, sourceCode});
checkAssemblyLocations(items, locations); checkAssemblyLocations(items, locations);
} }

View File

@ -282,12 +282,26 @@ BOOST_AUTO_TEST_CASE(comparison)
)"; )";
bytes code = compileFirstExpression(sourceCode); bytes code = compileFirstExpression(sourceCode);
bytes expectation({uint8_t(Instruction::PUSH1), 0x1, uint8_t(Instruction::ISZERO), uint8_t(Instruction::ISZERO), bytes expectation;
uint8_t(Instruction::PUSH2), 0x11, 0xaa, if (dev::test::Options::get().optimize)
uint8_t(Instruction::PUSH2), 0x10, 0xaa, expectation = {
uint8_t(Instruction::LT), uint8_t(Instruction::ISZERO), uint8_t(Instruction::ISZERO), uint8_t(Instruction::PUSH2), 0x11, 0xaa,
uint8_t(Instruction::EQ), uint8_t(Instruction::PUSH2), 0x10, 0xaa,
uint8_t(Instruction::ISZERO)}); uint8_t(Instruction::LT), uint8_t(Instruction::ISZERO), uint8_t(Instruction::ISZERO),
uint8_t(Instruction::PUSH1), 0x1,
uint8_t(Instruction::ISZERO), uint8_t(Instruction::ISZERO),
uint8_t(Instruction::EQ),
uint8_t(Instruction::ISZERO)
};
else
expectation = {
uint8_t(Instruction::PUSH1), 0x1, uint8_t(Instruction::ISZERO), uint8_t(Instruction::ISZERO),
uint8_t(Instruction::PUSH2), 0x11, 0xaa,
uint8_t(Instruction::PUSH2), 0x10, 0xaa,
uint8_t(Instruction::LT), uint8_t(Instruction::ISZERO), uint8_t(Instruction::ISZERO),
uint8_t(Instruction::EQ),
uint8_t(Instruction::ISZERO)
};
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end()); BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
} }
@ -300,23 +314,25 @@ BOOST_AUTO_TEST_CASE(short_circuiting)
)"; )";
bytes code = compileFirstExpression(sourceCode); bytes code = compileFirstExpression(sourceCode);
bytes expectation({uint8_t(Instruction::PUSH1), 0x12, // 8 + 10 bytes expectation{
uint8_t(Instruction::PUSH1), 0x4, uint8_t(Instruction::PUSH1), 0x12, // 8 + 10
uint8_t(Instruction::GT), uint8_t(Instruction::PUSH1), 0x4,
uint8_t(Instruction::ISZERO), // after this we have 4 <= 8 + 10 uint8_t(Instruction::GT),
uint8_t(Instruction::DUP1), uint8_t(Instruction::ISZERO), // after this we have 4 <= 8 + 10
uint8_t(Instruction::PUSH1), 0x11, uint8_t(Instruction::DUP1),
uint8_t(Instruction::JUMPI), // short-circuit if it is true uint8_t(Instruction::PUSH1), 0x11,
uint8_t(Instruction::POP), uint8_t(Instruction::JUMPI), // short-circuit if it is true
uint8_t(Instruction::PUSH1), 0x2, uint8_t(Instruction::POP),
uint8_t(Instruction::PUSH1), 0x9, uint8_t(Instruction::PUSH1), 0x2,
uint8_t(Instruction::EQ), uint8_t(Instruction::PUSH1), 0x9,
uint8_t(Instruction::ISZERO), // after this we have 9 != 2 uint8_t(Instruction::EQ),
uint8_t(Instruction::JUMPDEST), uint8_t(Instruction::ISZERO), // after this we have 9 != 2
uint8_t(Instruction::ISZERO), uint8_t(Instruction::ISZERO), uint8_t(Instruction::JUMPDEST),
uint8_t(Instruction::PUSH1), 0x1, uint8_t(Instruction::ISZERO), uint8_t(Instruction::ISZERO), uint8_t(Instruction::ISZERO), uint8_t(Instruction::ISZERO),
uint8_t(Instruction::EQ), uint8_t(Instruction::PUSH1), 0x1, uint8_t(Instruction::ISZERO), uint8_t(Instruction::ISZERO),
uint8_t(Instruction::ISZERO)}); uint8_t(Instruction::EQ),
uint8_t(Instruction::ISZERO)
};
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end()); BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
} }
@ -328,37 +344,76 @@ BOOST_AUTO_TEST_CASE(arithmetic)
} }
)"; )";
bytes code = compileFirstExpression(sourceCode, {}, {{"test", "f", "y"}}); bytes code = compileFirstExpression(sourceCode, {}, {{"test", "f", "y"}});
bytes expectation({uint8_t(Instruction::PUSH1), 0x1,
uint8_t(Instruction::PUSH1), 0x2, bytes expectation;
uint8_t(Instruction::PUSH1), 0x3, if (dev::test::Options::get().optimize)
uint8_t(Instruction::PUSH1), 0x4, expectation = {
uint8_t(Instruction::PUSH1), 0x5, uint8_t(Instruction::PUSH1), 0x2,
uint8_t(Instruction::PUSH1), 0x6, uint8_t(Instruction::PUSH1), 0x3,
uint8_t(Instruction::PUSH1), 0x7, uint8_t(Instruction::PUSH1), 0x5,
uint8_t(Instruction::PUSH1), 0x8, uint8_t(Instruction::DUP4),
uint8_t(Instruction::DUP9), uint8_t(Instruction::PUSH1), 0x8,
uint8_t(Instruction::XOR), uint8_t(Instruction::XOR),
uint8_t(Instruction::AND), uint8_t(Instruction::PUSH1), 0x7,
uint8_t(Instruction::OR), uint8_t(Instruction::AND),
uint8_t(Instruction::SUB), uint8_t(Instruction::PUSH1), 0x6,
uint8_t(Instruction::ADD), uint8_t(Instruction::OR),
uint8_t(Instruction::DUP2), uint8_t(Instruction::SUB),
uint8_t(Instruction::ISZERO), uint8_t(Instruction::PUSH1), 0x4,
uint8_t(Instruction::ISZERO), uint8_t(Instruction::ADD),
uint8_t(Instruction::PUSH1), 0x1d, uint8_t(Instruction::DUP2),
uint8_t(Instruction::JUMPI), uint8_t(Instruction::ISZERO),
uint8_t(Instruction::INVALID), uint8_t(Instruction::ISZERO),
uint8_t(Instruction::JUMPDEST), uint8_t(Instruction::PUSH1), 0x1b,
uint8_t(Instruction::MOD), uint8_t(Instruction::JUMPI),
uint8_t(Instruction::DUP2), uint8_t(Instruction::INVALID),
uint8_t(Instruction::ISZERO), uint8_t(Instruction::JUMPDEST),
uint8_t(Instruction::ISZERO), uint8_t(Instruction::MOD),
uint8_t(Instruction::PUSH1), 0x26, uint8_t(Instruction::DUP2),
uint8_t(Instruction::JUMPI), uint8_t(Instruction::ISZERO),
uint8_t(Instruction::INVALID), uint8_t(Instruction::ISZERO),
uint8_t(Instruction::JUMPDEST), uint8_t(Instruction::PUSH1), 0x24,
uint8_t(Instruction::DIV), uint8_t(Instruction::JUMPI),
uint8_t(Instruction::MUL)}); uint8_t(Instruction::INVALID),
uint8_t(Instruction::JUMPDEST),
uint8_t(Instruction::DIV),
uint8_t(Instruction::PUSH1), 0x1,
uint8_t(Instruction::MUL)
};
else
expectation = {
uint8_t(Instruction::PUSH1), 0x1,
uint8_t(Instruction::PUSH1), 0x2,
uint8_t(Instruction::PUSH1), 0x3,
uint8_t(Instruction::PUSH1), 0x4,
uint8_t(Instruction::PUSH1), 0x5,
uint8_t(Instruction::PUSH1), 0x6,
uint8_t(Instruction::PUSH1), 0x7,
uint8_t(Instruction::PUSH1), 0x8,
uint8_t(Instruction::DUP9),
uint8_t(Instruction::XOR),
uint8_t(Instruction::AND),
uint8_t(Instruction::OR),
uint8_t(Instruction::SUB),
uint8_t(Instruction::ADD),
uint8_t(Instruction::DUP2),
uint8_t(Instruction::ISZERO),
uint8_t(Instruction::ISZERO),
uint8_t(Instruction::PUSH1), 0x1d,
uint8_t(Instruction::JUMPI),
uint8_t(Instruction::INVALID),
uint8_t(Instruction::JUMPDEST),
uint8_t(Instruction::MOD),
uint8_t(Instruction::DUP2),
uint8_t(Instruction::ISZERO),
uint8_t(Instruction::ISZERO),
uint8_t(Instruction::PUSH1), 0x26,
uint8_t(Instruction::JUMPI),
uint8_t(Instruction::INVALID),
uint8_t(Instruction::JUMPDEST),
uint8_t(Instruction::DIV),
uint8_t(Instruction::MUL)
};
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end()); BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
} }
@ -371,13 +426,27 @@ BOOST_AUTO_TEST_CASE(unary_operators)
)"; )";
bytes code = compileFirstExpression(sourceCode, {}, {{"test", "f", "y"}}); bytes code = compileFirstExpression(sourceCode, {}, {{"test", "f", "y"}});
bytes expectation({uint8_t(Instruction::PUSH1), 0x2, bytes expectation;
uint8_t(Instruction::DUP2), if (dev::test::Options::get().optimize)
uint8_t(Instruction::PUSH1), 0x0, expectation = {
uint8_t(Instruction::SUB), uint8_t(Instruction::DUP1),
uint8_t(Instruction::NOT), uint8_t(Instruction::PUSH1), 0x0,
uint8_t(Instruction::EQ), uint8_t(Instruction::SUB),
uint8_t(Instruction::ISZERO)}); uint8_t(Instruction::NOT),
uint8_t(Instruction::PUSH1), 0x2,
uint8_t(Instruction::EQ),
uint8_t(Instruction::ISZERO)
};
else
expectation = {
uint8_t(Instruction::PUSH1), 0x2,
uint8_t(Instruction::DUP2),
uint8_t(Instruction::PUSH1), 0x0,
uint8_t(Instruction::SUB),
uint8_t(Instruction::NOT),
uint8_t(Instruction::EQ),
uint8_t(Instruction::ISZERO)
};
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end()); BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
} }
@ -391,48 +460,50 @@ BOOST_AUTO_TEST_CASE(unary_inc_dec)
bytes code = compileFirstExpression(sourceCode, {}, {{"test", "f", "a"}, {"test", "f", "x"}}); bytes code = compileFirstExpression(sourceCode, {}, {{"test", "f", "a"}, {"test", "f", "x"}});
// Stack: a, x // Stack: a, x
bytes expectation({uint8_t(Instruction::DUP2), bytes expectation{
uint8_t(Instruction::DUP1), uint8_t(Instruction::DUP2),
uint8_t(Instruction::PUSH1), 0x1, uint8_t(Instruction::DUP1),
uint8_t(Instruction::ADD), uint8_t(Instruction::PUSH1), 0x1,
// Stack here: a x a (a+1) uint8_t(Instruction::ADD),
uint8_t(Instruction::SWAP3), // Stack here: a x a (a+1)
uint8_t(Instruction::POP), // first ++ uint8_t(Instruction::SWAP3),
// Stack here: (a+1) x a uint8_t(Instruction::POP), // first ++
uint8_t(Instruction::DUP3), // Stack here: (a+1) x a
uint8_t(Instruction::PUSH1), 0x1, uint8_t(Instruction::DUP3),
uint8_t(Instruction::ADD), uint8_t(Instruction::PUSH1), 0x1,
// Stack here: (a+1) x a (a+2) uint8_t(Instruction::ADD),
uint8_t(Instruction::SWAP3), // Stack here: (a+1) x a (a+2)
uint8_t(Instruction::POP), uint8_t(Instruction::SWAP3),
// Stack here: (a+2) x a uint8_t(Instruction::POP),
uint8_t(Instruction::DUP3), // second ++ // Stack here: (a+2) x a
uint8_t(Instruction::XOR), uint8_t(Instruction::DUP3), // second ++
// Stack here: (a+2) x a^(a+2) uint8_t(Instruction::XOR),
uint8_t(Instruction::DUP3), // Stack here: (a+2) x a^(a+2)
uint8_t(Instruction::DUP1), uint8_t(Instruction::DUP3),
uint8_t(Instruction::PUSH1), 0x1, uint8_t(Instruction::DUP1),
uint8_t(Instruction::SWAP1), uint8_t(Instruction::PUSH1), 0x1,
uint8_t(Instruction::SUB), uint8_t(Instruction::SWAP1),
// Stack here: (a+2) x a^(a+2) (a+2) (a+1) uint8_t(Instruction::SUB),
uint8_t(Instruction::SWAP4), // Stack here: (a+2) x a^(a+2) (a+2) (a+1)
uint8_t(Instruction::POP), // first -- uint8_t(Instruction::SWAP4),
uint8_t(Instruction::XOR), uint8_t(Instruction::POP), // first --
// Stack here: (a+1) x a^(a+2)^(a+2) uint8_t(Instruction::XOR),
uint8_t(Instruction::DUP3), // Stack here: (a+1) x a^(a+2)^(a+2)
uint8_t(Instruction::PUSH1), 0x1, uint8_t(Instruction::DUP3),
uint8_t(Instruction::SWAP1), uint8_t(Instruction::PUSH1), 0x1,
uint8_t(Instruction::SUB), uint8_t(Instruction::SWAP1),
// Stack here: (a+1) x a^(a+2)^(a+2) a uint8_t(Instruction::SUB),
uint8_t(Instruction::SWAP3), // Stack here: (a+1) x a^(a+2)^(a+2) a
uint8_t(Instruction::POP), // second ++ uint8_t(Instruction::SWAP3),
// Stack here: a x a^(a+2)^(a+2) uint8_t(Instruction::POP), // second ++
uint8_t(Instruction::DUP3), // will change // Stack here: a x a^(a+2)^(a+2)
uint8_t(Instruction::XOR), uint8_t(Instruction::DUP3), // will change
uint8_t(Instruction::SWAP1), uint8_t(Instruction::XOR),
uint8_t(Instruction::POP), uint8_t(Instruction::SWAP1),
uint8_t(Instruction::DUP1)}); uint8_t(Instruction::POP),
// Stack here: a x a^(a+2)^(a+2)^a uint8_t(Instruction::DUP1)
};
// Stack here: a x a^(a+2)^(a+2)^a
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end()); BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
} }
@ -446,16 +517,31 @@ BOOST_AUTO_TEST_CASE(assignment)
bytes code = compileFirstExpression(sourceCode, {}, {{"test", "f", "a"}, {"test", "f", "b"}}); bytes code = compileFirstExpression(sourceCode, {}, {{"test", "f", "a"}, {"test", "f", "b"}});
// Stack: a, b // Stack: a, b
bytes expectation({uint8_t(Instruction::PUSH1), 0x2, bytes expectation;
uint8_t(Instruction::DUP2), if (dev::test::Options::get().optimize)
uint8_t(Instruction::DUP4), expectation = {
uint8_t(Instruction::ADD), uint8_t(Instruction::DUP1),
// Stack here: a b 2 a+b uint8_t(Instruction::DUP3),
uint8_t(Instruction::SWAP3), uint8_t(Instruction::ADD),
uint8_t(Instruction::POP), uint8_t(Instruction::SWAP2),
uint8_t(Instruction::DUP3), uint8_t(Instruction::POP),
// Stack here: a+b b 2 a+b uint8_t(Instruction::DUP2),
uint8_t(Instruction::MUL)}); uint8_t(Instruction::PUSH1), 0x2,
uint8_t(Instruction::MUL)
};
else
expectation = {
uint8_t(Instruction::PUSH1), 0x2,
uint8_t(Instruction::DUP2),
uint8_t(Instruction::DUP4),
uint8_t(Instruction::ADD),
// Stack here: a b 2 a+b
uint8_t(Instruction::SWAP3),
uint8_t(Instruction::POP),
uint8_t(Instruction::DUP3),
// Stack here: a+b b 2 a+b
uint8_t(Instruction::MUL)
};
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end()); BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
} }