github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/catalog/node_test.go (about) 1 // Copyright 2021 Matrix Origin 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 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package catalog 16 17 import ( 18 "testing" 19 "time" 20 21 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common" 22 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/testutils" 23 "github.com/stretchr/testify/assert" 24 ) 25 26 type testNode struct { 27 val int 28 } 29 30 func newTestNode(val int) *testNode { 31 return &testNode{val: val} 32 } 33 func compareTestNode(n, on *testNode) int { 34 if n.val > on.val { 35 return 1 36 } else if n.val < on.val { 37 return -1 38 } 39 return 0 40 } 41 42 func TestDLNode(t *testing.T) { 43 defer testutils.AfterTest(t)() 44 link := common.NewGenericSortedDList(compareTestNode) 45 now := time.Now() 46 var node *common.GenericDLNode[*testNode] 47 nodeCnt := 10 48 for i := 0; i < nodeCnt; i++ { 49 n := link.Insert(newTestNode(i)) 50 if i == 5 { 51 node = n 52 } 53 } 54 t.Log(time.Since(now)) 55 cnt := 0 56 link.Loop(func(n *common.GenericDLNode[*testNode]) bool { 57 cnt++ 58 return true 59 }, true) 60 assert.Equal(t, nodeCnt, cnt) 61 assert.Equal(t, 5, node.GetPayload().val) 62 63 link.Delete(node) 64 cnt = 0 65 link.Loop(func(n *common.GenericDLNode[*testNode]) bool { 66 t.Logf("%d", node.GetPayload().val) 67 cnt++ 68 return true 69 }, true) 70 assert.Equal(t, nodeCnt-1, cnt) 71 }