From b56e0deca78adc49338e1daa3d148f2c6dee13f9 Mon Sep 17 00:00:00 2001 From: dragoncoder047 <101021094+dragoncoder047@users.noreply.github.com> Date: Mon, 8 Apr 2024 16:37:26 +0000 Subject: [PATCH] test inheritance --- pickle.cpp | 12 ++++++++++-- pickle_test.cpp | 6 ++++++ test/out32.txt | 4 ++++ test/out64.txt | 4 ++++ test/valgrind32.txt | 26 +++++++++++++------------- test/valgrind64.txt | 26 +++++++++++++------------- 6 files changed, 50 insertions(+), 28 deletions(-) diff --git a/pickle.cpp b/pickle.cpp index 99f146c..e30b383 100644 --- a/pickle.cpp +++ b/pickle.cpp @@ -526,14 +526,22 @@ object* pvm::get_property(object* obj, uint64_t hash, bool recurse) { // Nil has no properties if (!obj) return nil; if (recurse) { + DBG("Inheritance requested get_property() {"); // Try to find it directly. object* val = this->get_property(obj, hash, false); - if (val) return val; + if (val) { + DBG("Own property. }"); + return val; + } // Not found, traverse prototypes list. for (object* p = car(obj); p; p = cdr(p)) { val = this->get_property(car(p), hash, true); - if (val) return val; + if (val) { + DBG("Parent property. }"); + return val; + } } + DBG("Property not found in inheritance tree. }"); return nil; } // Check if it is an object-object (primitives have no own properties) diff --git a/pickle_test.cpp b/pickle_test.cpp index 632332a..91ec2fc 100644 --- a/pickle_test.cpp +++ b/pickle_test.cpp @@ -107,6 +107,12 @@ int main() { vm.dump(hash0); putchar('\n'); vm.dump(foo); + printf("\nCreate child object\n"); + auto bar = vm.newobject(vm.cons(foo, nil)); + vm.dump(bar); + printf("\nGet property 0 with inheritance and without\n"); + ASSERT(vm.get_property(bar, 0, false) == nil); + ASSERT(vm.get_property(bar, 0, true) != nil); SEPARATOR; printf("all done -- cleaning up\n"); // implicit destruction of vm; diff --git a/test/out32.txt b/test/out32.txt index b21a7dd..22d89d4 100644 --- a/test/out32.txt +++ b/test/out32.txt @@ -98,6 +98,10 @@ Dump of object: #1=object{ 0 -> 0 ;[hash=0] 1 -> #1# ;[hash=1] 3 -> #1# ;[hash=3 Get hash 0: 0 #1=object{ 0 -> 0 ;[hash=0] 1 -> #1# ;[hash=1] 3 -> #1# ;[hash=3] 7 -> #1# ;[hash=7] 5 -> #1# ;[hash=5] 9 -> #1# ;[hash=9] 2 -> 2 ;[hash=2] 6 -> 6 ;[hash=6] 4 -> 4 ;[hash=4] 8 -> 8 ;[hash=8] } +Create child object +object{ } +Get property 0 with inheritance and without + ---------------------------------------------------------------------------------------- diff --git a/test/out64.txt b/test/out64.txt index b21a7dd..22d89d4 100644 --- a/test/out64.txt +++ b/test/out64.txt @@ -98,6 +98,10 @@ Dump of object: #1=object{ 0 -> 0 ;[hash=0] 1 -> #1# ;[hash=1] 3 -> #1# ;[hash=3 Get hash 0: 0 #1=object{ 0 -> 0 ;[hash=0] 1 -> #1# ;[hash=1] 3 -> #1# ;[hash=3] 7 -> #1# ;[hash=7] 5 -> #1# ;[hash=5] 9 -> #1# ;[hash=9] 2 -> 2 ;[hash=2] 6 -> 6 ;[hash=6] 4 -> 4 ;[hash=4] 8 -> 8 ;[hash=8] } +Create child object +object{ } +Get property 0 with inheritance and without + ---------------------------------------------------------------------------------------- diff --git a/test/valgrind32.txt b/test/valgrind32.txt index 6792e5d..3e8f34c 100644 --- a/test/valgrind32.txt +++ b/test/valgrind32.txt @@ -1,13 +1,13 @@ -==76823== Memcheck, a memory error detector -==76823== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. -==76823== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info -==76823== Command: ./pickletest32 -==76823== -==76823== -==76823== HEAP SUMMARY: -==76823== in use at exit: 0 bytes in 0 blocks -==76823== total heap usage: 134 allocs, 134 frees, 64,846 bytes allocated -==76823== -==76823== All heap blocks were freed -- no leaks are possible -==76823== -==76823== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) +==5352== Memcheck, a memory error detector +==5352== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. +==5352== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info +==5352== Command: ./pickletest32 +==5352== +==5352== +==5352== HEAP SUMMARY: +==5352== in use at exit: 0 bytes in 0 blocks +==5352== total heap usage: 134 allocs, 134 frees, 64,846 bytes allocated +==5352== +==5352== All heap blocks were freed -- no leaks are possible +==5352== +==5352== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) diff --git a/test/valgrind64.txt b/test/valgrind64.txt index ad2b54f..8199175 100644 --- a/test/valgrind64.txt +++ b/test/valgrind64.txt @@ -1,13 +1,13 @@ -==76787== Memcheck, a memory error detector -==76787== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. -==76787== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info -==76787== Command: ./pickletest64 -==76787== -==76787== -==76787== HEAP SUMMARY: -==76787== in use at exit: 0 bytes in 0 blocks -==76787== total heap usage: 134 allocs, 134 frees, 140,166 bytes allocated -==76787== -==76787== All heap blocks were freed -- no leaks are possible -==76787== -==76787== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) +==5327== Memcheck, a memory error detector +==5327== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. +==5327== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info +==5327== Command: ./pickletest64 +==5327== +==5327== +==5327== HEAP SUMMARY: +==5327== in use at exit: 0 bytes in 0 blocks +==5327== total heap usage: 134 allocs, 134 frees, 140,166 bytes allocated +==5327== +==5327== All heap blocks were freed -- no leaks are possible +==5327== +==5327== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)