From edf53bf9bdfe3d8e234b4a54026fbcd89b134560 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Wed, 2 Oct 2024 10:51:12 -0700 Subject: [PATCH] Cleanup test_memcpy_alignment/test_memset_alignment. NFC - 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. --- test/test_core.py | 4 +- test/test_memcpy_alignment.c | 81 +++++++++++++++++++++++++++++++ test/test_memcpy_alignment.cpp | 88 ---------------------------------- test/test_memset_alignment.c | 74 ++++++++++++++++++++++++++++ test/test_memset_alignment.cpp | 83 -------------------------------- 5 files changed, 157 insertions(+), 173 deletions(-) create mode 100644 test/test_memcpy_alignment.c delete mode 100644 test/test_memcpy_alignment.cpp create mode 100644 test/test_memset_alignment.c delete mode 100644 test/test_memset_alignment.cpp diff --git a/test/test_core.py b/test/test_core.py index e6e14b9936be..012aaa243bfa 100644 --- a/test/test_core.py +++ b/test/test_core.py @@ -2695,10 +2695,10 @@ def test_memcpy3(self): @also_with_standalone_wasm() def test_memcpy_alignment(self): - self.do_runf('test_memcpy_alignment.cpp', 'OK.') + self.do_runf('test_memcpy_alignment.c', 'OK.') def test_memset_alignment(self): - self.do_runf('test_memset_alignment.cpp', 'OK.') + self.do_runf('test_memset_alignment.c', 'OK.') def test_memset(self): self.do_core_test('test_memset.c') diff --git a/test/test_memcpy_alignment.c b/test/test_memcpy_alignment.c new file mode 100644 index 000000000000..28c12c7868dd --- /dev/null +++ b/test/test_memcpy_alignment.c @@ -0,0 +1,81 @@ +// Copyright 2017 The Emscripten Authors. All rights reserved. +// Emscripten is available under two separate licenses, the MIT license and the +// University of Illinois/NCSA Open Source License. Both these licenses can be +// found in the LICENSE file. + +#include +#include +#include +#include + +char dst[1024*1024*2+64] = {}; +char src[1024*1024*2+64] = {}; + +#define GUARDSIZE 32 +void test_memcpy(int copySize, int srcOffset, int dstOffset) { + char *dstContent = dst + GUARDSIZE + dstOffset; + char *srcContent = src + GUARDSIZE + srcOffset; + + char *dstGuardBefore = dstContent - GUARDSIZE; + char *dstGuardAfter = dstContent + copySize; + + char *srcGuardBefore = srcContent - GUARDSIZE; + char *srcGuardAfter = srcContent + copySize; + + char guard = (char)rand(); + + for (int i = 0; i < GUARDSIZE; ++i) { + // Generate a guardband area around dst memory that should not change. + dstGuardBefore[i] = (char)(guard ^ i); + dstGuardAfter[i] = (char)(guard ^ i); + } + + // Generate copy source data + char s = (char)rand(); + for (int i = 0; i < copySize + srcOffset + 2*GUARDSIZE; ++i) { + src[i] = (char)(s - i); + } + + memcpy(dstContent, srcContent, copySize); + if (!!memcmp(dstContent, srcContent, copySize)) { + printf("test_memcpy(copySize=%d, srcOffset=%d, dstOffset=%d failed!\n", copySize, srcOffset, dstOffset); + exit(1); + } + + // Verify guardband area + for (int i = 0; i < GUARDSIZE; ++i) { + // Generate a guardband area around dst memory that should not change. + if (dstGuardBefore[i] != (char)(guard ^ i)) { + printf("test_memcpy(copySize=%d, srcOffset=%d, dstOffset=%d failed! Pre-guardband area at i=%d overwritten!\n", copySize, srcOffset, dstOffset, i); + exit(1); + } + if (dstGuardAfter[i] != (char)(guard ^ i)) { + printf("test_memcpy(copySize=%d, srcOffset=%d, dstOffset=%d failed! Post-guardband area at i=%d overwritten!\n", copySize, srcOffset, dstOffset, i); + exit(1); + } + } +} + +void test_copysize(int copySize) { + int offsets[6] = { 0, 3, 4, 5, 8, 11 }; + + for (int srcOffset = 0; srcOffset < 6; ++srcOffset) { + for (int dstOffset = 0; dstOffset < 6; ++dstOffset) { + test_memcpy(copySize, offsets[srcOffset], offsets[dstOffset]); + } + } +} + +int main() { + for (int copySize = 0; copySize < 128; ++copySize) { + test_copysize(copySize); + } + + for (int copySizeI = 128; copySizeI <= 1048576; copySizeI <<= 1) { + for (int copySizeJ = 1; copySizeJ <= 16; copySizeJ <<= 1) { + test_copysize(copySizeI | copySizeJ); + } + } + + printf("OK.\n"); +} diff --git a/test/test_memcpy_alignment.cpp b/test/test_memcpy_alignment.cpp deleted file mode 100644 index 89ded8be5363..000000000000 --- a/test/test_memcpy_alignment.cpp +++ /dev/null @@ -1,88 +0,0 @@ -// Copyright 2017 The Emscripten Authors. All rights reserved. -// Emscripten is available under two separate licenses, the MIT license and the -// University of Illinois/NCSA Open Source License. Both these licenses can be -// found in the LICENSE file. - -#include -#include -#include -#include -#include -#include -#include - -#include - -char dst[1024*1024*2+64] = {}; -char src[1024*1024*2+64] = {}; - -#define GUARDSIZE 32 -void test_memcpy(int copySize, int srcOffset, int dstOffset) -{ - char *dstContent = dst + GUARDSIZE + dstOffset; - char *srcContent = src + GUARDSIZE + srcOffset; - - char *dstGuardBefore = dstContent - GUARDSIZE; - char *dstGuardAfter = dstContent + copySize; - - char *srcGuardBefore = srcContent - GUARDSIZE; - char *srcGuardAfter = srcContent + copySize; - - char guard = (char)rand(); - - for(int i = 0; i < GUARDSIZE; ++i) - { - // Generate a guardband area around dst memory that should not change. - dstGuardBefore[i] = (char)(guard ^ i); - dstGuardAfter[i] = (char)(guard ^ i); - } - - // Generate copy source data - char s = (char)rand(); - for(int i = 0; i < copySize + srcOffset + 2*GUARDSIZE; ++i) - src[i] = (char)(s - i); - - memcpy(dstContent, srcContent, copySize); - if (!!memcmp(dstContent, srcContent, copySize)) - { - printf("test_memcpy(copySize=%d, srcOffset=%d, dstOffset=%d failed!\n", copySize, srcOffset, dstOffset); - exit(1); - } - - // Verify guardband area - for(int i = 0; i < GUARDSIZE; ++i) - { - // Generate a guardband area around dst memory that should not change. - if (dstGuardBefore[i] != (char)(guard ^ i)) - { - printf("test_memcpy(copySize=%d, srcOffset=%d, dstOffset=%d failed! Pre-guardband area at i=%d overwritten!\n", copySize, srcOffset, dstOffset, i); - exit(1); - } - if (dstGuardAfter[i] != (char)(guard ^ i)) - { - printf("test_memcpy(copySize=%d, srcOffset=%d, dstOffset=%d failed! Post-guardband area at i=%d overwritten!\n", copySize, srcOffset, dstOffset, i); - exit(1); - } - } -} - -void test_copysize(int copySize) -{ - int offsets[6] = { 0, 3, 4, 5, 8, 11 }; - - for(int srcOffset = 0; srcOffset < 6; ++srcOffset) - for(int dstOffset = 0; dstOffset < 6; ++dstOffset) - test_memcpy(copySize, offsets[srcOffset], offsets[dstOffset]); -} - -int main() -{ - for(int copySize = 0; copySize < 128; ++copySize) - test_copysize(copySize); - - for(int copySizeI = 128; copySizeI <= 1048576; copySizeI <<= 1) - for(int copySizeJ = 1; copySizeJ <= 16; copySizeJ <<= 1) - test_copysize(copySizeI | copySizeJ); - - printf("OK.\n"); -} diff --git a/test/test_memset_alignment.c b/test/test_memset_alignment.c new file mode 100644 index 000000000000..8402f7661786 --- /dev/null +++ b/test/test_memset_alignment.c @@ -0,0 +1,74 @@ +// Copyright 2017 The Emscripten Authors. All rights reserved. +// Emscripten is available under two separate licenses, the MIT license and the +// University of Illinois/NCSA Open Source License. Both these licenses can be +// found in the LICENSE file. + +#include +#include +#include +#include +#include + +char ptr[1024*1024*2+64] = {}; + +#define GUARDSIZE 32 +void test_memset(int copySize, int offset) { + char *content = ptr + GUARDSIZE + offset; + + char *guardBefore = content - GUARDSIZE; + char *guardAfter = content + copySize; + + char guard = (char)rand(); + + for (int i = 0; i < GUARDSIZE; ++i) { + // Generate a guardband area around memory that should not change. + guardBefore[i] = (char)(guard ^ i); + guardAfter[i] = (char)(guard ^ i); + } + + // Generate fill source data + char s = (char)(rand() ^ 0xF0); + + memset(content, s, copySize); + for (int i = 0; i < copySize; ++i) { + if (content[i] != s) { + printf("test_memset(copySize=%d, offset=%d failed!\n", copySize, offset); + exit(1); + } + } + + // Verify guardband area + for (int i = 0; i < GUARDSIZE; ++i) { + // Generate a guardband area around memory that should not change. + if (guardBefore[i] != (char)(guard ^ i)) { + printf("test_memset(copySize=%d, offset=%d failed! Pre-guardband area at i=%d overwritten!\n", copySize, offset, i); + exit(1); + } + if (guardAfter[i] != (char)(guard ^ i)) { + printf("test_memset(copySize=%d, offset=%d failed! Post-guardband area at i=%d overwritten!\n", copySize, offset, i); + exit(1); + } + } +} + +void test_copysize(int copySize) { + for (int offset = 0; offset < 31; ++offset) { + test_memset(copySize, offset); + } +} + +int main() { + srand(time(NULL)); + + for (int copySize = 0; copySize < 128; ++copySize) { + test_copysize(copySize); + } + + for (int copySizeI = 128; copySizeI <= 1048576; copySizeI <<= 1) { + for (int copySizeJ = 1; copySizeJ <= 16; copySizeJ <<= 1) { + test_copysize(copySizeI | copySizeJ); + } + } + + printf("OK.\n"); +} diff --git a/test/test_memset_alignment.cpp b/test/test_memset_alignment.cpp deleted file mode 100644 index b5700328b0e3..000000000000 --- a/test/test_memset_alignment.cpp +++ /dev/null @@ -1,83 +0,0 @@ -// Copyright 2017 The Emscripten Authors. All rights reserved. -// Emscripten is available under two separate licenses, the MIT license and the -// University of Illinois/NCSA Open Source License. Both these licenses can be -// found in the LICENSE file. - -#include -#include -#include -#include -#include -#include -#include - -#include - -char ptr[1024*1024*2+64] = {}; - -#define GUARDSIZE 32 -void test_memset(int copySize, int offset) -{ - char *content = ptr + GUARDSIZE + offset; - - char *guardBefore = content - GUARDSIZE; - char *guardAfter = content + copySize; - - char guard = (char)rand(); - - for(int i = 0; i < GUARDSIZE; ++i) - { - // Generate a guardband area around memory that should not change. - guardBefore[i] = (char)(guard ^ i); - guardAfter[i] = (char)(guard ^ i); - } - - // Generate fill source data - char s = (char)(rand() ^ 0xF0); - - memset(content, s, copySize); - for(int i = 0; i < copySize; ++i) - { - if (content[i] != s) - { - printf("test_memset(copySize=%d, offset=%d failed!\n", copySize, offset); - exit(1); - } - } - - // Verify guardband area - for(int i = 0; i < GUARDSIZE; ++i) - { - // Generate a guardband area around memory that should not change. - if (guardBefore[i] != (char)(guard ^ i)) - { - printf("test_memset(copySize=%d, offset=%d failed! Pre-guardband area at i=%d overwritten!\n", copySize, offset, i); - exit(1); - } - if (guardAfter[i] != (char)(guard ^ i)) - { - printf("test_memset(copySize=%d, offset=%d failed! Post-guardband area at i=%d overwritten!\n", copySize, offset, i); - exit(1); - } - } -} - -void test_copysize(int copySize) -{ - for(int offset = 0; offset < 31; ++offset) - test_memset(copySize, offset); -} - -int main() -{ - srand(time(NULL)); - - for(int copySize = 0; copySize < 128; ++copySize) - test_copysize(copySize); - - for(int copySizeI = 128; copySizeI <= 1048576; copySizeI <<= 1) - for(int copySizeJ = 1; copySizeJ <= 16; copySizeJ <<= 1) - test_copysize(copySizeI | copySizeJ); - - printf("OK.\n"); -}