| 5 | ) |
| 6 | |
| 7 | func TestThreadSafeMap(t *testing.T) { |
| 8 | m := NewThreadSafeMap[int, int]() |
| 9 | |
| 10 | m.Set(1, 1) |
| 11 | m.Set(2, 2) |
| 12 | m.Set(3, 3) |
| 13 | |
| 14 | if m.Len() != 3 { |
| 15 | t.Errorf("Expected length to be 3, got %d", m.Len()) |
| 16 | } |
| 17 | |
| 18 | if !m.Has(1) { |
| 19 | t.Errorf("Expected to have key 1") |
| 20 | } |
| 21 | |
| 22 | if m.Has(4) { |
| 23 | t.Errorf("Expected to not have key 4") |
| 24 | } |
| 25 | |
| 26 | if _, ok := m.Get(1); !ok { |
| 27 | t.Errorf("Expected to have key 1") |
| 28 | } |
| 29 | |
| 30 | if _, ok := m.Get(4); ok { |
| 31 | t.Errorf("Expected to not have key 4") |
| 32 | } |
| 33 | |
| 34 | m.Delete(1) |
| 35 | |
| 36 | if m.Has(1) { |
| 37 | t.Errorf("Expected to not have key 1") |
| 38 | } |
| 39 | |
| 40 | m.Clear() |
| 41 | |
| 42 | if m.Len() != 0 { |
| 43 | t.Errorf("Expected length to be 0, got %d", m.Len()) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func TestThreadSafeMapConcurrentReadWrite(t *testing.T) { |
| 48 | m := NewThreadSafeMap[int, int]() |