Skip to content

Commit 0a149a1

Browse files
committed
[test] Minor test refactoring. NFC
- Convert some malloc tests from C++ for C - Use @parameterize to split up test_embind. Split out from #20071
1 parent dee7d9a commit 0a149a1

9 files changed

+130
-172
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <stdio.h>
2+
#include <emscripten/emmalloc.h>
3+
4+
size_t round_to_4k(size_t val) {
5+
return (val + 4095) & ~4095;
6+
}
7+
8+
int main() {
9+
void *ptr = malloc(32*1024*1024);
10+
void *ptr2 = malloc(4*1024*1024);
11+
void *ptr3 = malloc(64*1024*1024);
12+
void *ptr4 = malloc(16*1024);
13+
void *ptr5 = malloc(2*1024*1024);
14+
printf("valid allocs: %d\n", (int)(ptr && ptr2 && ptr3 && ptr4 && ptr5));
15+
free(ptr2);
16+
free(ptr4);
17+
printf("emmalloc_validate_memory_regions: %d\n", emmalloc_validate_memory_regions());
18+
printf("emmalloc_dynamic_heap_size : %zu\n", emmalloc_dynamic_heap_size());
19+
printf("emmalloc_free_dynamic_memory : %zu\n", emmalloc_free_dynamic_memory());
20+
emmalloc_dump_free_dynamic_memory_fragmentation_map();
21+
printf("emmalloc_unclaimed_heap_memory : %zu\n", round_to_4k(emmalloc_unclaimed_heap_memory()));
22+
}

test/core/test_emmalloc_memory_statistics.cpp

Lines changed: 0 additions & 24 deletions
This file was deleted.

test/core/test_mallinfo.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2016 The Emscripten Authors. All rights reserved.
2+
// Emscripten is available under two separate licenses, the MIT license and the
3+
// University of Illinois/NCSA Open Source License. Both these licenses can be
4+
// found in the LICENSE file.
5+
6+
// Tests that we can use the dlmalloc mallinfo() function to obtain information
7+
// about malloc()ed blocks and compute how much memory is used/freed.
8+
9+
#include <stdio.h>
10+
#include <stdlib.h>
11+
#include <unistd.h>
12+
#include <assert.h>
13+
#include <malloc.h>
14+
#include <emscripten/em_asm.h>
15+
16+
size_t getTotalMemory() {
17+
return (size_t)EM_ASM_PTR(return HEAP8.length);
18+
}
19+
20+
size_t getFreeMemory() {
21+
struct mallinfo i = mallinfo();
22+
uintptr_t totalMemory = getTotalMemory();
23+
uintptr_t dynamicTop = (uintptr_t)sbrk(0);
24+
return totalMemory - dynamicTop + i.fordblks;
25+
}
26+
27+
int main() {
28+
size_t total_mem = getTotalMemory();
29+
size_t free_mem = getFreeMemory();
30+
31+
printf("Before allocation:\n");
32+
printf("Total memory: %zu bytes\n", getTotalMemory());
33+
printf("Free memory: %zu bytes\n", getFreeMemory());
34+
printf("Used: %zu bytes (%.2f%%)\n", getTotalMemory() - getFreeMemory(), (getTotalMemory() - getFreeMemory()) * 100.0 / getTotalMemory());
35+
assert(getTotalMemory() == total_mem);
36+
assert(getFreeMemory() == free_mem);
37+
38+
void *ptr = malloc(1024*1024);
39+
printf("\nAfter 1MB allocation: %p\n", ptr);
40+
printf("Total memory: %zu bytes\n", getTotalMemory());
41+
printf("Free memory: %zu bytes\n", getFreeMemory());
42+
printf("Used: %zu bytes (%.2f%%)\n", getTotalMemory() - getFreeMemory(), (getTotalMemory() - getFreeMemory()) * 100.0 / getTotalMemory());
43+
assert(getTotalMemory() == total_mem);
44+
assert(getFreeMemory() < free_mem);
45+
46+
free(ptr);
47+
printf("\nAfter freeing:\n");
48+
printf("Total memory: %zu bytes\n", getTotalMemory());
49+
printf("Free memory: %zu bytes\n", getFreeMemory());
50+
printf("Used: %zu bytes (%.2f%%)\n", getTotalMemory() - getFreeMemory(), (getTotalMemory() - getFreeMemory()) * 100.0 / getTotalMemory());
51+
assert(getTotalMemory() == total_mem);
52+
assert(getFreeMemory() == free_mem);
53+
54+
printf("OK.\n");
55+
}

test/core/test_mallinfo.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
OK.

test/core/test_memorygrowth.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,25 @@
1313

1414
int main(int argc, char **argv)
1515
{
16-
char *buf1 = (char*)malloc(100);
17-
char *data1 = (char*)"hello";
16+
char *buf1 = malloc(100);
17+
char *data1 = "hello";
1818
memcpy(buf1, data1, strlen(data1)+1);
1919

20-
float *buf2 = (float*)malloc(100);
20+
float *buf2 = malloc(100);
2121
float pie = 4.955;
2222
memcpy(buf2, &pie, sizeof(float));
2323

2424
printf("*pre: %s,%.3f*\n", buf1, buf2[0]);
2525

26-
int totalMemory = EM_ASM_INT({ return HEAP8.length });
27-
char *buf3 = (char*)malloc(totalMemory+1);
28-
buf3[argc] = (long)buf2;
29-
if (argc % 7 == 6) printf("%ld\n", (long)memcpy(buf3, buf1, argc));
30-
char *buf4 = (char*)malloc(100);
31-
float *buf5 = (float*)malloc(100);
26+
size_t totalMemory = (size_t)EM_ASM_PTR({ return HEAP8.length });
27+
//printf("totalMemory: %zu, argc: %d\n", totalMemory, argc);
28+
char *buf3 = malloc(totalMemory+1);
29+
assert(buf3 && "fail to perform large allocation");
30+
if (argc % 7 == 6) printf("%p\n", memcpy(buf3, buf1, argc));
31+
char *buf4 = malloc(100);
32+
float *buf5 = malloc(100);
3233
//printf("totalMemory: %d bufs: %d,%d,%d,%d,%d\n", totalMemory, buf1, buf2, buf3, buf4, buf5);
33-
assert((long)buf4 > (long)totalMemory && (long)buf5 > (long)totalMemory);
34+
assert((intptr_t)buf4 > (intptr_t)totalMemory && (intptr_t)buf5 > (intptr_t)totalMemory);
3435

3536
printf("*%s,%.3f*\n", buf1, buf2[0]); // the old heap data should still be there
3637

test/embind/test_i64_binding.cpp

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,13 @@
1313
using namespace emscripten;
1414
using namespace std;
1515

16-
void fail()
17-
{
18-
cout << "fail\n";
19-
}
20-
21-
void pass()
22-
{
23-
cout << "pass\n";
24-
}
16+
#define assert_js(X) assert(run_js(X))
2517

2618
void test(string message)
2719
{
2820
cout << "test:\n" << message << "\n";
2921
}
3022

31-
void ensure(bool value)
32-
{
33-
if (value)
34-
pass();
35-
else
36-
fail();
37-
}
3823

3924
void execute_js(string js_code)
4025
{
@@ -46,22 +31,22 @@ void execute_js(string js_code)
4631
}, js_code_pointer);
4732
}
4833

49-
void ensure_js(string js_code)
34+
int run_js(string js_code)
5035
{
5136
js_code.append(";");
5237
const char* js_code_pointer = js_code.c_str();
53-
ensure(EM_ASM_INT({
38+
return EM_ASM_INT({
5439
var js_code = UTF8ToString($0);
5540
return eval(js_code);
56-
}, js_code_pointer));
41+
}, js_code_pointer);
5742
}
5843

5944
void ensure_js_throws(string js_code, string error_type)
6045
{
6146
js_code.append(";");
6247
const char* js_code_pointer = js_code.c_str();
6348
const char* error_type_pointer = error_type.c_str();
64-
ensure(EM_ASM_INT({
49+
assert(EM_ASM_INT({
6550
var js_code = UTF8ToString($0);
6651
var error_type = UTF8ToString($1);
6752
try {
@@ -91,14 +76,14 @@ int main()
9176
test("vector<int64_t>");
9277
val myval(std::vector<int64_t>{1, 2, 3, -4});
9378
val::global().set("v64", myval);
94-
ensure_js("v64.get(0) === 1n");
95-
ensure_js("v64.get(1) === 2n");
96-
ensure_js("v64.get(2) === 3n");
97-
ensure_js("v64.get(3) === -4n");
79+
assert_js("v64.get(0) === 1n");
80+
assert_js("v64.get(1) === 2n");
81+
assert_js("v64.get(2) === 3n");
82+
assert_js("v64.get(3) === -4n");
9883

9984
execute_js("v64.push_back(1234n)");
100-
ensure_js("v64.size() === 5");
101-
ensure_js("v64.get(4) === 1234n");
85+
assert_js("v64.size() === 5");
86+
assert_js("v64.get(4) === 1234n");
10287

10388
test("vector<int64_t> Cannot convert number to int64_t");
10489
ensure_js_throws("v64.push_back(1234)", "TypeError");
@@ -109,14 +94,14 @@ int main()
10994
test("vector<uint64_t>");
11095
val myval2(vector<uint64_t>{1, 2, 3, 4});
11196
val::global().set("vU64", myval2);
112-
ensure_js("vU64.get(0) === 1n");
113-
ensure_js("vU64.get(1) === 2n");
114-
ensure_js("vU64.get(2) === 3n");
115-
ensure_js("vU64.get(3) === 4n");
97+
assert_js("vU64.get(0) === 1n");
98+
assert_js("vU64.get(1) === 2n");
99+
assert_js("vU64.get(2) === 3n");
100+
assert_js("vU64.get(3) === 4n");
116101

117102
execute_js("vU64.push_back(1234n)");
118-
ensure_js("vU64.size() === 5");
119-
ensure_js("vU64.get(4) === 1234n");
103+
assert_js("vU64.size() === 5");
104+
assert_js("vU64.get(4) === 1234n");
120105

121106
test("vector<uint64_t> Cannot convert number to uint64_t");
122107
ensure_js_throws("vU64.push_back(1234)", "TypeError");

test/embind/test_i64_binding.out

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,16 @@
11
start
22
test:
33
vector<int64_t>
4-
pass
5-
pass
6-
pass
7-
pass
8-
pass
9-
pass
104
test:
115
vector<int64_t> Cannot convert number to int64_t
12-
pass
136
test:
147
vector<int64_t> Cannot convert bigint that is too big
15-
pass
168
test:
179
vector<uint64_t>
18-
pass
19-
pass
20-
pass
21-
pass
22-
pass
23-
pass
2410
test:
2511
vector<uint64_t> Cannot convert number to uint64_t
26-
pass
2712
test:
2813
vector<uint64_t> Cannot convert bigint that is too big
29-
pass
3014
test:
3115
vector<uint64_t> Cannot convert bigint that is negative
32-
pass
3316
end

test/mallinfo.cpp

Lines changed: 0 additions & 70 deletions
This file was deleted.

0 commit comments

Comments
 (0)