-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds basic tests and patterns for type inference
Issue: #92
- Loading branch information
Showing
4 changed files
with
254 additions
and
4 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
Binary file not shown.
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,232 @@ | ||
#include <gtest/gtest.h> | ||
#include "patterns/InitVMImage.h" | ||
#include <inference.h> | ||
|
||
class P_Inference : public P_InitVM_Image | ||
{ | ||
public: | ||
type::TypeSystem* m_type_system; | ||
|
||
P_Inference() : P_InitVM_Image(), m_type_system(0) {} | ||
virtual ~P_Inference() {} | ||
|
||
virtual void SetUp() | ||
{ | ||
P_InitVM_Image::SetUp(); | ||
m_type_system = new type::TypeSystem(*m_vm); | ||
} | ||
virtual void TearDown() { | ||
P_InitVM_Image::TearDown(); | ||
delete m_type_system; | ||
} | ||
|
||
type::InferContext* inferMessage( | ||
TClass* const objectClass, | ||
const std::string& methodName, | ||
const type::Type& args, | ||
type::TContextStack* parent, | ||
bool sendToSuper = false | ||
) { | ||
EXPECT_NE(static_cast<TClass*>(0), objectClass) << "Class is null"; | ||
TMethod* const method = objectClass->methods->find<TMethod>(methodName.c_str()); | ||
EXPECT_NE(static_cast<TMethod*>(0), method) << "Method is null"; | ||
|
||
TSymbol* const selector = method->name; | ||
return m_type_system->inferMessage(selector, args, parent, sendToSuper); | ||
} | ||
|
||
type::InferContext* inferMessage( | ||
const std::string& className, | ||
const std::string& methodName, | ||
const type::Type& args, | ||
type::TContextStack* parent, | ||
bool sendToSuper = false | ||
) { | ||
TClass* const objectClass = m_image->getGlobal<TClass>(className.c_str()); | ||
return this->inferMessage(objectClass, methodName, args, parent, sendToSuper); | ||
} | ||
|
||
}; | ||
|
||
INSTANTIATE_TEST_CASE_P(_, P_Inference, ::testing::Values(std::string("Inference")) ); | ||
|
||
TEST_P(P_Inference, SmallInt) | ||
{ | ||
{ | ||
SCOPED_TRACE("SmallInt>>asSmallInt"); | ||
type::Type args(globals.arrayClass, type::Type::tkArray); | ||
args.pushSubType(type::Type(TInteger(42))); | ||
|
||
type::InferContext* const inferContext = this->inferMessage("SmallInt", "asSmallInt", args, 0); | ||
ASSERT_EQ( type::Type(TInteger(42)), inferContext->getReturnType() ); | ||
} | ||
{ | ||
SCOPED_TRACE("SmallInt>>+ SmallInt"); | ||
type::Type args(globals.arrayClass, type::Type::tkArray); | ||
args.pushSubType(type::Type(TInteger(40))); | ||
args.pushSubType(type::Type(TInteger(2))); | ||
|
||
type::InferContext* const inferContext = this->inferMessage("SmallInt", "+", args, 0); | ||
ASSERT_EQ( type::Type(TInteger(42)), inferContext->getReturnType() ); | ||
} | ||
} | ||
|
||
TEST_P(P_Inference, Number) | ||
{ | ||
{ | ||
SCOPED_TRACE("Number::new"); | ||
type::Type args(globals.arrayClass, type::Type::tkArray); | ||
TClass* const metaNumberClass = m_image->getGlobal<TClass>("Number")->getClass(); | ||
args.pushSubType(type::Type(metaNumberClass)); | ||
|
||
type::InferContext* const inferContext = this->inferMessage(metaNumberClass, "new", args, 0); | ||
ASSERT_EQ( type::Type(TInteger(0)), inferContext->getReturnType() ); | ||
} | ||
{ | ||
SCOPED_TRACE("Number>>factorial"); | ||
type::Type args(globals.arrayClass, type::Type::tkArray); | ||
args.pushSubType(type::Type(TInteger(4))); | ||
|
||
type::InferContext* const inferContext = this->inferMessage("Number", "factorial", args, 0); | ||
ASSERT_EQ( type::Type(TInteger(24)), inferContext->getReturnType() ); | ||
} | ||
{ | ||
SCOPED_TRACE("Number>>negative"); | ||
{ | ||
SCOPED_TRACE("-SmallInt"); | ||
type::Type args(globals.arrayClass, type::Type::tkArray); | ||
args.pushSubType(type::Type(TInteger(-1))); | ||
|
||
type::InferContext* const inferContext = this->inferMessage("Number", "negative", args, 0); | ||
ASSERT_EQ( type::Type(globals.trueObject), inferContext->getReturnType() ); | ||
} | ||
{ | ||
SCOPED_TRACE("+SmallInt"); | ||
type::Type args(globals.arrayClass, type::Type::tkArray); | ||
args.pushSubType(type::Type(TInteger(1))); | ||
|
||
type::InferContext* const inferContext = this->inferMessage("Number", "negative", args, 0); | ||
ASSERT_EQ( type::Type(globals.falseObject), inferContext->getReturnType() ); | ||
} | ||
} | ||
{ | ||
SCOPED_TRACE("Number>>negated"); | ||
{ | ||
SCOPED_TRACE("-SmallInt"); | ||
type::Type args(globals.arrayClass, type::Type::tkArray); | ||
args.pushSubType(type::Type(TInteger(-1))); | ||
|
||
type::InferContext* const inferContext = this->inferMessage("Number", "negated", args, 0); | ||
ASSERT_EQ( type::Type(TInteger(1)), inferContext->getReturnType() ); | ||
} | ||
{ | ||
SCOPED_TRACE("+SmallInt"); | ||
type::Type args(globals.arrayClass, type::Type::tkArray); | ||
args.pushSubType(type::Type(TInteger(1))); | ||
|
||
type::InferContext* const inferContext = this->inferMessage("Number", "negated", args, 0); | ||
ASSERT_EQ( type::Type(TInteger(-1)), inferContext->getReturnType() ); | ||
} | ||
} | ||
{ | ||
SCOPED_TRACE("Number>>absolute"); | ||
{ | ||
SCOPED_TRACE("-SmallInt"); | ||
type::Type args(globals.arrayClass, type::Type::tkArray); | ||
args.pushSubType(type::Type(TInteger(-1))); | ||
|
||
type::InferContext* const inferContext = this->inferMessage("Number", "absolute", args, 0); | ||
ASSERT_EQ( type::Type(TInteger(1)), inferContext->getReturnType() ); | ||
} | ||
{ | ||
SCOPED_TRACE("+SmallInt"); | ||
type::Type args(globals.arrayClass, type::Type::tkArray); | ||
args.pushSubType(type::Type(TInteger(1))); | ||
|
||
type::InferContext* const inferContext = this->inferMessage("Number", "absolute", args, 0); | ||
ASSERT_EQ( type::Type(TInteger(1)), inferContext->getReturnType() ); | ||
} | ||
} | ||
} | ||
|
||
TEST_P(P_Inference, True) | ||
{ | ||
{ | ||
SCOPED_TRACE("True>>not"); | ||
type::Type args(globals.arrayClass, type::Type::tkArray); | ||
args.pushSubType(type::Type(globals.trueObject)); | ||
|
||
type::InferContext* const inferContext = this->inferMessage("True", "not", args, 0); | ||
ASSERT_EQ( type::Type(globals.falseObject), inferContext->getReturnType() ); | ||
} | ||
{ | ||
SCOPED_TRACE("True>>and:"); | ||
type::Type args(globals.arrayClass, type::Type::tkArray); | ||
args.pushSubType(type::Type(globals.trueObject)); | ||
args.pushSubType(type::Type(globals.blockClass)); | ||
|
||
type::InferContext* const inferContext = this->inferMessage("True", "and:", args, 0); | ||
ASSERT_EQ( type::Type(type::Type::tkPolytype), inferContext->getReturnType() ); // FIXME: expect Boolean type | ||
} | ||
{ | ||
SCOPED_TRACE("True>>or:"); | ||
type::Type args(globals.arrayClass, type::Type::tkArray); | ||
args.pushSubType(type::Type(globals.trueObject)); | ||
args.pushSubType(type::Type(globals.blockClass)); | ||
|
||
type::InferContext* const inferContext = this->inferMessage("True", "or:", args, 0); | ||
ASSERT_EQ( type::Type(globals.trueObject), inferContext->getReturnType() ); | ||
} | ||
} | ||
|
||
TEST_P(P_Inference, False) | ||
{ | ||
{ | ||
SCOPED_TRACE("False>>not"); | ||
type::Type args(globals.arrayClass, type::Type::tkArray); | ||
args.pushSubType(type::Type(globals.falseObject)); | ||
|
||
type::InferContext* const inferContext = this->inferMessage("False", "not", args, 0); | ||
ASSERT_EQ( type::Type(globals.trueObject), inferContext->getReturnType() ); | ||
} | ||
{ | ||
SCOPED_TRACE("False>>and:"); | ||
type::Type args(globals.arrayClass, type::Type::tkArray); | ||
args.pushSubType(type::Type(globals.falseObject)); | ||
args.pushSubType(type::Type(globals.blockClass)); | ||
|
||
type::InferContext* const inferContext = this->inferMessage("False", "and:", args, 0); | ||
ASSERT_EQ( type::Type(globals.falseObject), inferContext->getReturnType() ); | ||
} | ||
{ | ||
SCOPED_TRACE("False>>or:"); | ||
type::Type args(globals.arrayClass, type::Type::tkArray); | ||
args.pushSubType(type::Type(globals.falseObject)); | ||
args.pushSubType(type::Type(globals.blockClass)); | ||
|
||
type::InferContext* const inferContext = this->inferMessage("False", "or:", args, 0); | ||
ASSERT_EQ( type::Type(type::Type::tkPolytype), inferContext->getReturnType() ); // FIXME: expect Boolean type | ||
} | ||
} | ||
|
||
TEST_P(P_Inference, Array) | ||
{ | ||
{ | ||
SCOPED_TRACE("Array>>sort:"); | ||
type::Type args(globals.arrayClass, type::Type::tkArray); | ||
args.pushSubType(type::Type(globals.arrayClass)); | ||
args.pushSubType(type::Type(globals.blockClass)); | ||
|
||
type::InferContext* const inferContext = this->inferMessage("Array", "sort:", args, 0); | ||
ASSERT_EQ( type::Type(globals.arrayClass), inferContext->getReturnType() ); | ||
} | ||
{ | ||
SCOPED_TRACE("Array::new"); | ||
type::Type args(globals.arrayClass, type::Type::tkArray); | ||
TClass* const metaArrayClass = m_image->getGlobal<TClass>("Array")->getClass(); | ||
args.pushSubType(type::Type(metaArrayClass)); | ||
|
||
type::InferContext* const inferContext = this->inferMessage(metaArrayClass, "new", args, 0); | ||
ASSERT_EQ( type::Type(globals.arrayClass), inferContext->getReturnType() ); | ||
} | ||
} |
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