diff options
Diffstat (limited to 'utils')
-rw-r--r-- | utils/collection.go | 199 | ||||
-rw-r--r-- | utils/collection_test.go | 155 |
2 files changed, 0 insertions, 354 deletions
diff --git a/utils/collection.go b/utils/collection.go deleted file mode 100644 index 3f6e2ed1..00000000 --- a/utils/collection.go +++ /dev/null @@ -1,199 +0,0 @@ -// Copyright (C) 2014 Nippon Telegraph and Telephone Corporation. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or -// implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package utils - -import ( - "container/list" - "fmt" - "reflect" -) - -// Elements of abstraction the key. -type K struct { - Element interface{} -} - -/* -// Elements of abstraction the key. -type V struct { - Element interface{} -} -*/ -/* This method may be required -func (e *E) Elem() interface{} { - return e.Element -} -*/ - -// Structure for extending the map collection. -// -// keyType : Key type of Map . -// keyType : Value type of Map. -// TMap : Map of the object to be extended. -// TOrder : Insertion order list of map. -type OrderedMap struct { - keyType interface{} - valType interface{} - TMap map[K]interface{} - TOrder *list.List -} - -// Constructor of OrderedMap. -func NewOrderedMap() *OrderedMap { - om := &OrderedMap{} - om.TMap = make(map[K]interface{}) - om.TOrder = list.New() - return om -} - -// Check whether the same of type of Map and List -// -// Returns: - error contents -// and nil if no error occurred. -func (om *OrderedMap) checkType(keyInfs interface{}, valInfs interface{}) (e error) { - if om.keyType == nil && om.valType == nil { - om.keyType = keyInfs - om.valType = valInfs - return nil - } else { - if reflect.TypeOf(om.keyType) != reflect.TypeOf(keyInfs) { - return fmt.Errorf("Map Key Type mismatch [ %s ] and [ %s ].", reflect.TypeOf(om.keyType), reflect.TypeOf(keyInfs)) - } - } - return nil -} - -// Check whether the same key exists in the map -// -// Returns: - error contents -// and nil if no error occurred. -func (om *OrderedMap) checkDuplicate(keyInfs interface{}) (e error) { - for elem := om.TOrder.Front(); elem != nil; elem = elem.Next() { - if elem.Value == keyInfs { - return fmt.Errorf("Map key Duplicated [%s].", elem.Value) - } - } - return -} - -// Append Elements to Map -// -// Returns: - error contents -// and nil if no error occurred. -func (om *OrderedMap) Append(keyInfs interface{}, valInfs interface{}) (e error) { - e = om.checkType(keyInfs, valInfs) - if e != nil { - return e - } - // Append key Elements to Map - om.TMap[K{Element: keyInfs}] = valInfs - e = om.checkDuplicate(K{Element: keyInfs}) - if e != nil { - /* - for elem := om.TOrder.Front(); elem != nil; elem = elem.Next() { - if elem.Value == (K{Element: keyInfs}) { - tmp := elem.Next() - om.TOrder.Remove(elem) - elem = tmp - } - }*/ - return nil - } - // Append Elements to List - om.TOrder.PushBack(K{Element: keyInfs}) - return nil -} - -// Get Elements from receive parameter. -// -// Returns: - Value of Map -// Return the interface that value has entered the Map. -func (om *OrderedMap) Get(keyInfs interface{}) interface{} { - elem := om.TMap[K{Element: keyInfs}] - return elem -} - -// Convert Map keys to List. -// -// Returns: - List of Map Keys -func (om *OrderedMap) KeyLists() *list.List { - keys := list.New() - for key := om.TOrder.Front(); key != nil; key = key.Next() { - keyElem := key.Value.(K).Element - keys.PushBack(keyElem) - } - return keys -} - -// Convert Map values to List. -// -// Returns: - List of Map Values -func (om *OrderedMap) ValueLists() *list.List { - vals := list.New() - for key := om.TOrder.Front(); key != nil; key = key.Next() { - keyElem := key.Value.(K).Element - value := om.Get(keyElem) - vals.PushBack(value) - } - return vals -} - -// Get Map length -// -// Returns: - Length of the map Element. -func (om *OrderedMap) Len() int { - return om.TOrder.Len() -} - -// Delete Map Element -// -// Returns: - error contents -// and nil if no error occurred. -func (om *OrderedMap) Delete(keyInfs interface{}) (e error) { - // Delete key Elements from Map - delete(om.TMap, K{Element: keyInfs}) - // Delete key Elements from List - for elem := om.TOrder.Front(); elem != nil; elem = elem.Next() { - if elem.Value == (K{Element: keyInfs}) { - tmp := elem.Next() - if tmp == nil { - break - } - om.TOrder.Remove(elem) - elem = tmp - } - } - return -} - -// Get Elements from Map and delete from List -// -// Returns: - Value of Map -// Return the interface that value has entered the Map. -func (om *OrderedMap) Pop(keyInfs interface{}) interface{} { - elem := om.TMap[K{Element: keyInfs}] - key := (K{Element: keyInfs}).Element - om.Delete(key) - return elem -} - -// Clear Map and List -func (om *OrderedMap) Clear() { - om.keyType = nil - om.valType = nil - om.TMap = make(map[K]interface{}) - om.TOrder = list.New() -} diff --git a/utils/collection_test.go b/utils/collection_test.go deleted file mode 100644 index 59097e09..00000000 --- a/utils/collection_test.go +++ /dev/null @@ -1,155 +0,0 @@ -// collection_test.go -package utils - -import ( - "container/list" - "fmt" - "github.com/stretchr/testify/assert" - "testing" -) - -func TestOrderedMapNew(t *testing.T) { - oMap1 := NewOrderedMap() - oMap2 := new(OrderedMap) - oMap2.TMap = make(map[K]interface{}) - oMap2.TOrder = list.New() - assert.NotNil(t, oMap1) - assert.Equal(t, oMap1, oMap2) -} - -func TestOrderedMapAppend(t *testing.T) { - oMap := NewOrderedMap() - e := oMap.Append(1, "test1") - assert.NoError(t, e) - expected := "test1" - assert.Equal(t, expected, oMap.Get(1).(string)) -} - -func TestOrderedMapGet(t *testing.T) { - oMap := NewOrderedMap() - e := oMap.Append(2, "test2") - assert.NoError(t, e) - expected := "test2" - assert.Nil(t, oMap.Get(1)) - assert.Equal(t, expected, oMap.Get(2).(string)) - assert.Nil(t, oMap.Get(3)) -} - -func TestOrderedMapDelete(t *testing.T) { - oMap := NewOrderedMap() - e := oMap.Append(3, "test3") - assert.NoError(t, e) - expected := "test3" - assert.Equal(t, expected, oMap.Get(3).(string)) - oMap.Delete(3) - assert.Nil(t, oMap.Get(3)) -} - -func TestOrderdMapPop(t *testing.T) { - oMap := NewOrderedMap() - e := oMap.Append(4, "test4") - assert.NoError(t, e) - expected := "test4" - assert.Equal(t, expected, oMap.Pop(4).(string)) - assert.Nil(t, oMap.Get(4)) -} - -func TestOrderdMapLen(t *testing.T) { - oMap := NewOrderedMap() - count := 10 - for i := 0; i < count; i++ { - e := oMap.Append(i, "test") - assert.NoError(t, e) - } - assert.Equal(t, count, oMap.Len()) -} - -func TestOrderdMapKeyLists(t *testing.T) { - oMap := NewOrderedMap() - count := 10 - for i := 0; i < count; i++ { - str := fmt.Sprintf("%s%d", "test", i) - e := oMap.Append(i, str) - assert.NoError(t, e) - } - expectedList := list.New() - for i := 0; i < count; i++ { - expectedList.PushBack(i) - } - kList := oMap.KeyLists() - assert.Equal(t, expectedList, kList) -} - -func TestOrderdMapValueLists(t *testing.T) { - oMap := NewOrderedMap() - count := 10 - for i := 0; i < count; i++ { - str := fmt.Sprintf("%s%d", "test", i) - e := oMap.Append(i, str) - assert.NoError(t, e) - } - expectedList := list.New() - for i := 0; i < count; i++ { - str := fmt.Sprintf("%s%d", "test", i) - expectedList.PushBack(str) - } - vList := oMap.ValueLists() - assert.Equal(t, expectedList, vList) -} - -func TestOrderedMapDiffKeyType(t *testing.T) { - oMap := NewOrderedMap() - e1 := oMap.Append(11, "test11") - assert.NoError(t, e1) - e2 := oMap.Append("test12", "test12") - assert.Error(t, e2) - //t.Log(e2) -} - -func TestOrderedMapDiffValueType(t *testing.T) { - oMap := NewOrderedMap() - e1 := oMap.Append(13, "test13") - assert.NoError(t, e1) - e2 := oMap.Append(14, 14) - assert.NoError(t, e2) - expectedStr := "test13" - expectedNum := 14 - assert.Equal(t, expectedStr, oMap.Get(13).(string)) - assert.Equal(t, expectedNum, oMap.Get(14).(int)) -} - -func TestOrderedMapDupKey(t *testing.T) { - oMap := NewOrderedMap() - e1 := oMap.Append(15, "test15") - assert.NoError(t, e1) - e2 := oMap.Append(15, "test15-1") - assert.NoError(t, e2) - expected := "test15-1" - assert.Equal(t, oMap.Get(15).(string), expected) -} - -func TestOrderedMapElementsOfStruct(t *testing.T) { - oMap := NewOrderedMap() - ks1 := &KeyStructT{1, "test1"} - ks2 := &KeyStructT{2, "test2"} - vs1 := &ValStructT{"test1", 1} - vs2 := &ValStructT{"test2", 2} - vs1expected := &ValStructT{"test1", 1} - vs2expected := &ValStructT{"test2", 2} - e1 := oMap.Append(ks1, vs1) - assert.NoError(t, e1) - e2 := oMap.Append(ks2, vs2) - assert.NoError(t, e2) - assert.Equal(t, vs1expected, oMap.Get(ks1)) - assert.Equal(t, vs2expected, oMap.Get(ks2)) -} - -type KeyStructT struct { - a int - b string -} - -type ValStructT struct { - c string - d int -} |