mirror of
https://github.com/jpeletier/koolnova2mqtt.git
synced 2026-05-06 04:53:24 +00:00
bimap: added content initializer
This commit is contained in:
@@ -13,8 +13,15 @@ type BiMap struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewBiMap returns a an empty, mutable, biMap
|
// NewBiMap returns a an empty, mutable, biMap
|
||||||
func NewBiMap() *BiMap {
|
func NewBiMap(content map[interface{}]interface{}) *BiMap {
|
||||||
return &BiMap{forward: make(map[interface{}]interface{}), inverse: make(map[interface{}]interface{}), immutable: false}
|
b := &BiMap{forward: make(map[interface{}]interface{}), inverse: make(map[interface{}]interface{}), immutable: false}
|
||||||
|
if content != nil {
|
||||||
|
for k, v := range content {
|
||||||
|
b.Insert(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert puts a key and value into the BiMap, provided its mutable. Also creates the reverse mapping from value to key.
|
// Insert puts a key and value into the BiMap, provided its mutable. Also creates the reverse mapping from value to key.
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ func TestNewBiMap(tx *testing.T) {
|
|||||||
t := ut.BeginTest(tx, false)
|
t := ut.BeginTest(tx, false)
|
||||||
defer t.FinishTest()
|
defer t.FinishTest()
|
||||||
|
|
||||||
actual := NewBiMap()
|
actual := NewBiMap(nil)
|
||||||
expected := &BiMap{forward: make(map[interface{}]interface{}), inverse: make(map[interface{}]interface{})}
|
expected := &BiMap{forward: make(map[interface{}]interface{}), inverse: make(map[interface{}]interface{})}
|
||||||
t.Equals(expected, actual)
|
t.Equals(expected, actual)
|
||||||
}
|
}
|
||||||
@@ -75,7 +75,7 @@ func TestNewBiMap(tx *testing.T) {
|
|||||||
func TestBiMap_Insert(tx *testing.T) {
|
func TestBiMap_Insert(tx *testing.T) {
|
||||||
t := ut.BeginTest(tx, false)
|
t := ut.BeginTest(tx, false)
|
||||||
defer t.FinishTest()
|
defer t.FinishTest()
|
||||||
actual := NewBiMap()
|
actual := NewBiMap(nil)
|
||||||
actual.Insert(key, value)
|
actual.Insert(key, value)
|
||||||
|
|
||||||
fwdExpected := make(map[interface{}]interface{})
|
fwdExpected := make(map[interface{}]interface{})
|
||||||
@@ -90,7 +90,7 @@ func TestBiMap_Insert(tx *testing.T) {
|
|||||||
func TestBiMap_Exists(tx *testing.T) {
|
func TestBiMap_Exists(tx *testing.T) {
|
||||||
t := ut.BeginTest(tx, false)
|
t := ut.BeginTest(tx, false)
|
||||||
defer t.FinishTest()
|
defer t.FinishTest()
|
||||||
actual := NewBiMap()
|
actual := NewBiMap(nil)
|
||||||
|
|
||||||
actual.Insert(key, value)
|
actual.Insert(key, value)
|
||||||
t.Assert(!actual.Exists("ARBITARY_KEY"), "Key should not exist")
|
t.Assert(!actual.Exists("ARBITARY_KEY"), "Key should not exist")
|
||||||
@@ -100,7 +100,7 @@ func TestBiMap_Exists(tx *testing.T) {
|
|||||||
func TestBiMap_InverseExists(tx *testing.T) {
|
func TestBiMap_InverseExists(tx *testing.T) {
|
||||||
t := ut.BeginTest(tx, false)
|
t := ut.BeginTest(tx, false)
|
||||||
defer t.FinishTest()
|
defer t.FinishTest()
|
||||||
actual := NewBiMap()
|
actual := NewBiMap(nil)
|
||||||
|
|
||||||
actual.Insert(key, value)
|
actual.Insert(key, value)
|
||||||
t.Assert(!actual.ExistsInverse("ARBITARY_VALUE"), "Value should not exist")
|
t.Assert(!actual.ExistsInverse("ARBITARY_VALUE"), "Value should not exist")
|
||||||
@@ -110,7 +110,7 @@ func TestBiMap_InverseExists(tx *testing.T) {
|
|||||||
func TestBiMap_Get(tx *testing.T) {
|
func TestBiMap_Get(tx *testing.T) {
|
||||||
t := ut.BeginTest(tx, false)
|
t := ut.BeginTest(tx, false)
|
||||||
defer t.FinishTest()
|
defer t.FinishTest()
|
||||||
actual := NewBiMap()
|
actual := NewBiMap(nil)
|
||||||
|
|
||||||
actual.Insert(key, value)
|
actual.Insert(key, value)
|
||||||
|
|
||||||
@@ -128,7 +128,7 @@ func TestBiMap_Get(tx *testing.T) {
|
|||||||
func TestBiMap_GetInverse(tx *testing.T) {
|
func TestBiMap_GetInverse(tx *testing.T) {
|
||||||
t := ut.BeginTest(tx, false)
|
t := ut.BeginTest(tx, false)
|
||||||
defer t.FinishTest()
|
defer t.FinishTest()
|
||||||
actual := NewBiMap()
|
actual := NewBiMap(nil)
|
||||||
|
|
||||||
actual.Insert(key, value)
|
actual.Insert(key, value)
|
||||||
|
|
||||||
@@ -146,7 +146,7 @@ func TestBiMap_GetInverse(tx *testing.T) {
|
|||||||
func TestBiMap_Size(tx *testing.T) {
|
func TestBiMap_Size(tx *testing.T) {
|
||||||
t := ut.BeginTest(tx, false)
|
t := ut.BeginTest(tx, false)
|
||||||
defer t.FinishTest()
|
defer t.FinishTest()
|
||||||
actual := NewBiMap()
|
actual := NewBiMap(nil)
|
||||||
|
|
||||||
t.Equals(0, actual.Size())
|
t.Equals(0, actual.Size())
|
||||||
|
|
||||||
@@ -158,7 +158,7 @@ func TestBiMap_Size(tx *testing.T) {
|
|||||||
func TestBiMap_Delete(tx *testing.T) {
|
func TestBiMap_Delete(tx *testing.T) {
|
||||||
t := ut.BeginTest(tx, false)
|
t := ut.BeginTest(tx, false)
|
||||||
defer t.FinishTest()
|
defer t.FinishTest()
|
||||||
actual := NewBiMap()
|
actual := NewBiMap(nil)
|
||||||
dummyKey := "DummyKey"
|
dummyKey := "DummyKey"
|
||||||
dummyVal := "DummyVal"
|
dummyVal := "DummyVal"
|
||||||
actual.Insert(key, value)
|
actual.Insert(key, value)
|
||||||
@@ -187,7 +187,7 @@ func TestBiMap_Delete(tx *testing.T) {
|
|||||||
func TestBiMap_InverseDelete(tx *testing.T) {
|
func TestBiMap_InverseDelete(tx *testing.T) {
|
||||||
t := ut.BeginTest(tx, false)
|
t := ut.BeginTest(tx, false)
|
||||||
defer t.FinishTest()
|
defer t.FinishTest()
|
||||||
actual := NewBiMap()
|
actual := NewBiMap(nil)
|
||||||
dummyKey := "DummyKey"
|
dummyKey := "DummyKey"
|
||||||
dummyVal := "DummyVal"
|
dummyVal := "DummyVal"
|
||||||
actual.Insert(key, value)
|
actual.Insert(key, value)
|
||||||
@@ -216,7 +216,7 @@ func TestBiMap_InverseDelete(tx *testing.T) {
|
|||||||
func TestBiMap_WithVaryingType(tx *testing.T) {
|
func TestBiMap_WithVaryingType(tx *testing.T) {
|
||||||
t := ut.BeginTest(tx, false)
|
t := ut.BeginTest(tx, false)
|
||||||
defer t.FinishTest()
|
defer t.FinishTest()
|
||||||
actual := NewBiMap()
|
actual := NewBiMap(nil)
|
||||||
dummyKey := "Dummy key"
|
dummyKey := "Dummy key"
|
||||||
dummyVal := 3
|
dummyVal := 3
|
||||||
|
|
||||||
@@ -232,7 +232,7 @@ func TestBiMap_WithVaryingType(tx *testing.T) {
|
|||||||
func TestBiMap_MakeImmutable(tx *testing.T) {
|
func TestBiMap_MakeImmutable(tx *testing.T) {
|
||||||
t := ut.BeginTest(tx, false)
|
t := ut.BeginTest(tx, false)
|
||||||
defer t.FinishTest()
|
defer t.FinishTest()
|
||||||
actual := NewBiMap()
|
actual := NewBiMap(nil)
|
||||||
dummyKey := "Dummy key"
|
dummyKey := "Dummy key"
|
||||||
dummyVal := 3
|
dummyVal := 3
|
||||||
|
|
||||||
@@ -276,7 +276,7 @@ func TestBiMap_MakeImmutable(tx *testing.T) {
|
|||||||
func TestBiMap_GetForwardMap(tx *testing.T) {
|
func TestBiMap_GetForwardMap(tx *testing.T) {
|
||||||
t := ut.BeginTest(tx, false)
|
t := ut.BeginTest(tx, false)
|
||||||
defer t.FinishTest()
|
defer t.FinishTest()
|
||||||
actual := NewBiMap()
|
actual := NewBiMap(nil)
|
||||||
dummyKey := "Dummy key"
|
dummyKey := "Dummy key"
|
||||||
dummyVal := 42
|
dummyVal := 42
|
||||||
|
|
||||||
@@ -293,7 +293,7 @@ func TestBiMap_GetForwardMap(tx *testing.T) {
|
|||||||
func TestBiMap_GetInverseMap(tx *testing.T) {
|
func TestBiMap_GetInverseMap(tx *testing.T) {
|
||||||
t := ut.BeginTest(tx, false)
|
t := ut.BeginTest(tx, false)
|
||||||
defer t.FinishTest()
|
defer t.FinishTest()
|
||||||
actual := NewBiMap()
|
actual := NewBiMap(nil)
|
||||||
dummyKey := "Dummy key"
|
dummyKey := "Dummy key"
|
||||||
dummyVal := 42
|
dummyVal := 42
|
||||||
|
|
||||||
@@ -306,3 +306,25 @@ func TestBiMap_GetInverseMap(tx *testing.T) {
|
|||||||
eq := reflect.DeepEqual(actualInverseMap, inverseMap)
|
eq := reflect.DeepEqual(actualInverseMap, inverseMap)
|
||||||
t.Assert(eq, "Inverse maps should be equal")
|
t.Assert(eq, "Inverse maps should be equal")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBiMap_Initialize(tx *testing.T) {
|
||||||
|
t := ut.BeginTest(tx, false)
|
||||||
|
defer t.FinishTest()
|
||||||
|
|
||||||
|
content := map[interface{}]interface{}{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
}
|
||||||
|
|
||||||
|
m := NewBiMap(content)
|
||||||
|
|
||||||
|
v, present := m.Get("a")
|
||||||
|
t.Equals(1, v)
|
||||||
|
t.Equals(true, present)
|
||||||
|
|
||||||
|
k, present := m.GetInverse(3)
|
||||||
|
t.Equals("c", k)
|
||||||
|
t.Equals(true, present)
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user