Skip to content

Commit

Permalink
Assign Copy Construction
Browse files Browse the repository at this point in the history
  • Loading branch information
rdtscp committed Aug 18, 2020
1 parent 137bb3b commit 4b91ba2
Showing 2 changed files with 83 additions and 0 deletions.
21 changes: 21 additions & 0 deletions test/Test_CodeGeneration.cpp
Original file line number Diff line number Diff line change
@@ -38,6 +38,27 @@ TEST(Test_CodeGeneration, Allocations) {
ASSERT_EQ(system(binary.c_str()), 0);
}

TEST(Test_CodeGeneration, AssignOverload) {
const atl::string filepath = test_prefix + "AssignOverload/test.cpp";
const atl::shared_ptr<SourceFileHandler> src(new SourceFileHandler(filepath));
ACC::Preprocessor preprocessor(src, {});
ACC::Scanner scanner(preprocessor.getSource());
ACC::Lexer lexer(scanner);
ACC::Parser parser(lexer);
atl::shared_ptr<Program> progAST = parser.getAST();

SemanticAnalysis nameAnalysis(progAST);
nameAnalysis.run();
ASSERT_EQ(0, nameAnalysis.errorCount);

GenerateX64 x64Generator(progAST);
const atl::shared_ptr<SourceMemHandler> assembly = x64Generator.run();
const atl::string binary_name = test_prefix + "AssignOverload/binary";
LinkerBuilder linkAndBuilder(assembly, binary_name);
const atl::string binary = linkAndBuilder.linkAndBuild();
ASSERT_EQ(system(binary.c_str()), 0);
}

TEST(Test_CodeGeneration, BinOp) {
const atl::string filepath = test_prefix + "BinOp/test.cpp";
const atl::shared_ptr<SourceFileHandler> src(new SourceFileHandler(filepath));
62 changes: 62 additions & 0 deletions test/tests/Test_CodeGeneration/AssignOverload/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

void printf(const char *, const char *);

typedef unsigned int uint;

namespace test {
void swap(uint &lhs, uint &rhs) {
uint temp = lhs;
lhs = rhs;
rhs = temp;
}
void swap(const char *&lhs, const char *&rhs) {
const char *temp = lhs;
lhs = rhs;
rhs = temp;
}
} // namespace test

uint char_buf_len(const char *buf) {
uint length = 0u;
while (*buf != '\0') {
++length;
++buf;
}
return length;
}

class string_view {
private:
uint m_size;
const char *m_value;

public:
string_view(const char *value)
: m_size(char_buf_len(value)), m_value(value) {}

string_view(const string_view &rhs)
: m_size(rhs.m_size), m_value(rhs.m_value) {}

string_view &operator=(string_view rhs) {
test::swap(this->m_size, rhs.m_size);
test::swap(this->m_value, rhs.m_value);
}

~string_view() {}

const char *c_str() { return this->m_value; }

const char *c_str() const { return this->m_value; }

uint size() const { return this->m_size; }
};

int main(int argc, char **argv) {
string_view str("Test");

string_view copy = str;

printf("Copy: '%s'\n", copy.c_str());

return 4u - char_buf_len(copy.c_str());
}

0 comments on commit 4b91ba2

Please sign in to comment.