mirror of
https://github.com/usatiuk/psil.git
synced 2025-10-29 03:07:49 +01:00
compiler!
This commit is contained in:
@@ -28,7 +28,19 @@ target_link_libraries(
|
||||
GTest::gtest_main
|
||||
)
|
||||
|
||||
add_executable(
|
||||
CompilerTest
|
||||
CompilerTest.cpp
|
||||
)
|
||||
target_link_libraries(
|
||||
CompilerTest
|
||||
vm
|
||||
GTest::gtest_main
|
||||
)
|
||||
|
||||
|
||||
include(GoogleTest)
|
||||
gtest_discover_tests(VMTest)
|
||||
gtest_discover_tests(VMWithParserTest)
|
||||
gtest_discover_tests(GCTest)
|
||||
gtest_discover_tests(CompilerTest)
|
||||
|
||||
96
test/vm/CompilerTest.cpp
Normal file
96
test/vm/CompilerTest.cpp
Normal file
@@ -0,0 +1,96 @@
|
||||
//
|
||||
// Created by Stepan Usatiuk on 27.12.2023.
|
||||
//
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "MemoryContext.h"
|
||||
#include "Parser.h"
|
||||
#include "VM.h"
|
||||
|
||||
TEST(CompilerTest, BasicHello) {
|
||||
std::stringstream ssin;
|
||||
std::stringstream ssout;
|
||||
{
|
||||
MemoryContext mc;
|
||||
VM vm(ssin, ssout);
|
||||
Parser parser;
|
||||
parser.loadStr("(LDC 3 EVAL PRINT STOP)");
|
||||
vm.loadControl(parser.parseExpr());
|
||||
vm.run();
|
||||
}
|
||||
ssout.flush();
|
||||
EXPECT_EQ(ssout.str(), "3");
|
||||
}
|
||||
|
||||
TEST(CompilerTest, BasicLet) {
|
||||
std::stringstream ssin;
|
||||
std::stringstream ssout;
|
||||
{
|
||||
MemoryContext mc;
|
||||
VM vm(ssin, ssout);
|
||||
Parser parser;
|
||||
parser.loadStr("(LDC (let ((x 1)) x) EVAL PRINT STOP)");
|
||||
vm.loadControl(parser.parseExpr());
|
||||
vm.run();
|
||||
}
|
||||
ssout.flush();
|
||||
EXPECT_EQ(ssout.str(), "1");
|
||||
}
|
||||
|
||||
TEST(CompilerTest, BasicFn) {
|
||||
std::stringstream ssin;
|
||||
std::stringstream ssout;
|
||||
{
|
||||
MemoryContext mc;
|
||||
VM vm(ssin, ssout);
|
||||
Parser parser;
|
||||
parser.loadStr("(LDC (let ((plfn (lambda (a b) (+ a b)))) (plfn 2 3)) EVAL PRINT STOP)");
|
||||
vm.loadControl(parser.parseExpr());
|
||||
vm.run();
|
||||
}
|
||||
ssout.flush();
|
||||
EXPECT_EQ(ssout.str(), "5");
|
||||
}
|
||||
|
||||
TEST(CompilerTest, BasicFnIfT) {
|
||||
std::stringstream ssin;
|
||||
std::stringstream ssout;
|
||||
{
|
||||
MemoryContext mc;
|
||||
VM vm(ssin, ssout);
|
||||
Parser parser;
|
||||
parser.loadStr("(LDC (let ((plfn (lambda (a) (if a 1 2)))) (plfn 1)) EVAL PRINT STOP)");
|
||||
vm.loadControl(parser.parseExpr());
|
||||
vm.run();
|
||||
}
|
||||
ssout.flush();
|
||||
EXPECT_EQ(ssout.str(), "1");
|
||||
}
|
||||
TEST(CompilerTest, BasicFnIfF) {
|
||||
std::stringstream ssin;
|
||||
std::stringstream ssout;
|
||||
{
|
||||
MemoryContext mc;
|
||||
VM vm(ssin, ssout);
|
||||
Parser parser;
|
||||
parser.loadStr("(LDC (let ((plfn (lambda (a) (if a 1 2)))) (plfn 0)) EVAL PRINT STOP)");
|
||||
vm.loadControl(parser.parseExpr());
|
||||
vm.run();
|
||||
}
|
||||
ssout.flush();
|
||||
EXPECT_EQ(ssout.str(), "2");
|
||||
}
|
||||
TEST(CompilerTest, RecursiveFn) {
|
||||
std::stringstream ssin;
|
||||
std::stringstream ssout;
|
||||
{
|
||||
MemoryContext mc;
|
||||
VM vm(ssin, ssout);
|
||||
Parser parser;
|
||||
parser.loadStr("(LDC (letrec ((fib (lambda (n) (if n (if (+ n -1) (+ (fib (+ n -1)) (fib(+ n -2))) 1) 0) ))) (fib 10)) EVAL PRINT STOP)");
|
||||
vm.loadControl(parser.parseExpr());
|
||||
vm.run();
|
||||
}
|
||||
ssout.flush();
|
||||
EXPECT_EQ(ssout.str(), "55");
|
||||
}
|
||||
@@ -85,8 +85,9 @@ TEST(GCTest, GCTestAppend2) {
|
||||
|
||||
TEST(GCTest, GCTestAppend3) {
|
||||
MemoryContext mc;
|
||||
for (int i = 0; i < 25000; i++) {
|
||||
for (int i = 0; i < 250000; i++) {
|
||||
Handle c = Handle::cons(nullptr, nullptr);
|
||||
mc.request_gc();
|
||||
c.append(Handle::makeNumCell(1));
|
||||
c.append(Handle::makeNumCell(2));
|
||||
mc.request_gc();
|
||||
|
||||
Reference in New Issue
Block a user