Skip to content

Commit 6c7ba78

Browse files
committed
add two more test case
1 parent bcc9ab8 commit 6c7ba78

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

tests/unit/shared-heap/shared_heap_test.cc

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,37 @@ create_test_shared_heap(uint8 *preallocated_buf, size_t size,
292292
}
293293
}
294294

295+
static WASMSharedHeap *
296+
create_preallocated_shared_heap(size_t size, uint8 *buffer)
297+
{
298+
SharedHeapInitArgs args = {};
299+
WASMSharedHeap *shared_heap = NULL;
300+
301+
memset(buffer, 0, size);
302+
args.pre_allocated_addr = buffer;
303+
args.size = size;
304+
305+
shared_heap = wasm_runtime_create_shared_heap(&args);
306+
if (!shared_heap) {
307+
ADD_FAILURE() << "Create preallocated shared heap failed.\n";
308+
return NULL;
309+
}
310+
311+
return shared_heap;
312+
}
313+
314+
static WASMSharedHeap *
315+
chain_shared_heaps(WASMSharedHeap *head, WASMSharedHeap *next)
316+
{
317+
WASMSharedHeap *chain = wasm_runtime_chain_shared_heaps(head, next);
318+
if (!chain) {
319+
ADD_FAILURE() << "Create shared heap chain failed.\n";
320+
return NULL;
321+
}
322+
323+
return chain;
324+
}
325+
295326
static void
296327
create_test_shared_heap_chain(uint8 *preallocated_buf, size_t size,
297328
uint8 *preallocated_buf2, size_t size2,
@@ -411,6 +442,102 @@ TEST_F(shared_heap_test, test_destroy_shared_heap_when_attached)
411442
EXPECT_EQ(nullptr, new_head);
412443
}
413444

445+
TEST_F(shared_heap_test, test_destroy_shared_heap_three_nodes_chain)
446+
{
447+
const uint32 BUF_SIZE = os_getpagesize();
448+
uint8 head_buf[BUF_SIZE], body_buf[BUF_SIZE], tail_buf[BUF_SIZE];
449+
WASMSharedHeap *head = nullptr;
450+
WASMSharedHeap *body = nullptr;
451+
WASMSharedHeap *tail = nullptr;
452+
WASMSharedHeap *new_head = nullptr;
453+
WASMSharedHeap *chain = nullptr;
454+
455+
head = create_preallocated_shared_heap(BUF_SIZE, head_buf);
456+
body = create_preallocated_shared_heap(BUF_SIZE, body_buf);
457+
tail = create_preallocated_shared_heap(BUF_SIZE, tail_buf);
458+
459+
ASSERT_NE(nullptr, head);
460+
ASSERT_NE(nullptr, body);
461+
ASSERT_NE(nullptr, tail);
462+
463+
chain = chain_shared_heaps(body, tail);
464+
ASSERT_NE(nullptr, chain);
465+
466+
chain = chain_shared_heaps(head, chain);
467+
ASSERT_NE(nullptr, chain);
468+
469+
EXPECT_TRUE(wasm_runtime_destroy_shared_heap(chain, true, &new_head));
470+
EXPECT_EQ(nullptr, new_head);
471+
}
472+
473+
TEST_F(shared_heap_test, test_destroy_shared_heap_cross_chains)
474+
{
475+
const uint32 BUF_SIZE = os_getpagesize();
476+
uint8 chain1_bufs[3][BUF_SIZE];
477+
uint8 chain2_bufs[3][BUF_SIZE];
478+
WASMSharedHeap *chain1_nodes[3];
479+
WASMSharedHeap *chain2_nodes[3];
480+
WASMSharedHeap *new_head = nullptr;
481+
482+
chain1_nodes[0] = create_preallocated_shared_heap(BUF_SIZE, chain1_bufs[0]);
483+
chain2_nodes[0] = create_preallocated_shared_heap(BUF_SIZE, chain2_bufs[0]);
484+
chain1_nodes[1] = create_preallocated_shared_heap(BUF_SIZE, chain1_bufs[1]);
485+
chain2_nodes[1] = create_preallocated_shared_heap(BUF_SIZE, chain2_bufs[1]);
486+
chain1_nodes[2] = create_preallocated_shared_heap(BUF_SIZE, chain1_bufs[2]);
487+
chain2_nodes[2] = create_preallocated_shared_heap(BUF_SIZE, chain2_bufs[2]);
488+
489+
ASSERT_NE(nullptr, chain1_nodes[0]);
490+
ASSERT_NE(nullptr, chain2_nodes[0]);
491+
ASSERT_NE(nullptr, chain1_nodes[1]);
492+
ASSERT_NE(nullptr, chain2_nodes[1]);
493+
ASSERT_NE(nullptr, chain1_nodes[2]);
494+
ASSERT_NE(nullptr, chain2_nodes[2]);
495+
496+
WASMSharedHeap *shared_heap_chain1 =
497+
chain_shared_heaps(chain1_nodes[1], chain1_nodes[2]);
498+
ASSERT_NE(nullptr, shared_heap_chain1);
499+
500+
shared_heap_chain1 =
501+
chain_shared_heaps(chain1_nodes[0], shared_heap_chain1);
502+
ASSERT_NE(nullptr, shared_heap_chain1);
503+
504+
WASMSharedHeap *shared_heap_chain2 =
505+
chain_shared_heaps(chain2_nodes[1], chain2_nodes[2]);
506+
ASSERT_NE(nullptr, shared_heap_chain2);
507+
508+
shared_heap_chain2 =
509+
chain_shared_heaps(chain2_nodes[0], shared_heap_chain2);
510+
ASSERT_NE(nullptr, shared_heap_chain2);
511+
512+
EXPECT_TRUE(
513+
wasm_runtime_destroy_shared_heap(shared_heap_chain1, false, &new_head));
514+
ASSERT_EQ(chain1_nodes[1], new_head);
515+
shared_heap_chain1 = new_head;
516+
517+
EXPECT_TRUE(
518+
wasm_runtime_destroy_shared_heap(shared_heap_chain2, false, &new_head));
519+
ASSERT_EQ(chain2_nodes[1], new_head);
520+
shared_heap_chain2 = new_head;
521+
522+
EXPECT_TRUE(
523+
wasm_runtime_destroy_shared_heap(shared_heap_chain1, false, &new_head));
524+
ASSERT_EQ(chain1_nodes[2], new_head);
525+
shared_heap_chain1 = new_head;
526+
527+
EXPECT_TRUE(
528+
wasm_runtime_destroy_shared_heap(shared_heap_chain2, false, &new_head));
529+
ASSERT_EQ(chain2_nodes[2], new_head);
530+
shared_heap_chain2 = new_head;
531+
532+
EXPECT_TRUE(
533+
wasm_runtime_destroy_shared_heap(shared_heap_chain1, false, &new_head));
534+
EXPECT_EQ(nullptr, new_head);
535+
536+
EXPECT_TRUE(
537+
wasm_runtime_destroy_shared_heap(shared_heap_chain2, false, &new_head));
538+
EXPECT_EQ(nullptr, new_head);
539+
}
540+
414541
TEST_F(shared_heap_test, test_shared_heap_rmw)
415542
{
416543
WASMSharedHeap *shared_heap = nullptr;

0 commit comments

Comments
 (0)