Skip to content

Commit c09f9dd

Browse files
committed
[test] Simplify test_openjpeg. NFC
This change simplifies the test in 2 ways: 1. Remove the "extra testing" part of the test that tries to run the test again if `ALLOW_MEMORY_GROWTH` is set. The only test modes that have `ALLOW_MEMORY_GROWTH` are the santizier modes and it seems odd for an individual test to decide to disable this. 2. Remove the alternate `is_sanitizing` method of running the test. I believe this was only needed becuase of (1), and so can be safely removed. By removing this complexity this test now has less nesting and is more directly readable and more similar to other tests.
1 parent 34c83d8 commit c09f9dd

File tree

1 file changed

+90
-110
lines changed

1 file changed

+90
-110
lines changed

test/test_core.py

Lines changed: 90 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -6807,117 +6807,97 @@ def test_poppler(self):
68076807
@no_wasm64('MEMORY64 does not yet support SJLJ')
68086808
@is_slow_test
68096809
def test_openjpeg(self):
6810-
def do_test_openjpeg():
6811-
def line_splitter(data):
6812-
out = ''
6813-
counter = 0
6814-
6815-
for ch in data:
6816-
out += ch
6817-
if ch == ' ' and counter > 60:
6818-
out += '\n'
6819-
counter = 0
6820-
else:
6821-
counter += 1
6822-
6823-
return out
6824-
6825-
# remove -g, so we have one test without it by default
6826-
self.emcc_args = [x for x in self.emcc_args if x != '-g']
6827-
6828-
original_j2k = test_file('openjpeg/syntensity_lobby_s.j2k')
6829-
image_bytes = list(bytearray(read_binary(original_j2k)))
6830-
create_file('pre.js', """
6831-
Module.preRun = () => FS.createDataFile('/', 'image.j2k', %s, true, false, false);
6832-
Module.postRun = () => {
6833-
out('Data: ' + JSON.stringify(Array.from(FS.readFile('image.raw'))));
6834-
};
6835-
""" % line_splitter(str(image_bytes)))
6836-
6837-
# ensure libpng is built so that openjpeg's configure step can detect it.
6838-
# If we don't do this then we don't know what the state of the cache will be
6839-
# and this test would different non-deterministic results based on, for example,
6840-
# what other tests had previously run.
6841-
builder_cmd = [EMBUILDER, 'build', 'libpng']
6842-
if self.get_setting('MEMORY64'):
6843-
builder_cmd.append('--wasm64')
6844-
self.emcc_args.append('-Wno-pointer-to-int-cast')
6845-
self.run_process(builder_cmd)
6846-
lib = self.get_library('third_party/openjpeg',
6847-
[Path('codec/CMakeFiles/j2k_to_image.dir/index.c.o'),
6848-
Path('codec/CMakeFiles/j2k_to_image.dir/convert.c.o'),
6849-
Path('codec/CMakeFiles/j2k_to_image.dir/__/common/color.c.o'),
6850-
Path('bin/libopenjpeg.a')],
6851-
configure=['cmake', '.'],
6852-
# configure_args=['--enable-tiff=no', '--enable-jp3d=no', '--enable-png=no'],
6853-
make_args=[]) # no -j 2, since parallel builds can fail
6854-
6855-
# We use doubles in JS, so we get slightly different values than native code. So we
6856-
# check our output by comparing the average pixel difference
6857-
def image_compare(output):
6858-
# Get the image generated by JS, from the JSON.stringify'd array
6859-
m = re.search(r'\[[\d, -]*\]', output)
6860-
self.assertIsNotNone(m, 'Failed to find proper image output in: ' + output)
6861-
# Evaluate the output as a python array
6862-
js_data = eval(m.group(0))
6863-
6864-
js_data = [x if x >= 0 else 256 + x for x in js_data] # Our output may be signed, so unsign it
6865-
6866-
# Get the correct output
6867-
true_data = bytearray(read_binary(test_file('openjpeg/syntensity_lobby_s.raw')))
6868-
6869-
# Compare them
6870-
self.assertEqual(len(js_data), len(true_data))
6871-
num = len(js_data)
6872-
diff_total = js_total = true_total = 0
6873-
for i in range(num):
6874-
js_total += js_data[i]
6875-
true_total += true_data[i]
6876-
diff_total += abs(js_data[i] - true_data[i])
6877-
js_mean = js_total / float(num)
6878-
true_mean = true_total / float(num)
6879-
diff_mean = diff_total / float(num)
6880-
6881-
image_mean = 83.265
6882-
# print '[image stats:', js_mean, image_mean, true_mean, diff_mean, num, ']'
6883-
assert abs(js_mean - image_mean) < 0.01, [js_mean, image_mean]
6884-
assert abs(true_mean - image_mean) < 0.01, [true_mean, image_mean]
6885-
assert diff_mean < 0.01, diff_mean
6886-
6887-
return output
6888-
6889-
self.emcc_args += ['--minify=0'] # to compare the versions
6890-
self.emcc_args += ['--pre-js', 'pre.js']
6891-
6892-
def do_test():
6893-
self.do_runf('third_party/openjpeg/codec/j2k_to_image.c',
6894-
'Successfully generated', # The real test for valid output is in image_compare
6895-
args='-i image.j2k -o image.raw'.split(),
6896-
emcc_args=['-sUSE_LIBPNG'],
6897-
libraries=lib,
6898-
includes=[test_file('third_party/openjpeg/libopenjpeg'),
6899-
test_file('third_party/openjpeg/codec'),
6900-
test_file('third_party/openjpeg/common'),
6901-
Path(self.get_build_dir(), 'third_party/openjpeg')],
6902-
output_nicerizer=image_compare)
6903-
6904-
do_test()
6905-
6906-
# extra testing
6907-
if self.get_setting('ALLOW_MEMORY_GROWTH') == 1:
6908-
print('no memory growth', file=sys.stderr)
6909-
self.set_setting('ALLOW_MEMORY_GROWTH', 0)
6910-
do_test()
6810+
def line_splitter(data):
6811+
out = ''
6812+
counter = 0
6813+
6814+
for ch in data:
6815+
out += ch
6816+
if ch == ' ' and counter > 60:
6817+
out += '\n'
6818+
counter = 0
6819+
else:
6820+
counter += 1
69116821

6912-
if is_sanitizing(self.emcc_args):
6913-
# In ASan mode we need a large initial memory (or else wasm-ld fails).
6914-
# The OpenJPEG CMake will build several executables (which we need parts
6915-
# of in our testing, see above), so we must enable the flag for them all.
6916-
with env_modify({'EMCC_CFLAGS': '-sINITIAL_MEMORY=300MB'}):
6917-
self.emcc_args.append('-Wno-unused-command-line-argument')
6918-
do_test_openjpeg()
6919-
else:
6920-
do_test_openjpeg()
6822+
return out
6823+
6824+
# remove -g, so we have one test without it by default
6825+
self.emcc_args = [x for x in self.emcc_args if x != '-g']
6826+
6827+
original_j2k = test_file('openjpeg/syntensity_lobby_s.j2k')
6828+
image_bytes = list(bytearray(read_binary(original_j2k)))
6829+
create_file('pre.js', """
6830+
Module.preRun = () => FS.createDataFile('/', 'image.j2k', %s, true, false, false);
6831+
Module.postRun = () => {
6832+
out('Data: ' + JSON.stringify(Array.from(FS.readFile('image.raw'))));
6833+
};
6834+
""" % line_splitter(str(image_bytes)))
6835+
6836+
# ensure libpng is built so that openjpeg's configure step can detect it.
6837+
# If we don't do this then we don't know what the state of the cache will be
6838+
# and this test would different non-deterministic results based on, for example,
6839+
# what other tests had previously run.
6840+
builder_cmd = [EMBUILDER, 'build', 'libpng']
6841+
if self.get_setting('MEMORY64'):
6842+
builder_cmd.append('--wasm64')
6843+
self.emcc_args.append('-Wno-pointer-to-int-cast')
6844+
self.run_process(builder_cmd)
6845+
lib = self.get_library('third_party/openjpeg',
6846+
[Path('codec/CMakeFiles/j2k_to_image.dir/index.c.o'),
6847+
Path('codec/CMakeFiles/j2k_to_image.dir/convert.c.o'),
6848+
Path('codec/CMakeFiles/j2k_to_image.dir/__/common/color.c.o'),
6849+
Path('bin/libopenjpeg.a')],
6850+
configure=['cmake', '.'],
6851+
# configure_args=['--enable-tiff=no', '--enable-jp3d=no', '--enable-png=no'],
6852+
make_args=[]) # no -j 2, since parallel builds can fail
6853+
6854+
# We use doubles in JS, so we get slightly different values than native code. So we
6855+
# check our output by comparing the average pixel difference
6856+
def image_compare(output):
6857+
# Get the image generated by JS, from the JSON.stringify'd array
6858+
m = re.search(r'\[[\d, -]*\]', output)
6859+
6860+
self.assertIsNotNone(m, 'Failed to find proper image output in: ' + output)
6861+
# Evaluate the output as a python array
6862+
js_data = eval(m.group(0))
6863+
6864+
js_data = [x if x >= 0 else 256 + x for x in js_data] # Our output may be signed, so unsign it
6865+
# Get the correct output
6866+
true_data = bytearray(read_binary(test_file('openjpeg/syntensity_lobby_s.raw')))
6867+
6868+
# Compare them
6869+
self.assertEqual(len(js_data), len(true_data))
6870+
num = len(js_data)
6871+
diff_total = js_total = true_total = 0
6872+
for i in range(num):
6873+
js_total += js_data[i]
6874+
true_total += true_data[i]
6875+
diff_total += abs(js_data[i] - true_data[i])
6876+
js_mean = js_total / float(num)
6877+
true_mean = true_total / float(num)
6878+
diff_mean = diff_total / float(num)
6879+
6880+
image_mean = 83.265
6881+
# print '[image stats:', js_mean, image_mean, true_mean, diff_mean, num, ']'
6882+
assert abs(js_mean - image_mean) < 0.01, [js_mean, image_mean]
6883+
assert abs(true_mean - image_mean) < 0.01, [true_mean, image_mean]
6884+
assert diff_mean < 0.01, diff_mean
6885+
6886+
return output
6887+
6888+
self.emcc_args += ['--minify=0'] # to compare the versions
6889+
self.emcc_args += ['--pre-js', 'pre.js']
6890+
6891+
self.do_runf('third_party/openjpeg/codec/j2k_to_image.c',
6892+
'Successfully generated', # The real test for valid output is in image_compare
6893+
args='-i image.j2k -o image.raw'.split(),
6894+
emcc_args=['-sUSE_LIBPNG'],
6895+
libraries=lib,
6896+
includes=[test_file('third_party/openjpeg/libopenjpeg'),
6897+
test_file('third_party/openjpeg/codec'),
6898+
test_file('third_party/openjpeg/common'),
6899+
Path(self.get_build_dir(), 'third_party/openjpeg')],
6900+
output_nicerizer=image_compare)
69216901

69226902
@also_with_standalone_wasm(impure=True)
69236903
@no_asan('autodebug logging interferes with asan')

0 commit comments

Comments
 (0)