Skip to content

Commit

Permalink
Merge pull request #1775 from influxdb/fix-1764
Browse files Browse the repository at this point in the history
fix #1764: panic index out of range
  • Loading branch information
dgnorton committed Feb 27, 2015
2 parents 3985bb0 + 0d4a8dc commit 0597cc3
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
2 changes: 1 addition & 1 deletion database.go
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ func (a seriesIDs) intersect(other seriesIDs) seriesIDs {
var i, j int

ids := make([]uint32, 0, len(l))
for i < len(l) {
for i < len(l) && j < len(r) {
if l[i] == r[j] {
ids = append(ids, l[i])
i++
Expand Down
82 changes: 82 additions & 0 deletions internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,88 @@ func TestCreateMeasurementsCommand_Errors(t *testing.T) {
}
}

// Test comparing seriesIDs for equality.
func Test_seriesIDs_equals(t *testing.T) {
ids1 := seriesIDs{1, 2, 3}
ids2 := seriesIDs{1, 2, 3}
ids3 := seriesIDs{4, 5, 6}

if !ids1.equals(ids2) {
t.Fatal("expected ids1 == ids2")
} else if ids1.equals(ids3) {
t.Fatal("expected ids1 != ids3")
}
}

// Test intersecting sets of seriesIDs.
func Test_seriesIDs_intersect(t *testing.T) {
// Test swaping l & r, all branches of if-else, and exit loop when 'j < len(r)'
ids1 := seriesIDs{1, 3, 4, 5, 6}
ids2 := seriesIDs{1, 2, 3, 7}
exp := seriesIDs{1, 3}
got := ids1.intersect(ids2)

if !exp.equals(got) {
t.Fatalf("exp=%v, got=%v", exp, got)
}

// Test exit for loop when 'i < len(l)'
ids1 = seriesIDs{1}
ids2 = seriesIDs{1, 2}
exp = seriesIDs{1}
got = ids1.intersect(ids2)

if !exp.equals(got) {
t.Fatalf("exp=%v, got=%v", exp, got)
}
}

// Test union sets of seriesIDs.
func Test_seriesIDs_union(t *testing.T) {
// Test all branches of if-else, exit loop because of 'j < len(r)', and append remainder from left.
ids1 := seriesIDs{1, 2, 3, 7}
ids2 := seriesIDs{1, 3, 4, 5, 6}
exp := seriesIDs{1, 2, 3, 4, 5, 6, 7}
got := ids1.union(ids2)

if !exp.equals(got) {
t.Fatalf("exp=%v, got=%v", exp, got)
}

// Test exit because of 'i < len(l)' and append remainder from right.
ids1 = seriesIDs{1}
ids2 = seriesIDs{1, 2}
exp = seriesIDs{1, 2}
got = ids1.union(ids2)

if !exp.equals(got) {
t.Fatalf("exp=%v, got=%v", exp, got)
}
}

// Test removing one set of seriesIDs from another.
func Test_seriesIDs_reject(t *testing.T) {
// Test all branches of if-else, exit loop because of 'j < len(r)', and append remainder from left.
ids1 := seriesIDs{1, 2, 3, 7}
ids2 := seriesIDs{1, 3, 4, 5, 6}
exp := seriesIDs{2, 7}
got := ids1.reject(ids2)

if !exp.equals(got) {
t.Fatalf("exp=%v, got=%v", exp, got)
}

// Test exit because of 'i < len(l)'.
ids1 = seriesIDs{1}
ids2 = seriesIDs{1, 2}
exp = seriesIDs{}
got = ids1.reject(ids2)

if !exp.equals(got) {
t.Fatalf("exp=%v, got=%v", exp, got)
}
}

// MustParseExpr parses an expression string and returns its AST representation.
func MustParseExpr(s string) influxql.Expr {
expr, err := influxql.ParseExpr(s)
Expand Down

0 comments on commit 0597cc3

Please sign in to comment.