Skip to content

Commit

Permalink
Add trivial method tests for literal return
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Marr <[email protected]>
  • Loading branch information
smarr committed Aug 5, 2024
1 parent 5a416f1 commit 43d8163
Show file tree
Hide file tree
Showing 8 changed files with 253 additions and 3 deletions.
6 changes: 6 additions & 0 deletions SOM.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
0A5A7E922C5D45A00011C783 /* VMSafePrimitive.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0A5A7E902C5D45A00011C783 /* VMSafePrimitive.cpp */; };
0A5A7E962C60F5BB0011C783 /* VMTrivialMethod.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0A5A7E952C60F5BB0011C783 /* VMTrivialMethod.cpp */; };
0A5A7E972C60F5BB0011C783 /* VMTrivialMethod.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0A5A7E952C60F5BB0011C783 /* VMTrivialMethod.cpp */; };
0A5A7E9A2C617E400011C783 /* TrivialMethodTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0A5A7E992C617E400011C783 /* TrivialMethodTest.cpp */; };
0A5A7E9D2C617EE00011C783 /* TestWithParsing.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0A5A7E9C2C617EE00011C783 /* TestWithParsing.cpp */; };
0A67EA7519ACD43A00830E3B /* CloneObjectsTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0A67EA7019ACD43A00830E3B /* CloneObjectsTest.cpp */; };
0A67EA7619ACD43A00830E3B /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0A67EA7119ACD43A00830E3B /* main.cpp */; };
Expand Down Expand Up @@ -230,6 +231,8 @@
0A5A7E932C5DA9A90011C783 /* Primitives.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Primitives.h; sourceTree = "<group>"; };
0A5A7E942C602E8C0011C783 /* VMTrivialMethod.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = VMTrivialMethod.h; sourceTree = "<group>"; };
0A5A7E952C60F5BB0011C783 /* VMTrivialMethod.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = VMTrivialMethod.cpp; sourceTree = "<group>"; };
0A5A7E982C617E2B0011C783 /* TrivialMethodTest.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = TrivialMethodTest.h; path = unitTests/TrivialMethodTest.h; sourceTree = "<group>"; };
0A5A7E992C617E400011C783 /* TrivialMethodTest.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = TrivialMethodTest.cpp; path = unitTests/TrivialMethodTest.cpp; sourceTree = "<group>"; };
0A5A7E9B2C617EC70011C783 /* TestWithParsing.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = TestWithParsing.h; path = unitTests/TestWithParsing.h; sourceTree = "<group>"; };
0A5A7E9C2C617EE00011C783 /* TestWithParsing.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = TestWithParsing.cpp; path = unitTests/TestWithParsing.cpp; sourceTree = "<group>"; };
0A67EA6719ACD37200830E3B /* unittests */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = unittests; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -456,6 +459,8 @@
0A67EA7419ACD43A00830E3B /* WriteBarrierTest.cpp */,
0A1C98562C3DD87300735850 /* unitTests/BytecodeGenerationTest.h */,
0A1C98572C3DD88500735850 /* unitTests/BytecodeGenerationTest.cpp */,
0A5A7E982C617E2B0011C783 /* TrivialMethodTest.h */,
0A5A7E992C617E400011C783 /* TrivialMethodTest.cpp */,
0A5A7E9B2C617EC70011C783 /* TestWithParsing.h */,
0A5A7E9C2C617EE00011C783 /* TestWithParsing.cpp */,
);
Expand Down Expand Up @@ -963,6 +968,7 @@
0A3A3CB21A5D5476004CB03B /* PrimitiveContainer.cpp in Sources */,
0A1C98582C3DD88500735850 /* unitTests/BytecodeGenerationTest.cpp in Sources */,
0A1C986F2C4F1D3900735850 /* debug.cpp in Sources */,
0A5A7E9A2C617E400011C783 /* TrivialMethodTest.cpp in Sources */,
0A67EA8419ACD74800830E3B /* VMFrame.cpp in Sources */,
0A67EA7D19ACD74800830E3B /* Signature.cpp in Sources */,
0A67EA7E19ACD74800830E3B /* VMArray.cpp in Sources */,
Expand Down
4 changes: 2 additions & 2 deletions src/misc/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ std::string DebugGetClassName(gc_oop_t obj) {
return CLASS_OF(obj)->GetName()->GetStdString();
}

void DebugDumpMethod(VMMethod* method) {
Disassembler::DumpMethod(method, "", false);
void DebugDumpMethod(VMInvokable* method) {
Disassembler::DumpMethod((VMMethod*)method, "", false);
}
2 changes: 1 addition & 1 deletion src/misc/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,4 @@ static inline void DebugTrace(const char* fmt, ...) {

std::string DebugGetClassName(vm_oop_t);
std::string DebugGetClassName(gc_oop_t);
void DebugDumpMethod(VMMethod* method);
void DebugDumpMethod(VMInvokable* method);
217 changes: 217 additions & 0 deletions src/unitTests/TrivialMethodTest.cpp
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)
*/
19 changes: 19 additions & 0 deletions src/unitTests/TrivialMethodTest.h
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);
};
2 changes: 2 additions & 0 deletions src/unitTests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "BasicInterpreterTests.h"
#include "BytecodeGenerationTest.h"
#include "CloneObjectsTest.h"
#include "TrivialMethodTest.h"
#include "WalkObjectsTest.h"

#if GC_TYPE == GENERATIONAL
Expand All @@ -32,6 +33,7 @@ CPPUNIT_TEST_SUITE_REGISTRATION(CloneObjectsTest);
CPPUNIT_TEST_SUITE_REGISTRATION(WriteBarrierTest);
#endif
CPPUNIT_TEST_SUITE_REGISTRATION(BytecodeGenerationTest);
CPPUNIT_TEST_SUITE_REGISTRATION(TrivialMethodTest);
CPPUNIT_TEST_SUITE_REGISTRATION(BasicInterpreterTests);

int main(int ac, char** av) {
Expand Down
5 changes: 5 additions & 0 deletions src/vm/IsValidObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ bool IsVMSymbol(vm_oop_t obj) {
return get_vtable(AS_OBJ(obj)) == vt_symbol;
}

bool IsLiteralReturn(vm_oop_t obj) {
assert(vt_literal_return != nullptr);
return get_vtable(AS_OBJ(obj)) == vt_literal_return;
}

void obtain_vtables_of_known_classes(VMSymbol* someValidSymbol) {
// These objects are allocated on the heap. So, they will get GC'ed soon
// enough.
Expand Down
1 change: 1 addition & 0 deletions src/vm/IsValidObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ bool IsValidObject(vm_oop_t obj);
bool IsVMInteger(vm_oop_t obj);
bool IsVMMethod(vm_oop_t obj);
bool IsVMSymbol(vm_oop_t obj);
bool IsLiteralReturn(vm_oop_t obj);

void set_vt_to_null();

Expand Down

0 comments on commit 43d8163

Please sign in to comment.