Skip to content

Commit 4c34a0d

Browse files
authored
Cleanup test_memcpy_alignment/test_memset_alignment. NFC (#22668)
- Remove a bunch of unused C++ headers - Convert to C - Tabs to spaces - Apply coding style. I'm not sure we really need these tests anymore but maybe its good to have them for test our different optimized versions of memset/memcpy.
1 parent b9a0186 commit 4c34a0d

5 files changed

+157
-173
lines changed

test/test_core.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2695,10 +2695,10 @@ def test_memcpy3(self):
26952695

26962696
@also_with_standalone_wasm()
26972697
def test_memcpy_alignment(self):
2698-
self.do_runf('test_memcpy_alignment.cpp', 'OK.')
2698+
self.do_runf('test_memcpy_alignment.c', 'OK.')
26992699

27002700
def test_memset_alignment(self):
2701-
self.do_runf('test_memset_alignment.cpp', 'OK.')
2701+
self.do_runf('test_memset_alignment.c', 'OK.')
27022702

27032703
def test_memset(self):
27042704
self.do_core_test('test_memset.c')

test/test_memcpy_alignment.c

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2017 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+
#include <memory.h>
7+
#include <string.h>
8+
#include <stdio.h>
9+
#include <stdlib.h>
10+
11+
char dst[1024*1024*2+64] = {};
12+
char src[1024*1024*2+64] = {};
13+
14+
#define GUARDSIZE 32
15+
void test_memcpy(int copySize, int srcOffset, int dstOffset) {
16+
char *dstContent = dst + GUARDSIZE + dstOffset;
17+
char *srcContent = src + GUARDSIZE + srcOffset;
18+
19+
char *dstGuardBefore = dstContent - GUARDSIZE;
20+
char *dstGuardAfter = dstContent + copySize;
21+
22+
char *srcGuardBefore = srcContent - GUARDSIZE;
23+
char *srcGuardAfter = srcContent + copySize;
24+
25+
char guard = (char)rand();
26+
27+
for (int i = 0; i < GUARDSIZE; ++i) {
28+
// Generate a guardband area around dst memory that should not change.
29+
dstGuardBefore[i] = (char)(guard ^ i);
30+
dstGuardAfter[i] = (char)(guard ^ i);
31+
}
32+
33+
// Generate copy source data
34+
char s = (char)rand();
35+
for (int i = 0; i < copySize + srcOffset + 2*GUARDSIZE; ++i) {
36+
src[i] = (char)(s - i);
37+
}
38+
39+
memcpy(dstContent, srcContent, copySize);
40+
if (!!memcmp(dstContent, srcContent, copySize)) {
41+
printf("test_memcpy(copySize=%d, srcOffset=%d, dstOffset=%d failed!\n", copySize, srcOffset, dstOffset);
42+
exit(1);
43+
}
44+
45+
// Verify guardband area
46+
for (int i = 0; i < GUARDSIZE; ++i) {
47+
// Generate a guardband area around dst memory that should not change.
48+
if (dstGuardBefore[i] != (char)(guard ^ i)) {
49+
printf("test_memcpy(copySize=%d, srcOffset=%d, dstOffset=%d failed! Pre-guardband area at i=%d overwritten!\n", copySize, srcOffset, dstOffset, i);
50+
exit(1);
51+
}
52+
if (dstGuardAfter[i] != (char)(guard ^ i)) {
53+
printf("test_memcpy(copySize=%d, srcOffset=%d, dstOffset=%d failed! Post-guardband area at i=%d overwritten!\n", copySize, srcOffset, dstOffset, i);
54+
exit(1);
55+
}
56+
}
57+
}
58+
59+
void test_copysize(int copySize) {
60+
int offsets[6] = { 0, 3, 4, 5, 8, 11 };
61+
62+
for (int srcOffset = 0; srcOffset < 6; ++srcOffset) {
63+
for (int dstOffset = 0; dstOffset < 6; ++dstOffset) {
64+
test_memcpy(copySize, offsets[srcOffset], offsets[dstOffset]);
65+
}
66+
}
67+
}
68+
69+
int main() {
70+
for (int copySize = 0; copySize < 128; ++copySize) {
71+
test_copysize(copySize);
72+
}
73+
74+
for (int copySizeI = 128; copySizeI <= 1048576; copySizeI <<= 1) {
75+
for (int copySizeJ = 1; copySizeJ <= 16; copySizeJ <<= 1) {
76+
test_copysize(copySizeI | copySizeJ);
77+
}
78+
}
79+
80+
printf("OK.\n");
81+
}

test/test_memcpy_alignment.cpp

-88
This file was deleted.

test/test_memset_alignment.c

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2017 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+
#include <memory.h>
7+
#include <string.h>
8+
#include <stdio.h>
9+
#include <stdlib.h>
10+
#include <time.h>
11+
12+
char ptr[1024*1024*2+64] = {};
13+
14+
#define GUARDSIZE 32
15+
void test_memset(int copySize, int offset) {
16+
char *content = ptr + GUARDSIZE + offset;
17+
18+
char *guardBefore = content - GUARDSIZE;
19+
char *guardAfter = content + copySize;
20+
21+
char guard = (char)rand();
22+
23+
for (int i = 0; i < GUARDSIZE; ++i) {
24+
// Generate a guardband area around memory that should not change.
25+
guardBefore[i] = (char)(guard ^ i);
26+
guardAfter[i] = (char)(guard ^ i);
27+
}
28+
29+
// Generate fill source data
30+
char s = (char)(rand() ^ 0xF0);
31+
32+
memset(content, s, copySize);
33+
for (int i = 0; i < copySize; ++i) {
34+
if (content[i] != s) {
35+
printf("test_memset(copySize=%d, offset=%d failed!\n", copySize, offset);
36+
exit(1);
37+
}
38+
}
39+
40+
// Verify guardband area
41+
for (int i = 0; i < GUARDSIZE; ++i) {
42+
// Generate a guardband area around memory that should not change.
43+
if (guardBefore[i] != (char)(guard ^ i)) {
44+
printf("test_memset(copySize=%d, offset=%d failed! Pre-guardband area at i=%d overwritten!\n", copySize, offset, i);
45+
exit(1);
46+
}
47+
if (guardAfter[i] != (char)(guard ^ i)) {
48+
printf("test_memset(copySize=%d, offset=%d failed! Post-guardband area at i=%d overwritten!\n", copySize, offset, i);
49+
exit(1);
50+
}
51+
}
52+
}
53+
54+
void test_copysize(int copySize) {
55+
for (int offset = 0; offset < 31; ++offset) {
56+
test_memset(copySize, offset);
57+
}
58+
}
59+
60+
int main() {
61+
srand(time(NULL));
62+
63+
for (int copySize = 0; copySize < 128; ++copySize) {
64+
test_copysize(copySize);
65+
}
66+
67+
for (int copySizeI = 128; copySizeI <= 1048576; copySizeI <<= 1) {
68+
for (int copySizeJ = 1; copySizeJ <= 16; copySizeJ <<= 1) {
69+
test_copysize(copySizeI | copySizeJ);
70+
}
71+
}
72+
73+
printf("OK.\n");
74+
}

test/test_memset_alignment.cpp

-83
This file was deleted.

0 commit comments

Comments
 (0)