Skip to content

Commit

Permalink
Added Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arjunsk committed Oct 21, 2023
1 parent 798cc41 commit d3800ea
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 27 deletions.
6 changes: 3 additions & 3 deletions containers/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (c *Cluster) Recenter() {
return
}

// newCenter = "Mean" of the Members
// newCenter = "Mean" of the members
newCenter := make(Vector, len(c.center))
for _, member := range c.members {
newCenter.Add(member)
Expand All @@ -40,7 +40,7 @@ func (c *Cluster) RecenterWithMovedDistance(distFn DistanceFunction) (moveDistan
return 0, nil
}

// newCenter is the "Mean" of the Members
// newCenter is the "Mean" of the members
newCenter := make(Vector, len(c.center))
for _, member := range c.members {
newCenter.Add(member)
Expand Down Expand Up @@ -73,7 +73,7 @@ func (c *Cluster) Reset() {
}

func (c *Cluster) String() string {
return fmt.Sprintf("Center: %v, Members: %v", c.center, c.members)
return fmt.Sprintf("center: %v, members: %v", c.center, c.members)
}

func (c *Cluster) Center() Vector {
Expand Down
80 changes: 56 additions & 24 deletions containers/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

func TestCluster_Recenter(t *testing.T) {
type fields struct {
Center Vector
Members []Vector
center Vector
members []Vector
}
tests := []struct {
name string
Expand All @@ -17,25 +17,25 @@ func TestCluster_Recenter(t *testing.T) {
{
name: "test1",
fields: fields{
Center: Vector{1, 1},
Members: []Vector{{1, 1}, {2, 2}},
center: Vector{1, 1},
members: []Vector{{1, 1}, {2, 2}},
},
wantCenter: Vector{1.5, 1.5},
},
{
name: "test2",
fields: fields{
Center: Vector{1, 1},
Members: []Vector{{1, 1}, {2, 2}, {3, 3}},
center: Vector{1, 1},
members: []Vector{{1, 1}, {2, 2}, {3, 3}},
},
wantCenter: Vector{2, 2},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Cluster{
center: tt.fields.Center,
members: tt.fields.Members,
center: tt.fields.center,
members: tt.fields.members,
}
c.Recenter()
if c.center.Compare(tt.wantCenter) != 0 {
Expand All @@ -47,8 +47,8 @@ func TestCluster_Recenter(t *testing.T) {

func TestCluster_RecenterReturningMovedDistance(t *testing.T) {
type fields struct {
Center Vector
Members []Vector
center Vector
members []Vector
}
type args struct {
distFn DistanceFunction
Expand All @@ -64,17 +64,17 @@ func TestCluster_RecenterReturningMovedDistance(t *testing.T) {
{
name: "empty cluster",
fields: fields{
Center: Vector{1, 1},
Members: []Vector{},
center: Vector{1, 1},
members: []Vector{},
},
args: args{distFn: EuclideanDistance},
wantCenter: Vector{1, 1}, // unchanged
},
{
name: "non-empty cluster",
fields: fields{
Center: Vector{1, 1},
Members: []Vector{{1, 1}, {2, 2}},
center: Vector{1, 1},
members: []Vector{{1, 1}, {2, 2}},
},
args: args{distFn: EuclideanDistance},
wantMoveDistances: 0.7071067811865476,
Expand All @@ -84,8 +84,8 @@ func TestCluster_RecenterReturningMovedDistance(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Cluster{
center: tt.fields.Center,
members: tt.fields.Members,
center: tt.fields.center,
members: tt.fields.members,
}
gotMoveDistance, err := c.RecenterWithMovedDistance(tt.args.distFn)
if (err != nil) != tt.wantErr {
Expand All @@ -104,8 +104,8 @@ func TestCluster_RecenterReturningMovedDistance(t *testing.T) {

func TestCluster_Reset(t *testing.T) {
type fields struct {
Center Vector
Members []Vector
center Vector
members []Vector
}
tests := []struct {
name string
Expand All @@ -114,23 +114,55 @@ func TestCluster_Reset(t *testing.T) {
{
name: "Test1",
fields: fields{
Center: Vector{1, 1},
Members: []Vector{{1, 1}, {2, 2}, {3, 3}},
center: Vector{1, 1},
members: []Vector{{1, 1}, {2, 2}, {3, 3}},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Cluster{
center: tt.fields.Center,
members: tt.fields.Members,
center: tt.fields.center,
members: tt.fields.members,
}
c.Reset()
if len(c.members) != 0 {
t.Errorf("Reset() = %v, want %v", c.members, []Vector{})
}
if c.center.Compare(tt.fields.Center) != 0 {
t.Errorf("Reset() = %v, want %v", c.center, tt.fields.Center)
if c.center.Compare(tt.fields.center) != 0 {
t.Errorf("Reset() = %v, want %v", c.center, tt.fields.center)
}
})
}
}

func TestCluster_SSE(t *testing.T) {
type fields struct {
center Vector
members []Vector
}
tests := []struct {
name string
fields fields
want float64
}{
{
name: "Test1",
fields: fields{
center: Vector{1, 1},
members: []Vector{{1, 1}, {3, 3}, {3, 3}},
},
want: 16.000000000000004,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Cluster{
center: tt.fields.center,
members: tt.fields.members,
}
if got := c.SSE(); got != tt.want {
t.Errorf("SSE() = %v, want %v", got, tt.want)
}
})
}
Expand Down

0 comments on commit d3800ea

Please sign in to comment.