protobuf-go/internal/set/strings_test.go
Joe Tsai d55639e713 internal/set: add set package for set data structures
Package set provides simple set data structures for uint64 and string types.

High-level API:
	type Set(T {}) xxx
	func (Set) Len() int
	func (Set) Has(T) bool
	func (Set) Set(T)
	func (Set) Clear(T)

These data structures are useful for implementing required fields efficiently
or ensuring that protobuf identifiers do not conflict.

Change-Id: If846630a9034909a43121b3e0f6720275f4b7aaf
Reviewed-on: https://go-review.googlesource.com/128898
Reviewed-by: Chris Manghane <cmang@golang.org>
2018-08-09 21:46:35 +00:00

64 lines
1.3 KiB
Go

// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package set
import (
"strconv"
"testing"
)
func TestStrings(t *testing.T) {
var ss Strings
// Check that set starts empty.
wantLen := 0
if ss.Len() != wantLen {
t.Errorf("init: Len() = %d, want %d", ss.Len(), wantLen)
}
for i := 0; i < maxLimit; i++ {
if ss.Has(strconv.Itoa(i)) {
t.Errorf("init: Has(%d) = true, want false", i)
}
}
// Set some strings.
for i, b := range toSet[:maxLimit] {
if b {
ss.Set(strconv.Itoa(i))
wantLen++
}
}
// Check that strings were set.
if ss.Len() != wantLen {
t.Errorf("after Set: Len() = %d, want %d", ss.Len(), wantLen)
}
for i := 0; i < maxLimit; i++ {
if got := ss.Has(strconv.Itoa(i)); got != toSet[i] {
t.Errorf("after Set: Has(%d) = %v, want %v", i, got, !got)
}
}
// Clear some strings.
for i, b := range toClear[:maxLimit] {
if b {
ss.Clear(strconv.Itoa(i))
if toSet[i] {
wantLen--
}
}
}
// Check that strings were cleared.
if ss.Len() != wantLen {
t.Errorf("after Clear: Len() = %d, want %d", ss.Len(), wantLen)
}
for i := 0; i < maxLimit; i++ {
if got := ss.Has(strconv.Itoa(i)); got != toSet[i] && !toClear[i] {
t.Errorf("after Clear: Has(%d) = %v, want %v", i, got, !got)
}
}
}