mirror of
https://github.com/luxfi/node.git
synced 2026-07-27 03:39:39 +00:00
BiMap is generic over comparable K,V — bolting on a json/v2 Marshaler/Unmarshaler bound the type to text I/O it has no business caring about. Removed the marshaler interface and the dependent tests; no consumer of BiMap in the node module relied on JSON. Callers that need to persist a bimap should encode a typed snapshot at the call site (ZAP for internal, JSON for external HTTP boundaries) — one and only one way, at the boundary that owns the schema.
343 lines
6.8 KiB
Go
343 lines
6.8 KiB
Go
// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package bimap
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestBiMapPut(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
state *BiMap[int, int]
|
|
key int
|
|
value int
|
|
expectedRemoved []Entry[int, int]
|
|
expectedState *BiMap[int, int]
|
|
}{
|
|
{
|
|
name: "none removed",
|
|
state: New[int, int](),
|
|
key: 1,
|
|
value: 2,
|
|
expectedRemoved: nil,
|
|
expectedState: &BiMap[int, int]{
|
|
keyToValue: map[int]int{
|
|
1: 2,
|
|
},
|
|
valueToKey: map[int]int{
|
|
2: 1,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "key removed",
|
|
state: &BiMap[int, int]{
|
|
keyToValue: map[int]int{
|
|
1: 2,
|
|
},
|
|
valueToKey: map[int]int{
|
|
2: 1,
|
|
},
|
|
},
|
|
key: 1,
|
|
value: 3,
|
|
expectedRemoved: []Entry[int, int]{
|
|
{
|
|
Key: 1,
|
|
Value: 2,
|
|
},
|
|
},
|
|
expectedState: &BiMap[int, int]{
|
|
keyToValue: map[int]int{
|
|
1: 3,
|
|
},
|
|
valueToKey: map[int]int{
|
|
3: 1,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "value removed",
|
|
state: &BiMap[int, int]{
|
|
keyToValue: map[int]int{
|
|
1: 2,
|
|
},
|
|
valueToKey: map[int]int{
|
|
2: 1,
|
|
},
|
|
},
|
|
key: 3,
|
|
value: 2,
|
|
expectedRemoved: []Entry[int, int]{
|
|
{
|
|
Key: 1,
|
|
Value: 2,
|
|
},
|
|
},
|
|
expectedState: &BiMap[int, int]{
|
|
keyToValue: map[int]int{
|
|
3: 2,
|
|
},
|
|
valueToKey: map[int]int{
|
|
2: 3,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "key and value removed",
|
|
state: &BiMap[int, int]{
|
|
keyToValue: map[int]int{
|
|
1: 2,
|
|
3: 4,
|
|
},
|
|
valueToKey: map[int]int{
|
|
2: 1,
|
|
4: 3,
|
|
},
|
|
},
|
|
key: 1,
|
|
value: 4,
|
|
expectedRemoved: []Entry[int, int]{
|
|
{
|
|
Key: 1,
|
|
Value: 2,
|
|
},
|
|
{
|
|
Key: 3,
|
|
Value: 4,
|
|
},
|
|
},
|
|
expectedState: &BiMap[int, int]{
|
|
keyToValue: map[int]int{
|
|
1: 4,
|
|
},
|
|
valueToKey: map[int]int{
|
|
4: 1,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
require := require.New(t)
|
|
|
|
removed := test.state.Put(test.key, test.value)
|
|
require.Equal(test.expectedRemoved, removed)
|
|
require.Equal(test.expectedState, test.state)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestBiMapHasValueAndGetKey(t *testing.T) {
|
|
m := New[int, int]()
|
|
require.Empty(t, m.Put(1, 2))
|
|
|
|
tests := []struct {
|
|
name string
|
|
value int
|
|
expectedKey int
|
|
expectedExists bool
|
|
}{
|
|
{
|
|
name: "fetch unknown",
|
|
value: 3,
|
|
expectedKey: 0,
|
|
expectedExists: false,
|
|
},
|
|
{
|
|
name: "fetch known value",
|
|
value: 2,
|
|
expectedKey: 1,
|
|
expectedExists: true,
|
|
},
|
|
{
|
|
name: "fetch known key",
|
|
value: 1,
|
|
expectedKey: 0,
|
|
expectedExists: false,
|
|
},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
require := require.New(t)
|
|
|
|
exists := m.HasValue(test.value)
|
|
require.Equal(test.expectedExists, exists)
|
|
|
|
key, exists := m.GetKey(test.value)
|
|
require.Equal(test.expectedKey, key)
|
|
require.Equal(test.expectedExists, exists)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestBiMapHasKeyAndGetValue(t *testing.T) {
|
|
m := New[int, int]()
|
|
require.Empty(t, m.Put(1, 2))
|
|
|
|
tests := []struct {
|
|
name string
|
|
key int
|
|
expectedValue int
|
|
expectedExists bool
|
|
}{
|
|
{
|
|
name: "fetch unknown",
|
|
key: 3,
|
|
expectedValue: 0,
|
|
expectedExists: false,
|
|
},
|
|
{
|
|
name: "fetch known key",
|
|
key: 1,
|
|
expectedValue: 2,
|
|
expectedExists: true,
|
|
},
|
|
{
|
|
name: "fetch known value",
|
|
key: 2,
|
|
expectedValue: 0,
|
|
expectedExists: false,
|
|
},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
require := require.New(t)
|
|
|
|
exists := m.HasKey(test.key)
|
|
require.Equal(test.expectedExists, exists)
|
|
|
|
value, exists := m.GetValue(test.key)
|
|
require.Equal(test.expectedValue, value)
|
|
require.Equal(test.expectedExists, exists)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestBiMapDeleteKey(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
state *BiMap[int, int]
|
|
key int
|
|
expectedValue int
|
|
expectedRemoved bool
|
|
expectedState *BiMap[int, int]
|
|
}{
|
|
{
|
|
name: "none removed",
|
|
state: New[int, int](),
|
|
key: 1,
|
|
expectedValue: 0,
|
|
expectedRemoved: false,
|
|
expectedState: New[int, int](),
|
|
},
|
|
{
|
|
name: "key removed",
|
|
state: &BiMap[int, int]{
|
|
keyToValue: map[int]int{
|
|
1: 2,
|
|
},
|
|
valueToKey: map[int]int{
|
|
2: 1,
|
|
},
|
|
},
|
|
key: 1,
|
|
expectedValue: 2,
|
|
expectedRemoved: true,
|
|
expectedState: New[int, int](),
|
|
},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
require := require.New(t)
|
|
|
|
value, removed := test.state.DeleteKey(test.key)
|
|
require.Equal(test.expectedValue, value)
|
|
require.Equal(test.expectedRemoved, removed)
|
|
require.Equal(test.expectedState, test.state)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestBiMapDeleteValue(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
state *BiMap[int, int]
|
|
value int
|
|
expectedKey int
|
|
expectedRemoved bool
|
|
expectedState *BiMap[int, int]
|
|
}{
|
|
{
|
|
name: "none removed",
|
|
state: New[int, int](),
|
|
value: 1,
|
|
expectedKey: 0,
|
|
expectedRemoved: false,
|
|
expectedState: New[int, int](),
|
|
},
|
|
{
|
|
name: "key removed",
|
|
state: &BiMap[int, int]{
|
|
keyToValue: map[int]int{
|
|
1: 2,
|
|
},
|
|
valueToKey: map[int]int{
|
|
2: 1,
|
|
},
|
|
},
|
|
value: 2,
|
|
expectedKey: 1,
|
|
expectedRemoved: true,
|
|
expectedState: New[int, int](),
|
|
},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
require := require.New(t)
|
|
|
|
key, removed := test.state.DeleteValue(test.value)
|
|
require.Equal(test.expectedKey, key)
|
|
require.Equal(test.expectedRemoved, removed)
|
|
require.Equal(test.expectedState, test.state)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestBiMapLenAndLists(t *testing.T) {
|
|
require := require.New(t)
|
|
|
|
m := New[int, int]()
|
|
require.Zero(m.Len())
|
|
require.Empty(m.Keys())
|
|
require.Empty(m.Values())
|
|
|
|
m.Put(1, 2)
|
|
require.Equal(1, m.Len())
|
|
require.ElementsMatch([]int{1}, m.Keys())
|
|
require.ElementsMatch([]int{2}, m.Values())
|
|
|
|
m.Put(2, 3)
|
|
require.Equal(2, m.Len())
|
|
require.ElementsMatch([]int{1, 2}, m.Keys())
|
|
require.ElementsMatch([]int{2, 3}, m.Values())
|
|
|
|
m.Put(1, 3)
|
|
require.Equal(1, m.Len())
|
|
require.ElementsMatch([]int{1}, m.Keys())
|
|
require.ElementsMatch([]int{3}, m.Values())
|
|
|
|
m.DeleteKey(1)
|
|
require.Zero(m.Len())
|
|
require.Empty(m.Keys())
|
|
require.Empty(m.Values())
|
|
}
|
|
|
|
// JSON marshal/unmarshal tests removed: BiMap no longer carries a
|
|
// generic codec. The internal Lux wire format is ZAP, which is element-
|
|
// typed; persist a typed snapshot at the call site if needed.
|