Skip to content
Snippets Groups Projects
Commit e698f7b2 authored by Jason Hiser's avatar Jason Hiser :tractor:
Browse files

Add rust tests

parent 715bc909
No related branches found
No related tags found
1 merge request!38Go support
hello
8q
panic
*.zipr
peasoup_exe*
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
}
......@@ -30,7 +30,7 @@ testone()
main()
{
for bench in panic hello 8q
for bench in panic hello
do
for opts in "-c rida" ""
do
......
hello
8q
panic
*.zipr
peasoup_exe*
// 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.");
}
}
// This is the main function
fn main() {
// Print text to the console
println!("Hello World!");
}
#!/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 "$@"
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),
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment