diff --git a/syntax/std_seq_helper.go b/syntax/std_seq_helper.go new file mode 100644 index 00000000..dd98b21e --- /dev/null +++ b/syntax/std_seq_helper.go @@ -0,0 +1,28 @@ +package syntax + +import ( + "github.com/arr-ai/arrai/rel" +) + +// ContainsArray check if array a contains array b. +func ContainsArray(a rel.Array, b rel.Array) rel.Value { + // Get index of b[0] in a + bOffset := 0 + bVals := b.Values() + arrayEnum, _ := a.ArrayEnumerator() + for arrayEnum.MoveNext() { + if bOffset < len(bVals) && arrayEnum.Current().Equal(bVals[bOffset]) { + bOffset++ + } else { + if bOffset > 0 && bOffset < len(bVals) { + return rel.NewBool(false) + } + } + } + + if bOffset == len(bVals) { + return rel.NewBool(true) + } + + return rel.NewBool(false) +} diff --git a/syntax/std_seq_test.go b/syntax/std_seq_test.go index ce4ea482..0d0a26e9 100644 --- a/syntax/std_seq_test.go +++ b/syntax/std_seq_test.go @@ -40,20 +40,26 @@ func TestSeqRepeat(t *testing.T) { func TestContains(t *testing.T) { t.Parallel() // string - // AssertCodesEvalToSameValue(t, `true `, `//seq.contains("this is a test", "") `) - // AssertCodesEvalToSameValue(t, `true `, `//seq.contains("this is a test", "is a test") `) - // AssertCodesEvalToSameValue(t, `false`, `//seq.contains("this is a test", "is not a test")`) - // assertExprPanics(t, `//seq.contains(123, 124)`) + AssertCodesEvalToSameValue(t, `true `, `//seq.contains("this is a test", "") `) + AssertCodesEvalToSameValue(t, `true `, `//seq.contains("this is a test", "is a test") `) + AssertCodesEvalToSameValue(t, `false`, `//seq.contains("this is a test", "is not a test")`) + assertExprPanics(t, `//seq.contains(123, 124)`) // array - // AssertCodesEvalToSameValue(t, `true`, `//seq.contains([1,2,3,4,5],1)`) - // AssertCodesEvalToSameValue(t, `true`, `//seq.contains([1,2,3,4,5],3)`) - // AssertCodesEvalToSameValue(t, `true`, `//seq.contains([1,2,3,4,5],5)`) + AssertCodesEvalToSameValue(t, `true`, `//seq.contains([1,2,3,4,5],1)`) + AssertCodesEvalToSameValue(t, `true`, `//seq.contains([1,2,3,4,5],3)`) + AssertCodesEvalToSameValue(t, `true`, `//seq.contains([1,2,3,4,5],5)`) - // AssertCodesEvalToSameValue(t, `true`, `//seq.contains(['A','B','C','D','E'],'A')`) - // AssertCodesEvalToSameValue(t, `true`, `//seq.contains(['A','B','C','D','E'],'E')`) - // AssertCodesEvalToSameValue(t, `true`, `//seq.contains(['A','B','C','D','E'],'C')`) + AssertCodesEvalToSameValue(t, `true`, `//seq.contains(['A','B','C','D','E'],'A')`) + AssertCodesEvalToSameValue(t, `true`, `//seq.contains(['A','B','C','D','E'],'E')`) + AssertCodesEvalToSameValue(t, `true`, `//seq.contains(['A','B','C','D','E'],'C')`) + AssertCodesEvalToSameValue(t, `true`, `//seq.contains(['A','B','C','D','E'],['A','B','C'])`) AssertCodesEvalToSameValue(t, `true`, `//seq.contains(['A','B','C','D','E'],['B','C'])`) + AssertCodesEvalToSameValue(t, `true`, `//seq.contains(['A','B','C','D','E'],['C','D','E'])`) + AssertCodesEvalToSameValue(t, `true`, `//seq.contains(['A','B','C','D','E'],['A','B','C','D','E'])`) + AssertCodesEvalToSameValue(t, `false`, `//seq.contains(['A','B','C','D','E'],['B','C','E'])`) + AssertCodesEvalToSameValue(t, `false`, `//seq.contains(['A','B','C','D','E'],['A','B','C','E'])`) + AssertCodesEvalToSameValue(t, `false`, `//seq.contains(['A','B','C','D','E'],['A','B','C','D','E','F'])`) } ///////////////////