Skip to content

Commit

Permalink
[cpp refactor] Consolidate tests into cpp/obj_layout_test.cc
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy C committed Nov 14, 2022
1 parent 90754be commit 7e83c1e
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 108 deletions.
9 changes: 8 additions & 1 deletion cpp/NINJA_subgraph.py
Expand Up @@ -21,6 +21,14 @@
def NinjaGraph(ru):
ru.comment('Generated by %s' % __name__)

ru.cc_binary(
'cpp/obj_layout_test.cc',
deps = [
'//core/runtime.asdl',
'//mycpp/runtime',
],
matrix = ninja_lib.COMPILERS_VARIANTS)

ru.cc_library(
'//cpp/core',
srcs = ['cpp/core.cc'],
Expand Down Expand Up @@ -139,7 +147,6 @@ def NinjaGraph(ru):
ru.cc_binary(
'cpp/stdlib_test.cc',
deps = [
'//core/runtime.asdl', # sizeof_test uses this
'//cpp/stdlib',
],
matrix = ninja_lib.COMPILERS_VARIANTS)
Expand Down
4 changes: 4 additions & 0 deletions cpp/TEST.sh
Expand Up @@ -68,6 +68,8 @@ unit() {
# TODO: Move all tests here

for variant in ubsan gcevery; do
run-one-test cpp/obj_layout_test '' $variant

run-test-in-dir cpp/core_test '' $variant # has testdata

run-one-test cpp/qsn_test '' $variant
Expand Down Expand Up @@ -104,6 +106,8 @@ coverage() {
local compiler=clang
local variant=coverage

run-one-test cpp/obj_layout_test $compiler $variant

run-test-in-dir cpp/core_test $compiler $variant # has testdata

run-one-test cpp/qsn_test $compiler $variant
Expand Down
114 changes: 114 additions & 0 deletions cpp/obj_layout_test.cc
@@ -0,0 +1,114 @@
#include "_gen/core/runtime.asdl.h" // cell, etc
#include "_gen/frontend/syntax.asdl.h"
#include "vendor/greatest.h"

TEST sizeof_syntax() {
// Without sed hack, it's 24 bytes because we have tag (2), id (4), val,
// span_id.
// Now 16 bytes.
log("sizeof(Token) = %d", sizeof(syntax_asdl::Token));
log("alignof(Token) = %d", alignof(syntax_asdl::Token));
log("alignof(Token*) = %d", alignof(syntax_asdl::Token*));

// Without sed hack, it's 12 bytes for tag (2) id (4) and span_id (4).
// Now 8 bytes.
log("sizeof(speck) = %d", sizeof(syntax_asdl::speck));

// 16 bytes: 2 byte tag + 3 integer fields
log("sizeof(line_span) = %d", sizeof(syntax_asdl::line_span));

// Reordered to be 16 bytes
log("sizeof(cell) = %d", sizeof(runtime_asdl::cell));

// 24 bytes: std::vector
log("sizeof(List<int>) = %d", sizeof(List<int>));
log("sizeof(List<Str*>) = %d", sizeof(List<Str*>));

// Unlike Python, this is -1, not 255!
int mod = -1 % 256;
log("mod = %d", mod);

log("alignof(bool) = %d", alignof(bool));
log("alignof(int) = %d", alignof(int));
log("alignof(float) = %d", alignof(float));

log("sizeof(Str) = %d", sizeof(Str));
log("alignof(Str) = %d", alignof(Str));

log("sizeof(Str*) = %d", sizeof(Str*));
log("alignof(Str*) = %d", alignof(Str*));

log("alignof(max_align_t) = %d", alignof(max_align_t));

PASS();
}

// Doesn't really test anything
TEST sizeof_core_types() {
log("");

// 24 = 4 + (4 + 4 + 4) + 8
// Feels like a small string optimization here would be nice.
log("sizeof(Str) = %d", sizeof(Str));
// 16 = 4 + pad4 + 8
log("sizeof(List) = %d", sizeof(List<int>));
// 32 = 4 + pad4 + 8 + 8 + 8
log("sizeof(Dict) = %d", sizeof(Dict<int, int>));

// 8 byte sheader
log("sizeof(Obj) = %d", sizeof(Obj));
// 8 + 128 possible entries
// log("sizeof(LayoutFixed) = %d", sizeof(LayoutFixed));

/* log("sizeof(Heap) = %d", sizeof(Heap)); */

int min_obj_size = sizeof(LayoutForwarded);
int short_str_size = aligned(kStrHeaderSize + 1);

log("kStrHeaderSize = %d", kStrHeaderSize);
log("aligned(kStrHeaderSize + 1) = %d", short_str_size);
log("sizeof(LayoutForwarded) = %d", min_obj_size);

ASSERT(min_obj_size <= short_str_size);

char* p = static_cast<char*>(gHeap.Allocate(17));
char* q = static_cast<char*>(gHeap.Allocate(9));
log("p = %p", p);
log("q = %p", q);

// Str = 16 and List = 24.
// Rejected ideas about slicing:
//
// - Use data[len] == '\0' as OWNING and data[len] != '\0' as a slice?
// It doesn't work because s[1:] would always have that problem
//
// - s->data == (void*)(s + 1)
// Owning string has the data RIGHT AFTER?
// Maybe works? but probably a bad idea because of GLOBAL Str instances.

log("");
log("sizeof(Str) = %zu", sizeof(Str));
log("sizeof(List<int>) = %zu", sizeof(List<int>));
log("sizeof(Dict<int, Str*>) = %zu", sizeof(Dict<int, Str*>));
log("sizeof(Tuple2<int, int>) = %zu", sizeof(Tuple2<int, int>));
log("sizeof(Tuple2<Str*, Str*>) = %zu", sizeof(Tuple2<Str*, Str*>));
log("sizeof(Tuple3<int, int, int>) = %zu", sizeof(Tuple3<int, int, int>));

PASS();
}

GREATEST_MAIN_DEFS();

int main(int argc, char** argv) {
gHeap.Init();

GREATEST_MAIN_BEGIN();

RUN_TEST(sizeof_syntax);
RUN_TEST(sizeof_core_types);

gHeap.CleanProcessExit();

GREATEST_MAIN_END();
return 0;
}
48 changes: 1 addition & 47 deletions cpp/stdlib_test.cc
@@ -1,55 +1,10 @@

#include "cpp/stdlib.h"

#include <errno.h>

#include "_gen/core/runtime.asdl.h" // cell, etc
#include "cpp/core_error.h" // FatalRuntime
#include "cpp/pylib.h"
#include "mycpp/gc_builtins.h"
#include "vendor/greatest.h"

TEST show_sizeof() {
// Without sed hack, it's 24 bytes because we have tag (2), id (4), val,
// span_id.
// Now 16 bytes.
log("sizeof(Token) = %d", sizeof(syntax_asdl::Token));
log("alignof(Token) = %d", alignof(syntax_asdl::Token));
log("alignof(Token*) = %d", alignof(syntax_asdl::Token*));

// Without sed hack, it's 12 bytes for tag (2) id (4) and span_id (4).
// Now 8 bytes.
log("sizeof(speck) = %d", sizeof(syntax_asdl::speck));

// 16 bytes: 2 byte tag + 3 integer fields
log("sizeof(line_span) = %d", sizeof(syntax_asdl::line_span));

// Reordered to be 16 bytes
log("sizeof(cell) = %d", sizeof(runtime_asdl::cell));

// 24 bytes: std::vector
log("sizeof(List<int>) = %d", sizeof(List<int>));
log("sizeof(List<Str*>) = %d", sizeof(List<Str*>));

// Unlike Python, this is -1, not 255!
int mod = -1 % 256;
log("mod = %d", mod);

log("alignof(bool) = %d", alignof(bool));
log("alignof(int) = %d", alignof(int));
log("alignof(float) = %d", alignof(float));

log("sizeof(Str) = %d", sizeof(Str));
log("alignof(Str) = %d", alignof(Str));

log("sizeof(Str*) = %d", sizeof(Str*));
log("alignof(Str*) = %d", alignof(Str*));

log("alignof(max_align_t) = %d", alignof(max_align_t));

PASS();
}

TEST time_test() {
int ts = time_::time();
log("ts = %d", ts);
Expand Down Expand Up @@ -86,13 +41,12 @@ int main(int argc, char** argv) {

GREATEST_MAIN_BEGIN();

RUN_TEST(show_sizeof);
RUN_TEST(time_test);
RUN_TEST(posix_test);
RUN_TEST(putenv_test);

gHeap.CleanProcessExit();

GREATEST_MAIN_END(); /* display results */
GREATEST_MAIN_END();
return 0;
}
37 changes: 0 additions & 37 deletions mycpp/gc_heap_test.cc
Expand Up @@ -30,42 +30,6 @@ static_assert(offsetof(List<int>, slab_) ==
offsetof(GlobalList<int COMMA 1>, slab_),
"List and GlobalList should be consistent");

// Doesn't really test anything
TEST sizeof_test() {
log("");

// 24 = 4 + (4 + 4 + 4) + 8
// Feels like a small string optimization here would be nice.
log("sizeof(Str) = %d", sizeof(Str));
// 16 = 4 + pad4 + 8
log("sizeof(List) = %d", sizeof(List<int>));
// 32 = 4 + pad4 + 8 + 8 + 8
log("sizeof(Dict) = %d", sizeof(Dict<int, int>));

// 8 byte sheader
log("sizeof(Obj) = %d", sizeof(Obj));
// 8 + 128 possible entries
// log("sizeof(LayoutFixed) = %d", sizeof(LayoutFixed));

/* log("sizeof(Heap) = %d", sizeof(Heap)); */

int min_obj_size = sizeof(LayoutForwarded);
int short_str_size = aligned(kStrHeaderSize + 1);

log("kStrHeaderSize = %d", kStrHeaderSize);
log("aligned(kStrHeaderSize + 1) = %d", short_str_size);
log("sizeof(LayoutForwarded) = %d", min_obj_size);

ASSERT(min_obj_size <= short_str_size);

char* p = static_cast<char*>(gHeap.Allocate(17));
char* q = static_cast<char*>(gHeap.Allocate(9));
log("p = %p", p);
log("q = %p", q);

PASS();
}

void ShowSlab(Obj* obj) {
assert(obj->heap_tag_ == Tag::Scanned);
auto slab = reinterpret_cast<Slab<void*>*>(obj);
Expand Down Expand Up @@ -463,7 +427,6 @@ int main(int argc, char** argv) {

GREATEST_MAIN_BEGIN();

RUN_TEST(sizeof_test);
RUN_TEST(field_masks_test);
RUN_TEST(offsets_test);

Expand Down
23 changes: 0 additions & 23 deletions mycpp/gc_list_test.cc
Expand Up @@ -14,28 +14,6 @@ void Print(List<Str*>* parts) {
}
}

TEST test_sizeof() {
// Str = 16 and List = 24.
// Rejected ideas about slicing:
//
// - Use data[len] == '\0' as OWNING and data[len] != '\0' as a slice?
// It doesn't work because s[1:] would always have that problem
//
// - s->data == (void*)(s + 1)
// Owning string has the data RIGHT AFTER?
// Maybe works? but probably a bad idea because of GLOBAL Str instances.

log("");
log("sizeof(Str) = %zu", sizeof(Str));
log("sizeof(List<int>) = %zu", sizeof(List<int>));
log("sizeof(Dict<int, Str*>) = %zu", sizeof(Dict<int, Str*>));
log("sizeof(Tuple2<int, int>) = %zu", sizeof(Tuple2<int, int>));
log("sizeof(Tuple2<Str*, Str*>) = %zu", sizeof(Tuple2<Str*, Str*>));
log("sizeof(Tuple3<int, int, int>) = %zu", sizeof(Tuple3<int, int, int>));

PASS();
}

// TODO:
//
// - Test what happens append() runs over the max heap size
Expand Down Expand Up @@ -315,7 +293,6 @@ int main(int argc, char** argv) {
gHeap.Init();

GREATEST_MAIN_BEGIN();
RUN_TEST(test_sizeof);

RUN_TEST(test_list_gc_header);
RUN_TEST(test_global_list);
Expand Down

0 comments on commit 7e83c1e

Please sign in to comment.