mirror of
https://github.com/usatiuk/psil.git
synced 2025-10-29 03:07:49 +01:00
get MemoryContext with get()
This commit is contained in:
@@ -11,7 +11,7 @@ TEST(CompilerTest, BasicHello) {
|
||||
std::stringstream ssin;
|
||||
std::stringstream ssout;
|
||||
{
|
||||
MemoryContext mc;
|
||||
|
||||
VM vm(ssin, ssout);
|
||||
Parser parser;
|
||||
parser.loadStr("(LDC 3 EVAL PRINT STOP)");
|
||||
@@ -26,7 +26,7 @@ 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)");
|
||||
@@ -41,7 +41,7 @@ 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)");
|
||||
@@ -56,7 +56,7 @@ 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)");
|
||||
@@ -70,7 +70,7 @@ 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)");
|
||||
@@ -84,7 +84,7 @@ 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)");
|
||||
|
||||
@@ -7,96 +7,96 @@
|
||||
#include "MemoryContext.h"
|
||||
|
||||
TEST(GCTest, GCTest) {
|
||||
MemoryContext mc;
|
||||
|
||||
{
|
||||
Handle c = Handle::cons(nullptr, nullptr);
|
||||
mc.request_gc_and_wait();
|
||||
MemoryContext::get().request_gc_and_wait();
|
||||
c.append(Handle::makeNumCell(1));
|
||||
c.append(Handle::makeNumCell(2));
|
||||
mc.request_gc_and_wait();
|
||||
MemoryContext::get().request_gc_and_wait();
|
||||
EXPECT_EQ(c.car().val(), 1);
|
||||
EXPECT_EQ(c.cdr().car().val(), 2);
|
||||
}
|
||||
mc.request_gc_and_wait();
|
||||
mc.request_gc_and_wait();
|
||||
EXPECT_EQ(mc.cell_count(), 0);
|
||||
MemoryContext::get().request_gc_and_wait();
|
||||
MemoryContext::get().request_gc_and_wait();
|
||||
EXPECT_EQ(MemoryContext::get().cell_count(), 0);
|
||||
{
|
||||
Handle c = Handle::cons(nullptr, nullptr);
|
||||
mc.request_gc_and_wait();
|
||||
MemoryContext::get().request_gc_and_wait();
|
||||
c.push(Handle::makeNumCell(1));
|
||||
c.push(Handle::makeNumCell(2));
|
||||
mc.request_gc_and_wait();
|
||||
MemoryContext::get().request_gc_and_wait();
|
||||
EXPECT_EQ(c.car().val(), 2);
|
||||
EXPECT_EQ(c.cdr().car().val(), 1);
|
||||
}
|
||||
mc.request_gc_and_wait();
|
||||
mc.request_gc_and_wait();
|
||||
EXPECT_EQ(mc.cell_count(), 0);
|
||||
MemoryContext::get().request_gc_and_wait();
|
||||
MemoryContext::get().request_gc_and_wait();
|
||||
EXPECT_EQ(MemoryContext::get().cell_count(), 0);
|
||||
}
|
||||
|
||||
TEST(GCTest, GCTestAppend) {
|
||||
MemoryContext mc;
|
||||
|
||||
for (int i = 0; i < 25000; i++) {
|
||||
Handle c = Handle::cons(nullptr, nullptr);
|
||||
mc.request_gc();
|
||||
MemoryContext::get().request_gc();
|
||||
c.append(Handle::makeNumCell(1));
|
||||
mc.request_gc();
|
||||
MemoryContext::get().request_gc();
|
||||
EXPECT_EQ(c.car().val(), 1);
|
||||
}
|
||||
mc.request_gc_and_wait();
|
||||
mc.request_gc_and_wait();
|
||||
EXPECT_EQ(mc.cell_count(), 0);
|
||||
MemoryContext::get().request_gc_and_wait();
|
||||
MemoryContext::get().request_gc_and_wait();
|
||||
EXPECT_EQ(MemoryContext::get().cell_count(), 0);
|
||||
}
|
||||
TEST(GCTest, GCTestPop) {
|
||||
MemoryContext mc;
|
||||
|
||||
{
|
||||
Handle c = Handle::cons(nullptr, nullptr);
|
||||
static constexpr int test_size = 20000;
|
||||
for (int i = 0; i < test_size; i++) {
|
||||
mc.request_gc();
|
||||
MemoryContext::get().request_gc();
|
||||
c.push(Handle::makeNumCell(i));
|
||||
}
|
||||
for (int i = test_size - 1; i >= 0; i--) {
|
||||
mc.request_gc();
|
||||
MemoryContext::get().request_gc();
|
||||
EXPECT_EQ(i, c.pop().val());
|
||||
}
|
||||
}
|
||||
mc.request_gc_and_wait();
|
||||
mc.request_gc_and_wait();
|
||||
EXPECT_EQ(mc.cell_count(), 0);
|
||||
MemoryContext::get().request_gc_and_wait();
|
||||
MemoryContext::get().request_gc_and_wait();
|
||||
EXPECT_EQ(MemoryContext::get().cell_count(), 0);
|
||||
}
|
||||
|
||||
TEST(GCTest, GCTestAppend2) {
|
||||
MemoryContext mc;
|
||||
|
||||
Handle c = Handle::cons(nullptr, nullptr);
|
||||
static constexpr int test_size = 2000;
|
||||
for (int i = 0; i < test_size; i++) {
|
||||
mc.request_gc();
|
||||
MemoryContext::get().request_gc();
|
||||
c.append(Handle::makeNumCell(i));
|
||||
}
|
||||
for (int i = 0; i < test_size; i++) {
|
||||
mc.request_gc();
|
||||
MemoryContext::get().request_gc();
|
||||
EXPECT_EQ(i, c.pop().val());
|
||||
}
|
||||
mc.request_gc_and_wait();
|
||||
mc.request_gc_and_wait();
|
||||
EXPECT_EQ(mc.cell_count(), 0);
|
||||
MemoryContext::get().request_gc_and_wait();
|
||||
MemoryContext::get().request_gc_and_wait();
|
||||
EXPECT_EQ(MemoryContext::get().cell_count(), 0);
|
||||
}
|
||||
|
||||
TEST(GCTest, GCTestAppend3) {
|
||||
MemoryContext mc;
|
||||
|
||||
for (int i = 0; i < 250000; i++) {
|
||||
Handle c = Handle::cons(nullptr, nullptr);
|
||||
mc.request_gc();
|
||||
MemoryContext::get().request_gc();
|
||||
c.append(Handle::makeNumCell(1));
|
||||
c.append(Handle::makeNumCell(2));
|
||||
mc.request_gc();
|
||||
MemoryContext::get().request_gc();
|
||||
Handle n = c.cdr();
|
||||
c.setcdr(nullptr);
|
||||
EXPECT_EQ(n.car().val(), 2);
|
||||
EXPECT_EQ(c.car().val(), 1);
|
||||
}
|
||||
mc.request_gc_and_wait();
|
||||
mc.request_gc_and_wait();
|
||||
EXPECT_EQ(mc.cell_count(), 0);
|
||||
MemoryContext::get().request_gc_and_wait();
|
||||
MemoryContext::get().request_gc_and_wait();
|
||||
EXPECT_EQ(MemoryContext::get().cell_count(), 0);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ TEST(VMTest, BasicHello) {
|
||||
std::stringstream ssin;
|
||||
std::stringstream ssout;
|
||||
{
|
||||
MemoryContext mc;
|
||||
|
||||
VM vm(ssin, ssout);
|
||||
Handle newc(Handle::cons(nullptr, nullptr));
|
||||
newc.append(Handle::makeStrCell("NIL"));
|
||||
|
||||
@@ -7,7 +7,7 @@ TEST(VMWithParserTest, BasicHello) {
|
||||
std::stringstream ssin;
|
||||
std::stringstream ssout;
|
||||
{
|
||||
MemoryContext mc;
|
||||
|
||||
VM vm(ssin, ssout);
|
||||
Parser parser;
|
||||
parser.loadStr("(LDC 104 PUTCHAR STOP)");
|
||||
@@ -22,7 +22,7 @@ TEST(VMWithParserTest, BasicBranch) {
|
||||
std::stringstream ssin;
|
||||
std::stringstream ssout;
|
||||
{
|
||||
MemoryContext mc;
|
||||
|
||||
VM vm(ssin, ssout);
|
||||
Parser parser;
|
||||
parser.loadStr(
|
||||
@@ -38,7 +38,7 @@ TEST(VMWithParserTest, BasicFunction) {
|
||||
std::stringstream ssin;
|
||||
std::stringstream ssout;
|
||||
{
|
||||
MemoryContext mc;
|
||||
|
||||
VM vm(ssin, ssout);
|
||||
Parser parser;
|
||||
parser.loadStr("(NIL LDC 1 CONS LDC 2 CONS LDF (LD (1 . 1) LD (1.2) ADD RET) AP PUTNUM STOP)");
|
||||
@@ -52,7 +52,7 @@ TEST(VMWithParserTest, BasicFunction) {
|
||||
TEST(VMWithParserTest, RecFunction) {
|
||||
std::stringstream ssin;
|
||||
std::stringstream ssout;
|
||||
MemoryContext mc;
|
||||
|
||||
{
|
||||
VM vm(ssin, ssout);
|
||||
Parser parser;
|
||||
@@ -61,9 +61,9 @@ TEST(VMWithParserTest, RecFunction) {
|
||||
vm.loadControl(parser.parseExpr());
|
||||
vm.run();
|
||||
}
|
||||
mc.request_gc_and_wait();
|
||||
mc.request_gc_and_wait();
|
||||
EXPECT_EQ(mc.cell_count(), 0);
|
||||
MemoryContext::get().request_gc_and_wait();
|
||||
MemoryContext::get().request_gc_and_wait();
|
||||
EXPECT_EQ(MemoryContext::get().cell_count(), 0);
|
||||
ssout.flush();
|
||||
EXPECT_EQ(ssout.str(), "6765");
|
||||
}
|
||||
Reference in New Issue
Block a user