github.com/Aoi-hosizora/ahlib@v1.5.1-0.20230404072829-241b93cf91c7/xorderedmap/xorderedmap_test.go (about)

     1  package xorderedmap
     2  
     3  import (
     4  	"encoding/json"
     5  	"github.com/Aoi-hosizora/ahlib/xtesting"
     6  	"sync"
     7  	"testing"
     8  )
     9  
    10  func TestNew(t *testing.T) {
    11  	m := New()
    12  	xtesting.Equal(t, len(m.keys), 0)
    13  	xtesting.Equal(t, cap(m.keys), 0)
    14  	xtesting.Equal(t, len(m.kv), 0)
    15  
    16  	m = NewWithCap(5)
    17  	xtesting.Equal(t, len(m.keys), 0)
    18  	xtesting.Equal(t, cap(m.keys), 5)
    19  	xtesting.Equal(t, len(m.kv), 0)
    20  }
    21  
    22  func TestSet(t *testing.T) {
    23  	m := New()
    24  	m.Set("a", "1")
    25  	m.Set("c", "3")
    26  	m.Set("b", "2")
    27  
    28  	xtesting.Equal(t, m.Keys(), []string{"a", "c", "b"})
    29  	xtesting.Equal(t, m.Values(), []interface{}{"1", "3", "2"})
    30  	xtesting.Equal(t, m.Len(), 3)
    31  	xtesting.True(t, m.Has("a"))
    32  	xtesting.True(t, m.Has("c"))
    33  	xtesting.True(t, m.Has("b"))
    34  	xtesting.False(t, m.Has("d"))
    35  
    36  	m.Set("c", "3") // do not change the order
    37  	m.Set("e", "5")
    38  	m.Set("d", "4")
    39  
    40  	xtesting.Equal(t, m.Keys(), []string{"a", "c", "b", "e", "d"})
    41  	xtesting.Equal(t, m.Values(), []interface{}{"1", "3", "2", "5", "4"})
    42  	xtesting.Equal(t, m.Len(), 5)
    43  	xtesting.True(t, m.Has("e"))
    44  	xtesting.True(t, m.Has("d"))
    45  	xtesting.False(t, m.Has("f"))
    46  
    47  	m.Clear()
    48  	xtesting.Equal(t, m.Keys(), []string{})
    49  	xtesting.Equal(t, m.Values(), []interface{}{})
    50  	xtesting.Equal(t, m.Len(), 0)
    51  }
    52  
    53  func TestGet(t *testing.T) {
    54  	m := New()
    55  	m.Set("a", "1")
    56  	m.Set("c", "3")
    57  	m.Set("b", "2")
    58  
    59  	i, ok := m.Get("a")
    60  	xtesting.Equal(t, i, "1")
    61  	xtesting.True(t, ok)
    62  	i, ok = m.Get("c")
    63  	xtesting.Equal(t, i, "3")
    64  	xtesting.True(t, ok)
    65  	i, ok = m.Get("b")
    66  	xtesting.Equal(t, i, "2")
    67  	xtesting.True(t, ok)
    68  	i, ok = m.Get("d")
    69  	xtesting.Equal(t, i, nil)
    70  	xtesting.False(t, ok)
    71  
    72  	xtesting.Equal(t, m.MustGet("a"), "1")
    73  	xtesting.Equal(t, m.MustGet("c"), "3")
    74  	xtesting.Equal(t, m.MustGet("b"), "2")
    75  	xtesting.Panic(t, func() { m.MustGet("d") })
    76  
    77  	xtesting.Equal(t, m.GetOr("a", ""), "1")
    78  	xtesting.Equal(t, m.GetOr("c", ""), "3")
    79  	xtesting.Equal(t, m.GetOr("b", ""), "2")
    80  	xtesting.Equal(t, m.GetOr("d", ""), "")
    81  }
    82  
    83  func TestRemove(t *testing.T) {
    84  	m := New()
    85  	m.Set("a", "1")
    86  	m.Set("c", "3")
    87  	m.Set("b", "2")
    88  
    89  	i, ok := m.Remove("a")
    90  	xtesting.Equal(t, i, "1")
    91  	xtesting.True(t, ok)
    92  	xtesting.Equal(t, m.Len(), 2)
    93  	xtesting.False(t, m.Has("a"))
    94  	xtesting.True(t, m.Has("c"))
    95  	xtesting.True(t, m.Has("b"))
    96  
    97  	i, ok = m.Remove("b")
    98  	xtesting.Equal(t, i, "2")
    99  	xtesting.True(t, ok)
   100  	xtesting.Equal(t, m.Len(), 1)
   101  	xtesting.False(t, m.Has("b"))
   102  	xtesting.True(t, m.Has("c"))
   103  
   104  	i, ok = m.Remove("c")
   105  	xtesting.Equal(t, i, "3")
   106  	xtesting.True(t, ok)
   107  	xtesting.Equal(t, m.Len(), 0)
   108  	xtesting.False(t, m.Has("c"))
   109  
   110  	i, ok = m.Remove("d")
   111  	xtesting.Equal(t, i, nil)
   112  	xtesting.False(t, ok)
   113  
   114  	m.Clear()
   115  	i, ok = m.Remove("a")
   116  	xtesting.Equal(t, i, nil)
   117  	xtesting.False(t, ok)
   118  }
   119  
   120  func TestMarshal(t *testing.T) {
   121  	m := New()
   122  	m.Set("a", 1)
   123  	m.Set("c", 3)
   124  	m.Set("b", 2)
   125  
   126  	bs, err := m.MarshalJSON()
   127  	xtesting.Equal(t, string(bs), `{"a":1,"c":3,"b":2}`)
   128  	xtesting.Nil(t, err)
   129  	bs, err = json.Marshal(m)
   130  	xtesting.Equal(t, string(bs), `{"a":1,"c":3,"b":2}`)
   131  	xtesting.Nil(t, err)
   132  	xtesting.Equal(t, m.String(), `{"a":1,"c":3,"b":2}`)
   133  
   134  	m.Clear()
   135  	m.Set("f", func() {})
   136  	bs, err = m.MarshalJSON()
   137  	xtesting.Equal(t, len(bs), 0)
   138  	xtesting.NotNil(t, err)
   139  	xtesting.Equal(t, m.String(), "")
   140  
   141  	m.Clear()
   142  	m.Set("a", 1)
   143  	m.Set("c", 3)
   144  	m.Set("b", 2)
   145  	i, err := m.MarshalYAML()
   146  	xtesting.Equal(t, i, map[string]interface{}{"a": 1, "c": 3, "b": 2})
   147  	xtesting.Nil(t, err)
   148  
   149  	m.Clear()
   150  	m.Set("z", 1)
   151  	m.Set("2", 3)
   152  	m.Set("a", 2)
   153  	type mapItem struct { // yaml.MapItem
   154  		Key, Value interface{}
   155  	}
   156  	CreateYamlMapSliceFunc = func(kvPairs [][2]interface{}) (interface{}, error) {
   157  		slice := make([]mapItem, 0, len(kvPairs)) // yaml.MapSlice
   158  		for _, pair := range kvPairs {
   159  			slice = append(slice, mapItem{Key: pair[0], Value: pair[1]})
   160  		}
   161  		return slice, nil
   162  	}
   163  	defer func() { CreateYamlMapSliceFunc = nil }()
   164  	i, err = m.MarshalYAML()
   165  	xtesting.Equal(t, i, []mapItem{{"z", 1}, {"2", 3}, {"a", 2}})
   166  	xtesting.Nil(t, err)
   167  }
   168  
   169  func TestMutex(t *testing.T) {
   170  	om := New()
   171  	wg := sync.WaitGroup{}
   172  	for i := 0; i < 20; i++ {
   173  		wg.Add(1)
   174  		go func() {
   175  			defer wg.Done()
   176  
   177  			xtesting.NotPanic(t, func() {
   178  				om.Keys()
   179  				om.Values()
   180  				om.Len()
   181  				om.Set("", 0)
   182  				om.Has("")
   183  				om.Get("")
   184  				om.GetOr("", 0)
   185  				// om.MustGet("")
   186  				om.Remove("")
   187  				om.Clear()
   188  				_, _ = om.MarshalJSON()
   189  				_, _ = om.MarshalYAML()
   190  				_ = om.String()
   191  			})
   192  		}()
   193  	}
   194  	wg.Wait()
   195  }
   196  
   197  func TestFromInterface(t *testing.T) {
   198  	xtesting.Panic(t, func() { FromInterface(nil) })
   199  	xtesting.Panic(t, func() { FromInterface(0) })
   200  	dummy := 0
   201  	xtesting.Panic(t, func() { FromInterface(&dummy) })
   202  
   203  	type testStruct1 struct {
   204  		Int    int
   205  		Uint   uint    `json:"omitempty"`
   206  		Float  float64 `json:"float"`
   207  		Bool   bool    `json:"bool"`
   208  		String string  `json:"-"`
   209  	}
   210  	test1 := &testStruct1{
   211  		Int:    1,
   212  		Uint:   0,
   213  		Float:  1.5,
   214  		Bool:   false,
   215  		String: "",
   216  	}
   217  	om := FromInterface(*test1)
   218  	xtesting.NotPanic(t, func() { om = FromInterface(test1) })
   219  	xtesting.Equal(t, om.Keys(), []string{"Int", "omitempty", "float", "bool"})
   220  	xtesting.Equal(t, om.Values(), []interface{}{1, uint(0), 1.5, false})
   221  
   222  	type testStruct2 struct {
   223  		Int     int                    `json:"int,omitempty"`
   224  		Uint    uint                   `json:"uint,omitempty"`
   225  		Float   float64                `json:"float,omitempty"`
   226  		Complex complex128             `json:"complex,omitempty"`
   227  		Bool    bool                   `json:"bool,omitempty"`
   228  		String  string                 `json:"string,omitempty"`
   229  		Slice   []int                  `json:"slice,omitempty"`
   230  		Array   [0]int                 `json:"array,omitempty"`
   231  		Map     map[string]interface{} `json:"map,omitempty"`
   232  	}
   233  	test2 := &testStruct2{}
   234  	om = FromInterface(test2)
   235  	xtesting.Equal(t, om.keys, []string{})
   236  	xtesting.Equal(t, om.Values(), []interface{}{})
   237  }