Skip to content

Commit

Permalink
Factor out getting map size and test it
Browse files Browse the repository at this point in the history
This is an implementation detail that can't be tested in a black-box
manner.
  • Loading branch information
krnowak committed Jan 30, 2020
1 parent f208843 commit 316c0ef
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 50 deletions.
34 changes: 19 additions & 15 deletions api/correlation/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,9 @@ func NewMap(update MapUpdate) Map {
// DropMultiK, then add key-value pairs from SingleKV and MultiKV.
func (m Map) Apply(update MapUpdate) Map {
delSet, addSet := getModificationSets(update)
mapSize := getNewMapSize(m.m, delSet, addSet)

mapSizeDiff := 0
for k := range addSet {
if _, ok := m.m[k]; !ok {
mapSizeDiff++
}
}
for k := range delSet {
if _, ok := m.m[k]; ok {
if _, inAddSet := addSet[k]; !inAddSet {
mapSizeDiff--
}
}
}

r := make(rawMap, len(m.m)+mapSizeDiff)
r := make(rawMap, mapSize)
for k, v := range m.m {
// do not copy items we want to drop
if _, ok := delSet[k]; ok {
Expand Down Expand Up @@ -142,6 +129,23 @@ func getModificationSets(update MapUpdate) (keySet, keySet) {
return delSet, addSet
}

func getNewMapSize(m rawMap, delSet keySet, addSet keySet) int {
mapSizeDiff := 0
for k := range addSet {
if _, ok := m[k]; !ok {
mapSizeDiff++
}
}
for k := range delSet {
if _, ok := m[k]; ok {
if _, inAddSet := addSet[k]; !inAddSet {
mapSizeDiff--
}
}
}
return len(m) + mapSizeDiff
}

// Value gets a value from correlations map and returns a boolean
// value indicating whether the key exist in the map.
func (m Map) Value(k core.Key) (core.Value, bool) {
Expand Down
96 changes: 61 additions & 35 deletions api/correlation/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,68 @@ import (
"go.opentelemetry.io/otel/api/key"
)

type testCase struct {
name string
value MapUpdate
init []int
wantKVs []core.KeyValue
}

func TestMap(t *testing.T) {
for _, testcase := range []struct {
name string
value MapUpdate
init []int
wantKVs []core.KeyValue
}{
for _, testcase := range getTestCases() {
t.Logf("Running test case %s", testcase.name)
var got Map
if len(testcase.init) > 0 {
got = makeTestMap(testcase.init).Apply(testcase.value)
} else {
got = NewMap(testcase.value)
}
for _, s := range testcase.wantKVs {
if ok := got.HasValue(s.Key); !ok {
t.Errorf("Expected Key %s to have Value", s.Key)
}
if g, ok := got.Value(s.Key); !ok || g != s.Value {
t.Errorf("+got: %v, -want: %v", g, s.Value)
}
}
// test Foreach()
got.Foreach(func(kv core.KeyValue) bool {
for _, want := range testcase.wantKVs {
if kv == want {
return false
}
}
t.Errorf("Expected kv %v, but not found", kv)
return true
})
if l, exp := got.Len(), len(testcase.wantKVs); l != exp {
t.Errorf("+got: %d, -want: %d", l, exp)
}
}
}

func TestSizeComputation(t *testing.T) {
for _, testcase := range getTestCases() {
t.Logf("Running test case %s", testcase.name)
var initMap Map
if len(testcase.init) > 0 {
initMap = makeTestMap(testcase.init)
} else {
initMap = NewEmptyMap()
}
gotMap := initMap.Apply(testcase.value)

delSet, addSet := getModificationSets(testcase.value)
mapSize := getNewMapSize(initMap.m, delSet, addSet)

if gotMap.Len() != mapSize {
t.Errorf("Expected computed size to be %d, got %d", gotMap.Len(), mapSize)
}
}
}

func getTestCases() []testCase {
return []testCase{
{
name: "NewMap with MultiKV",
value: MapUpdate{MultiKV: []core.KeyValue{
Expand Down Expand Up @@ -219,35 +274,6 @@ func TestMap(t *testing.T) {
key.Int("key7", 7),
},
},
} {
t.Logf("Running test case %s", testcase.name)
var got Map
if len(testcase.init) > 0 {
got = makeTestMap(testcase.init).Apply(testcase.value)
} else {
got = NewMap(testcase.value)
}
for _, s := range testcase.wantKVs {
if ok := got.HasValue(s.Key); !ok {
t.Errorf("Expected Key %s to have Value", s.Key)
}
if g, ok := got.Value(s.Key); !ok || g != s.Value {
t.Errorf("+got: %v, -want: %v", g, s.Value)
}
}
// test Foreach()
got.Foreach(func(kv core.KeyValue) bool {
for _, want := range testcase.wantKVs {
if kv == want {
return false
}
}
t.Errorf("Expected kv %v, but not found", kv)
return true
})
if l, exp := got.Len(), len(testcase.wantKVs); l != exp {
t.Errorf("+got: %d, -want: %d", l, exp)
}
}
}

Expand Down

0 comments on commit 316c0ef

Please sign in to comment.