Skip to content

Commit 53cc8d1

Browse files
Nathan LeeChromeos LUCI
Nathan Lee
authored and
Chromeos LUCI
committed
cras: dsp: Add testtool for comparing behaviors of C code and rust code
Use python to write a script which complie the dsp tests with libraries in C and rust respectively, and run the two binaries with same input to compare their behaavior. To run the test tool, an additional 'adhd' directory is needed and would be passed to the tool as an argument. BUG=None TEST=None Change-Id: Iabb49d86469bc0fff06cf4dc08b1fe1ea756fa08 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/adhd/+/5677488 Commit-Queue: Nathan Lee <[email protected]> Tested-by: [email protected] <[email protected]> Reviewed-by: Li-Yu Yu <[email protected]>
1 parent 116822e commit 53cc8d1

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env vpython3
2+
# Copyright 2024 The ChromiumOS Authors
3+
# Use of this source code is governed by a BSD-style license that can be
4+
# found in the LICENSE file.
5+
6+
import argparse
7+
import glob
8+
import os
9+
import shutil
10+
import subprocess
11+
import sys
12+
13+
14+
parser = argparse.ArgumentParser(
15+
description='Build the same target with C and Rust from two adhd directory.'
16+
)
17+
parser.add_argument(
18+
'test_target', metavar='"test_target.c"', type=str, help='Build target to be test'
19+
)
20+
parser.add_argument(
21+
'adhd_dir',
22+
metavar='"comparing adhd directory"',
23+
type=str,
24+
help='Origin adhd directory with C libraries',
25+
)
26+
parser.add_argument(
27+
'testfiles_dir', metavar='"testfiles directory"', type=str, help='Directory with .wav testfiles'
28+
)
29+
args = parser.parse_args()
30+
31+
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
32+
CUR_ADHD = os.path.join(SCRIPT_DIR, "../..")
33+
34+
if (not os.path.isfile(os.path.join(CUR_ADHD, "cras/src/dsp/tests/", args.test_target))) or (
35+
not os.path.isfile(os.path.join(args.adhd_dir, "cras/src/dsp/tests/", args.test_target))
36+
):
37+
raise SystemExit("Test targets not found")
38+
39+
if not os.path.isfile(os.path.join(SCRIPT_DIR, "cmpraw")):
40+
subprocess.run(["bazel", "build", "//cras/src/dsp/tests:cmpraw"], cwd=CUR_ADHD, check=True)
41+
shutil.copy("bazel-bin/cras/src/dsp/tests/cmpraw", SCRIPT_DIR)
42+
BUILD_TARGET = args.test_target.removesuffix(".c")
43+
try:
44+
os.mkdir(os.path.join(SCRIPT_DIR, "sandbox"))
45+
except FileExistsError:
46+
pass
47+
shutil.copy(
48+
os.path.join("bazel-bin/cras/src/dsp/tests/", BUILD_TARGET),
49+
os.path.join(SCRIPT_DIR, "sandbox/rust"),
50+
)
51+
subprocess.run(["bazel", "build", "//cras/src/dsp/tests:" + BUILD_TARGET], cwd=args.adhd_dir)
52+
shutil.copy(
53+
os.path.join("bazel-bin/cras/src/dsp/tests/", BUILD_TARGET),
54+
os.path.join(SCRIPT_DIR, "sandbox/c"),
55+
)
56+
os.chdir(SCRIPT_DIR)
57+
for file in os.listdir(args.testfiles_dir):
58+
f = os.path.join(args.testfiles_dir, file)
59+
subprocess.run(
60+
[
61+
"sox",
62+
f,
63+
"--bits",
64+
"16",
65+
"--encoding",
66+
"signed-integer",
67+
"--endian",
68+
"little",
69+
"./sandbox/test_file.raw",
70+
],
71+
cwd=SCRIPT_DIR,
72+
check=True,
73+
)
74+
subprocess.run(
75+
["./sandbox/rust", "sandbox/test_file.raw", "sandbox/rust_output.raw"],
76+
cwd=SCRIPT_DIR,
77+
check=True,
78+
)
79+
subprocess.run(
80+
["./sandbox/c", "sandbox/test_file.raw", "sandbox/c_output.raw"], cwd=SCRIPT_DIR, check=True
81+
)
82+
print(file + " result:", end='', flush=True)
83+
subprocess.run(
84+
["./cmpraw", "sandbox/c_output.raw", "sandbox/rust_output.raw"], cwd=SCRIPT_DIR, check=True
85+
)
86+
shutil.rmtree("sandbox")
87+
print("Remove generated binary with the command if there is no further testing:")
88+
print("\trm -f devtools/test_dsp_rust/cmpraw")

0 commit comments

Comments
 (0)