You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
552 lines
12 KiB
552 lines
12 KiB
|
1 year ago
|
package cval
|
||
|
|
|
||
|
|
import (
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
// TestMakeCValue verifica la función MakeCValue
|
||
|
|
func TestMakeCValue(t *testing.T) {
|
||
|
|
tests := []struct {
|
||
|
|
name string
|
||
|
|
ctyp CType
|
||
|
|
expected CValueSpec
|
||
|
|
}{
|
||
|
|
{
|
||
|
|
name: "ValidTypeString",
|
||
|
|
ctyp: TypeString,
|
||
|
|
expected: CValueSpec(TypeString),
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "ValidTypeInt",
|
||
|
|
ctyp: TypeInt,
|
||
|
|
expected: CValueSpec(TypeInt),
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "InvalidType",
|
||
|
|
ctyp: TypeInvalid,
|
||
|
|
expected: CValueSpec(TypeInvalid),
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "OutOfRangeType",
|
||
|
|
ctyp: TypeError + 1,
|
||
|
|
expected: CValueSpec(TypeError+1) & valueTypeMask,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "AggregateTypeAsInput",
|
||
|
|
ctyp: TypeDefer,
|
||
|
|
expected: CValueSpec(0),
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, tt := range tests {
|
||
|
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
|
result := MakeCValue(tt.ctyp)
|
||
|
|
if result != tt.expected {
|
||
|
|
t.Errorf("MakeCValue(%v): expected %v (0x%04x), got %v (0x%04x)", tt.ctyp, tt.expected, tt.expected, result, result)
|
||
|
|
}
|
||
|
|
if result&0xC000 != 0 {
|
||
|
|
t.Errorf("MakeCValue(%v): bits 14-15 should be 0, got %v (0x%04x)", tt.ctyp, result, result)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestMakeCList verifica la función MakeCList
|
||
|
|
func TestMakeCList(t *testing.T) {
|
||
|
|
tests := []struct {
|
||
|
|
name string
|
||
|
|
ctyp CType
|
||
|
|
expected CValueSpec
|
||
|
|
}{
|
||
|
|
{
|
||
|
|
name: "ValidTypeString",
|
||
|
|
ctyp: TypeString,
|
||
|
|
expected: CValueSpec(TypeString) | compoundList,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "ValidTypeInt",
|
||
|
|
ctyp: TypeInt,
|
||
|
|
expected: CValueSpec(TypeInt) | compoundList,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "InvalidType",
|
||
|
|
ctyp: TypeInvalid,
|
||
|
|
expected: CValueSpec(TypeInvalid) | compoundList,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "AggregateTypeAsInput",
|
||
|
|
ctyp: TypeList,
|
||
|
|
expected: CValueSpec(0) | compoundList,
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, tt := range tests {
|
||
|
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
|
result := MakeCList(tt.ctyp)
|
||
|
|
if result != tt.expected {
|
||
|
|
t.Errorf("MakeCList(%v): expected %v (0x%04x), got %v (0x%04x)", tt.ctyp, tt.expected, tt.expected, result, result)
|
||
|
|
}
|
||
|
|
if result&0xC000 != 0 {
|
||
|
|
t.Errorf("MakeCList(%v): bits 14-15 should be 0, got %v (0x%04x)", tt.ctyp, result, result)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestMakeCMap verifica la función MakeCMap
|
||
|
|
func TestMakeCMap(t *testing.T) {
|
||
|
|
tests := []struct {
|
||
|
|
name string
|
||
|
|
ctyp CType
|
||
|
|
ktyp CType
|
||
|
|
expected CValueSpec
|
||
|
|
}{
|
||
|
|
{
|
||
|
|
name: "ValidStringInt",
|
||
|
|
ctyp: TypeString,
|
||
|
|
ktyp: TypeInt,
|
||
|
|
expected: CValueSpec(TypeString) | CValueSpec(TypeInt<<keyTypeShift) | compoundMap,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "InvalidValueType",
|
||
|
|
ctyp: TypeInvalid,
|
||
|
|
ktyp: TypeInt,
|
||
|
|
expected: CValueSpec(TypeInvalid) | CValueSpec(TypeInt<<keyTypeShift) | compoundMap,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "InvalidKeyType",
|
||
|
|
ctyp: TypeString,
|
||
|
|
ktyp: TypeInvalid,
|
||
|
|
expected: CValueSpec(TypeString) | CValueSpec(TypeInvalid<<keyTypeShift) | compoundMap,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "AggregateTypeAsValue",
|
||
|
|
ctyp: TypeMap,
|
||
|
|
ktyp: TypeInt,
|
||
|
|
expected: CValueSpec(0) | CValueSpec(TypeInt<<keyTypeShift) | compoundMap,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "AggregateTypeAsKey",
|
||
|
|
ctyp: TypeString,
|
||
|
|
ktyp: TypeMap,
|
||
|
|
expected: CValueSpec(TypeString) | CValueSpec(0<<keyTypeShift) | compoundMap,
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, tt := range tests {
|
||
|
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
|
result := MakeCMap(tt.ctyp, tt.ktyp)
|
||
|
|
if result != tt.expected {
|
||
|
|
t.Errorf("MakeCMap(%v, %v): expected %v (0x%04x), got %v (0x%04x)", tt.ctyp, tt.ktyp, tt.expected, tt.expected, result, result)
|
||
|
|
}
|
||
|
|
if result&0xC000 != 0 {
|
||
|
|
t.Errorf("MakeCMap(%v, %v): bits 14-15 should be 0, got %v (0x%04x)", tt.ctyp, tt.ktyp, result, result)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestMakeCDeferred verifica la función MakeCDeferred
|
||
|
|
func TestMakeCDeferred(t *testing.T) {
|
||
|
|
tests := []struct {
|
||
|
|
name string
|
||
|
|
ctyp CType
|
||
|
|
expected CValueSpec
|
||
|
|
}{
|
||
|
|
{
|
||
|
|
name: "ValidTypeString",
|
||
|
|
ctyp: TypeString,
|
||
|
|
expected: CValueSpec(TypeString) | compoundDefer,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "ValidTypeInt",
|
||
|
|
ctyp: TypeInt,
|
||
|
|
expected: CValueSpec(TypeInt) | compoundDefer,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "InvalidType",
|
||
|
|
ctyp: TypeInvalid,
|
||
|
|
expected: CValueSpec(TypeInvalid) | compoundDefer,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "AggregateTypeAsInput",
|
||
|
|
ctyp: TypeDefer,
|
||
|
|
expected: CValueSpec(0) | compoundDefer,
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, tt := range tests {
|
||
|
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
|
result := MakeCDeferred(tt.ctyp)
|
||
|
|
if result != tt.expected {
|
||
|
|
t.Errorf("MakeCDeferred(%v): expected %v (0x%04x), got %v (0x%04x)", tt.ctyp, tt.expected, tt.expected, result, result)
|
||
|
|
}
|
||
|
|
if result&0xC000 != 0 {
|
||
|
|
t.Errorf("MakeCDeferred(%v): bits 14-15 should be 0, got %v (0x%04x)", tt.ctyp, result, result)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestValueType verifica el método ValueType
|
||
|
|
func TestValueType(t *testing.T) {
|
||
|
|
tests := []struct {
|
||
|
|
name string
|
||
|
|
input CValueSpec
|
||
|
|
expected CType
|
||
|
|
}{
|
||
|
|
{
|
||
|
|
name: "SimpleString",
|
||
|
|
input: MakeCValue(TypeString),
|
||
|
|
expected: TypeString,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "ListInt",
|
||
|
|
input: MakeCList(TypeInt),
|
||
|
|
expected: TypeInt,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "MapStringInt",
|
||
|
|
input: MakeCMap(TypeString, TypeInt),
|
||
|
|
expected: TypeString,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "DeferredInvalid",
|
||
|
|
input: MakeCDeferred(TypeInvalid),
|
||
|
|
expected: TypeInvalid,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "InvalidBits",
|
||
|
|
input: CValueSpec(0xFFFF),
|
||
|
|
expected: 0x003F, // Máscara 0x003F limita a 6 bits (63)
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, tt := range tests {
|
||
|
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
|
result := tt.input.ValueType()
|
||
|
|
if result != tt.expected {
|
||
|
|
t.Errorf("ValueType(): expected %v (0x%04x), got %v (0x%04x)", tt.expected, tt.expected, result, result)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestKeyType verifica el método KeyType
|
||
|
|
func TestKeyType(t *testing.T) {
|
||
|
|
tests := []struct {
|
||
|
|
name string
|
||
|
|
input CValueSpec
|
||
|
|
expected CType
|
||
|
|
}{
|
||
|
|
{
|
||
|
|
name: "SimpleString",
|
||
|
|
input: MakeCValue(TypeString),
|
||
|
|
expected: 0,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "MapStringInt",
|
||
|
|
input: MakeCMap(TypeString, TypeInt),
|
||
|
|
expected: TypeInt,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "ListInt",
|
||
|
|
input: MakeCList(TypeInt),
|
||
|
|
expected: 0,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "InvalidBits",
|
||
|
|
input: CValueSpec(0xFFFF),
|
||
|
|
expected: 0x3F,
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, tt := range tests {
|
||
|
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
|
result := tt.input.KeyType()
|
||
|
|
if result != tt.expected {
|
||
|
|
t.Errorf("KeyType(): expected %v (0x%04x), got %v (0x%04x)", tt.expected, tt.expected, result, result)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestCompoundType verifica el método CompoundType
|
||
|
|
func TestCompoundType(t *testing.T) {
|
||
|
|
tests := []struct {
|
||
|
|
name string
|
||
|
|
input CValueSpec
|
||
|
|
expected CType
|
||
|
|
}{
|
||
|
|
{
|
||
|
|
name: "SimpleString",
|
||
|
|
input: MakeCValue(TypeString),
|
||
|
|
expected: TypeSimple,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "ListInt",
|
||
|
|
input: MakeCList(TypeInt),
|
||
|
|
expected: TypeList,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "MapStringInt",
|
||
|
|
input: MakeCMap(TypeString, TypeInt),
|
||
|
|
expected: TypeMap,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "DeferredString",
|
||
|
|
input: MakeCDeferred(TypeString),
|
||
|
|
expected: TypeDefer,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "InvalidBits",
|
||
|
|
input: CValueSpec(0xFFFF),
|
||
|
|
expected: TypeMap, // Máscara 0x00C0 limita a bits 6-7 (0x00C0 = TypeMap)
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, tt := range tests {
|
||
|
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
|
result := tt.input.CompoundType()
|
||
|
|
if result != tt.expected {
|
||
|
|
t.Errorf("CompoundType(): expected %v (0x%04x), got %v (0x%04x)", tt.expected, tt.expected, result, result)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestIsDefer verifica el método IsDefer
|
||
|
|
func TestIsDefer(t *testing.T) {
|
||
|
|
tests := []struct {
|
||
|
|
name string
|
||
|
|
input CValueSpec
|
||
|
|
expected bool
|
||
|
|
}{
|
||
|
|
{
|
||
|
|
name: "DeferredString",
|
||
|
|
input: MakeCDeferred(TypeString),
|
||
|
|
expected: true,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "SimpleString",
|
||
|
|
input: MakeCValue(TypeString),
|
||
|
|
expected: false,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "ListInt",
|
||
|
|
input: MakeCList(TypeInt),
|
||
|
|
expected: false,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "MapStringInt",
|
||
|
|
input: MakeCMap(TypeString, TypeInt),
|
||
|
|
expected: false,
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, tt := range tests {
|
||
|
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
|
result := tt.input.IsDefer()
|
||
|
|
if result != tt.expected {
|
||
|
|
t.Errorf("IsDefer(): expected %v, got %v", tt.expected, result)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestIsMap verifica el método IsMap
|
||
|
|
func TestIsMap(t *testing.T) {
|
||
|
|
tests := []struct {
|
||
|
|
name string
|
||
|
|
input CValueSpec
|
||
|
|
expected bool
|
||
|
|
}{
|
||
|
|
{
|
||
|
|
name: "MapStringInt",
|
||
|
|
input: MakeCMap(TypeString, TypeInt),
|
||
|
|
expected: true,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "SimpleString",
|
||
|
|
input: MakeCValue(TypeString),
|
||
|
|
expected: false,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "ListInt",
|
||
|
|
input: MakeCList(TypeInt),
|
||
|
|
expected: false,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "DeferredString",
|
||
|
|
input: MakeCDeferred(TypeString),
|
||
|
|
expected: false,
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, tt := range tests {
|
||
|
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
|
result := tt.input.IsMap()
|
||
|
|
if result != tt.expected {
|
||
|
|
t.Errorf("IsMap(): expected %v, got %v", tt.expected, result)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestIsList verifica el método IsList
|
||
|
|
func TestIsList(t *testing.T) {
|
||
|
|
tests := []struct {
|
||
|
|
name string
|
||
|
|
input CValueSpec
|
||
|
|
expected bool
|
||
|
|
}{
|
||
|
|
{
|
||
|
|
name: "ListInt",
|
||
|
|
input: MakeCList(TypeInt),
|
||
|
|
expected: true,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "SimpleString",
|
||
|
|
input: MakeCValue(TypeString),
|
||
|
|
expected: false,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "MapStringInt",
|
||
|
|
input: MakeCMap(TypeString, TypeInt),
|
||
|
|
expected: false,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "DeferredString",
|
||
|
|
input: MakeCDeferred(TypeString),
|
||
|
|
expected: false,
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, tt := range tests {
|
||
|
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
|
result := tt.input.IsList()
|
||
|
|
if result != tt.expected {
|
||
|
|
t.Errorf("IsList(): expected %v, got %v", tt.expected, result)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestIsValid verifica el método IsValid
|
||
|
|
func TestIsValid(t *testing.T) {
|
||
|
|
tests := []struct {
|
||
|
|
name string
|
||
|
|
input CValueSpec
|
||
|
|
expected bool
|
||
|
|
}{
|
||
|
|
{
|
||
|
|
name: "SimpleString",
|
||
|
|
input: MakeCValue(TypeString),
|
||
|
|
expected: true,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "ListInt",
|
||
|
|
input: MakeCList(TypeInt),
|
||
|
|
expected: true,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "MapStringInt",
|
||
|
|
input: MakeCMap(TypeString, TypeInt),
|
||
|
|
expected: true,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "DeferredString",
|
||
|
|
input: MakeCDeferred(TypeString),
|
||
|
|
expected: true,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "InvalidValueType",
|
||
|
|
input: MakeCValue(TypeInvalid),
|
||
|
|
expected: false,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "InvalidList",
|
||
|
|
input: MakeCList(TypeInvalid),
|
||
|
|
expected: false,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "InvalidMapValueType",
|
||
|
|
input: MakeCMap(TypeInvalid, TypeInt),
|
||
|
|
expected: false,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "InvalidMapKeyType",
|
||
|
|
input: MakeCMap(TypeString, TypeInvalid),
|
||
|
|
expected: false,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "OutOfRangeValueType",
|
||
|
|
input: CValueSpec(maxValueType << valueTypeShift) | compoundNone,
|
||
|
|
expected: false,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "OutOfRangeAggType",
|
||
|
|
input: CValueSpec(TypeString) | CValueSpec(4<<compoundTypeShift),
|
||
|
|
expected: false,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "OutOfRangeKeyType",
|
||
|
|
input: MakeCMap(TypeString, maxValueType),
|
||
|
|
expected: false,
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, tt := range tests {
|
||
|
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
|
result := tt.input.IsValid()
|
||
|
|
if result != tt.expected {
|
||
|
|
t.Errorf("IsValid(): expected %v, got %v for input %v (0x%04x)", tt.expected, result, tt.input, tt.input)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestSanitize verifica el método Sanitize
|
||
|
|
func TestSanitize(t *testing.T) {
|
||
|
|
tests := []struct {
|
||
|
|
name string
|
||
|
|
input CValueSpec
|
||
|
|
expected CValueSpec
|
||
|
|
}{
|
||
|
|
{
|
||
|
|
name: "ValidSimpleString",
|
||
|
|
input: MakeCValue(TypeString),
|
||
|
|
expected: MakeCValue(TypeString),
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "ValidMapStringInt",
|
||
|
|
input: MakeCMap(TypeString, TypeInt),
|
||
|
|
expected: MakeCMap(TypeString, TypeInt),
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "InvalidValueType",
|
||
|
|
input: MakeCValue(TypeInvalid),
|
||
|
|
expected: CValueSpec(TypeInvalid),
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "InvalidMapKeyType",
|
||
|
|
input: MakeCMap(TypeString, TypeInvalid),
|
||
|
|
expected: CValueSpec(TypeInvalid),
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "OutOfRangeAggType",
|
||
|
|
input: CValueSpec(TypeString) | CValueSpec(4<<compoundTypeShift),
|
||
|
|
expected: CValueSpec(TypeInvalid),
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, tt := range tests {
|
||
|
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
|
result := tt.input.Sanitize()
|
||
|
|
if result != tt.expected {
|
||
|
|
t.Errorf("Sanitize(): expected %v (0x%04x), got %v (0x%04x) for input %v (0x%04x)",
|
||
|
|
tt.expected, tt.expected, result, result, tt.input, tt.input)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|