-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6818279
commit 7b06b8d
Showing
12 changed files
with
129 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package solve_test | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
gs "github.com/deanveloper/gridspech-go" | ||
"github.com/deanveloper/gridspech-go/solve" | ||
) | ||
|
||
func testSolveAbstract(t *testing.T, level string, expectedSolutionsStrings []string, maxColors int, f func(solve.GridSolver) <-chan gs.TileSet) { | ||
t.Helper() | ||
|
||
solver := solve.NewGridSolver(gs.MakeGridFromString(level, maxColors)) | ||
|
||
var expectedSolutions []gs.TileSet | ||
var actualSolutions []gs.TileSet | ||
|
||
for _, solutionString := range expectedSolutionsStrings { | ||
expectedSolutions = append(expectedSolutions, tileSetFromString(solver.Grid, solutionString)) | ||
} | ||
|
||
solutionsCh := f(solver) | ||
for solution := range solutionsCh { | ||
actualSolutions = append(actualSolutions, solution) | ||
} | ||
|
||
testUnorderedTilesetSliceEq(t, expectedSolutions, actualSolutions) | ||
} | ||
|
||
func tileSetFromString(grid gs.Grid, str string) gs.TileSet { | ||
lines := strings.Split(strings.Trim(str, "{}"), "|") | ||
var ts gs.TileSet | ||
for i, line := range lines { | ||
y := len(lines) - i - 1 | ||
x := -1 | ||
for { | ||
index := strings.IndexAny(line[x+1:], "012345") | ||
if index < 0 { | ||
break | ||
} | ||
x = x + 1 + index | ||
tileWithColor := *grid.TileAt(x, y) | ||
tileWithColor.Data.Color = gs.TileColor(line[x] - '0') | ||
ts.Add(tileWithColor) | ||
} | ||
} | ||
return ts | ||
} | ||
|
||
func commaSeparatedSlice(slice []gs.TileSet) string { | ||
var asStr []string | ||
for _, v := range slice { | ||
asStr = append(asStr, v.String()) | ||
} | ||
return strings.Join(asStr, ",") | ||
} | ||
|
||
func testUnorderedTilesetSliceEq(t *testing.T, expected, actual []gs.TileSet) { | ||
t.Helper() | ||
|
||
for i1 := range expected { | ||
var found bool | ||
for i2 := range actual { | ||
if expected[i1].Eq(actual[i2]) { | ||
found = true | ||
break | ||
} | ||
} | ||
if !found { | ||
t.Errorf("expected to find tileset %v", expected[i1]) | ||
} | ||
} | ||
|
||
for i1 := range actual { | ||
var found bool | ||
for i2 := range expected { | ||
if actual[i1].Eq(expected[i2]) { | ||
found = true | ||
break | ||
} | ||
} | ||
if !found { | ||
t.Errorf("incorrect solution %v", actual[i1]) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package solve_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/deanveloper/gridspech-go/solve" | ||
) | ||
|
||
func testSolveJoinsAbstract(t *testing.T, level string, expectedSolutionsStrings []string, maxColors int) { | ||
t.Helper() | ||
|
||
testSolveAbstract(t, level, expectedSolutionsStrings, maxColors, solve.GridSolver.SolveJoins) | ||
} | ||
|
||
func TestSolveJoins_basic(t *testing.T) { | ||
const level = ` | ||
0j1 0 0 0j1 | ||
` | ||
solutions := []string{ | ||
"0000", | ||
"1111", | ||
"2222", | ||
} | ||
|
||
testSolveJoinsAbstract(t, level, solutions, 3) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.