From 8cb50a9fbb3d1815ff6fbbfbd8d4431b76db8a6d Mon Sep 17 00:00:00 2001 From: mrsombre <376535+mrsombre@users.noreply.github.com> Date: Tue, 16 Jan 2024 14:09:11 +0100 Subject: [PATCH] Game input read and data processing improvements --- codecov.yml => .github/codecov.yml | 5 ++++ LICENSE | 2 +- command.go | 2 +- command_test.go | 8 +++--- data.go | 46 ++++++------------------------ game.go | 16 +++++++++++ geometry_line_test.go | 16 +++++++++-- input.go | 20 +++---------- input_test.go | 11 +++++++ main.go | 8 +++++- math_test.go | 6 +++- reader.go | 4 +-- types_test.go | 2 ++ 13 files changed, 80 insertions(+), 66 deletions(-) rename codecov.yml => .github/codecov.yml (62%) create mode 100644 game.go diff --git a/codecov.yml b/.github/codecov.yml similarity index 62% rename from codecov.yml rename to .github/codecov.yml index 3f356b0..f09a4e7 100644 --- a/codecov.yml +++ b/.github/codecov.yml @@ -1,3 +1,8 @@ +coverage: + range: "70...100" comment: layout: "condensed_header, condensed_files, condensed_footer" hide_project_coverage: true +ignore: + - main.go + - debug.go diff --git a/LICENSE b/LICENSE index fd24bd7..1240e0b 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2024 Dmitrii Barsukov +Copyright (c) 2024-present Dmitrii Barsukov Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/command.go b/command.go index 542a24d..39f0041 100644 --- a/command.go +++ b/command.go @@ -27,6 +27,6 @@ func (c MockCommand) String() string { func ExecuteCommands(commands Commands) { for _, command := range commands { - fmt.Fprintln(commandOutput, command) + _, _ = fmt.Fprintln(commandOutput, command) } } diff --git a/command_test.go b/command_test.go index 416ecbc..4ed2036 100644 --- a/command_test.go +++ b/command_test.go @@ -6,11 +6,11 @@ import ( "github.com/stretchr/testify/assert" ) -type mockWriter struct { +type commandMockWriter struct { data []byte } -func (w *mockWriter) Write(p []byte) (n int, err error) { +func (w *commandMockWriter) Write(p []byte) (n int, err error) { w.data = p return len(p), nil } @@ -21,10 +21,10 @@ func TestMockCommand_String(t *testing.T) { } func TestExecuteCommand(t *testing.T) { - commandOutput = &mockWriter{} + commandOutput = &commandMockWriter{} ExecuteCommands(Commands{MockCommand{1, 2}}) want := "1 2\n" - got := commandOutput.(*mockWriter).data + got := commandOutput.(*commandMockWriter).data assert.Equal(t, want, string(got)) } diff --git a/data.go b/data.go index 4f5f707..149e197 100644 --- a/data.go +++ b/data.go @@ -14,21 +14,11 @@ import ( // DataExport serializes and compresses a slice of strings, // returning a base64 encoded string. func DataExport(data []string) string { - var err error - - jsonData, err := json.Marshal(data) - if err != nil { - panic(err) - } - + jsonData, _ := json.Marshal(data) var gzBuf bytes.Buffer gz := gzip.NewWriter(&gzBuf) - if _, err = gz.Write(jsonData); err != nil { - panic(err) - } - if err = gz.Close(); err != nil { - panic(err) - } + _, _ = gz.Write(jsonData) + _ = gz.Close() return base64.StdEncoding.EncodeToString(gzBuf.Bytes()) } @@ -36,33 +26,13 @@ func DataExport(data []string) string { // DataImport decodes a base64 string, decompresses it, // and deserializes the JSON data into a slice of strings. func DataImport(encodedData string) []string { - var err error - - gzData, err := base64.StdEncoding.DecodeString(encodedData) - if err != nil { - panic(err) - } - - gz, err := gzip.NewReader(bytes.NewBuffer(gzData)) - if err != nil { - panic(err) - } - defer func(gz *gzip.Reader) { - err = gz.Close() - if err != nil { - panic(err) - } - }(gz) - + gzData, _ := base64.StdEncoding.DecodeString(encodedData) + gz, _ := gzip.NewReader(bytes.NewBuffer(gzData)) + _ = gz.Close() var jsonData bytes.Buffer - if _, err = jsonData.ReadFrom(gz); err != nil { - panic(err) - } - + _, _ = jsonData.ReadFrom(gz) var data []string - if err = json.Unmarshal(jsonData.Bytes(), &data); err != nil { - panic(err) - } + _ = json.Unmarshal(jsonData.Bytes(), &data) return data } diff --git a/game.go b/game.go new file mode 100644 index 0000000..a777e99 --- /dev/null +++ b/game.go @@ -0,0 +1,16 @@ +package main + +// Example of game business logic. + +type Unit struct { + x, y, z float64 +} + +type Turn struct { + Power float64 + L, R string +} + +type Game struct { + Units []Unit +} diff --git a/geometry_line_test.go b/geometry_line_test.go index d27a4d6..c89b44d 100644 --- a/geometry_line_test.go +++ b/geometry_line_test.go @@ -290,7 +290,13 @@ func TestLine_ClosestPointToLine(t *testing.T) { want: Point{0, 400}, }, { - name: `diagonal`, + name: `diagonal from`, + line: Line{Point{150, 150}, Point{250, 300}}, + point: Point{50, 50}, + want: Point{73, 35}, + }, + { + name: `diagonal to`, line: Line{Point{0, 0}, Point{100, 50}}, point: Point{400, 0}, want: Point{320, 160}, @@ -324,7 +330,13 @@ func TestLine_ClosestPointToSegment(t *testing.T) { want: Point{0, 300}, }, { - name: `diagonal`, + name: `diagonal from`, + line: Line{Point{150, 150}, Point{250, 300}}, + point: Point{50, 50}, + want: Point{150, 150}, + }, + { + name: `diagonal to`, line: Line{Point{0, 0}, Point{100, 50}}, point: Point{400, 0}, want: Point{100, 50}, diff --git a/input.go b/input.go index d578bee..8bbb9ca 100644 --- a/input.go +++ b/input.go @@ -1,22 +1,11 @@ package main +// Example of how to read input data into game business objects. + import ( "fmt" ) -type Unit struct { - x, y, z float64 -} - -type Turn struct { - Power float64 - L, R string -} - -type Game struct { - Units []Unit -} - func InputGame(data []string) Game { var err error var game Game @@ -25,17 +14,16 @@ func InputGame(data []string) Game { size = StrToInt(data[0]) data = data[1:] var unit Unit - units := make([]Unit, 0, size) + game.Units = make([]Unit, 0, size) for i := 0; i < size; i++ { _, err = fmt.Sscan(data[i], &unit.x, &unit.y, &unit.z) if err != nil { panic(err) } - units = append(units, unit) + game.Units = append(game.Units, unit) } // some additional logic - game.Units = units return game } diff --git a/input_test.go b/input_test.go index 3ac65df..8c08e8b 100644 --- a/input_test.go +++ b/input_test.go @@ -16,6 +16,12 @@ func TestInputGame(t *testing.T) { }, } assert.Equal(t, want, game) + + data := []string{ + "1", + "1 2 a", + } + assert.Panics(t, func() { InputGame(data) }) } func TestInputStep(t *testing.T) { @@ -26,4 +32,9 @@ func TestInputStep(t *testing.T) { R: "L", } assert.Equal(t, want, turn) + + data := []string{ + "a a a", + } + assert.Panics(t, func() { InputStep(data) }) } diff --git a/main.go b/main.go index b4c1204..71afdf3 100644 --- a/main.go +++ b/main.go @@ -1,5 +1,7 @@ package main +// Example main: Read game state, construct business objects, apply logic, and execute commands. + import ( "bufio" "math/rand" @@ -17,7 +19,6 @@ func init() { } func main() { - // example scanner := bufio.NewScanner(os.Stdin) scanner.Buffer(make([]byte, 1000000), 1000000) @@ -39,5 +40,10 @@ func main() { // some game logic for the next step u(game, step) + + commands := Commands{ + MockCommand{1, 2}, + } + ExecuteCommands(commands) } } diff --git a/math_test.go b/math_test.go index 538700e..e4dc3da 100644 --- a/math_test.go +++ b/math_test.go @@ -10,8 +10,12 @@ func TestMedian(t *testing.T) { values := []float64{ 0, 2, 4, 6, 8, 10, 1000, } - assert.EqualValues(t, 6, Median(values)) + + values = []float64{ + 0, 2, 4, 6, 8, 10, + } + assert.EqualValues(t, 5, Median(values)) } func TestExpectedExpDiff(t *testing.T) { diff --git a/reader.go b/reader.go index 91cb89a..96bb223 100644 --- a/reader.go +++ b/reader.go @@ -1,11 +1,11 @@ package main +// Example of reading game state from the standard input stream. + import ( "bufio" ) -// Reading the game state from the standard input stream. - // ReadGame reads the game state from the standard input stream. func ReadGame(s *bufio.Scanner) []string { data := make([]string, 0, 32) diff --git a/types_test.go b/types_test.go index 6bef6bc..08143ed 100644 --- a/types_test.go +++ b/types_test.go @@ -24,6 +24,8 @@ func TestStrToInt(t *testing.T) { assert.Equal(t, -1, StrToInt(s)) s = "0" assert.Equal(t, 0, StrToInt(s)) + s = "a" + assert.Panics(t, func() { StrToInt(s) }) } func TestBoolToInt(t *testing.T) {