From f2c08d34908cff4457a8722fe8c1be819518795f Mon Sep 17 00:00:00 2001 From: Cooper Oneida Date: Thu, 28 Nov 2024 22:15:31 -0700 Subject: [PATCH] better tests and method fixes --- htk/annotations_test.go | 105 ++++----------------- htk/lab_test.go | 60 +++--------- textgrid/interval_test.go | 36 ++++++-- textgrid/point_test.go | 16 +++- textgrid/textgrid.go | 33 +++++-- textgrid/textgrid_test.go | 109 ++++++++++++++++++++++ textgrid/tiers.go | 20 ++-- textgrid/tiers_test.go | 186 ++++++++++++++++++++++++++++++++------ 8 files changed, 379 insertions(+), 186 deletions(-) diff --git a/htk/annotations_test.go b/htk/annotations_test.go index 740a113..616b67b 100644 --- a/htk/annotations_test.go +++ b/htk/annotations_test.go @@ -2,100 +2,33 @@ package htk import "testing" -func TestGettingAnnotationDuration(t *testing.T) { - // loading the first annotation from the first example as a dummy annotation - lab, err := ReadLab("examples/01.lab") - if err != nil { - t.Fatal(err) - } - - annotation := lab.annotations[0] - trueDuration := annotation.end - annotation.start +func TestCreatingAnnotation(t *testing.T) { + annotation := Annotation{0.0, 10.0, "test"} - if annotation.GetDuration() != trueDuration { - t.Fatalf("wanted duration %f, received %f", trueDuration, annotation.GetDuration()) + if annotation.GetStart() != 0.0 { + t.Errorf("expected 0.0, got %f", annotation.GetStart()) } - - t.Log("Getting annotation duration successful!") -} - -func TestGettingAnnotationStart(t *testing.T) { - lab, err := ReadLab("examples/01.lab") - if err != nil { - t.Fatal(err) + if annotation.GetEnd() != 10.0 { + t.Errorf("expected 10.0, got %f", annotation.GetEnd()) } - - annotation := lab.annotations[0] - trueStart := annotation.start - - if annotation.GetStart() != trueStart { - t.Fatalf("wanted start time of %f, received %f", trueStart, annotation.GetStart()) + if annotation.GetLabel() != "test" { + t.Errorf("expected test, got %s", annotation.GetLabel()) } - - t.Log("getting annotation start successful!") -} - -func TestGettingAnnotationEnd(t *testing.T) { - lab, err := ReadLab("examples/01.lab") - if err != nil { - t.Fatal(err) + if annotation.GetDuration() != 10.0 { + t.Errorf("expected 10.0, got %f", annotation.GetDuration()) } - annotation := lab.annotations[0] - trueEnd := annotation.end + annotation.SetStart(15.0) + annotation.SetEnd(20.0) + annotation.SetLabel("test2") - if annotation.GetEnd() != trueEnd { - t.Fatalf("wanted end time of %f, received %f", trueEnd, annotation.GetEnd()) + if annotation.GetStart() != 15.0 { + t.Errorf("expected 15.0, got %f", annotation.GetStart()) } - - t.Log("getting annotation end successful!") -} - -func TestGettingAnnotationLabel(t *testing.T) { - lab, err := ReadLab("examples/01.lab") - if err != nil { - t.Fatal(err) + if annotation.GetEnd() != 20.0 { + t.Errorf("expected 20.0, got %f", annotation.GetEnd()) } - - annotation := lab.annotations[0] - trueLabel := annotation.label - - if annotation.GetLabel() != trueLabel { - t.Fatalf("wanted label %s, received %s", trueLabel, annotation.GetLabel()) - } - - t.Log("test setting annotation label successful!") -} - -func TestSettingAnnotationStartEnd(t *testing.T) { - lab, err := ReadLab("examples/01.lab") - if err != nil { - t.Fatal(err) - } - - annotation := lab.annotations[0] - annotation.SetStart(10) - annotation.SetEnd(20) - - if annotation.start != 10 { - t.Fatalf("wanted start time of 10, received %f", annotation.start) - } else if annotation.end != 20 { - t.Fatalf("wanted end time of 20, received %f", annotation.end) - } - - t.Log("setting annotation start and end successful!") -} - -func TestSettingAnnotationLabel(t *testing.T) { - lab, err := ReadLab("examples/01.lab") - if err != nil { - t.Fatal(err) - } - - annotation := lab.annotations[0] - annotation.SetLabel("label") - - if annotation.label != "label" { - t.Fatalf("wanted label string of \"label\", recieved %s", annotation.label) + if annotation.GetLabel() != "test2" { + t.Errorf("expected test2, got %s", annotation.GetLabel()) } } diff --git a/htk/lab_test.go b/htk/lab_test.go index 1c54d0a..f748a23 100644 --- a/htk/lab_test.go +++ b/htk/lab_test.go @@ -8,15 +8,13 @@ func TestReadingLab(t *testing.T) { t.Fatal(err) } - if lab.name != "short.lab" { - t.Fatalf("wanted 'short', recieved %s", lab.name) - } else if lab.annotations == nil { + if lab.GetName() != "short.lab" { + t.Fatalf("wanted 'short', recieved %s", lab.GetName()) + } else if lab.GetAnnotations() == nil { t.Fatalf("wanted annotations, recieved nil") - } else if lab.precision != 7 { - t.Fatalf("wanted precision of 7, recieved %d", lab.precision) + } else if lab.GetPrecision() != 7 { + t.Fatalf("wanted precision of 7, recieved %d", lab.GetPrecision()) } - - t.Log("lab reading successful!") } func TestReadingDifferentPrecision(t *testing.T) { @@ -25,11 +23,9 @@ func TestReadingDifferentPrecision(t *testing.T) { t.Fatal(err) } - if lab.precision != 6 { - t.Fatalf("wanted precision of 6, recieved %d", lab.precision) + if lab.GetPrecision() != 6 { + t.Fatalf("wanted precision of 6, recieved %d", lab.GetPrecision()) } - - t.Log("different precisions reading successful!") } func TestWritingLab(t *testing.T) { @@ -42,8 +38,6 @@ func TestWritingLab(t *testing.T) { if err != nil { return } - - t.Log("lab writing successful!") } func TestWritingDifferentPrecision(t *testing.T) { @@ -56,8 +50,6 @@ func TestWritingDifferentPrecision(t *testing.T) { if err != nil { return } - - t.Log("different precision writing successful!") } func TestPrintingLabString(t *testing.T) { @@ -69,8 +61,6 @@ func TestPrintingLabString(t *testing.T) { if lab.ToString() != "0.0000000 10.0000000 test\n" { t.Fatal("malformed lab string!") } - - t.Log("printing lab to string successful!") } func TestSettingAnnotations(t *testing.T) { @@ -84,8 +74,6 @@ func TestSettingAnnotations(t *testing.T) { if lab.ToString() != "0 1 test1\n1 2 test2\n2 3 test3\n" { t.Fatal("annotations not set correctly!") } - - t.Log("setting annotations successful!") } func TestGettingAnnotations(t *testing.T) { @@ -94,15 +82,13 @@ func TestGettingAnnotations(t *testing.T) { t.Fatal(err) } - if lab.annotations[0].start != 0 { - t.Fatalf("incorrect annotation start time! wanted 0, recieved %f", lab.annotations[0].start) - } else if lab.annotations[0].end != 10 { - t.Fatalf("incorrect annotation end time! wanted 10, recieved %f", lab.annotations[0].end) - } else if lab.annotations[0].label != "test" { - t.Fatalf("incorrect annotation label!, wanted string \"test\", recieved string \"%s\"", lab.annotations[0].label) + if lab.GetAnnotations()[0].GetStart() != 0 { + t.Fatalf("incorrect annotation start time! wanted 0, recieved %f", lab.GetAnnotations()[0].GetStart()) + } else if lab.GetAnnotations()[0].GetEnd() != 10 { + t.Fatalf("incorrect annotation end time! wanted 10, recieved %f", lab.GetAnnotations()[0].GetEnd()) + } else if lab.GetAnnotations()[0].GetLabel() != "test" { + t.Fatalf("incorrect annotation label!, wanted string \"test\", recieved string \"%s\"", lab.GetAnnotations()[0].GetLabel()) } - - t.Log("getting annotations successful!") } func TestPushingAnnotation(t *testing.T) { @@ -120,8 +106,6 @@ func TestPushingAnnotation(t *testing.T) { } else if lab.annotations[2].end != 30 { t.Fatalf("incorrect annotation end time! wanted end time 30, recieved %f", lab.annotations[2].end) } - - t.Log("pushing annotations successful!") } func TestAppendingAnnotations(t *testing.T) { @@ -150,8 +134,6 @@ func TestAppendingAnnotations(t *testing.T) { } else if lab.annotations[3].label != "new_annotation2" { t.Fatalf("incorrect annotation label!") } - - t.Log("appending annotations successful!") } func TestClearingAnnotations(t *testing.T) { @@ -165,8 +147,6 @@ func TestClearingAnnotations(t *testing.T) { if lab.ToString() != "" { t.Fatal("lab not properly cleared!") } - - t.Log("clearing annotations successful!") } func TestDumpingLabels(t *testing.T) { @@ -181,8 +161,6 @@ func TestDumpingLabels(t *testing.T) { if isEqualSlice(labSlice, groundTruthSlice) == false { t.Fatal("returned slices not identical!") } - - t.Log("annotation dumping successful!") } func TestGettingLabName(t *testing.T) { @@ -194,8 +172,6 @@ func TestGettingLabName(t *testing.T) { if lab.GetName() != lab.name { t.Fatalf("wanted lab name %s, recieved %s", lab.name, lab.GetName()) } - - t.Log("getting lab name successful!") } func TestSettingLabName(t *testing.T) { @@ -209,8 +185,6 @@ func TestSettingLabName(t *testing.T) { if lab.name != "newName" { t.Fatalf("wanted lab name of \"newName\", recieved \"%s\"", lab.name) } - - t.Log("setting lab name successful!") } func TestGettingPrecision(t *testing.T) { @@ -222,8 +196,6 @@ func TestGettingPrecision(t *testing.T) { if lab.GetPrecision() != lab.precision { t.Fatalf("wanted precision %d, recieved %d", lab.precision, lab.GetPrecision()) } - - t.Log("getting precision successful!") } func TestSettingPrecision(t *testing.T) { @@ -242,8 +214,6 @@ func TestSettingPrecision(t *testing.T) { if err != nil { return } - - t.Log("setting precision successful!") } func TestGettingLabDuration(t *testing.T) { @@ -257,8 +227,6 @@ func TestGettingLabDuration(t *testing.T) { if lab.GetDuration() != trueDuration { t.Fatalf("wanted duration of %f, recieved %f", trueDuration, lab.GetDuration()) } - - t.Log("getting lab duration successful!") } func TestGettingLabLength(t *testing.T) { @@ -272,6 +240,4 @@ func TestGettingLabLength(t *testing.T) { if lab.GetLength() != trueLength { t.Fatalf("wanted length of %d, recieved %d", trueLength, lab.GetLength()) } - - t.Log("getting lab length successful!") } diff --git a/textgrid/interval_test.go b/textgrid/interval_test.go index 294626b..4fd840f 100644 --- a/textgrid/interval_test.go +++ b/textgrid/interval_test.go @@ -5,17 +5,37 @@ import "testing" func TestCreatingInterval(t *testing.T) { interval := Interval{xmin: 0.0, xmax: 1.0, text: "test"} - if interval.xmin != 0.0 { - t.Errorf("Expected 0.0, got %f", interval.xmin) + // testing accessors + if interval.GetXmin() != 0.0 { + t.Errorf("Expected xmin 0.0, got %f", interval.xmin) } - if interval.xmax != 1.0 { - t.Errorf("Expected 1.0, got %f", interval.xmax) + if interval.GetXmax() != 1.0 { + t.Errorf("Expected xmax 1.0, got %f", interval.xmax) } - if interval.text != "test" { - t.Errorf("Expected 'test', got %s", interval.text) + if interval.GetText() != "test" { + t.Errorf("Expected interval label \"test\", got %q", interval.text) } - if !t.Failed() { - t.Logf("interval created successfully") + // testing mutators + interval.SetXmin(2.0) + interval.SetXmax(3.0) + interval.SetText("test2") + + if interval.GetXmin() != 2.0 { + t.Errorf("Expected xmin 2.0, got %f", interval.xmin) + } + if interval.GetXmax() != 3.0 { + t.Errorf("Expected xmax 3.0, got %f", interval.xmax) + } + if interval.GetText() != "test2" { + t.Errorf("Expected interval label \"test2\", got %q", interval.text) + } + + // testing misc methods + if interval.GetDuration() != 1.0 { + t.Errorf("Expected dur of 1.0, got %f", interval.GetDuration()) + } + if interval.GetMedian() != 2.5 { + t.Errorf("Expected median of 2.5, got %f", interval.GetMedian()) } } diff --git a/textgrid/point_test.go b/textgrid/point_test.go index b861e48..faa7245 100644 --- a/textgrid/point_test.go +++ b/textgrid/point_test.go @@ -5,14 +5,22 @@ import "testing" func TestCreatingPoint(t *testing.T) { point := Point{mark: "point", value: 10.0} - if point.value != 10.0 { + // testing accessors + if point.GetValue() != 10.0 { t.Errorf("Expected 10.0, got %f", point.value) } - if point.mark != "point" { + if point.GetMark() != "point" { t.Errorf("Expected 'mark', got %s", point.mark) } - if !t.Failed() { - t.Logf("point created successfully") + // testing mutators + point.SetValue(15.0) + point.SetMark("test") + + if point.GetValue() != 15.0 { + t.Errorf("Expected 15.0, got %f", point.value) + } + if point.GetMark() != "test" { + t.Errorf("Expected 'mark', got %s", point.mark) } } diff --git a/textgrid/textgrid.go b/textgrid/textgrid.go index ea488b5..b84686c 100644 --- a/textgrid/textgrid.go +++ b/textgrid/textgrid.go @@ -55,11 +55,16 @@ func (tg *TextGrid) SetName(name string) { tg.name = name } -// GetTiers returns tiers of a Textgrid. +// GetTiers returns Tier slice of a TextGrid. func (tg *TextGrid) GetTiers() []Tier { return tg.tiers } +// SetTiers sets TextGrid tier field to Tier slice. +func (tg *TextGrid) SetTiers(tiers []Tier) { + tg.tiers = tiers +} + // HasIntervalTier returns true if TextGrid has IntervalTier. func (tg *TextGrid) HasIntervalTier() bool { for _, tier := range tg.tiers { @@ -73,14 +78,14 @@ func (tg *TextGrid) HasIntervalTier() bool { // HasPointTier returns true if TextGrid has PointTier. func (tg *TextGrid) HasPointTier() bool { for _, tier := range tg.tiers { - if tier.GetType() == "PointTier" { + if tier.GetType() == "TextTier" { return true } } return false } -// GetTier returns given tier with specified name, if it exists. +// GetTier returns given Tier with specified name, if it exists. func (tg *TextGrid) GetTier(name string) Tier { for _, tier := range tg.tiers { if tier.GetName() == name { @@ -90,11 +95,29 @@ func (tg *TextGrid) GetTier(name string) Tier { return nil } -// TierAtIndex returns given tier with specified index. Does not return nil if index is out of range. +// SetTier sets Tier with specified name, if it exists. +func (tg *TextGrid) SetTier(name string, newTier Tier) { + for i, tier := range tg.tiers { + if tier.GetName() == name { + tg.tiers[i] = newTier + } + } +} + +// TierAtIndex returns given Tier with specified index. Does not return nil if index is out of range. func (tg *TextGrid) TierAtIndex(index int) Tier { return tg.tiers[index] } +// SetTierAtIndex sets Tier at given index. +func (tg *TextGrid) SetTierAtIndex(index int, newTier Tier) { + for i := range tg.tiers { + if i == index { + tg.tiers[i] = newTier + } + } +} + // GetSize returns the amount of Tier entries in a TextGrid. func (tg *TextGrid) GetSize() int { return len(tg.tiers) @@ -150,8 +173,6 @@ func ReadTextgrid(path string) (TextGrid, error) { return tg, fmt.Errorf("error: cannot parse textgrid xmax in %s:\n %s", tg.name, err.Error()) } - fmt.Println(globalXmax) - // set the xmin and xmax preemptively in case the status is tg.xmin = globalXmin tg.xmax = globalXmax diff --git a/textgrid/textgrid_test.go b/textgrid/textgrid_test.go index 1b8f917..ca487b7 100644 --- a/textgrid/textgrid_test.go +++ b/textgrid/textgrid_test.go @@ -2,6 +2,115 @@ package textgrid import "testing" +func TestCreatingTextGrid(t *testing.T) { + tg := TextGrid{ + xmin: 0, + xmax: 10.0, + tiers: []Tier{&IntervalTier{ + name: "IntervalTier", + xmin: 0, + xmax: 10.0, + intervals: []Interval{{ + xmin: 0, + xmax: 10.0, + text: "Interval", + }}, + }, &PointTier{ + name: "PointTier", + xmin: 0, + xmax: 10.0, + points: []Point{{ + value: 5.0, + mark: "Point", + }}, + }}, + name: "TextGrid", + } + + newTiers := []Tier{&PointTier{ + name: "NewPointTier", + xmin: 10.0, + xmax: 20.0, + points: []Point{{ + value: 15.0, + mark: "NewPoint", + }}, + }, &IntervalTier{ + name: "NewIntervalTier", + xmin: 10.0, + xmax: 20.0, + intervals: []Interval{{ + xmin: 10.0, + xmax: 20.0, + text: "NewInterval", + }}, + }} + + // testing accessors + if tg.GetXmin() != 0 { + t.Errorf("expected xmin 0, got %f", tg.GetXmin()) + } + if tg.GetXmax() != 10.0 { + t.Errorf("expected xmax 10.0, got %f", tg.GetXmax()) + } + if tg.GetName() != "TextGrid" { + t.Errorf("expected name \"TextGrid\", got %q", tg.GetName()) + } + if tg.GetTiers()[0].GetType() != "IntervalTier" { + t.Errorf("expected type \"IntervalTier\", got %q", tg.GetTiers()[0].GetType()) + } + if tg.GetTiers()[1].GetType() != "TextTier" { + t.Errorf("expected type \"TextTier\", got %q", tg.GetTiers()[1].GetType()) + } + + // testing misc methods + if !tg.HasIntervalTier() { + t.Errorf("expected HasIntervalTier to be true") + } + if !tg.HasPointTier() { + t.Errorf("expected HasPointTier to be true") + } + + if tg.GetTier("IntervalTier").GetType() != "IntervalTier" { + t.Errorf("\"IntervalTier\" incorrectly retrieved") + } + if tg.GetTier("PointTier").GetType() != "TextTier" { + t.Errorf("\"TextTier\" incorrectly retrieved") + } + if tg.TierAtIndex(0).GetType() != "IntervalTier" { + t.Errorf("\"IntervalTier\" incorrectly retrieved") + } + if tg.TierAtIndex(1).GetType() != "TextTier" { + t.Errorf("\"TextTier\" incorrectly retrieved") + } + + if tg.GetSize() != 2 { + t.Errorf("expected size 2, got %d", tg.GetSize()) + } + + // testing mutators + tg.SetXmin(10.0) + tg.SetXmax(20.0) + tg.SetName("NewTextGrid") + tg.SetTiers(newTiers) + + if tg.GetXmin() != 10.0 { + t.Errorf("expected xmin 10.0, got %f", tg.GetXmin()) + } + if tg.GetXmax() != 20.0 { + t.Errorf("expected xmax 20.0, got %f", tg.GetXmax()) + } + if tg.GetName() != "NewTextGrid" { + t.Errorf("expected name NewTextGrid, got %q", tg.GetName()) + } + if tg.GetTiers()[0].GetType() != "TextTier" { + t.Errorf("expected type \"TextTier\", got %q", tg.GetTiers()[0].GetType()) + } + if tg.GetTiers()[1].GetType() != "IntervalTier" { + t.Errorf("expected type \"IntervalTier\", got %q", tg.GetTiers()[1].GetType()) + } +} + func TestReadingTextgridASCIILong(t *testing.T) { _, err := ReadTextgrid("examples/long.TextGrid") if err != nil { diff --git a/textgrid/tiers.go b/textgrid/tiers.go index 1ad4194..cbe3561 100644 --- a/textgrid/tiers.go +++ b/textgrid/tiers.go @@ -298,6 +298,9 @@ func (pTier *PointTier) SetPoints(newPoints []Point, warn ...bool) error { } } + pTier.points = newPoints + pTier.sort() + return nil } @@ -335,7 +338,6 @@ func (iTier *IntervalTier) PushIntervals(intervalsPush []Interval, warn ...bool) // SetIntervals sets intervals field of IntervalTier. // By default, will return an error if there is an Interval that has an xmin or xmax not inside the TextGrid range. func (iTier *IntervalTier) SetIntervals(newIntervals []Interval, warn ...bool) error { - if len(warn) == 0 { warn = append(warn, true) } @@ -371,10 +373,12 @@ func (pTier *PointTier) GetOverlapping() [][]int { // iterate over each pair of points, comparing the value to the next value (which should not be the same) for i, point := range pTier.points { - nextPoint := pTier.points[i+1] + if len(pTier.points) > i+1 { + nextPoint := pTier.points[i+1] - if point.value == nextPoint.value { - overlaps = append(overlaps, []int{i, i + 1}) + if point.value == nextPoint.value { + overlaps = append(overlaps, []int{i, i + 1}) + } } } @@ -391,10 +395,12 @@ func (iTier *IntervalTier) GetOverlapping() [][]int { // iterate over each pair of intervals, comparing the xmax to the next xmin (which should be the same) for i, interval := range iTier.intervals { - nextInterval := iTier.intervals[i+1] + if len(iTier.intervals) > i+1 { + nextInterval := iTier.intervals[i+1] - if interval.xmax != nextInterval.xmin { - overlaps = append(overlaps, []int{i, i + 1}) + if interval.xmax != nextInterval.xmin { + overlaps = append(overlaps, []int{i, i + 1}) + } } } diff --git a/textgrid/tiers_test.go b/textgrid/tiers_test.go index 4838030..ae942ec 100644 --- a/textgrid/tiers_test.go +++ b/textgrid/tiers_test.go @@ -1,6 +1,8 @@ package textgrid -import "testing" +import ( + "testing" +) func TestCreatingIntervalTier(t *testing.T) { intervals := []Interval{ @@ -9,38 +11,142 @@ func TestCreatingIntervalTier(t *testing.T) { {2.0, 3.0, "test3"}, } + singlePushInterval := Interval{3.0, 4.0, "test4"} + + multiplePushIntervals := []Interval{ + {4.0, 5.0, "test5"}, + {6.0, 7.0, "test7"}, + {5.0, 6.0, "test6"}, + } + tier := IntervalTier{name: "TestTier", xmin: 0.0, xmax: 3.0, intervals: intervals} - if tier.name != "TestTier" { + // testing accessors + if tier.GetName() != "TestTier" { t.Errorf("Expected 'TestTier', got %s", tier.name) } - if tier.xmin != 0.0 { + if tier.GetXmin() != 0.0 { t.Errorf("Expected 0.0, got %f", tier.xmin) } - if tier.xmax != 3.0 { + if tier.GetXmax() != 3.0 { t.Errorf("Expected 3.0, got %f", tier.xmax) } - if len(tier.intervals) != 3 { - t.Errorf("Expected 3 intervals, got %d", len(tier.intervals)) + if tier.GetSize() != 3 { + t.Errorf("Expected 3 intervals, got %d", tier.GetSize()) + } + if tier.GetType() != "IntervalTier" { + t.Errorf("Expected 'IntervalTier', got %s", tier.GetType()) } + if tier.GetDuration() != 3.0 { + t.Errorf("Expected 3.0, got %f", tier.GetDuration()) + } + + for _, interval := range tier.GetIntervals() { + if interval.GetText() != "test1" && interval.GetText() != "test2" && interval.GetText() != "test3" { + t.Errorf("Expected 'test1', 'test2', or 'test3', got %s", interval.GetText()) + } - for _, interval := range tier.intervals { - if interval.text != "test1" && interval.text != "test2" && interval.text != "test3" { - t.Errorf("Expected 'test1', 'test2', or 'test3', got %s", interval.text) + if interval.GetXmin() != 0.0 && interval.GetXmin() != 1.0 && interval.GetXmin() != 2.0 { + t.Errorf("Expected 0.0, 1.0, or 2.0, got %f", interval.GetXmin()) } - if interval.xmin != 0.0 && interval.xmin != 1.0 && interval.xmin != 2.0 { - t.Errorf("Expected 0.0, 1.0, or 2.0, got %f", interval.xmin) + if interval.GetXmax() != 1.0 && interval.GetXmax() != 2.0 && interval.GetXmax() != 3.0 { + t.Errorf("Expected 1.0, 2.0, or 3.0, got %f", interval.GetXmax()) + } + } + + err := tier.PushInterval(singlePushInterval) + if err != nil { + t.Error(err) + } + + err = tier.PushIntervals(multiplePushIntervals) + if err != nil { + t.Error(err) + } + + // should be fully sorted + t.Logf("Interval Tier: %v\n", tier) +} + +func TestOverlapping(t *testing.T) { + overlappingIntervalTier := IntervalTier{ + name: "OverlappingIntervalTier", + xmin: 0, + xmax: 10.0, + intervals: []Interval{ + {0.0, 1.0, "test1"}, + {0.5, 1.0, "test2"}, + }, + } + + overlappingPointTier := PointTier{ + name: "OverlappingPointTier", + xmin: 0.0, + xmax: 10.0, + points: []Point{ + {5.0, "test3"}, + {5.0, "test4"}, + }, + } + + // should be [0, 1] + t.Logf("Overlapping Interval Indicies: %v\n", overlappingIntervalTier.GetOverlapping()) + t.Logf("Overlapping Point Indicies: %v\n", overlappingPointTier.GetOverlapping()) +} + +func TestCreatingPointTier(t *testing.T) { + points := []Point{ + {0.0, "test1"}, + {1.0, "test2"}, + {2.0, "test3"}, + } + + singlePushPoint := Point{3.0, "test4"} + + multiplePushPoints := []Point{ + {4.0, "test5"}, + {6.0, "test7"}, + {5.0, "test6"}, + } + + tier := PointTier{name: "TestTier", xmin: 0.0, xmax: 3.0, points: points} + + if tier.GetName() != "TestTier" { + t.Errorf("Expected 'TestTier', got %s", tier.name) + } + if tier.GetXmin() != 0.0 { + t.Errorf("Expected 0.0, got %f", tier.xmin) + } + if tier.GetXmax() != 3.0 { + t.Errorf("Expected 3.0, got %f", tier.xmax) + } + if tier.GetSize() != 3 { + t.Errorf("Expected 3 intervals, got %d", tier.GetSize()) + } + + for _, point := range tier.GetPoints() { + if point.GetMark() != "test1" && point.GetMark() != "test2" && point.GetMark() != "test3" { + t.Errorf("Expected 'test1', 'test2', or 'test3', got %s", point.GetMark()) } - if interval.xmax != 1.0 && interval.xmax != 2.0 && interval.xmax != 3.0 { - t.Errorf("Expected 1.0, 2.0, or 3.0, got %f", interval.xmax) + if point.GetValue() != 0.0 && point.GetValue() != 1.0 && point.GetValue() != 2.0 { + t.Errorf("Expected 0.0, 1.0, or 2.0, got %f", point.GetValue()) } } - if !t.Failed() { - t.Logf("interval tier created successfully") + err := tier.PushPoint(singlePushPoint) + if err != nil { + t.Error(err) + } + + err = tier.PushPoints(multiplePushPoints) + if err != nil { + t.Error(err) } + + // should be fully sorted + t.Logf("Point Tier: %v\n", tier) } func TestSettingIntervals(t *testing.T) { @@ -53,30 +159,54 @@ func TestSettingIntervals(t *testing.T) { tier := IntervalTier{name: "TestTier", xmin: 0.0, xmax: 3.0} err := tier.SetIntervals(intervals) - if err != nil { - t.Errorf("Expected nil, got %s", err) + t.Errorf(err.Error()) } - if len(tier.intervals) != 3 { - t.Errorf("Expected 3 intervals, got %d", len(tier.intervals)) + if tier.GetSize() != 3 { + t.Errorf("Expected 3 intervals, got %d", tier.GetSize()) } - for _, interval := range tier.intervals { - if interval.text != "test1" && interval.text != "test2" && interval.text != "test3" { - t.Errorf("Expected 'test1', 'test2', or 'test3', got %s", interval.text) + for _, interval := range tier.GetIntervals() { + if interval.GetText() != "test1" && interval.GetText() != "test2" && interval.GetText() != "test3" { + t.Errorf("Expected 'test1', 'test2', or 'test3', got %s", interval.GetText()) } - if interval.xmin != 0.0 && interval.xmin != 1.0 && interval.xmin != 2.0 { - t.Errorf("Expected 0.0, 1.0, or 2.0, got %f", interval.xmin) + if interval.GetXmin() != 0.0 && interval.GetXmin() != 1.0 && interval.GetXmin() != 2.0 { + t.Errorf("Expected 0.0, 1.0, or 2.0, got %f", interval.GetXmin()) } - if interval.xmax != 1.0 && interval.xmax != 2.0 && interval.xmax != 3.0 { - t.Errorf("Expected 1.0, 2.0, or 3.0, got %f", interval.xmax) + if interval.GetXmax() != 1.0 && interval.GetXmax() != 2.0 && interval.GetXmax() != 3.0 { + t.Errorf("Expected 1.0, 2.0, or 3.0, got %f", interval.GetXmax()) } } +} + +func TestSettingPoints(t *testing.T) { + points := []Point{ + {0.0, "test1"}, + {1.0, "test2"}, + {2.0, "test3"}, + } + + tier := PointTier{name: "TestTier", xmin: 0.0, xmax: 3.0} + + err := tier.SetPoints(points) + if err != nil { + t.Errorf(err.Error()) + } + + if tier.GetSize() != 3 { + t.Errorf("Expected 3 intervals, got %d", tier.GetSize()) + } - if !t.Failed() { - t.Logf("intervals set successfully") + for _, point := range tier.GetPoints() { + if point.GetMark() != "test1" && point.GetMark() != "test2" && point.GetMark() != "test3" { + t.Errorf("Expected 'test1', 'test2', or 'test3', got %s", point.GetMark()) + } + + if point.GetValue() != 0.0 && point.GetValue() != 1.0 && point.GetValue() != 2.0 { + t.Errorf("Expected 0.0, 1.0, or 2.0, got %f", point.GetValue()) + } } }