From cf2fc3b0151863d520a1437da4396b88fc7091e0 Mon Sep 17 00:00:00 2001 From: George Xie Date: Sat, 14 Apr 2018 16:17:03 -0700 Subject: [PATCH 1/3] write the tests --- sudoku_test.go | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 sudoku_test.go diff --git a/sudoku_test.go b/sudoku_test.go new file mode 100644 index 0000000..3871503 --- /dev/null +++ b/sudoku_test.go @@ -0,0 +1,97 @@ +package main + +import ( + "bufio" + "errors" + "fmt" + "os" + "regexp" + "testing" +) + +type Used [9]bool + +func (u *Used) Add(n int) error { + n-- //change from 1 to 9 in sudoku to 0 to 8 for indexing + if u[n] { + return errors.New("number is used") + } + u[n] = true + return nil +} + +func TestSudoku(t *testing.T) { + f, err := os.Open("README.md") + if err != nil { + t.Fatal(err) + } + defer f.Close() + r := bufio.NewReader(f) + skipLines(r, 10) + var rs, cs, bs [9]Used + for i := 0; i < 9; i++ { + for j := 0; j < 9; j++ { + t.Run(fmt.Sprintf("line %v%v", i+1, j+1), func(t *testing.T) { + n, err := nextNumber(r) + if err != nil { + t.Fatal(err) + } + if n == 0 { + return + } + err = rs[i].Add(n) + if err != nil { + t.Fatal(err) + } + err = cs[j].Add(n) + if err != nil { + t.Fatal(err) + } + k := i/3 + (j/3)*3 + err = bs[k].Add(n) + if err != nil { + t.Fatal(err) + } + }) + } + skipLines(r, 1) + } +} + +var numberRegexp = regexp.MustCompile(`[1-9]`) +var spaceRegexp = regexp.MustCompile(` `) + +func nextNumber(r *bufio.Reader) (int, error) { + line, pre, err := r.ReadLine() + if err != nil { + return 0, err + } + if pre { + return 0, errors.New("unhandled pre") + } + numbers := numberRegexp.FindAll(line, -1) + if len(numbers) > 1 { + return 0, errors.New("too many numbers") + } + if len(numbers) == 1 { + return int(numbers[0][0] - '0'), nil + } + space := spaceRegexp.Find(line) + if space == nil { + return 0, errors.New("expected space filler for empty cells") + } + return 0, nil +} + +func skipLines(r *bufio.Reader, n int) error { + for i := 0; i < n; i++ { + _, pre, err := r.ReadLine() + if err != nil { + return err + } + if pre { + i-- + } + } + return nil +} From c2a445258c0505901ad6ec42f3da32ad80871449 Mon Sep 17 00:00:00 2001 From: George Xie Date: Sat, 14 Apr 2018 16:24:02 -0700 Subject: [PATCH 2/3] clarify error message --- sudoku_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sudoku_test.go b/sudoku_test.go index 3871503..bb5e0a9 100644 --- a/sudoku_test.go +++ b/sudoku_test.go @@ -71,7 +71,7 @@ func nextNumber(r *bufio.Reader) (int, error) { } numbers := numberRegexp.FindAll(line, -1) if len(numbers) > 1 { - return 0, errors.New("too many numbers") + return 0, errors.New("too many digits") } if len(numbers) == 1 { return int(numbers[0][0] - '0'), nil From bafa32076db8339ac70172b95cbbeb4d5a43e46a Mon Sep 17 00:00:00 2001 From: George Xie Date: Sat, 14 Apr 2018 16:30:21 -0700 Subject: [PATCH 3/3] add travis ci --- .travis.yml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..9046ef8 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,6 @@ +language: go + +go: + - "1.x" + + \ No newline at end of file