Skip to content

Commit

Permalink
feat: added fuzzy testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Saphereye committed Sep 24, 2024
1 parent b1ec217 commit 851cf30
Show file tree
Hide file tree
Showing 7 changed files with 299 additions and 104 deletions.
193 changes: 193 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ version = "0.1.0"
edition = "2021"

[dependencies]
clap = { version = "4.5.18", features = ["derive"] }
rand = "0.8.5"
62 changes: 62 additions & 0 deletions fuzzy_testing/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import chess
import subprocess
import random

def generate_random_fen():
"""Generates a random legal board position (FEN string)."""
board = chess.Board()
move_count = random.randint(0, 100) # Random number of moves to play

for _ in range(move_count):
if board.is_game_over():
break
legal_moves = list(board.legal_moves)
board.push(random.choice(legal_moves)) # Play random legal move

return board.fen()

def get_legal_moves_from_executable(fen):
"""Runs the executable with the given FEN string and gets legal moves."""
# Modify this command to point to your executable's path
result = subprocess.run(['/home/adarsh/Coding/shard/target/release/shard', fen], capture_output=True, text=True)

# Assuming the executable outputs moves as space-separated UCI strings
moves = result.stdout.strip().split()
return moves

def run_fuzzy_test():
"""Performs fuzzy testing by comparing executable output with python-chess."""
i = 0
subprocess.run(["cargo", "build", "--release"])
while True:
print(f"Test iteration {i + 1}")

# Generate random FEN
random_fen = generate_random_fen()
print("Testing FEN:", random_fen)

# Get legal moves using python-chess
board = chess.Board(random_fen)
python_moves = [move.uci() for move in board.legal_moves]

# Get legal moves from executable
executable_moves = get_legal_moves_from_executable(random_fen)

# Compare results
python_moves_set = set(python_moves)
executable_moves_set = set(executable_moves)

if python_moves_set != executable_moves_set:
print(f"Discrepancy found in test {i + 1}!")
print(f"FEN: {random_fen}")
print(f"python-chess moves: {sorted(python_moves_set)}")
print(f"Executable moves: {sorted(executable_moves_set)}")
print(f"Moves in python-chess not present in executable: {sorted(python_moves_set - executable_moves_set)}")
print(f"Moves in executable not present in python-chess: {sorted(executable_moves_set - python_moves_set)}")
break
else:
print(f"Test {i + 1} passed!\n")
i += 1

if __name__ == "__main__":
run_fuzzy_test()
14 changes: 0 additions & 14 deletions reference_testing/main.py

This file was deleted.

Loading

0 comments on commit 851cf30

Please sign in to comment.