Skip to content

Commit

Permalink
Merge branch 'master' into lab7
Browse files Browse the repository at this point in the history
  • Loading branch information
jimbosoft authored Feb 2, 2020
2 parents 79bd3fa + b79a437 commit d94cb17
Show file tree
Hide file tree
Showing 46 changed files with 1,685 additions and 1,172 deletions.
27 changes: 27 additions & 0 deletions 02_bubble/alextmz/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"fmt"
"io"
"os"
)

var out io.Writer = os.Stdout

func main() {
fmt.Fprintln(out, bubble([]int{3, 2, 1, 5}))
}

func bubble(s []int) []int {
r := make([]int, len(s))
copy(r, s)

for i := len(r); i > 0; i-- {
for j := 1; j < i; j++ {
if r[j-1] > r[j] {
r[j-1], r[j] = r[j], r[j-1]
}
}
}
return r
}
66 changes: 66 additions & 0 deletions 02_bubble/alextmz/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
"bytes"
"reflect"
"testing"
)

func Test_main(t *testing.T) {
want := "[1 2 3 5]\n"

t.Run("Test Main", func(t *testing.T) {
var buf bytes.Buffer
out = &buf
main()
result := buf.String()

if result != want {
t.Errorf("expected %#v, got %#v", want, result)
}
})
}

func Test_bubble(t *testing.T) {

tests := []struct {
name string
args []int
want []int
}{
{"Bubblesort for {-3, -2, -1, -5}", []int{-3, -2, -1, -5}, []int{-5, -3, -2, -1}},
{"Bubblesort for {-1}", []int{-1}, []int{-1}},
{"Bubblesort for no parameters", []int{}, []int{}},
{"Bubblesort for {0}", []int{0}, []int{0}},
{"Bubblesort for {1}", []int{1}, []int{1}},
{"Bubblesort for {0,1}", []int{0, 1}, []int{0, 1}},
{"Bubblesort for {1,0}", []int{1, 0}, []int{0, 1}},
{"Bubblesort for {3, 2, 1, 5}", []int{3, 2, 1, 5}, []int{1, 2, 3, 5}},
{"Bubblesort for {1,1,2,2,9,4,1,0,1}", []int{1, 1, 2, 2, 9, 4, 1, 0, 1}, []int{0, 1, 1, 1, 1, 2, 2, 4, 9}},
}

for _, tr := range tests {
tr2 := tr

t.Run(tr2.name, func(t *testing.T) {

origarg := make([]int, len(tr2.args))
copy(origarg, tr2.args)

result := bubble(tr2.args)

if !reflect.DeepEqual(origarg, tr2.args) {
t.Errorf("original slice was modified, expected %#v, got %#v", tr2.args, origarg)
}
if len(result) != len(tr2.want) {
t.Errorf("wrong slice size, expected len=%d for %#v, got %d for %#v", len(tr2.want), tr2.want, len(result), result)
}
for k, v := range result {
if v != tr2.want[k] {
t.Errorf("wrong slice content, expected %#v, got %#v", tr2.want, result)
break
}
}
})
}
}
34 changes: 34 additions & 0 deletions 04_numeronym/alextmz/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"fmt"
"io"
"os"
"strconv"
"unicode/utf8"
)

var out io.Writer = os.Stdout

func main() {
fmt.Fprintln(out, numeronyms("accessibility", "Kubernetes", "abc"))
}

func numeronyms(vals ...string) []string {
r := make([]string, len(vals))
for k, v := range vals {
r[k] = numeronym(v)
}
return r
}

func numeronym(s string) string {
numrunes := utf8.RuneCountInString(s)
if numrunes < 4 {
return s
}
firstrune, _ := utf8.DecodeRuneInString(s)
lastrune, _ := utf8.DecodeLastRuneInString(s)

return string(firstrune) + strconv.Itoa(numrunes-2) + string(lastrune)
}
67 changes: 67 additions & 0 deletions 04_numeronym/alextmz/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package main

import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
)

func TestNumeronym(t *testing.T) {
var tests = map[string]struct {
arg string
want string
}{
"empty string": {"", ""},
"single string of 1 element": {"a", "a"},
"single long string": {"abracadabra", "a9a"},
"unicode at the start": {"🤪bracadabra", "🤪9a"},
"unicode in the middle": {"abra🤪🤪🤪abra", "a9a"},
"unicode in the end": {"abracadabr🤪", "a9🤪"},
"1 poo": {"💩", "💩"},
"2 poos": {"💩💩", "💩💩"},
"3 poos": {"💩💩💩", "💩💩💩"},
"4 poos": {"💩💩💩💩", "💩2💩"},
}

for name, tt := range tests {
test := tt
t.Run(name, func(t *testing.T) {
got := numeronym(test.arg)
assert.Equal(t, test.want, got)
})
}
}

func TestNumeronyms(t *testing.T) {
var tests = map[string]struct {
arg []string
want []string
}{
"empty string": {
[]string{""},
[]string{""}},
"mixed strings": {
[]string{"", "alakazam", "hocuspocus", "mumbojumbo", "💩💩💩💩"},
[]string{"", "a6m", "h8s", "m8o", "💩2💩"}},
}

for name, tt := range tests {
test := tt
t.Run(name, func(t *testing.T) {
got := numeronyms(test.arg...)
assert.Equal(t, test.want, got)
})
}
}

func TestMain(t *testing.T) {
want := "[a11y K8s abc]\n"
t.Run("main test", func(t *testing.T) {
var buf bytes.Buffer
out = &buf
main()
got := buf.String()
assert.Equal(t, got, want)
})
}
19 changes: 19 additions & 0 deletions 05_stringer/alextmz/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"fmt"
"io"
"os"
)

type ipAddr [4]byte

var out io.Writer = os.Stdout

func main() {
fmt.Fprintln(out, ipAddr{127, 0, 0, 1})
}

func (i ipAddr) String() string {
return fmt.Sprintf("%d.%d.%d.%d", i[0], i[1], i[2], i[3])
}
53 changes: 53 additions & 0 deletions 05_stringer/alextmz/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
)

func TestIPAddrString(t *testing.T) {
var tests = map[string]struct {
arg ipAddr
want string
}{
"empty IPv4": {
ipAddr{},
"0.0.0.0",
},
"8bits IPv4": {
ipAddr{254},
"254.0.0.0",
},
"16bits IPv4": {
ipAddr{254, 254},
"254.254.0.0",
},
"24bits IPv4": {
ipAddr{254, 254, 254},
"254.254.254.0",
},
"full IPv4": {
ipAddr{254, 254, 254, 254},
"254.254.254.254",
},
}

for name, test := range tests {
test := test
t.Run(name, func(t *testing.T) {
actual := test.arg.String()
assert.Equal(t, test.want, actual)
})
}
}

func TestMain(t *testing.T) {
want := "127.0.0.1\n"
var buf bytes.Buffer
out = &buf
main()
got := buf.String()
assert.Equal(t, want, got)
}
17 changes: 17 additions & 0 deletions 06_puppy/alextmz/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"fmt"
"io"
"os"
)

var out io.Writer = os.Stdout

func main() {
ps := NewMapStore()
p1 := &Puppy{Breed: "Dogo", Colour: "white", Value: 50.0}
_ = ps.CreatePuppy(p1)
p2, _ := ps.ReadPuppy(p1.ID)
fmt.Fprintf(out, "Read Puppy by ID: %v\n", p2)
}
17 changes: 17 additions & 0 deletions 06_puppy/alextmz/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
)

func TestMain(t *testing.T) {
want := "Read Puppy by ID: {1 Dogo white 50}\n"
var buf bytes.Buffer
out = &buf
main()
got := buf.String()
assert.Equal(t, want, got)
}
52 changes: 52 additions & 0 deletions 06_puppy/alextmz/mapstore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
"fmt"
)

type MapStore struct {
pmap map[int]Puppy
nextID int
}

func NewMapStore() MapStore {
a := MapStore{pmap: make(map[int]Puppy)}
return a
}

func (m MapStore) CreatePuppy(p *Puppy) error {
if p == nil {
return fmt.Errorf("puppy pointer is nil")
}
if p.ID != 0 {
return fmt.Errorf("trying to create a puppy already initialized with ID %d", p.ID)
}
m.nextID++
p.ID = m.nextID
m.pmap[p.ID] = *p
return nil
}

func (m MapStore) ReadPuppy(id int) (Puppy, error) {
v, ok := m.pmap[id]
if !ok {
return Puppy{}, fmt.Errorf("puppy ID %d being read does not exist", id)
}
return v, nil
}

func (m MapStore) UpdatePuppy(p Puppy) error {
if _, ok := m.pmap[p.ID]; !ok {
return fmt.Errorf("puppy ID %d being updated does not exist", p.ID)
}
m.pmap[p.ID] = p
return nil
}

func (m MapStore) DeletePuppy(id int) error {
if _, ok := m.pmap[id]; !ok {
return fmt.Errorf("puppy ID %d being deleted does not exist", id)
}
delete(m.pmap, id)
return nil
}
Loading

0 comments on commit d94cb17

Please sign in to comment.