From be126242ff46638b2a770ec7392e9146632d5fb7 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Thu, 20 Nov 2025 13:05:04 -0800 Subject: [PATCH] Split test_nested_structs into its own file. NFC --- test/core/test_nested_structs.cpp | 90 +++++++++++++++++++++++++++++ test/test_core.py | 96 +------------------------------ 2 files changed, 92 insertions(+), 94 deletions(-) create mode 100644 test/core/test_nested_structs.cpp diff --git a/test/core/test_nested_structs.cpp b/test/core/test_nested_structs.cpp new file mode 100644 index 0000000000000..c9e87c20ac9db --- /dev/null +++ b/test/core/test_nested_structs.cpp @@ -0,0 +1,90 @@ +#include +#include "emscripten.h" + +struct base { + int x; + float y; + union { + int a; + float b; + }; + char c; +}; + +struct hashtableentry { + int key; + base data; +}; + +struct hashset { + typedef hashtableentry entry; + struct chain { entry elem; chain *next; }; +// struct chainchunk { chain chains[100]; chainchunk *next; }; +}; + +struct hashtable : hashset { + hashtable() { + base b; + entry e; + chain c; + printf("*%zu,%ld,%ld,%ld,%ld,%ld|%zu,%ld,%ld,%ld,%ld,%ld,%ld,%ld|%zu,%ld,%ld,%ld,%ld,%ld,%ld,%ld,%ld,%ld*\n", + sizeof(base), + long(&b.x) - long(&b), + long(&b.y) - long(&b), + long(&b.a) - long(&b), + long(&b.b) - long(&b), + long(&b.c) - long(&b), + sizeof(hashtableentry), + long(&e.key) - long(&e), + long(&e.data) - long(&e), + long(&e.data.x) - long(&e), + long(&e.data.y) - long(&e), + long(&e.data.a) - long(&e), + long(&e.data.b) - long(&e), + long(&e.data.c) - long(&e), + sizeof(hashset::chain), + long(&c.elem) - long(&c), + long(&c.next) - long(&c), + long(&c.elem.key) - long(&c), + long(&c.elem.data) - long(&c), + long(&c.elem.data.x) - long(&c), + long(&c.elem.data.y) - long(&c), + long(&c.elem.data.a) - long(&c), + long(&c.elem.data.b) - long(&c), + long(&c.elem.data.c) - long(&c) + ); + } +}; + +struct B { char buffer[62]; int last; char laster; char laster2; }; + +struct Bits { + unsigned short A : 1; + unsigned short B : 1; + unsigned short C : 1; + unsigned short D : 1; + unsigned short x1 : 1; + unsigned short x2 : 1; + unsigned short x3 : 1; + unsigned short x4 : 1; +}; + +int main() { + hashtable t; + + // Part 2 - the char[] should be compressed, BUT have a padding space at the end so the next + // one is aligned properly. Also handle char; char; etc. properly. + B b; + printf("*%ld,%ld,%ld,%ld,%ld,%ld,%ld,%zu*\n", long(&b.buffer) - long(&b), + long(&b.buffer[0]) - long(&b), + long(&b.buffer[1]) - long(&b), + long(&b.buffer[2]) - long(&b), + long(&b.last) - long(&b), + long(&b.laster) - long(&b), + long(&b.laster2) - long(&b), + sizeof(B)); + + // Part 3 - bitfields, and small structures + printf("*%zu*\n", sizeof(Bits)); + return 0; +} diff --git a/test/test_core.py b/test/test_core.py index 1805f0c0b2b4e..5ed5bbd671147 100644 --- a/test/test_core.py +++ b/test/test_core.py @@ -2877,105 +2877,13 @@ def test_legacy_stack_deps(self): }''') self.do_runf('main.c', cflags=['--js-library=lib.js']) - def test_nestedstructs(self): - src = r''' - #include - #include "emscripten.h" - - struct base { - int x; - float y; - union { - int a; - float b; - }; - char c; - }; - - struct hashtableentry { - int key; - base data; - }; - - struct hashset { - typedef hashtableentry entry; - struct chain { entry elem; chain *next; }; - // struct chainchunk { chain chains[100]; chainchunk *next; }; - }; - - struct hashtable : hashset { - hashtable() { - base b; - entry e; - chain c; - printf("*%zu,%ld,%ld,%ld,%ld,%ld|%zu,%ld,%ld,%ld,%ld,%ld,%ld,%ld|%zu,%ld,%ld,%ld,%ld,%ld,%ld,%ld,%ld,%ld*\n", - sizeof(base), - long(&b.x) - long(&b), - long(&b.y) - long(&b), - long(&b.a) - long(&b), - long(&b.b) - long(&b), - long(&b.c) - long(&b), - sizeof(hashtableentry), - long(&e.key) - long(&e), - long(&e.data) - long(&e), - long(&e.data.x) - long(&e), - long(&e.data.y) - long(&e), - long(&e.data.a) - long(&e), - long(&e.data.b) - long(&e), - long(&e.data.c) - long(&e), - sizeof(hashset::chain), - long(&c.elem) - long(&c), - long(&c.next) - long(&c), - long(&c.elem.key) - long(&c), - long(&c.elem.data) - long(&c), - long(&c.elem.data.x) - long(&c), - long(&c.elem.data.y) - long(&c), - long(&c.elem.data.a) - long(&c), - long(&c.elem.data.b) - long(&c), - long(&c.elem.data.c) - long(&c) - ); - } - }; - - struct B { char buffer[62]; int last; char laster; char laster2; }; - - struct Bits { - unsigned short A : 1; - unsigned short B : 1; - unsigned short C : 1; - unsigned short D : 1; - unsigned short x1 : 1; - unsigned short x2 : 1; - unsigned short x3 : 1; - unsigned short x4 : 1; - }; - - int main() { - hashtable t; - - // Part 2 - the char[] should be compressed, BUT have a padding space at the end so the next - // one is aligned properly. Also handle char; char; etc. properly. - B b; - printf("*%ld,%ld,%ld,%ld,%ld,%ld,%ld,%zu*\n", long(&b.buffer) - long(&b), - long(&b.buffer[0]) - long(&b), - long(&b.buffer[1]) - long(&b), - long(&b.buffer[2]) - long(&b), - long(&b.last) - long(&b), - long(&b.laster) - long(&b), - long(&b.laster2) - long(&b), - sizeof(B)); - - // Part 3 - bitfields, and small structures - printf("*%zu*\n", sizeof(Bits)); - return 0; - } - ''' + def test_nested_structs(self): # Bloated memory; same layout as C/C++ if self.is_wasm64(): expected = '*16,0,4,8,8,12|20,0,4,4,8,12,12,16|32,0,24,0,4,4,8,12,12,16*\n*0,0,1,2,64,68,69,72*\n*2*' else: expected = '*16,0,4,8,8,12|20,0,4,4,8,12,12,16|24,0,20,0,4,4,8,12,12,16*\n*0,0,1,2,64,68,69,72*\n*2*' - self.do_run(src, expected) + self.do_runf('core/test_nested_structs.cpp', expected) def prep_dlfcn_main(self, libs=None): if libs is None: