Skip to content

Commit

Permalink
add tests for fixed-sized array and fix return type of function
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdrag00nv2 committed Jul 16, 2023
1 parent bbb3bdd commit 344ce6a
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 95 deletions.
3 changes: 2 additions & 1 deletion runtime/interpreter/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -2434,7 +2434,7 @@ func (v *ArrayValue) GetMember(interpreter *Interpreter, locationRange LocationR
return NewHostFunctionValue(
interpreter,
sema.ArrayReverseFunctionType(
v.SemaType(interpreter).ElementType(false),
v.SemaType(interpreter),
),
func(invocation Invocation) Value {
return v.Reverse(
Expand Down Expand Up @@ -2940,6 +2940,7 @@ func (v *ArrayValue) Reverse(
atree.Address{},
false,
nil,
nil,
)
},
)
Expand Down
10 changes: 4 additions & 6 deletions runtime/sema/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -1908,7 +1908,7 @@ func getArrayMembers(arrayType ArrayType) map[string]MemberResolver {
memoryGauge,
arrayType,
identifier,
ArrayReverseFunctionType(elementType),
ArrayReverseFunctionType(arrayType),
arrayTypeReverseFunctionDocString,
)
},
Expand Down Expand Up @@ -2225,12 +2225,10 @@ func ArraySliceFunctionType(elementType Type) *FunctionType {
}
}

func ArrayReverseFunctionType(elementType Type) *FunctionType {
func ArrayReverseFunctionType(arrayType Type) *FunctionType {
return &FunctionType{
Parameters: []Parameter{},
ReturnTypeAnnotation: NewTypeAnnotation(&VariableSizedType{
Type: elementType,
}),
Parameters: []Parameter{},
ReturnTypeAnnotation: NewTypeAnnotation(arrayType),
}
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/tests/checker/arrays_dictionaries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,7 @@ func TestCheckArrayReverseInvalidArgs(t *testing.T) {

errs := RequireCheckerErrors(t, err, 1)

assert.IsType(t, &sema.ArgumentCountError{}, errs[0])
assert.IsType(t, &sema.ExcessiveArgumentsError{}, errs[0])
}

func TestCheckResourceArrayReverseInvalid(t *testing.T) {
Expand Down
233 changes: 146 additions & 87 deletions runtime/tests/interpreter/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10513,7 +10513,10 @@ func TestInterpretArrayReverse(t *testing.T) {
inter := parseCheckAndInterpret(t, `
let xs = [1, 2, 3, 100, 200]
let ys = [100, 467, 297, 23]
let xs_fixed: [Int; 5] = [1, 2, 3, 100, 200]
let ys_fixed: [Int; 4] = [100, 467, 297, 23]
let emptyVals: [Int] = []
let emptyVals_fixed: [Int; 0] = []
fun reversexs(): [Int] {
return xs.reverse()
Expand All @@ -10529,13 +10532,34 @@ func TestInterpretArrayReverse(t *testing.T) {
return ys
}
fun reversexs_fixed(): [Int; 5] {
return xs_fixed.reverse()
}
fun originalxs_fixed(): [Int; 5] {
return xs_fixed
}
fun reverseys_fixed(): [Int; 4] {
return ys_fixed.reverse()
}
fun originalys_fixed(): [Int; 4] {
return ys_fixed
}
fun reverseempty(): [Int] {
return emptyVals.reverse()
}
fun originalempty(): [Int] {
return emptyVals
}
fun reverseempty_fixed(): [Int; 0] {
return emptyVals_fixed.reverse()
}
fun originalempty_fixed(): [Int; 0] {
return emptyVals_fixed
}
pub struct TestStruct {
pub var test: Int
Expand All @@ -10545,6 +10569,7 @@ func TestInterpretArrayReverse(t *testing.T) {
}
let sa = [TestStruct(1), TestStruct(2), TestStruct(3)]
let sa_fixed: [TestStruct; 3] = [TestStruct(1), TestStruct(2), TestStruct(3)]
fun reversesa(): [Int] {
let sa_rev = sa.reverse()
Expand All @@ -10559,7 +10584,26 @@ func TestInterpretArrayReverse(t *testing.T) {
fun originalsa(): [Int] {
let res: [Int] = [];
for s in sa {
res.append(s.test)
res.append(s.test)
}
return res
}
fun reversesa_fixed(): [Int] {
let sa_rev = sa_fixed.reverse()
let res: [Int] = [];
for s in sa_rev {
res.append(s.test)
}
return res
}
fun originalsa_fixed(): [Int] {
let res: [Int] = [];
for s in sa_fixed {
res.append(s.test)
}
return res
Expand Down Expand Up @@ -10589,97 +10633,112 @@ func TestInterpretArrayReverse(t *testing.T) {
)
}

runValidCase(t, "reverseempty", "originalempty",
interpreter.NewArrayValue(
inter,
interpreter.EmptyLocationRange,
interpreter.VariableSizedStaticType{
Type: interpreter.PrimitiveStaticTypeInt,
},
common.ZeroAddress,
), interpreter.NewArrayValue(
inter,
interpreter.EmptyLocationRange,
interpreter.VariableSizedStaticType{
Type: interpreter.PrimitiveStaticTypeInt,
},
common.ZeroAddress,
))
for _, suffix := range []string{"_fixed", ""} {
fixed := suffix == "_fixed"

runValidCase(t, "reversexs", "originalxs",
interpreter.NewArrayValue(
inter,
interpreter.EmptyLocationRange,
interpreter.VariableSizedStaticType{
var arrayType interpreter.ArrayStaticType
if fixed {
arrayType = &interpreter.ConstantSizedStaticType{
Type: interpreter.PrimitiveStaticTypeInt,
},
common.ZeroAddress,
interpreter.NewUnmeteredIntValueFromInt64(200),
interpreter.NewUnmeteredIntValueFromInt64(100),
interpreter.NewUnmeteredIntValueFromInt64(3),
interpreter.NewUnmeteredIntValueFromInt64(2),
interpreter.NewUnmeteredIntValueFromInt64(1),
), interpreter.NewArrayValue(
inter,
interpreter.EmptyLocationRange,
interpreter.VariableSizedStaticType{
}
} else {
arrayType = &interpreter.VariableSizedStaticType{
Type: interpreter.PrimitiveStaticTypeInt,
},
common.ZeroAddress,
interpreter.NewUnmeteredIntValueFromInt64(1),
interpreter.NewUnmeteredIntValueFromInt64(2),
interpreter.NewUnmeteredIntValueFromInt64(3),
interpreter.NewUnmeteredIntValueFromInt64(100),
interpreter.NewUnmeteredIntValueFromInt64(200),
))
}
}

runValidCase(t, "reverseys", "originalys",
interpreter.NewArrayValue(
inter,
interpreter.EmptyLocationRange,
interpreter.VariableSizedStaticType{
Type: interpreter.PrimitiveStaticTypeInt,
},
common.ZeroAddress,
interpreter.NewUnmeteredIntValueFromInt64(23),
interpreter.NewUnmeteredIntValueFromInt64(297),
interpreter.NewUnmeteredIntValueFromInt64(467),
interpreter.NewUnmeteredIntValueFromInt64(100),
), interpreter.NewArrayValue(
inter,
interpreter.EmptyLocationRange,
interpreter.VariableSizedStaticType{
Type: interpreter.PrimitiveStaticTypeInt,
},
common.ZeroAddress,
interpreter.NewUnmeteredIntValueFromInt64(100),
interpreter.NewUnmeteredIntValueFromInt64(467),
interpreter.NewUnmeteredIntValueFromInt64(297),
interpreter.NewUnmeteredIntValueFromInt64(23),
))
setFixedSize := func(size int64) {
if fixed {
constSized, ok := arrayType.(*interpreter.ConstantSizedStaticType)
assert.True(t, ok)

runValidCase(t, "reversesa", "originalsa",
interpreter.NewArrayValue(
inter,
interpreter.EmptyLocationRange,
interpreter.VariableSizedStaticType{
Type: interpreter.PrimitiveStaticTypeInt,
},
common.ZeroAddress,
interpreter.NewUnmeteredIntValueFromInt64(3),
interpreter.NewUnmeteredIntValueFromInt64(2),
interpreter.NewUnmeteredIntValueFromInt64(1),
), interpreter.NewArrayValue(
inter,
interpreter.EmptyLocationRange,
interpreter.VariableSizedStaticType{
Type: interpreter.PrimitiveStaticTypeInt,
},
common.ZeroAddress,
interpreter.NewUnmeteredIntValueFromInt64(1),
interpreter.NewUnmeteredIntValueFromInt64(2),
interpreter.NewUnmeteredIntValueFromInt64(3),
))
constSized.Size = size
}
}

setFixedSize(0)
runValidCase(t, "reverseempty"+suffix, "originalempty"+suffix,
interpreter.NewArrayValue(
inter,
interpreter.EmptyLocationRange,
arrayType,
common.ZeroAddress,
), interpreter.NewArrayValue(
inter,
interpreter.EmptyLocationRange,
arrayType,
common.ZeroAddress,
))

setFixedSize(5)
runValidCase(t, "reversexs"+suffix, "originalxs"+suffix,
interpreter.NewArrayValue(
inter,
interpreter.EmptyLocationRange,
arrayType,
common.ZeroAddress,
interpreter.NewUnmeteredIntValueFromInt64(200),
interpreter.NewUnmeteredIntValueFromInt64(100),
interpreter.NewUnmeteredIntValueFromInt64(3),
interpreter.NewUnmeteredIntValueFromInt64(2),
interpreter.NewUnmeteredIntValueFromInt64(1),
), interpreter.NewArrayValue(
inter,
interpreter.EmptyLocationRange,
arrayType,
common.ZeroAddress,
interpreter.NewUnmeteredIntValueFromInt64(1),
interpreter.NewUnmeteredIntValueFromInt64(2),
interpreter.NewUnmeteredIntValueFromInt64(3),
interpreter.NewUnmeteredIntValueFromInt64(100),
interpreter.NewUnmeteredIntValueFromInt64(200),
))

setFixedSize(4)
runValidCase(t, "reverseys"+suffix, "originalys"+suffix,
interpreter.NewArrayValue(
inter,
interpreter.EmptyLocationRange,
arrayType,
common.ZeroAddress,
interpreter.NewUnmeteredIntValueFromInt64(23),
interpreter.NewUnmeteredIntValueFromInt64(297),
interpreter.NewUnmeteredIntValueFromInt64(467),
interpreter.NewUnmeteredIntValueFromInt64(100),
), interpreter.NewArrayValue(
inter,
interpreter.EmptyLocationRange,
arrayType,
common.ZeroAddress,
interpreter.NewUnmeteredIntValueFromInt64(100),
interpreter.NewUnmeteredIntValueFromInt64(467),
interpreter.NewUnmeteredIntValueFromInt64(297),
interpreter.NewUnmeteredIntValueFromInt64(23),
))

runValidCase(t, "reversesa"+suffix, "originalsa"+suffix,
interpreter.NewArrayValue(
inter,
interpreter.EmptyLocationRange,
&interpreter.VariableSizedStaticType{
Type: interpreter.PrimitiveStaticTypeInt,
},
common.ZeroAddress,
interpreter.NewUnmeteredIntValueFromInt64(3),
interpreter.NewUnmeteredIntValueFromInt64(2),
interpreter.NewUnmeteredIntValueFromInt64(1),
), interpreter.NewArrayValue(
inter,
interpreter.EmptyLocationRange,
&interpreter.VariableSizedStaticType{
Type: interpreter.PrimitiveStaticTypeInt,
},
common.ZeroAddress,
interpreter.NewUnmeteredIntValueFromInt64(1),
interpreter.NewUnmeteredIntValueFromInt64(2),
interpreter.NewUnmeteredIntValueFromInt64(3),
))
}
}

func TestInterpretOptionalReference(t *testing.T) {
Expand Down

0 comments on commit 344ce6a

Please sign in to comment.