|
| 1 | +/* |
| 2 | + * Copyright 2024 CloudWeGo Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package reflect |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | +) |
| 24 | + |
| 25 | +func TestMapStructDesc_GetEmpty(t *testing.T) { |
| 26 | + m := newMapStructDesc() |
| 27 | + assert.Nil(t, m.Get(1)) |
| 28 | + assert.Nil(t, m.Get(0)) |
| 29 | + assert.Nil(t, m.Get(0xffff)) |
| 30 | +} |
| 31 | + |
| 32 | +func TestMapStructDesc_SetGet(t *testing.T) { |
| 33 | + m := newMapStructDesc() |
| 34 | + sd1 := &structDesc{} |
| 35 | + sd2 := &structDesc{} |
| 36 | + |
| 37 | + m.Set(1, sd1) |
| 38 | + assert.Equal(t, sd1, m.Get(1)) |
| 39 | + assert.Nil(t, m.Get(2)) |
| 40 | + |
| 41 | + m.Set(2, sd2) |
| 42 | + assert.Equal(t, sd1, m.Get(1)) |
| 43 | + assert.Equal(t, sd2, m.Get(2)) |
| 44 | +} |
| 45 | + |
| 46 | +func TestMapStructDesc_Update(t *testing.T) { |
| 47 | + m := newMapStructDesc() |
| 48 | + sd1 := &structDesc{} |
| 49 | + sd2 := &structDesc{} |
| 50 | + |
| 51 | + m.Set(1, sd1) |
| 52 | + assert.Equal(t, sd1, m.Get(1)) |
| 53 | + |
| 54 | + // update same key with new value |
| 55 | + m.Set(1, sd2) |
| 56 | + assert.Equal(t, sd2, m.Get(1)) |
| 57 | + |
| 58 | + // slot should have exactly 1 item, not 2 |
| 59 | + bk := uintptr(1) & mapStructDescBuckets |
| 60 | + slot := m.slots[bk].Load() |
| 61 | + assert.Equal(t, 1, len(*slot)) |
| 62 | +} |
| 63 | + |
| 64 | +func TestMapStructDesc_SetNoop(t *testing.T) { |
| 65 | + m := newMapStructDesc() |
| 66 | + sd := &structDesc{} |
| 67 | + |
| 68 | + m.Set(1, sd) |
| 69 | + // set same key+value again should be a noop |
| 70 | + m.Set(1, sd) |
| 71 | + |
| 72 | + bk := uintptr(1) & mapStructDescBuckets |
| 73 | + slot := m.slots[bk].Load() |
| 74 | + assert.Equal(t, 1, len(*slot)) |
| 75 | +} |
| 76 | + |
| 77 | +func TestMapStructDesc_HashCollision(t *testing.T) { |
| 78 | + m := newMapStructDesc() |
| 79 | + sd1 := &structDesc{} |
| 80 | + sd2 := &structDesc{} |
| 81 | + |
| 82 | + // keys that map to the same bucket |
| 83 | + k1 := uintptr(1) |
| 84 | + k2 := k1 + mapStructDescBuckets + 1 // same bucket as k1 |
| 85 | + |
| 86 | + m.Set(k1, sd1) |
| 87 | + m.Set(k2, sd2) |
| 88 | + |
| 89 | + assert.Equal(t, sd1, m.Get(k1)) |
| 90 | + assert.Equal(t, sd2, m.Get(k2)) |
| 91 | + |
| 92 | + // both in the same slot |
| 93 | + bk := k1 & mapStructDescBuckets |
| 94 | + slot := m.slots[bk].Load() |
| 95 | + assert.Equal(t, 2, len(*slot)) |
| 96 | +} |
| 97 | + |
| 98 | +func TestMapStructDesc_UpdateWithCollision(t *testing.T) { |
| 99 | + m := newMapStructDesc() |
| 100 | + sd1 := &structDesc{} |
| 101 | + sd2 := &structDesc{} |
| 102 | + sd3 := &structDesc{} |
| 103 | + |
| 104 | + k1 := uintptr(1) |
| 105 | + k2 := k1 + mapStructDescBuckets + 1 |
| 106 | + |
| 107 | + m.Set(k1, sd1) |
| 108 | + m.Set(k2, sd2) |
| 109 | + |
| 110 | + // update k1, should not affect k2 |
| 111 | + m.Set(k1, sd3) |
| 112 | + assert.Equal(t, sd3, m.Get(k1)) |
| 113 | + assert.Equal(t, sd2, m.Get(k2)) |
| 114 | + |
| 115 | + bk := k1 & mapStructDescBuckets |
| 116 | + slot := m.slots[bk].Load() |
| 117 | + assert.Equal(t, 2, len(*slot)) |
| 118 | +} |
0 commit comments