Skip to content

adt: add tests for IntervalTree.Find() #19801

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 29, 2025

Conversation

redwrasse
Copy link
Contributor

Adds tests for IntervalTree.Find(), following up on #19768 (comment) as part of #19769.

cc @serathius

@k8s-ci-robot
Copy link

Hi @redwrasse. Thanks for your PR.

I'm waiting for a etcd-io member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

Comment on lines 272 to 280
func TestIntervalTreeFind(t *testing.T) {
ivt := NewIntervalTree()
ivl1 := NewInt64Interval(3, 6)
assert.Nilf(t, ivt.Find(ivl1), "find expected nil on empty tree")
ivt.Insert(ivl1, 123)
assert.Equalf(t, ivl1, ivt.Find(ivl1).Ivl, "find expected to return exact-matched interval")
ivl2 := NewInt64Interval(3, 7)
assert.Nilf(t, ivt.Find(ivl2), "find expected nil on single-matched endpoint")
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was looking for something more extensive. Quick prompt to ML gave me the following results:

func TestIntervalTreeFind(t *testing.T) {
	t.Run("Basic Find Operations", func(t *testing.T) {
		ivt := NewIntervalTree()
		ivl1 := NewInt64Interval(3, 6)
		val1 := 123

		// 1. Find on empty tree
		assert.Nil(t, ivt.Find(ivl1), "Find on empty tree should return nil")

		// 2. Insert and find exact match
		ivt.Insert(ivl1, val1)
		foundNode := ivt.Find(ivl1)
		assert.NotNil(t, foundNode, "Find should return the inserted interval")
		if foundNode != nil {
			assert.Equal(t, ivl1, foundNode.Ivl, "Found interval should match the inserted interval")
			assert.Equal(t, val1, foundNode.Val, "Found value should match the inserted value")
		}

		// 3. Find non-existent interval (overlapping start, different end)
		ivl2 := NewInt64Interval(3, 7)
		assert.Nil(t, ivt.Find(ivl2), "Find should return nil for an interval not exactly matching (different end)")

		// 4. Find non-existent interval (overlapping end, different start)
		ivl3 := NewInt64Interval(2, 6)
		assert.Nil(t, ivt.Find(ivl3), "Find should return nil for an interval not exactly matching (different start)")

		// 5. Find non-existent interval (completely different)
		ivl4 := NewInt64Interval(10, 20)
		assert.Nil(t, ivt.Find(ivl4), "Find should return nil for a completely different interval")

		// 6. Insert another interval and find both
		ivl5 := NewInt64Interval(10, 15)
		val5 := 456
		ivt.Insert(ivl5, val5)

		foundNode1 := ivt.Find(ivl1)
		assert.NotNil(t, foundNode1)
		if foundNode1 != nil {
			assert.Equal(t, ivl1, foundNode1.Ivl)
			assert.Equal(t, val1, foundNode1.Val)
		}

		foundNode5 := ivt.Find(ivl5)
		assert.NotNil(t, foundNode5)
		if foundNode5 != nil {
			assert.Equal(t, ivl5, foundNode5.Ivl)
			assert.Equal(t, val5, foundNode5.Val)
		}
	})

	t.Run("Find After Deletion", func(t *testing.T) {
		ivt := NewIntervalTree()
		ivlA := NewInt64Interval(1, 5)
		valA := "A"
		ivlB := NewInt64Interval(6, 10)
		valB := "B"

		ivt.Insert(ivlA, valA)
		ivt.Insert(ivlB, valB)

		assert.NotNil(t, ivt.Find(ivlA), "Should find ivlA before deletion")
		assert.NotNil(t, ivt.Find(ivlB), "Should find ivlB before deletion")

		deleted := ivt.Delete(ivlA)
		assert.True(t, deleted, "Delete should return true for an existing interval")
		assert.Nil(t, ivt.Find(ivlA), "Should not find ivlA after deletion")
		assert.NotNil(t, ivt.Find(ivlB), "Should still find ivlB after deleting ivlA")

		deleted = ivt.Delete(NewInt64Interval(100, 200)) // Non-existent
		assert.False(t, deleted, "Delete should return false for a non-existing interval")
	})

	t.Run("Find with StringComparable", func(t *testing.T) {
		ivt := NewIntervalTree()
		sivl1 := NewStringInterval("apple", "banana")
		sval1 := "fruit1"

		assert.Nil(t, ivt.Find(sivl1), "Find on empty string tree should return nil")

		ivt.Insert(sivl1, sval1)
		foundNode := ivt.Find(sivl1)
		assert.NotNil(t, foundNode, "Find should return the inserted string interval")
		if foundNode != nil {
			assert.Equal(t, sivl1, foundNode.Ivl)
			assert.Equal(t, sval1, foundNode.Val)
		}

		sivl2 := NewStringInterval("apple", "cherry") // Different end
		assert.Nil(t, ivt.Find(sivl2), "Find should return nil for a non-exact string interval")
	})

    t.Run("Find with overlapping but not identical intervals", func(t *testing.T) {
        ivt := NewIntervalTree()
        ivlBase := NewInt64Interval(5, 10)
        valBase := "base"
        ivt.Insert(ivlBase, valBase)

        // Test cases that overlap but are not identical
        testCases := []struct {
            name     string
            interval Interval
        }{
            {"Subset", NewInt64Interval(6, 9)},
            {"Superset", NewInt64Interval(4, 11)},
            {"OverlapLeft", NewInt64Interval(3, 7)},
            {"OverlapRight", NewInt64Interval(8, 12)},
            {"TouchesBegin", NewInt64Interval(3, 5)}, // Does not overlap [5,10)
            {"TouchesEnd", NewInt64Interval(10, 12)}, // Does not overlap [5,10)
        }

        for _, tc := range testCases {
            t.Run(tc.name, func(t *testing.T) {
                assert.Nil(t, ivt.Find(tc.interval), "Find(%v) should be nil as it's not an exact match for %v", tc.interval, ivlBase)
            })
        }
         // Ensure the base interval is still findable
        foundNode := ivt.Find(ivlBase)
        assert.NotNil(t, foundNode)
        if foundNode != nil {
            assert.Equal(t, ivlBase, foundNode.Ivl)
        }
    })
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I've expanded TestIntervalTreeFind with a couple workflows along the above code sample. Also checking Find is nil after Delete in TestIntervalTreeRandom.

Let me know if there's more specific cases I should include. Trying to keep the test style +coverage consistent with the rest of interval_tree_test.go.

@serathius
Copy link
Member

/ok-to-test

@k8s-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: redwrasse, serathius

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@serathius
Copy link
Member

/retest

@k8s-ci-robot
Copy link

k8s-ci-robot commented Apr 29, 2025

@redwrasse: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-etcd-coverage-report abf0439 link true /test pull-etcd-coverage-report

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

@serathius serathius merged commit e70740b into etcd-io:main Apr 29, 2025
25 of 32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants