From e698f7b27bb9c139bf1b614f79c0df007d72b9cb Mon Sep 17 00:00:00 2001
From: Jason Hiser <jdhiser@gmail.com>
Date: Wed, 1 Feb 2023 09:04:08 -0500
Subject: [PATCH] Add rust tests

---
 tests/go_test/.gitignore   |  1 -
 tests/go_test/8q.go        | 67 --------------------------------------
 tests/go_test/testit.sh    |  2 +-
 tests/rust_test/.gitignore |  5 +++
 tests/rust_test/8q.rs      | 52 +++++++++++++++++++++++++++++
 tests/rust_test/hello.rs   |  6 ++++
 tests/rust_test/testit.sh  | 44 +++++++++++++++++++++++++
 tests/rust_test/throw.rs   | 31 ++++++++++++++++++
 8 files changed, 139 insertions(+), 69 deletions(-)
 delete mode 100644 tests/go_test/8q.go
 create mode 100644 tests/rust_test/.gitignore
 create mode 100644 tests/rust_test/8q.rs
 create mode 100644 tests/rust_test/hello.rs
 create mode 100644 tests/rust_test/testit.sh
 create mode 100644 tests/rust_test/throw.rs

diff --git a/tests/go_test/.gitignore b/tests/go_test/.gitignore
index d5f318345..6ab5f19de 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 f560162fc..000000000
--- 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 1bb4b520a..4cdf8b1aa 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 000000000..d5f318345
--- /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 000000000..956ba2695
--- /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 000000000..f6cc5326d
--- /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 000000000..0073f5db2
--- /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 000000000..058b20d19
--- /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),
+    }
+}
+
-- 
GitLab