-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add trivial method tests for literal return
Signed-off-by: Stefan Marr <[email protected]>
- Loading branch information
Showing
8 changed files
with
253 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
#include "TrivialMethodTest.h" | ||
|
||
#include <cppunit/TestAssert.h> | ||
#include <string> | ||
|
||
#include "../compiler/MethodGenerationContext.h" | ||
#include "../vm/IsValidObject.h" | ||
#include "../vmobjects/VMInvokable.h" | ||
|
||
void TrivialMethodTest::literalReturn(std::string source) { | ||
std::string s = "test = ( ^ " + source + " )"; | ||
methodToBytecode(s.data()); | ||
VMInvokable* m = _mgenc->Assemble(); | ||
|
||
std::string expected = "Expected to be trivial: " + s; | ||
bool result = IsLiteralReturn(m); | ||
CPPUNIT_ASSERT_MESSAGE(expected.data(), result); | ||
|
||
tearDown(); | ||
} | ||
|
||
void TrivialMethodTest::testLiteralReturn() { | ||
literalReturn("0"); | ||
literalReturn("1"); | ||
literalReturn("-10"); | ||
literalReturn("3333"); | ||
literalReturn("'str'"); | ||
literalReturn("#sym"); | ||
literalReturn("1.1"); | ||
literalReturn("-2342.234"); | ||
literalReturn("true"); | ||
literalReturn("false"); | ||
literalReturn("nil"); | ||
} | ||
|
||
void TrivialMethodTest::blockLiteralReturn(std::string source) { | ||
std::string s = "[ " + source + " ]"; | ||
blockToBytecode(s.data()); | ||
VMInvokable* m = _bgenc->Assemble(); | ||
|
||
std::string expected = "Expected to be trivial: " + s; | ||
bool result = IsLiteralReturn(m); | ||
CPPUNIT_ASSERT_MESSAGE(expected.data(), result); | ||
|
||
tearDown(); | ||
} | ||
|
||
void TrivialMethodTest::testBlockLiteralReturn() { | ||
blockLiteralReturn("0"); | ||
blockLiteralReturn("1"); | ||
blockLiteralReturn("-10"); | ||
blockLiteralReturn("3333"); | ||
blockLiteralReturn("'str'"); | ||
blockLiteralReturn("#sym"); | ||
blockLiteralReturn("1.1"); | ||
blockLiteralReturn("-2342.234"); | ||
blockLiteralReturn("true"); | ||
blockLiteralReturn("false"); | ||
blockLiteralReturn("nil"); | ||
} | ||
|
||
/* | ||
@pytest.mark.parametrize("source", ["Nil", "system", "MyClassFooBar"]) | ||
def test_global_return(mgenc, source): | ||
body_or_none = parse_method(mgenc, "test = ( ^ " + source + " )") | ||
m = mgenc.assemble(body_or_none) | ||
assert isinstance(m, GlobalRead) | ||
def test_non_trivial_global_return(mgenc): | ||
body_or_none = parse_method(mgenc, "test = ( #foo. ^ system )") | ||
m = mgenc.assemble(body_or_none) | ||
assert isinstance(m, AstMethod) or isinstance(m, BcMethod) | ||
def test_field_getter_0(cgenc, mgenc): | ||
add_field(cgenc, "field") | ||
body_or_none = parse_method(mgenc, "test = ( ^ field )") | ||
m = mgenc.assemble(body_or_none) | ||
assert isinstance(m, FieldRead) | ||
def test_field_getter_n(cgenc, mgenc): | ||
add_field(cgenc, "a") | ||
add_field(cgenc, "b") | ||
add_field(cgenc, "c") | ||
add_field(cgenc, "d") | ||
add_field(cgenc, "e") | ||
add_field(cgenc, "field") | ||
body_or_none = parse_method(mgenc, "test = ( ^ field )") | ||
m = mgenc.assemble(body_or_none) | ||
assert isinstance(m, FieldRead) | ||
def test_non_trivial_getter_0(cgenc, mgenc): | ||
add_field(cgenc, "field") | ||
body = parse_method(mgenc, "test = ( 0. ^ field )") | ||
m = mgenc.assemble(body) | ||
assert isinstance(m, AstMethod) or isinstance(m, BcMethod) | ||
def test_non_trivial_getter_n(cgenc, mgenc): | ||
add_field(cgenc, "a") | ||
add_field(cgenc, "b") | ||
add_field(cgenc, "c") | ||
add_field(cgenc, "d") | ||
add_field(cgenc, "e") | ||
add_field(cgenc, "field") | ||
body = parse_method(mgenc, "test = ( 0. ^ field )") | ||
m = mgenc.assemble(body) | ||
assert isinstance(m, AstMethod) or isinstance(m, BcMethod) | ||
@pytest.mark.parametrize( | ||
"source", ["field := val", "field := val.", "field := val. ^ self"] | ||
) | ||
def test_field_setter_0(cgenc, mgenc, source): | ||
add_field(cgenc, "field") | ||
body_or_none = parse_method(mgenc, "test: val = ( " + source + " )") | ||
m = mgenc.assemble(body_or_none) | ||
assert isinstance(m, FieldWrite) | ||
@pytest.mark.parametrize( | ||
"source", ["field := value", "field := value.", "field := value. ^ self"] | ||
) | ||
def test_field_setter_n(cgenc, mgenc, source): | ||
add_field(cgenc, "a") | ||
add_field(cgenc, "b") | ||
add_field(cgenc, "c") | ||
add_field(cgenc, "d") | ||
add_field(cgenc, "e") | ||
add_field(cgenc, "field") | ||
body_or_none = parse_method(mgenc, "test: value = ( " + source + " )") | ||
m = mgenc.assemble(body_or_none) | ||
assert isinstance(m, FieldWrite) | ||
def test_non_trivial_field_setter_0(cgenc, mgenc): | ||
add_field(cgenc, "field") | ||
body_or_none = parse_method(mgenc, "test: val = ( 0. field := value )") | ||
m = mgenc.assemble(body_or_none) | ||
assert isinstance(m, AstMethod) or isinstance(m, BcMethod) | ||
def test_non_trivial_field_setter_n(cgenc, mgenc): | ||
add_field(cgenc, "a") | ||
add_field(cgenc, "b") | ||
add_field(cgenc, "c") | ||
add_field(cgenc, "d") | ||
add_field(cgenc, "e") | ||
add_field(cgenc, "field") | ||
body_or_none = parse_method(mgenc, "test: val = ( 0. field := value )") | ||
m = mgenc.assemble(body_or_none) | ||
assert isinstance(m, AstMethod) or isinstance(m, BcMethod) | ||
@pytest.mark.parametrize( | ||
"source", | ||
[ | ||
"0", | ||
"1", | ||
"-10", | ||
"'str'", | ||
"#sym", | ||
"1.1", | ||
"-2342.234", | ||
"true", | ||
"false", | ||
"nil", | ||
], | ||
) | ||
def test_literal_no_return(mgenc, source): | ||
body_or_none = parse_method(mgenc, "test = ( " + source + " )") | ||
m = mgenc.assemble(body_or_none) | ||
assert isinstance(m, AstMethod) or isinstance(m, BcMethod) | ||
@pytest.mark.parametrize( | ||
"source", | ||
[ | ||
"0", | ||
"1", | ||
"-10", | ||
"'str'", | ||
"#sym", | ||
"1.1", | ||
"-2342.234", | ||
"true", | ||
"false", | ||
"nil", | ||
], | ||
) | ||
def test_non_trivial_literal_return(mgenc, source): | ||
body_or_none = parse_method(mgenc, "test = ( 1. ^ " + source + " )") | ||
m = mgenc.assemble(body_or_none) | ||
assert isinstance(m, AstMethod) or isinstance(m, BcMethod) | ||
def test_block_return(mgenc): | ||
body_or_none = parse_method(mgenc, "test = ( ^ [] )") | ||
m = mgenc.assemble(body_or_none) | ||
assert isinstance(m, AstMethod) or isinstance(m, BcMethod) | ||
def test_unknown_global_in_block(bgenc): | ||
""" | ||
In PySOM we can actually support this, in TruffleSOM we can't | ||
because of the difference in frame format. | ||
""" | ||
body_or_none = parse_block(bgenc, "[ UnknownGlobalSSSS ]") | ||
m = bgenc.assemble(body_or_none) | ||
assert isinstance(m, GlobalRead) | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#pragma once | ||
|
||
#include <cppunit/extensions/HelperMacros.h> | ||
|
||
#include "TestWithParsing.h" | ||
|
||
class TrivialMethodTest : public TestWithParsing { | ||
CPPUNIT_TEST_SUITE(TrivialMethodTest); | ||
CPPUNIT_TEST(testLiteralReturn); | ||
CPPUNIT_TEST(testBlockLiteralReturn); | ||
CPPUNIT_TEST_SUITE_END(); | ||
|
||
private: | ||
void testLiteralReturn(); | ||
void literalReturn(std::string source); | ||
|
||
void testBlockLiteralReturn(); | ||
void blockLiteralReturn(std::string source); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters