Skip to content

Commit 1b7c6a4

Browse files
authored
Split test_nested_structs into its own file. NFC (#25841)
1 parent 4bd88b2 commit 1b7c6a4

File tree

2 files changed

+92
-94
lines changed

2 files changed

+92
-94
lines changed

test/core/test_nested_structs.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include <stdio.h>
2+
#include "emscripten.h"
3+
4+
struct base {
5+
int x;
6+
float y;
7+
union {
8+
int a;
9+
float b;
10+
};
11+
char c;
12+
};
13+
14+
struct hashtableentry {
15+
int key;
16+
base data;
17+
};
18+
19+
struct hashset {
20+
typedef hashtableentry entry;
21+
struct chain { entry elem; chain *next; };
22+
// struct chainchunk { chain chains[100]; chainchunk *next; };
23+
};
24+
25+
struct hashtable : hashset {
26+
hashtable() {
27+
base b;
28+
entry e;
29+
chain c;
30+
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",
31+
sizeof(base),
32+
long(&b.x) - long(&b),
33+
long(&b.y) - long(&b),
34+
long(&b.a) - long(&b),
35+
long(&b.b) - long(&b),
36+
long(&b.c) - long(&b),
37+
sizeof(hashtableentry),
38+
long(&e.key) - long(&e),
39+
long(&e.data) - long(&e),
40+
long(&e.data.x) - long(&e),
41+
long(&e.data.y) - long(&e),
42+
long(&e.data.a) - long(&e),
43+
long(&e.data.b) - long(&e),
44+
long(&e.data.c) - long(&e),
45+
sizeof(hashset::chain),
46+
long(&c.elem) - long(&c),
47+
long(&c.next) - long(&c),
48+
long(&c.elem.key) - long(&c),
49+
long(&c.elem.data) - long(&c),
50+
long(&c.elem.data.x) - long(&c),
51+
long(&c.elem.data.y) - long(&c),
52+
long(&c.elem.data.a) - long(&c),
53+
long(&c.elem.data.b) - long(&c),
54+
long(&c.elem.data.c) - long(&c)
55+
);
56+
}
57+
};
58+
59+
struct B { char buffer[62]; int last; char laster; char laster2; };
60+
61+
struct Bits {
62+
unsigned short A : 1;
63+
unsigned short B : 1;
64+
unsigned short C : 1;
65+
unsigned short D : 1;
66+
unsigned short x1 : 1;
67+
unsigned short x2 : 1;
68+
unsigned short x3 : 1;
69+
unsigned short x4 : 1;
70+
};
71+
72+
int main() {
73+
hashtable t;
74+
75+
// Part 2 - the char[] should be compressed, BUT have a padding space at the end so the next
76+
// one is aligned properly. Also handle char; char; etc. properly.
77+
B b;
78+
printf("*%ld,%ld,%ld,%ld,%ld,%ld,%ld,%zu*\n", long(&b.buffer) - long(&b),
79+
long(&b.buffer[0]) - long(&b),
80+
long(&b.buffer[1]) - long(&b),
81+
long(&b.buffer[2]) - long(&b),
82+
long(&b.last) - long(&b),
83+
long(&b.laster) - long(&b),
84+
long(&b.laster2) - long(&b),
85+
sizeof(B));
86+
87+
// Part 3 - bitfields, and small structures
88+
printf("*%zu*\n", sizeof(Bits));
89+
return 0;
90+
}

test/test_core.py

Lines changed: 2 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -2877,105 +2877,13 @@ def test_legacy_stack_deps(self):
28772877
}''')
28782878
self.do_runf('main.c', cflags=['--js-library=lib.js'])
28792879

2880-
def test_nestedstructs(self):
2881-
src = r'''
2882-
#include <stdio.h>
2883-
#include "emscripten.h"
2884-
2885-
struct base {
2886-
int x;
2887-
float y;
2888-
union {
2889-
int a;
2890-
float b;
2891-
};
2892-
char c;
2893-
};
2894-
2895-
struct hashtableentry {
2896-
int key;
2897-
base data;
2898-
};
2899-
2900-
struct hashset {
2901-
typedef hashtableentry entry;
2902-
struct chain { entry elem; chain *next; };
2903-
// struct chainchunk { chain chains[100]; chainchunk *next; };
2904-
};
2905-
2906-
struct hashtable : hashset {
2907-
hashtable() {
2908-
base b;
2909-
entry e;
2910-
chain c;
2911-
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",
2912-
sizeof(base),
2913-
long(&b.x) - long(&b),
2914-
long(&b.y) - long(&b),
2915-
long(&b.a) - long(&b),
2916-
long(&b.b) - long(&b),
2917-
long(&b.c) - long(&b),
2918-
sizeof(hashtableentry),
2919-
long(&e.key) - long(&e),
2920-
long(&e.data) - long(&e),
2921-
long(&e.data.x) - long(&e),
2922-
long(&e.data.y) - long(&e),
2923-
long(&e.data.a) - long(&e),
2924-
long(&e.data.b) - long(&e),
2925-
long(&e.data.c) - long(&e),
2926-
sizeof(hashset::chain),
2927-
long(&c.elem) - long(&c),
2928-
long(&c.next) - long(&c),
2929-
long(&c.elem.key) - long(&c),
2930-
long(&c.elem.data) - long(&c),
2931-
long(&c.elem.data.x) - long(&c),
2932-
long(&c.elem.data.y) - long(&c),
2933-
long(&c.elem.data.a) - long(&c),
2934-
long(&c.elem.data.b) - long(&c),
2935-
long(&c.elem.data.c) - long(&c)
2936-
);
2937-
}
2938-
};
2939-
2940-
struct B { char buffer[62]; int last; char laster; char laster2; };
2941-
2942-
struct Bits {
2943-
unsigned short A : 1;
2944-
unsigned short B : 1;
2945-
unsigned short C : 1;
2946-
unsigned short D : 1;
2947-
unsigned short x1 : 1;
2948-
unsigned short x2 : 1;
2949-
unsigned short x3 : 1;
2950-
unsigned short x4 : 1;
2951-
};
2952-
2953-
int main() {
2954-
hashtable t;
2955-
2956-
// Part 2 - the char[] should be compressed, BUT have a padding space at the end so the next
2957-
// one is aligned properly. Also handle char; char; etc. properly.
2958-
B b;
2959-
printf("*%ld,%ld,%ld,%ld,%ld,%ld,%ld,%zu*\n", long(&b.buffer) - long(&b),
2960-
long(&b.buffer[0]) - long(&b),
2961-
long(&b.buffer[1]) - long(&b),
2962-
long(&b.buffer[2]) - long(&b),
2963-
long(&b.last) - long(&b),
2964-
long(&b.laster) - long(&b),
2965-
long(&b.laster2) - long(&b),
2966-
sizeof(B));
2967-
2968-
// Part 3 - bitfields, and small structures
2969-
printf("*%zu*\n", sizeof(Bits));
2970-
return 0;
2971-
}
2972-
'''
2880+
def test_nested_structs(self):
29732881
# Bloated memory; same layout as C/C++
29742882
if self.is_wasm64():
29752883
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*'
29762884
else:
29772885
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*'
2978-
self.do_run(src, expected)
2886+
self.do_runf('core/test_nested_structs.cpp', expected)
29792887

29802888
def prep_dlfcn_main(self, libs=None):
29812889
if libs is None:

0 commit comments

Comments
 (0)