diff --git a/tests/go_test/.gitignore b/tests/go_test/.gitignore index d5f3183450ef710b3ef8890b028374be8f494629..6ab5f19de95c9729271f987ab449b9a6151eb2c7 100644 --- a/tests/go_test/.gitignore +++ b/tests/go_test/.gitignore @@ -1,5 +1,4 @@ hello -8q panic *.zipr peasoup_exe* diff --git a/tests/go_test/8q.go b/tests/go_test/8q.go deleted file mode 100644 index f560162fc645aa9e39e8b1fb9963cab8d6eae287..0000000000000000000000000000000000000000 --- a/tests/go_test/8q.go +++ /dev/null @@ -1,67 +0,0 @@ -package main - -import ( - "fmt" - "math" -) - -type Point struct { - x int - y int -} - -var results = make([][]Point, 0) - -func main() { - Solve(6) - -} - -func Solve(n int) { - for col := 0; col < n; col++ { - start := Point{x: col, y: 0} - current := make([]Point, 0) - Recurse(start, current, n) - } - fmt.Print("Results:\n") - for _, result := range results { - fmt.Println(result) - } - fmt.Printf("There were %d results\n", len(results)) -} -func Recurse(point Point, current []Point, n int) { - if CanPlace(point, current) { - current = append(current, point) - if len(current) == n { - c := make([]Point, n) - for i, point := range current { - c[i] = point - } - results = append(results, c) - } else { - for col := 0; col < n; col++ { - for row := point.y; row < n; row++ { - nextStart := Point{x: col, y: row} - Recurse(nextStart, current, n) - - } - - } - } - } -} -func CanPlace(target Point, board []Point) bool { - for _, point := range board { - if CanAttack(point, target) { - return false - } - } - return true -} - -func CanAttack(a, b Point) bool { - //fmt.Print(a, b) - answer := a.x == b.x || a.y == b.y || math.Abs(float64(a.y-b.y)) == math.Abs(float64(a.x-b.x)) - //fmt.Print(answer) - return answer -} diff --git a/tests/go_test/testit.sh b/tests/go_test/testit.sh index 1bb4b520a89e2e645c9124d6fac51070f8250c63..4cdf8b1aaa78e39bfdf3f37abb2ed916a6d98937 100755 --- a/tests/go_test/testit.sh +++ b/tests/go_test/testit.sh @@ -30,7 +30,7 @@ testone() main() { - for bench in panic hello 8q + for bench in panic hello do for opts in "-c rida" "" do diff --git a/tests/rust_test/.gitignore b/tests/rust_test/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d5f3183450ef710b3ef8890b028374be8f494629 --- /dev/null +++ b/tests/rust_test/.gitignore @@ -0,0 +1,5 @@ +hello +8q +panic +*.zipr +peasoup_exe* diff --git a/tests/rust_test/8q.rs b/tests/rust_test/8q.rs new file mode 100644 index 0000000000000000000000000000000000000000..956ba26952f9c8c3ac18f3a5e895e922d63fe07d --- /dev/null +++ b/tests/rust_test/8q.rs @@ -0,0 +1,52 @@ +// Function to print the solution +fn print_solution(board: &[i32]) { + for i in 0..8 { + for j in 0..8 { + print!(" {} ", if board[i] == j { "Q" } else { "." }); + } + println!(); + } +} + +// Function to check if the placement of a queen is safe +fn is_safe(board: &[i32], row: i32, col: i32) -> bool { + for i in 0..row { + if board[i as usize] == col || (i - row).abs() == (board[i as usize] - col).abs() { + return false; + } + } + true +} + +// Function to solve the 8 queens problem +fn solve_8_queens(board: &mut [i32], row: i32) -> bool { + if row == 8 { + return true; + } + + for col in 0..8 { + if is_safe(board, row, col) { + board[row as usize] = col; + + if solve_8_queens(board, row + 1) { + return true; + } + + board[row as usize] = -1; + } + } + + false +} + +fn main() { + let mut board = [-1; 8]; + + if solve_8_queens(&mut board, 0) { + println!("Solution found:"); + print_solution(&board); + } else { + println!("No solution found."); + } +} + diff --git a/tests/rust_test/hello.rs b/tests/rust_test/hello.rs new file mode 100644 index 0000000000000000000000000000000000000000..f6cc5326d38e6fa82ef54761f2ad3b0d20d26dda --- /dev/null +++ b/tests/rust_test/hello.rs @@ -0,0 +1,6 @@ +// This is the main function +fn main() { + // Print text to the console + println!("Hello World!"); +} + diff --git a/tests/rust_test/testit.sh b/tests/rust_test/testit.sh new file mode 100644 index 0000000000000000000000000000000000000000..0073f5db2a85ca1e8b2592dbd4db7e4a31100909 --- /dev/null +++ b/tests/rust_test/testit.sh @@ -0,0 +1,44 @@ +#!/bin/bash + +testone() +{ + local PUT=$1 + local OPTS=$2 + set -x + set -e + rustc $PUT.rs -o $PUT + + $PSZ $PUT ./$PUT.zipr $OPTS + diff <(./$PUT 2>&1 ) <(./$PUT.zipr 2>&1 ) + + # turn off exit-on-error because some programs err and we need their exit code. + set +e + ./$PUT > /dev/null 2>&1 + local putRes=$? + ./$PUT.zipr > /dev/null 2>&1 + local putResZipr=$? + set -e + + if [[ $putRes != $putResZipr ]]; + then + echo failed! + exit 1 + fi + + rm -rf peasoup* $PUT.zipr $PUT.zipr +} + +main() +{ + for bench in 8q hello throw + do + for opts in "-c rida" "" + do + testone $bench "$opts" + done + done + exit 0 + +} + +main "$@" diff --git a/tests/rust_test/throw.rs b/tests/rust_test/throw.rs new file mode 100644 index 0000000000000000000000000000000000000000..058b20d194869c0d335db7a31c75f6f199959d0a --- /dev/null +++ b/tests/rust_test/throw.rs @@ -0,0 +1,31 @@ +use std::fs::File; +use std::fs::OpenOptions; +use std::io::ErrorKind; +use std::io::Read; + +fn read_file(file_name: &str) -> Result<String, std::io::Error> { + let mut file = match OpenOptions::new().open(file_name) { + Ok(file) => file, + Err(error) => match error.kind() { + ErrorKind::NotFound => match File::create(file_name) { + Ok(fc) => fc, + Err(e) => return Err(e), + }, + _other_error => return Err(error), + }, + }; + + let mut contents = String::new(); + match file.read_to_string(&mut contents) { + Ok(_) => Ok(contents), + Err(error) => Err(error), + } +} + +fn main() { + match read_file("test.txt") { + Ok(contents) => println!("File contents: {}", contents), + Err(error) => println!("Error reading file: {}", error), + } +} +