Skip to content

Commit

Permalink
use unittest asserts
Browse files Browse the repository at this point in the history
  • Loading branch information
kripken committed Nov 18, 2024
1 parent e3b905e commit 99ba1ee
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions test/unit/test_cluster_fuzz.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ def setUpClass(cls):
def test_bundle(self):
# The bundle should contain certain files:
# 1. run.py, the main entry point.
assert os.path.exists(os.path.join(self.clusterfuzz_dir, 'run.py'))
self.assertTrue(os.path.exists(os.path.join(self.clusterfuzz_dir, 'run.py')))
# 2. scripts/fuzz_shell.js, the js testcase shell
assert os.path.exists(os.path.join(self.clusterfuzz_dir, 'scripts', 'fuzz_shell.js'))
self.assertTrue(os.path.exists(os.path.join(self.clusterfuzz_dir, 'scripts', 'fuzz_shell.js')))
# 3. bin/wasm-opt, the wasm-opt binary in a static build
wasm_opt = os.path.join(self.clusterfuzz_dir, 'bin', 'wasm-opt')
assert os.path.exists(wasm_opt)
self.assertTrue(os.path.exists(wasm_opt))

# See that we can execute the bundled wasm-opt. It should be able to
# print out its version.
out = subprocess.check_output([wasm_opt, '--version'], text=True)
assert 'wasm-opt version ' in out
self.assertIn('wasm-opt version ', out)

# Generate N testcases, using run.py from a temp dir, and outputting to a
# testcase dir.
Expand All @@ -65,7 +65,7 @@ def generate_testcases(self, N, testcase_dir):
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
assert proc.returncode == 0
self.assertEqual(proc.returncode, 0)
return proc

# Test the bundled run.py script.
Expand All @@ -76,19 +76,19 @@ def test_run_py(self):
proc = self.generate_testcases(N, temp_dir.name)

# We should have logged the creation of N testcases.
assert proc.stdout.count('Created testcase:') == N
self.assertEqual(proc.stdout.count('Created testcase:'), N)

# We should have actually created them.
for i in range(0, N + 2):
fuzz_file = os.path.join(temp_dir.name, f'fuzz-binaryen-{i}.js')
flags_file = os.path.join(temp_dir.name, f'flags-binaryen-{i}.js')
# We actually emit the range [1, N], so 0 or N+1 should not exist.
if i >= 1 and i <= N:
assert os.path.exists(fuzz_file)
assert os.path.exists(flags_file)
self.assertTrue(os.path.exists(fuzz_file))
self.assertTrue(os.path.exists(flags_file))
else:
assert not os.path.exists(fuzz_file)
assert not os.path.exists(flags_file)
self.assertTrue(not os.path.exists(fuzz_file))
self.assertTrue(not os.path.exists(flags_file))

def test_fuzz_passes(self):
# We should see interesting passes being run in run.py. This is *NOT* a
Expand Down Expand Up @@ -158,7 +158,7 @@ def test_file_contents(self):

# The flags file must contain --wasm-staging
with open(flags_file) as f:
assert f.read() == '--wasm-staging'
self.assertEqual(f.read(), '--wasm-staging')

# The fuzz files begin with
#
Expand All @@ -168,8 +168,8 @@ def test_file_contents(self):
first_line = f.readline().strip()
start = 'var binary = new Uint8Array(['
end = ']);'
assert first_line.startswith(start)
assert first_line.endswith(end)
self.assertTrue(first_line.startswith(start))
self.assertTrue(first_line.endswith(end))
numbers = first_line[len(start):-len(end)]

# Convert to binary, and see that it is a valid file.
Expand Down Expand Up @@ -201,26 +201,26 @@ def test_file_contents(self):
print(f'mean struct.news: {statistics.mean(seen_struct_news)}')
print(f'stdev struct.news: {statistics.stdev(seen_struct_news)}')
print(f'median struct.news: {statistics.median(seen_struct_news)}')
assert max(seen_struct_news) >= 10
assert statistics.stdev(seen_struct_news) > 0
self.assertGreaterEqual(max(seen_struct_news), 10)
self.assertGreater(statistics.stdev(seen_struct_news), 0)

print()

# sizes appear to be distributed as mean 2933, stddev 2011, median 2510.
print(f'mean sizes: {statistics.mean(seen_sizes)}')
print(f'stdev sizes: {statistics.stdev(seen_sizes)}')
print(f'median sizes: {statistics.median(seen_sizes)}')
assert max(seen_sizes) >= 1000
assert statistics.stdev(seen_sizes) > 0
self.assertGreaterEqual(max(seen_sizes), 1000)
self.assertGreater(statistics.stdev(seen_sizes), 0)

print()

# exports appear to be distributed as mean 9, stddev 6, median 8.
print(f'mean exports: {statistics.mean(seen_exports)}')
print(f'stdev exports: {statistics.stdev(seen_exports)}')
print(f'median exports: {statistics.median(seen_exports)}')
assert max(seen_exports) >= 8
assert statistics.stdev(seen_exports) > 0
self.assertGreaterEqual(max(seen_exports), 8)
self.assertGreater(statistics.stdev(seen_exports), 0)

print()

Expand All @@ -240,7 +240,7 @@ def test_zzz_bundle_build_dir(self):
except subprocess.CalledProcessError:
# Expected error.
failed = True
assert failed
self.assertTrue(failed)

# Test with a valid --build-dir.
cmd.pop()
Expand Down

0 comments on commit 99ba1ee

Please sign in to comment.