-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ Added simple GraphComparator for UnitTests based on gtest + Added new instruction Compare
- Loading branch information
1 parent
4cc05e8
commit 4f5b734
Showing
9 changed files
with
227 additions
and
5 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,62 @@ | ||
#pragma once | ||
|
||
#include "graph.h" | ||
#include "inst.h" | ||
#include <type_traits> | ||
|
||
namespace compiler { | ||
|
||
class IrConstructor { | ||
public: | ||
IrConstructor(): | ||
graph_(new Graph()) { | ||
graph_->SetUnitTestMode(); | ||
}; | ||
|
||
~IrConstructor() { | ||
delete graph_; | ||
} | ||
template<typename T> | ||
IrConstructor &CreateInst(uint32_t index) { | ||
auto inst = graph_->CreateInstByIndex<T>(index); | ||
current_inst_ = inst; | ||
return *this; | ||
} | ||
|
||
IrConstructor &Imm(int64_t imm) { | ||
assert(current_inst_ != nullptr); | ||
|
||
switch(current_inst_->GetOpcode()) { | ||
case Opcode::Constant: { | ||
static_cast<ConstantInst *>(current_inst_)->SetImm(imm); | ||
break; | ||
} | ||
default: { | ||
assert(false && ("Should be unreachable!")); | ||
} | ||
} | ||
return *this; | ||
} | ||
|
||
template<typename... Args> | ||
IrConstructor &Inputs(Args ...args) { | ||
for (auto it : {args...}) { | ||
static_assert(std::is_same<decltype(it), int>(), "Is not \"Int\" in argument"); | ||
} | ||
auto inputs = std::vector<int>({args...}); | ||
for (int i = 0; i < inputs.size(); i++) { | ||
current_inst_->SetInput(i, graph_->GetInstByIndex(inputs.at(i))); | ||
} | ||
return *this; | ||
} | ||
|
||
Graph *GetGraph() { | ||
return graph_; | ||
} | ||
|
||
private: | ||
Graph *graph_; | ||
Inst *current_inst_; | ||
}; | ||
|
||
} |
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,42 @@ | ||
#include "graph_comparator.h" | ||
#include "inst.h" | ||
|
||
namespace compiler { | ||
|
||
void GraphComparator::Compare() { | ||
ASSERT_EQ(left_->GetMethodName(), right_->GetMethodName()); | ||
ASSERT_EQ(left_->GetNumInsts(), right_->GetNumInsts()); | ||
for (int i = 0; i < left_->GetNumInsts(); i++) { | ||
auto left_inst = left_->GetInstByIndex(i); | ||
auto right_inst = right_->GetInstByIndex(i); | ||
|
||
CompareInstructions(left_inst, right_inst); | ||
} | ||
} | ||
|
||
void GraphComparator::CompareInstructions(Inst *left, Inst *right) { | ||
ASSERT_EQ(left->GetId(), right->GetId()); | ||
ASSERT_EQ(left->GetOpcode(), right->GetOpcode()); | ||
ASSERT_EQ(left->GetType(), right->GetType()); | ||
CompareInputs(left, right); | ||
|
||
// Specific checks | ||
switch (left->GetOpcode()) { | ||
case Opcode::Constant: { | ||
ASSERT_EQ(static_cast<ConstantInst *>(left)->GetImm(), static_cast<ConstantInst *>(right)->GetImm()); | ||
break; | ||
} | ||
default: { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
void GraphComparator::CompareInputs(Inst *left, Inst *right) { | ||
ASSERT_EQ(left->NumInputs(), right->NumInputs()); | ||
for (int i = 0; i < left->NumInputs(); i++) { | ||
ASSERT_EQ(left->GetInput(i)->GetId(), right->GetInput(i)->GetId()); | ||
} | ||
} | ||
|
||
} |
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,25 @@ | ||
#pragma once | ||
|
||
#include "graph.h" | ||
#include <gtest/gtest.h> | ||
|
||
namespace compiler { | ||
|
||
class GraphComparator | ||
{ | ||
public: | ||
GraphComparator(Graph *left, Graph *right): | ||
left_(left), right_(right) {} | ||
|
||
void Compare(); | ||
|
||
private: | ||
void CompareInstructions(Inst *left, Inst *right); | ||
void CompareInputs(Inst *left, Inst *right); | ||
|
||
private: | ||
Graph *left_; | ||
Graph *right_; | ||
}; | ||
|
||
} |
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