Skip to content

Commit

Permalink
Add interpreter test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdrag00nv2 committed Jun 29, 2023
1 parent 1ded3d2 commit 12eb1cc
Show file tree
Hide file tree
Showing 4 changed files with 505 additions and 34 deletions.
24 changes: 14 additions & 10 deletions runtime/interpreter/value_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,15 @@ func NewInclusiveRangeValue(
panic(errors.NewUnreachableError())
}

step := getValueForIntegerType(1, rangeType.ElementType)
step := GetValueForIntegerType(1, rangeType.ElementType)
if startComparable.Greater(interpreter, endInclusiveComparable, locationRange) {
// TODO: Disallow unsigned integers to have a negative step.
elemSemaTy := interpreter.MustConvertStaticToSemaType(rangeType.ElementType)
if _, ok := sema.AllUnsignedIntegerTypesSet[elemSemaTy]; ok {
panic(InclusiveRangeConstructionError{
LocationRange: locationRange,
Message: fmt.Sprintf("step value cannot be negative for unsigned integer type %s", elemSemaTy),
})
}

negatedStep, ok := step.Negate(interpreter, locationRange).(IntegerValue)
if !ok {
Expand All @@ -65,10 +71,8 @@ func NewInclusiveRangeValueWithStep(
rangeType InclusiveRangeStaticType,
) *CompositeValue {

// TODO: Validate that if start > end, then the type is signed integer.

// Validate that the step is non-zero.
if step.Equal(interpreter, locationRange, getValueForIntegerType(0, rangeType.ElementType)) {
if step.Equal(interpreter, locationRange, GetValueForIntegerType(0, rangeType.ElementType)) {
panic(InclusiveRangeConstructionError{
LocationRange: locationRange,
Message: "step value cannot be zero",
Expand All @@ -79,8 +83,8 @@ func NewInclusiveRangeValueWithStep(
// If start < end, step must be > 0
// If start > end, step must be < 0
// If start == end, step doesn't matter.
if (start.Less(interpreter, end, locationRange) && step.Less(interpreter, getValueForIntegerType(0, rangeType.ElementType), locationRange)) ||
(start.Greater(interpreter, end, locationRange) && step.Greater(interpreter, getValueForIntegerType(0, rangeType.ElementType), locationRange)) {
if (start.Less(interpreter, end, locationRange) && step.Less(interpreter, GetValueForIntegerType(0, rangeType.ElementType), locationRange)) ||
(start.Greater(interpreter, end, locationRange) && step.Greater(interpreter, GetValueForIntegerType(0, rangeType.ElementType), locationRange)) {

panic(InclusiveRangeConstructionError{
LocationRange: locationRange,
Expand Down Expand Up @@ -173,7 +177,7 @@ func rangeContains(
panic(errors.NewUnreachableError())
}

result = diff.Mod(interpreter, step, locationRange).Equal(interpreter, locationRange, getValueForIntegerType(0, rangeType.ElementType))
result = diff.Mod(interpreter, step, locationRange).Equal(interpreter, locationRange, GetValueForIntegerType(0, rangeType.ElementType))
}
}

Expand All @@ -182,7 +186,7 @@ func rangeContains(

// Get the provided int64 value in the required staticType.
// Note: Assumes that the provided value fits within the constraints of the staticType.
func getValueForIntegerType(value int64, staticType StaticType) IntegerValue {
func GetValueForIntegerType(value int64, staticType StaticType) IntegerValue {
switch staticType {
case PrimitiveStaticTypeInt:
return NewUnmeteredIntValueFromInt64(value)
Expand Down Expand Up @@ -242,7 +246,7 @@ func getFieldAsIntegerValue(
rangeValue.GetField(
interpreter,
locationRange,
sema.InclusiveRangeTypeStartFieldName,
name,
),
)
}
Expand Down
7 changes: 7 additions & 0 deletions runtime/sema/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -3239,6 +3239,8 @@ var AllUnsignedIntegerTypes = []Type{
Word256Type,
}

var AllUnsignedIntegerTypesSet = make(map[Type]struct{})

var AllIntegerTypes = append(
append(
AllUnsignedIntegerTypes[:],
Expand Down Expand Up @@ -3386,6 +3388,11 @@ func init() {
)
}
}

// Populate AllUnsignedIntegerTypesSet
for _, ty := range AllUnsignedIntegerTypes {
AllUnsignedIntegerTypesSet[ty] = struct{}{}
}
}

func NumberConversionFunctionType(numberType Type) *FunctionType {
Expand Down
4 changes: 2 additions & 2 deletions runtime/tests/checker/range_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,14 @@ func TestInclusiveRangeConstructionValid(t *testing.T) {
}

func TestInclusiveRangeConstructionInvalid(t *testing.T) {
// t.Parallel()
t.Parallel()

baseValueActivation := sema.NewVariableActivation(sema.BaseValueActivation)
baseValueActivation.DeclareValue(stdlib.InclusiveRangeConstructorFunction)

runInvalidCase := func(t *testing.T, label, code string, expectedErrorTypes []interface{}) {
t.Run(label, func(t *testing.T) {
// t.Parallel()
t.Parallel()

_, err := ParseAndCheckWithOptions(t, code,
ParseAndCheckOptions{
Expand Down
Loading

0 comments on commit 12eb1cc

Please sign in to comment.