github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/linkedlist/list_test.go (about) 1 // Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. 2 3 package linkedlist_test 4 5 import ( 6 "github.com/TeaOSLab/EdgeNode/internal/utils/linkedlist" 7 "github.com/iwind/TeaGo/types" 8 "runtime" 9 "strconv" 10 "testing" 11 ) 12 13 func TestNewList_Memory(t *testing.T) { 14 var stat1 = &runtime.MemStats{} 15 runtime.ReadMemStats(stat1) 16 17 var list = linkedlist.NewList[int]() 18 for i := 0; i < 1_000_000; i++ { 19 var item = &linkedlist.Item[int]{} 20 list.Push(item) 21 } 22 23 runtime.GC() 24 25 var stat2 = &runtime.MemStats{} 26 runtime.ReadMemStats(stat2) 27 t.Log((stat2.HeapInuse-stat1.HeapInuse)>>20, "MB") 28 t.Log(list.Len()) 29 30 var count = 0 31 list.Range(func(item *linkedlist.Item[int]) (goNext bool) { 32 count++ 33 return true 34 }) 35 t.Log(count) 36 } 37 38 func TestNewList_Memory_String(t *testing.T) { 39 var stat1 = &runtime.MemStats{} 40 runtime.ReadMemStats(stat1) 41 42 var list = linkedlist.NewList[string]() 43 for i := 0; i < 1_000_000; i++ { 44 var item = &linkedlist.Item[string]{} 45 item.Value = strconv.Itoa(i) 46 list.Push(item) 47 } 48 49 runtime.GC() 50 51 var stat2 = &runtime.MemStats{} 52 runtime.ReadMemStats(stat2) 53 t.Log((stat2.HeapInuse-stat1.HeapInuse)>>20, "MB") 54 t.Log(list.Len()) 55 } 56 57 func TestList_Push(t *testing.T) { 58 var list = linkedlist.NewList[int]() 59 list.Push(linkedlist.NewItem(1)) 60 list.Push(linkedlist.NewItem(2)) 61 62 var item3 = linkedlist.NewItem(3) 63 list.Push(item3) 64 65 var item4 = linkedlist.NewItem(4) 66 list.Push(item4) 67 list.Range(func(item *linkedlist.Item[int]) (goNext bool) { 68 t.Log(item.Value) 69 return true 70 }) 71 72 t.Log("=== after push 3 ===") 73 list.Push(item3) 74 list.Range(func(item *linkedlist.Item[int]) (goNext bool) { 75 t.Log(item.Value) 76 return true 77 }) 78 79 t.Log("=== after push 4 ===") 80 list.Push(item4) 81 list.Push(item3) 82 list.Push(item3) 83 list.Push(item3) 84 list.Push(item4) 85 list.Push(item4) 86 list.Range(func(item *linkedlist.Item[int]) (goNext bool) { 87 t.Log(item.Value) 88 return true 89 }) 90 91 t.Log("=== after remove 3 ===") 92 list.Remove(item3) 93 list.Range(func(item *linkedlist.Item[int]) (goNext bool) { 94 t.Log(item.Value) 95 return true 96 }) 97 } 98 99 func TestList_Shift(t *testing.T) { 100 var list = linkedlist.NewList[int]() 101 list.Push(linkedlist.NewItem(1)) 102 list.Push(linkedlist.NewItem(2)) 103 list.Push(linkedlist.NewItem(3)) 104 list.Push(linkedlist.NewItem(4)) 105 106 for i := 0; i < 10; i++ { 107 t.Log("=== before shift " + types.String(i) + " ===") 108 list.Range(func(item *linkedlist.Item[int]) (goNext bool) { 109 t.Log(item.Value) 110 return true 111 }) 112 113 t.Logf("shift: %+v", list.Shift()) 114 115 t.Log("=== after shift " + types.String(i) + " ===") 116 list.Range(func(item *linkedlist.Item[int]) (goNext bool) { 117 t.Log(item.Value) 118 return true 119 }) 120 } 121 } 122 123 func TestList_RangeReverse(t *testing.T) { 124 var list = linkedlist.NewList[int]() 125 list.Push(linkedlist.NewItem(1)) 126 list.Push(linkedlist.NewItem(2)) 127 128 var item3 = linkedlist.NewItem(3) 129 list.Push(item3) 130 131 list.Push(linkedlist.NewItem(4)) 132 133 //list.Push(item3) 134 //list.Remove(item3) 135 list.RangeReverse(func(item *linkedlist.Item[int]) (goNext bool) { 136 t.Log(item.Value) 137 return true 138 }) 139 } 140 141 func BenchmarkList_Add(b *testing.B) { 142 var list = linkedlist.NewList[int]() 143 for i := 0; i < b.N; i++ { 144 var item = &linkedlist.Item[int]{} 145 list.Push(item) 146 } 147 }