-
Notifications
You must be signed in to change notification settings - Fork 164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lab1 - Generate Fibonacci Series #625
Merged
Merged
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ff14d30
Add lab5 - stringer interface
harirakr 5c41970
Lab9 (#627)
nicko-lee b0ad9ec
Add Lab 8 - Project Layout (#621)
6c2ac60
Implementation of Fibonacci Series for lab1
ddb2210
Added handling of negative numbers
08cd89f
Unit tests to verify all operations
0f72e56
Added required README for documentation
5e0a7d3
Fixed issues resulting from the last PR review
95ed1ea
Fixed test file as a result of the last PR review
acc8c32
Fixed formatting as a result of the PR review
152df59
Changed sign reversal
df7df91
Merge branch 'master' into lab1
jimbosoft bd78645
Removed unwanted blank lines
dbf2201
Merge branch 'master' into lab1
jimbosoft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Fibonacci Number Generator | ||
## Overview | ||
As part of the ANZ "go" course the first lab is to produce a piece of software | ||
that will produce the Fibonacci series for fib(7). | ||
It was also an exercise in the tour.golang.org. | ||
|
||
Generating the series was implemented as "closure" which was a requirement in | ||
the tour-golang . There are other ways to solve this problem, | ||
but I kept it as an example of closure for later reference. | ||
|
||
The function maintains state and hence has to be called sequentially. |
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,55 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
) | ||
|
||
var out io.Writer = os.Stdout | ||
|
||
// fibonacci returns a function that returns | ||
// an number in the fibonacci sequence. | ||
// This is an implmentation of "closure". | ||
// The inside function returns the current number | ||
// in the fibonnacci sequence, starting at 1 | ||
// and sets up the next which will be returned | ||
// in the subsequent call | ||
func fibonacci() func() int { | ||
secondLast, last := 0, 1 | ||
|
||
return func() int { | ||
result := last | ||
secondLast, last = last, secondLast+last | ||
return result | ||
} | ||
} | ||
|
||
func fibSeries(n int) []int { | ||
counter := n | ||
if n < 0 { | ||
counter = -n | ||
} | ||
var fibSerial []int | ||
|
||
f := fibonacci() | ||
for i := 0; i < counter; i++ { | ||
factor := 1 | ||
if n < 0 && i%2 != 0 { | ||
factor = -1 | ||
} | ||
fibSerial = append(fibSerial, f()*factor) | ||
} | ||
return fibSerial | ||
} | ||
|
||
func fib(n int) { | ||
fibSer := fibSeries(n) | ||
for _, v := range fibSer { | ||
fmt.Fprintln(out, v) | ||
} | ||
} | ||
|
||
func main() { | ||
fib(7) | ||
} |
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,70 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
) | ||
|
||
func TestFib(t *testing.T) { | ||
|
||
fibResults := []int{1, 1, 2, 3, 5, 8, 13, 21, 34} | ||
|
||
f := fibonacci() | ||
|
||
for _, val := range fibResults { | ||
if r := f(); r != val { | ||
t.Errorf("returned value %v does not match %v", r, val) | ||
} | ||
} | ||
} | ||
|
||
func TestMain(t *testing.T) { | ||
|
||
want := "1\n1\n2\n3\n5\n8\n13\n" | ||
var buf bytes.Buffer | ||
out = &buf | ||
main() | ||
result := buf.String() | ||
|
||
if result != want { | ||
t.Errorf("expected %v, got %v", want, result) | ||
} | ||
} | ||
|
||
func TestNeg(t *testing.T) { | ||
|
||
want := "1\n-1\n2\n-3\n5\n-8\n13\n" | ||
var buf bytes.Buffer | ||
out = &buf | ||
fib(-7) | ||
result := buf.String() | ||
|
||
if result != want { | ||
t.Errorf("expected %v, got %v", want, result) | ||
} | ||
} | ||
|
||
func TestZero(t *testing.T) { | ||
|
||
var buf bytes.Buffer | ||
out = &buf | ||
fib(0) | ||
result := buf.String() | ||
|
||
if len(result) > 0 { | ||
t.Errorf("expected nothing to be printed, got %v", result) | ||
} | ||
} | ||
|
||
func TestOne(t *testing.T) { | ||
|
||
want := "1\n" | ||
var buf bytes.Buffer | ||
out = &buf | ||
fib(1) | ||
result := buf.String() | ||
|
||
if result != want { | ||
t.Errorf("expected %v, got %v", want, result) | ||
} | ||
} |
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,19 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
) | ||
|
||
var out io.Writer = os.Stdout | ||
|
||
type IPAddr [4]byte | ||
|
||
func (ip IPAddr) String() string { | ||
return fmt.Sprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]) | ||
} | ||
|
||
func main() { | ||
fmt.Fprintln(out, IPAddr{127, 0, 0, 1}) | ||
} |
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,38 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMain(t *testing.T) { | ||
var buf bytes.Buffer | ||
out = &buf | ||
main() | ||
assert.Equal(t, "127.0.0.1\n", buf.String()) | ||
} | ||
|
||
func TestStringer(t *testing.T) { | ||
tcs := map[string]struct { | ||
want string | ||
ip IPAddr | ||
}{ | ||
"null": {want: "0.0.0.0", ip: IPAddr{}}, | ||
"zero": {want: "0.0.0.0", ip: IPAddr{0}}, | ||
"classA": {want: "10.0.0.0", ip: IPAddr{10}}, | ||
"classB": {want: "192.168.0.0", ip: IPAddr{192, 168}}, | ||
"classC": {want: "172.16.100.0", ip: IPAddr{172, 16, 100}}, | ||
"classD": {want: "8.8.8.8", ip: IPAddr{8, 8, 8, 8}}, | ||
"mask": {want: "255.255.255.255", ip: IPAddr{255, 255, 255, 255}}, | ||
} | ||
|
||
for name, tc := range tcs { | ||
output := tc.ip.String() | ||
want := tc.want | ||
t.Run(name, func(t *testing.T) { | ||
assert.Equal(t, want, output) | ||
}) | ||
} | ||
} |
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,52 @@ | ||
# Puppy Store - An in-memory store for puppies | ||
|
||
<!-- MarkdownTOC --> | ||
|
||
- [Overview](#overview) | ||
- [Prerequisites](#prerequisites) | ||
- [Build, execute, test, lint](#betl) | ||
- [Build](#build) | ||
- [Execute](#execute) | ||
- [Test](#test) | ||
- [Format and Lint](#lint) | ||
- [Review unit test coverage](#coverage) | ||
|
||
<!-- /MarkdownTOC --> | ||
|
||
## Overview | ||
|
||
Puppy Store is a simple in-memory store for [Puppy](pkg/puppy/types.go) objects. Puppy Store | ||
is implemented with CRUD methods for creating, reading, updating, and deleting puppies in puppy store. | ||
|
||
|
||
## Prerequisites | ||
|
||
- Install `go 1.12` according to [official installation instruction](https://golang.org/doc/install) | ||
- Clone this project outside your `$GOPATH` to enable [Go Modules](https://github.com/golang/go/wiki/Modules) | ||
- Install `golangci-lint` according to [instructions](https://github.com/golangci/golangci-lint#local-installation) | ||
|
||
## Build, execute, test, lint <a name="betl"></a> | ||
|
||
#### Build | ||
|
||
go build -o puppystore cmd/puppy-server/main.go | ||
|
||
#### Execute | ||
|
||
./puppystore | ||
|
||
|
||
#### Test | ||
|
||
go test ./... | ||
|
||
|
||
#### Format and lint <a name="lint"></a> | ||
|
||
gofmt -w . | ||
goimports -w . | ||
golangci-lint run | ||
|
||
#### Review unit test coverage <a name="coverage"></a> | ||
|
||
go test -coverprofile=coverage.out ./... && go tool cover -html=coverage.out |
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,19 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/anz-bank/go-course/08_project/kasunfdo/pkg/puppy" | ||
"github.com/anz-bank/go-course/08_project/kasunfdo/pkg/puppy/store" | ||
) | ||
|
||
var out io.Writer = os.Stdout | ||
|
||
func main() { | ||
store := store.NewMapStore() | ||
p := puppy.Puppy{Breed: "Husky", Colour: "White", Value: 4999.98} | ||
id, _ := store.CreatePuppy(p) | ||
fmt.Fprintf(out, "Puppy(%d) added to store\n", id) | ||
} |
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,19 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMainOutput(t *testing.T) { | ||
var buf bytes.Buffer | ||
out = &buf | ||
|
||
main() | ||
|
||
expected := "Puppy(1) added to store\n" | ||
actual := buf.String() | ||
assert.Equal(t, expected, actual) | ||
} |
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,37 @@ | ||
package puppy | ||
|
||
import "fmt" | ||
|
||
type ErrCode uint32 | ||
|
||
type Error struct { | ||
Message string | ||
Code ErrCode | ||
} | ||
|
||
const ( | ||
ErrInvalid ErrCode = 400 | ||
ErrNotFound ErrCode = 404 | ||
) | ||
|
||
func (e ErrCode) String() string { | ||
switch e { | ||
case ErrInvalid: | ||
return "invalid input: %v" | ||
case ErrNotFound: | ||
return "not found: %v" | ||
default: | ||
return "error occurred" | ||
} | ||
} | ||
|
||
func (e *Error) Error() string { | ||
return e.Message | ||
} | ||
|
||
func NewError(code ErrCode, args ...interface{}) *Error { | ||
return &Error{ | ||
Message: fmt.Sprintf(code.String(), args...), | ||
Code: code, | ||
} | ||
} |
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,20 @@ | ||
package puppy | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestErrCodeString(t *testing.T) { | ||
assert.Equal(t, "invalid input: %v", ErrInvalid.String()) | ||
assert.Equal(t, "not found: %v", ErrNotFound.String()) | ||
|
||
var ErrFoo ErrCode = 900 | ||
assert.Equal(t, "error occurred", ErrFoo.String()) | ||
} | ||
|
||
func TestError(t *testing.T) { | ||
err := NewError(ErrNotFound, "error message") | ||
assert.Equal(t, "not found: error message", err.Error()) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: stray blank line. unnecessarily spaces out the code, which matters on real code that does not fit on one screen. blank lines to separate "paragraphs" of code are good, but do think about what your "code paragraphs" are.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done