Skip to content

Commit

Permalink
Added test for speed as opposed to accuracy. The new python script ma…
Browse files Browse the repository at this point in the history
…kes one file with many test cases instead of multiples files, each with one test case.
  • Loading branch information
Angelica-Schell committed Jul 11, 2024
1 parent 3e96d76 commit 51583e6
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 22 deletions.
46 changes: 46 additions & 0 deletions tools/data-conversion/scripts/binary_to_fixed_speed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import random
import os
import subprocess


def generate_binary_string(length):
"""Generate a random binary string of given length."""
return "".join(random.choice("01") for _ in range(length))


def generate_tests(num_tests):
"""Generate `num_tests` random tests."""
tests = []
for _ in range(num_tests):
# Generate a random binary string (up to 32 bits for u32 in Rust)
binary_string = generate_binary_string(random.randint(1, 24))
tests.append((binary_string))

return tests


def write_input_files(tests, input_dir):
os.makedirs(input_dir, exist_ok=True)
input_paths = []

for binary_string in tests:
input_path = os.path.join(input_dir, f"speed_test.in")
with open(input_path, "a") as f:
f.write(f"{binary_string}\n")
input_paths.append(input_path)

return input_paths


if __name__ == "__main__":
num_tests = 1_000_000
input_dir = "./testsuite/inputs"
exponent = -4

# Generate Tests
tests = generate_tests(num_tests)

# Write Input File
write_input_files(tests, input_dir)

print(f"Input files are located in {input_dir}.")
47 changes: 28 additions & 19 deletions tools/data-conversion/scripts/binary_to_fixed_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

def generate_binary_string(length):
"""Generate a random binary string of given length."""
return ''.join(random.choice('01') for _ in range(length))
return "".join(random.choice("01") for _ in range(length))


def generate_tests(num_tests):
"""Generate `num_tests` random tests."""
Expand All @@ -14,78 +15,86 @@ def generate_tests(num_tests):
# Generate a random binary string (up to 32 bits for u32 in Rust)
binary_string = generate_binary_string(random.randint(1, 24))
tests.append((binary_string))

return tests


def write_input_files(tests, input_dir):
os.makedirs(input_dir, exist_ok=True)
input_paths = []

for idx, binary_string in enumerate(tests):
input_path = os.path.join(input_dir, f"test_{idx+1}.in")
with open(input_path, 'w') as f:
with open(input_path, "w") as f:
f.write(f"{binary_string}")
input_paths.append(input_path)

return input_paths


def write_expect_files(tests, expect_dir):
os.makedirs(expect_dir, exist_ok=True)
expect_paths = []

for idx, binary_string in enumerate(tests):
expect_path = os.path.join(expect_dir, f"test_{idx+1}.expect")
with open(expect_path, 'w') as f:
with open(expect_path, "w") as f:
f.write(f"{binary_string}")
expect_paths.append(expect_path)


def convert_binary_to_fixed(binary_string, exponent):
"""Convert binary string to a fixed-point number."""
binary_value = int(binary_string, 2) # Convert binary string to integer
fixed_point_number = binary_value * (2 ** exponent) # Calculate the fixed-point number
formatted = '{:.8e}'.format(fixed_point_number)
fixed_point_number = binary_value * (
2 ** exponent
) # Calculate the fixed-point number
formatted = "{:.8e}".format(fixed_point_number)
return formatted + "\n"


def run_rust_function(input_file, output_file, exponent):
rust_command = f"../../target/debug/data-conversion --from {input_file} --to {output_file} --ftype 'binary' --totype 'fixed' --exp {exponent}"
result = subprocess.run(rust_command, shell=True, capture_output=True, text=True)
if result.returncode != 0:
print(f"Can't run rust function")
return result.returncode == 0


def compare_files(output_file, expect_file):
with open(output_file, 'r') as f:
with open(output_file, "r") as f:
output_content = f.read().strip()
output_content = float(output_content)
with open(expect_file, 'r') as f:
with open(expect_file, "r") as f:
expect_content = f.read().strip()
expect_content = float(expect_content)

return output_content == expect_content

if __name__ == '__main__':

if __name__ == "__main__":
num_tests = 100
input_dir = "../testsuite/inputs"
expect_dir = "../testsuite/expect"
output_dir = "../testsuite/outputs"
exponent = -4

#Generate Tests
# Generate Tests
tests = generate_tests(num_tests)
#Write Input Files

# Write Input Files
input_paths = write_input_files(tests, input_dir)
#Generate Expected Output

# Generate Expected Output
results = []
for binary_string in tests:
results.append(convert_binary_to_fixed(binary_string, exponent))
expect_paths = write_expect_files(results, expect_dir)

#Make sure the output directory exists
# Make sure the output directory exists
os.makedirs(output_dir, exist_ok=True)

#Run Tests and Compare Outputs
# Run Tests and Compare Outputs
for idx, test_file in enumerate(input_paths):
input_file = test_file
output_file = os.path.join(output_dir, f"test_{idx+1}.out")
Expand All @@ -98,7 +107,7 @@ def compare_files(output_file, expect_file):
print(f"Test {idx+1} failed: output does not match expected.")
else:
print(f"Test {idx+1} failed to run.")

print(f"Input files are located in {input_dir}/.")
print(f"Expected output files are located in {expect_dir}/.")
print(f"Output files are located in {output_dir}/.")
print(f"Output files are located in {output_dir}/.")
6 changes: 3 additions & 3 deletions tools/data-conversion/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ fn convert(
bits: bool,
) {
// Create the output file if filepath_send is Some
let mut converted: Option<File> = filepath_send.as_ref().map(|path| {
File::create(path).expect("creation failed")
});
let mut converted: Option<File> = filepath_send
.as_ref()
.map(|path| File::create(path).expect("creation failed"));

match (convert_from, convert_to) {
(NumType::Hex, NumType::Binary) => {
Expand Down

0 comments on commit 51583e6

Please sign in to comment.