diff --git a/kmeans_test.go b/kmeans_test.go new file mode 100644 index 0000000..2356bf7 --- /dev/null +++ b/kmeans_test.go @@ -0,0 +1,60 @@ +package kmeans + +import ( + "testing" +) + +func TestNewErrors(t *testing.T) { + _, err := NewWithOptions(0.00, false) + if err == nil { + t.Errorf("Expected invalid options to return an error, got nil") + } + + _, err = NewWithOptions(1.00, false) + if err == nil { + t.Errorf("Expected invalid options to return an error, got nil") + } +} + +func TestPartitioningError(t *testing.T) { + km := New() + if _, err := km.Partition(Points{}, 1); err == nil { + t.Errorf("Expected error partitioning with empty data set, got nil") + return + } + + d := Points{ + Point{ + 0.1, + 0.1, + }, + } + if _, err := km.Partition(d, 0); err == nil { + t.Errorf("Expected error partitioning with 0 clusters, got nil") + return + } +} + +func TestDimensions(t *testing.T) { + var d Points + for x := 0; x < 255; x += 32 { + for y := 0; y < 255; y += 32 { + d = append(d, Point{ + float64(x) / 255.0, + float64(y) / 255.0, + }) + } + } + + k := 4 + km := New() + clusters, err := km.Partition(d, k) + if err != nil { + t.Errorf("Unexpected error partitioning: %v", err) + return + } + + if len(clusters) != k { + t.Errorf("Expected %d clusters, got: %d", k, len(clusters)) + } +}