func TestTruncIndex(t *testing.T) {
	ids := []string{}
	index := NewTruncIndex(ids)
	if _, err := index.Get("foobar"); err == nil {
		t.Fatal("Get on an empty index should return an error")
	}
	if err := index.Add("I have a space"); err == nil {
		t.Fatalf("Adding an id with ' ' should return an error")
	}
	id := "99b36c2c326ccc11e726eee6ee78a0baf166ef96"
	if err := index.Add(id); err != nil {
		t.Fatal(err)
	}
	if err := index.Add(""); err == nil {
		t.Fatalf("Adding an empty id should return an error")
	}
	assertIndexGet(t, index, "abracadabra", "", true)
	assertIndexGet(t, index, "", "", true)
	assertIndexGet(t, index, id, id, false)
	assertIndexGet(t, index, id[:1], id, false)
	assertIndexGet(t, index, id[:len(id)/2], id, false)
	assertIndexGet(t, index, id[len(id)/2:], "", true)
	id2 := id[:6] + "blabla"
	if err := index.Add(id2); err != nil {
		t.Fatal(err)
	}
	assertIndexGet(t, index, id, id, false)
	assertIndexGet(t, index, id2, id2, false)
	assertIndexGet(t, index, id[:6], "", true)
	assertIndexGet(t, index, id[:4], "", true)
	assertIndexGet(t, index, id[:1], "", true)
	if _, err := index.Get(id[:4]); err == nil {
		t.Fatal("An ambiguous id prefix should return an error")
	}
	assertIndexGet(t, index, id[:7], id, false)
	assertIndexGet(t, index, id2[:7], id2, false)
	if err := index.Delete("non-existing"); err == nil {
		t.Fatalf("Deleting a non-existing id should return an error")
	}
	if err := index.Delete(""); err == nil {
		t.Fatal("Deleting an empty id should return an error")
	}
	if err := index.Delete(id2); err != nil {
		t.Fatal(err)
	}
	assertIndexGet(t, index, id2, "", true)
	assertIndexGet(t, index, id2[:7], "", true)
	assertIndexGet(t, index, id2[:11], "", true)
	assertIndexGet(t, index, id[:6], id, false)
	assertIndexGet(t, index, id[:4], id, false)
	assertIndexGet(t, index, id[:1], id, false)
	assertIndexGet(t, index, id[:7], id, false)
	assertIndexGet(t, index, id[:15], id, false)
	assertIndexGet(t, index, id, id, false)
	assertIndexIterate(t)
	assertIndexIterateDoNotPanic(t)
}
Test the behavior of TruncIndex, an index for querying IDs from a non-conflicting prefix.
package truncindex
import (
	"math/rand"
	"testing"
	"time"
	"github.com/containers/storage/pkg/stringid"
)