func testEnumerate(t *testing.T, kv sorted.KeyValue, start, end string, want ...string) { var got []string it := kv.Find(start, end) for it.Next() { key, val := it.Key(), it.Value() keyb, valb := it.KeyBytes(), it.ValueBytes() if key != string(keyb) { t.Errorf("Key and KeyBytes disagree: %q vs %q", key, keyb) } if val != string(valb) { t.Errorf("Value and ValueBytes disagree: %q vs %q", val, valb) } if key+"v" != val { t.Errorf("iterator returned unexpected pair for test: %q, %q", key, val) } got = append(got, val) } err := it.Close() if err != nil { t.Errorf("for enumerate of (%q, %q), Close error: %v", start, end, err) } if !reflect.DeepEqual(got, want) { t.Errorf("for enumerate of (%q, %q), got: %q; want %q", start, end, got, want) } }
func isEmpty(t *testing.T, kv sorted.KeyValue) bool { it := kv.Find("", "") hasRow := it.Next() if err := it.Close(); err != nil { t.Fatalf("Error closing iterator while testing for emptiness: %v", err) } return !hasRow }
func foreachSorted(t *testing.T, s sorted.KeyValue, fn func(string, string)) { it := s.Find("", "") for it.Next() { fn(it.Key(), it.Value()) } if err := it.Close(); err != nil { t.Fatal(err) } }
func queryPrefixString(s sorted.KeyValue, prefix string) sorted.Iterator { if prefix == "" { return s.Find("", "") } lastByte := prefix[len(prefix)-1] if lastByte == 0xff { panic("unsupported query prefix ending in 0xff") } end := prefix[:len(prefix)-1] + string(lastByte+1) return s.Find(prefix, end) }
func testInsertLarge(t *testing.T, kv sorted.KeyValue) { largeKey := make([]byte, sorted.MaxKeySize-1) // setting all the bytes because postgres whines about an invalid byte sequence // otherwise for k, _ := range largeKey { largeKey[k] = 'A' } largeKey[sorted.MaxKeySize-2] = 'B' largeValue := make([]byte, sorted.MaxValueSize-1) for k, _ := range largeValue { largeValue[k] = 'A' } largeValue[sorted.MaxValueSize-2] = 'B' // insert with large key if err := kv.Set(string(largeKey), "whatever"); err != nil { t.Fatalf("Insertion of large key failed: %v", err) } // and verify we can get it back, i.e. that the key hasn't been truncated. it := kv.Find(string(largeKey), "") if !it.Next() || it.Key() != string(largeKey) || it.Value() != "whatever" { it.Close() t.Fatalf("Find(largeKey) = %q, %q; want %q, %q", it.Key(), it.Value(), largeKey, "whatever") } it.Close() // insert with large value if err := kv.Set("whatever", string(largeValue)); err != nil { t.Fatalf("Insertion of large value failed: %v", err) } // and verify we can get it back, i.e. that the value hasn't been truncated. if v, err := kv.Get("whatever"); err != nil || v != string(largeValue) { t.Fatalf("get(\"whatever\") = %q, %v; want %q", v, err, largeValue) } // insert with large key and large value if err := kv.Set(string(largeKey), string(largeValue)); err != nil { t.Fatalf("Insertion of large key and value failed: %v", err) } // and verify we can get them back it = kv.Find(string(largeKey), "") defer it.Close() if !it.Next() || it.Key() != string(largeKey) || it.Value() != string(largeValue) { t.Fatalf("Find(largeKey) = %q, %q; want %q, %q", it.Key(), it.Value(), largeKey, largeValue) } }
func check(t *testing.T, kv sorted.KeyValue, prefix string, want []mod) { it := kv.Find("", "") for i, m := range want { if !it.Next() { t.Fatalf("%v: unexpected it.Next == false on iteration %d", prefix, i) } if k, v := it.Key(), it.Value(); k != m.key || v != m.value { t.Errorf("%v: got key == %q value == %q, want key == %q value == %q on iteration %d", prefix, k, v, m.key, m.value, i) } } if it.Next() { t.Errorf("%v: unexpected it.Next == true after complete iteration", prefix) } if err := it.Close(); err != nil { t.Errorf("%v: error closing iterator: %v", prefix, err) } }