gc seems to work!

This commit is contained in:
2023-12-26 22:40:39 +01:00
parent a0febfba0e
commit a86012108e
15 changed files with 672 additions and 86 deletions

View File

@@ -18,6 +18,17 @@ target_link_libraries(
GTest::gtest_main
)
add_executable(
GCTest
GCTest.cpp
)
target_link_libraries(
GCTest
vm
GTest::gtest_main
)
include(GoogleTest)
gtest_discover_tests(VMTest)
gtest_discover_tests(VMWithParserTest)
gtest_discover_tests(GCTest)

92
test/vm/GCTest.cpp Normal file
View File

@@ -0,0 +1,92 @@
//
// Created by Stepan Usatiuk on 26.12.2023.
//
#include <gtest/gtest.h>
#include "ConsUtils.h"
#include "MemoryContext.h"
using namespace ConsUtils;
TEST(GCTest, GCTest) {
MemoryContext mc;
{
MCHandle c = cons(nullptr, nullptr);
mc.request_gc_and_wait();
append(c, makeNumCell(1));
append(c, makeNumCell(2));
mc.request_gc_and_wait();
EXPECT_EQ(val(car(c)), 1);
EXPECT_EQ(val(car(cdr(c))), 2);
}
mc.request_gc_and_wait();
mc.request_gc_and_wait();
mc.request_gc_and_wait();
EXPECT_EQ(mc.cell_count(), 0);
{
MCHandle c = cons(nullptr, nullptr);
mc.request_gc_and_wait();
push(c, makeNumCell(1));
push(c, makeNumCell(2));
mc.request_gc_and_wait();
EXPECT_EQ(val(car(c)), 2);
EXPECT_EQ(val(car(cdr(c))), 1);
}
mc.request_gc_and_wait();
mc.request_gc_and_wait();
mc.request_gc_and_wait();
EXPECT_EQ(mc.cell_count(), 0);
}
TEST(GCTest, GCTestAppend) {
MemoryContext mc;
for (int i = 0; i < 25000; i++) {
MCHandle c = cons(nullptr, nullptr);
mc.request_gc();
append(c, makeNumCell(1));
mc.request_gc();
EXPECT_EQ(val(car(c)), 1);
}
mc.request_gc_and_wait();
mc.request_gc_and_wait();
mc.request_gc_and_wait();
EXPECT_EQ(mc.cell_count(), 0);
}
TEST(GCTest, GCTestPop) {
MemoryContext mc;
{
MCHandle c = cons(nullptr, nullptr);
static constexpr int test_size = 20000;
for (int i = 0; i < test_size; i++) {
mc.request_gc();
push(c, makeNumCell(i));
}
for (int i = test_size - 1; i >= 0; i--) {
mc.request_gc();
EXPECT_EQ(i, val(pop(c)));
}
}
mc.request_gc_and_wait();
mc.request_gc_and_wait();
mc.request_gc_and_wait();
EXPECT_EQ(mc.cell_count(), 0);
}
TEST(GCTest, GCTestAppend2) {
MemoryContext mc;
MCHandle c = cons(nullptr, nullptr);
static constexpr int test_size = 2000;
for (int i = 0; i < test_size; i++) {
mc.request_gc();
append(c, makeNumCell(i));
}
for (int i = 0; i < test_size; i++) {
mc.request_gc();
EXPECT_EQ(i, val(pop(c)));
}
mc.request_gc_and_wait();
mc.request_gc_and_wait();
mc.request_gc_and_wait();
EXPECT_EQ(mc.cell_count(), 0);
}

View File

@@ -52,15 +52,19 @@ TEST(VMWithParserTest, BasicFunction) {
TEST(VMWithParserTest, RecFunction) {
std::stringstream ssin;
std::stringstream ssout;
MemoryContext mc;
{
MemoryContext mc;
VM vm(ssin, ssout);
Parser parser;
parser.loadStr(
"( DUM NIL LDF ( LD ( 1 . 1 ) SEL ( LD ( 1 . 1 ) LDC -1 ADD SEL ( NIL LD ( 1 . 1 ) LDC -1 ADD CONS LD ( 2 . 1 ) AP NIL LD ( 1 . 1 ) LDC -2 ADD CONS LD ( 2 . 1 ) AP ADD JOIN ) ( LDC 1 JOIN ) JOIN ) ( LDC 0 JOIN ) RET ) CONS LDF ( NIL LDC 10 CONS LD ( 1 . 1 ) AP RET ) RAP PUTNUM STOP )");
"( DUM NIL LDF ( LD ( 1 . 1 ) SEL ( LD ( 1 . 1 ) LDC -1 ADD SEL ( NIL LD ( 1 . 1 ) LDC -1 ADD CONS LD ( 2 . 1 ) AP NIL LD ( 1 . 1 ) LDC -2 ADD CONS LD ( 2 . 1 ) AP ADD JOIN ) ( LDC 1 JOIN ) JOIN ) ( LDC 0 JOIN ) RET ) CONS LDF ( NIL LDC 20 CONS LD ( 1 . 1 ) AP RET ) RAP PUTNUM STOP )");
vm.loadControl(parser.parseExpr());
vm.run();
}
mc.request_gc_and_wait();
mc.request_gc_and_wait();
mc.request_gc_and_wait();
EXPECT_EQ(mc.cell_count(), 0);
ssout.flush();
EXPECT_EQ(ssout.str(), "55");
EXPECT_EQ(ssout.str(), "6765");
}