Compare commits

...

5 Commits

Author SHA1 Message Date
Karma Riuk
4f79d6f1ed Removed useless print statements 2023-08-14 15:09:24 +02:00
Karma Riuk
346cfe9705 Finally written some good tests for the image writer 2023-08-14 15:08:27 +02:00
Karma Riuk
94af003adc Added some assets 2023-08-14 15:08:13 +02:00
Karma Riuk
aec655610a Added files to gitignore 2023-08-14 15:07:10 +02:00
Karma Riuk
2423645f3a Removed log statement 2023-08-14 14:27:07 +02:00
11 changed files with 116 additions and 69 deletions

2
.gitignore vendored
View File

@ -20,3 +20,5 @@
# Go workspace file # Go workspace file
go.work go.work
/Session.vim /Session.vim
/maze.png
/maze_sol.png

BIN
assets/real.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 151 KiB

BIN
assets/solved/normal.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 987 B

BIN
assets/solved/normal2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

BIN
assets/solved/real.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 316 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 424 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 423 B

BIN
assets/solved/trivial.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 286 B

View File

@ -1,71 +1,122 @@
package writer package writer
import ( import (
"bytes"
"image/color"
"io"
"maze-solver/maze"
"os"
"testing" "testing"
"github.com/mazznoer/colorgrad"
)
const (
OUT_DIR = "./out"
EXPECTED_DIR = "../../assets/solved"
) )
func TestImageWriter(t *testing.T) { func TestImageWriter(t *testing.T) {
// pathGradient, err := colorgrad.NewGradient().Colors(color.White).Build() if _, err := os.Stat(OUT_DIR); os.IsNotExist(err) {
// if err != nil { os.Mkdir(OUT_DIR, 0700)
// panic(err) }
// }
// tests := []struct {
// tests := []struct { name string
// name string filename string
// filename string m *maze.SolvedMaze
// m *maze.SolvedMaze CellWidth, cellHeight int
// CellWidth, cellHeight int pathColor, wallColor color.Color
// pathColor, wallColor color.Color gradient colorgrad.Gradient
// gradient colorgrad.Gradient }{
// }{ {
// { "Trivial",
// "Trivial", "trivial.png",
// "../../out/trivial_sol.png", trivial(),
// trivial(), 20, 20,
// 40, 40, color.White, color.Black,
// color.White, color.Black, colorgrad.Warm(),
// colorgrad.Warm(), },
// }, {
// { "Trivial Bigger",
// "Bigger", "trivial-bigger.png",
// "../../out/bigger_sol.png", bigger(),
// bigger(), 20, 20,
// 40, 40, color.White, color.Black,
// color.White, color.Black, colorgrad.Warm(),
// colorgrad.Warm(), },
// }, {
// { "Trivial Bigger Staggered",
// "Bigger Staggered", "trivial-bigger-staggered.png",
// "../../out/bigger_staggered_sol.png", bigger_staggered(),
// bigger_staggered(), 20, 20,
// 40, 40, color.White, color.Black,
// color.White, color.Black, colorgrad.Warm(),
// pathGradient, },
// }, {
// { "Normal",
// "Normal", "normal.png",
// "../../out/normal_sol.png", normal(),
// normal(), 20, 20,
// 40, 40, color.White, color.Black,
// color.White, color.Black, colorgrad.Warm(),
// colorgrad.Warm(), },
// }, }
// }
// for _, test := range tests {
// for _, test := range tests { writer := ImageWriter{
// writer := ImageWriter{ Filename: OUT_DIR + "/" + test.filename,
// Filename: test.filename, Maze: test.m,
// Maze: test.m, CellWidth: test.CellWidth,
// CellWidth: test.CellWidth, CellHeight: test.cellHeight,
// CellHeight: test.cellHeight, WallColor: test.wallColor,
// WallColor: test.wallColor, PathColor: test.pathColor,
// PathColor: test.pathColor, SolutionGradient: test.gradient,
// SolutionGradient: test.gradient, }
// }
// err := writer.Write()
// err := writer.Write() if err != nil {
// if err != nil { t.Fatalf("%s: couldn't write solution, got following error\n%v", test.name, err)
// t.Fatalf("%s: couldn't write solution, got following error\n%v", test.name, err) }
// }
// } assertEqualFile(t, EXPECTED_DIR+"/"+test.filename, OUT_DIR+"/"+test.filename, test.name)
os.Remove(writer.Filename)
}
os.Remove(OUT_DIR)
}
const chunkSize = 64000
func assertEqualFile(t *testing.T, file1, file2, name string) {
f1, err := os.Open(file1)
if err != nil {
t.Fatal(err)
}
defer f1.Close()
f2, err := os.Open(file2)
if err != nil {
t.Fatal(err)
}
defer f2.Close()
for {
b1 := make([]byte, chunkSize)
_, err1 := f1.Read(b1)
b2 := make([]byte, chunkSize)
_, err2 := f2.Read(b2)
if err1 != nil || err2 != nil {
if err1 == io.EOF && err2 == io.EOF {
return
} else if err1 == io.EOF || err2 == io.EOF {
t.Fatalf("%s: files are not equal. Got %q, wanted %q", name, file1, file2)
}
}
if !bytes.Equal(b1, b2) {
t.Fatalf("%s: files are not equal. Got %q, wanted %q", name, file1, file2)
}
}
} }

View File

@ -1,7 +1,6 @@
package writer package writer
import ( import (
"fmt"
"maze-solver/maze" "maze-solver/maze"
"maze-solver/utils" "maze-solver/utils"
"testing" "testing"
@ -69,7 +68,6 @@ func TestStringsWriter(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
fmt.Printf("----------- %s -----------\n", test.name)
writer := StringsWriter{ writer := StringsWriter{
PathChar: test.pathChar, PathChar: test.pathChar,
WallChar: test.wallChar, WallChar: test.wallChar,

View File

@ -1,7 +1,6 @@
package solver package solver
import ( import (
"log"
"maze-solver/maze" "maze-solver/maze"
"maze-solver/utils" "maze-solver/utils"
) )
@ -13,9 +12,6 @@ type DFSSolver struct {
func (s *DFSSolver) Solve(m *maze.Maze) *maze.SolvedMaze { func (s *DFSSolver) Solve(m *maze.Maze) *maze.SolvedMaze {
defer utils.Timer("Turn left algorithm", 2)() defer utils.Timer("Turn left algorithm", 2)()
log.Println("Starting dfs")
log.Printf("m.Nodes: %v\n", len(m.Nodes))
current, end := m.Nodes[0], m.Nodes[len(m.Nodes)-1] current, end := m.Nodes[0], m.Nodes[len(m.Nodes)-1]
s.visited = make(map[*maze.Node]bool, len(m.Nodes)) s.visited = make(map[*maze.Node]bool, len(m.Nodes))