github.com/jmooring/hugo@v0.47.1/hugolib/orderedMap_test.go (about)

     1  // Copyright 2018 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package hugolib
    15  
    16  import (
    17  	"fmt"
    18  	"sync"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  func TestOrderedMap(t *testing.T) {
    25  	t.Parallel()
    26  	assert := require.New(t)
    27  
    28  	m := newOrderedMap()
    29  	m.Add("b", "vb")
    30  	m.Add("c", "vc")
    31  	m.Add("a", "va")
    32  	b, f1 := m.Get("b")
    33  
    34  	assert.True(f1)
    35  	assert.Equal(b, "vb")
    36  	assert.True(m.Contains("b"))
    37  	assert.False(m.Contains("e"))
    38  
    39  	assert.Equal([]interface{}{"b", "c", "a"}, m.Keys())
    40  
    41  }
    42  
    43  func TestOrderedMapConcurrent(t *testing.T) {
    44  	t.Parallel()
    45  	assert := require.New(t)
    46  
    47  	var wg sync.WaitGroup
    48  
    49  	m := newOrderedMap()
    50  
    51  	for i := 1; i < 20; i++ {
    52  		wg.Add(1)
    53  		go func(id int) {
    54  			defer wg.Done()
    55  			key := fmt.Sprintf("key%d", id)
    56  			val := key + "val"
    57  			m.Add(key, val)
    58  			v, found := m.Get(key)
    59  			assert.True(found)
    60  			assert.Equal(v, val)
    61  			assert.True(m.Contains(key))
    62  			assert.True(m.Len() > 0)
    63  			assert.True(len(m.Keys()) > 0)
    64  		}(i)
    65  
    66  	}
    67  
    68  	wg.Wait()
    69  }