diff --git a/bimap/bimap.go b/bimap/bimap.go index d992a39..94a5ba1 100644 --- a/bimap/bimap.go +++ b/bimap/bimap.go @@ -13,8 +13,15 @@ type BiMap struct { } // NewBiMap returns a an empty, mutable, biMap -func NewBiMap() *BiMap { - return &BiMap{forward: make(map[interface{}]interface{}), inverse: make(map[interface{}]interface{}), immutable: false} +func NewBiMap(content map[interface{}]interface{}) *BiMap { + 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. diff --git a/bimap/bimap_test.go b/bimap/bimap_test.go index bbdebaa..893d9c9 100644 --- a/bimap/bimap_test.go +++ b/bimap/bimap_test.go @@ -67,7 +67,7 @@ func TestNewBiMap(tx *testing.T) { t := ut.BeginTest(tx, false) defer t.FinishTest() - actual := NewBiMap() + actual := NewBiMap(nil) expected := &BiMap{forward: make(map[interface{}]interface{}), inverse: make(map[interface{}]interface{})} t.Equals(expected, actual) } @@ -75,7 +75,7 @@ func TestNewBiMap(tx *testing.T) { func TestBiMap_Insert(tx *testing.T) { t := ut.BeginTest(tx, false) defer t.FinishTest() - actual := NewBiMap() + actual := NewBiMap(nil) actual.Insert(key, value) fwdExpected := make(map[interface{}]interface{}) @@ -90,7 +90,7 @@ func TestBiMap_Insert(tx *testing.T) { func TestBiMap_Exists(tx *testing.T) { t := ut.BeginTest(tx, false) defer t.FinishTest() - actual := NewBiMap() + actual := NewBiMap(nil) actual.Insert(key, value) 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) { t := ut.BeginTest(tx, false) defer t.FinishTest() - actual := NewBiMap() + actual := NewBiMap(nil) actual.Insert(key, value) 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) { t := ut.BeginTest(tx, false) defer t.FinishTest() - actual := NewBiMap() + actual := NewBiMap(nil) actual.Insert(key, value) @@ -128,7 +128,7 @@ func TestBiMap_Get(tx *testing.T) { func TestBiMap_GetInverse(tx *testing.T) { t := ut.BeginTest(tx, false) defer t.FinishTest() - actual := NewBiMap() + actual := NewBiMap(nil) actual.Insert(key, value) @@ -146,7 +146,7 @@ func TestBiMap_GetInverse(tx *testing.T) { func TestBiMap_Size(tx *testing.T) { t := ut.BeginTest(tx, false) defer t.FinishTest() - actual := NewBiMap() + actual := NewBiMap(nil) t.Equals(0, actual.Size()) @@ -158,7 +158,7 @@ func TestBiMap_Size(tx *testing.T) { func TestBiMap_Delete(tx *testing.T) { t := ut.BeginTest(tx, false) defer t.FinishTest() - actual := NewBiMap() + actual := NewBiMap(nil) dummyKey := "DummyKey" dummyVal := "DummyVal" actual.Insert(key, value) @@ -187,7 +187,7 @@ func TestBiMap_Delete(tx *testing.T) { func TestBiMap_InverseDelete(tx *testing.T) { t := ut.BeginTest(tx, false) defer t.FinishTest() - actual := NewBiMap() + actual := NewBiMap(nil) dummyKey := "DummyKey" dummyVal := "DummyVal" actual.Insert(key, value) @@ -216,7 +216,7 @@ func TestBiMap_InverseDelete(tx *testing.T) { func TestBiMap_WithVaryingType(tx *testing.T) { t := ut.BeginTest(tx, false) defer t.FinishTest() - actual := NewBiMap() + actual := NewBiMap(nil) dummyKey := "Dummy key" dummyVal := 3 @@ -232,7 +232,7 @@ func TestBiMap_WithVaryingType(tx *testing.T) { func TestBiMap_MakeImmutable(tx *testing.T) { t := ut.BeginTest(tx, false) defer t.FinishTest() - actual := NewBiMap() + actual := NewBiMap(nil) dummyKey := "Dummy key" dummyVal := 3 @@ -276,7 +276,7 @@ func TestBiMap_MakeImmutable(tx *testing.T) { func TestBiMap_GetForwardMap(tx *testing.T) { t := ut.BeginTest(tx, false) defer t.FinishTest() - actual := NewBiMap() + actual := NewBiMap(nil) dummyKey := "Dummy key" dummyVal := 42 @@ -293,7 +293,7 @@ func TestBiMap_GetForwardMap(tx *testing.T) { func TestBiMap_GetInverseMap(tx *testing.T) { t := ut.BeginTest(tx, false) defer t.FinishTest() - actual := NewBiMap() + actual := NewBiMap(nil) dummyKey := "Dummy key" dummyVal := 42 @@ -306,3 +306,25 @@ func TestBiMap_GetInverseMap(tx *testing.T) { eq := reflect.DeepEqual(actualInverseMap, inverseMap) 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) + +}