Skip to content

Commit

Permalink
#234 Added code to implement API .
Browse files Browse the repository at this point in the history
  • Loading branch information
ericzhang6222 committed May 12, 2020
1 parent 2955085 commit 854cb2e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
28 changes: 28 additions & 0 deletions syntax/std_seq_helper.go
Original file line number Diff line number Diff line change
@@ -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)
}
26 changes: 16 additions & 10 deletions syntax/std_seq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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'])`)
}

///////////////////
Expand Down

0 comments on commit 854cb2e

Please sign in to comment.