github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/arrays/intersection_test.go (about)

     1  package arrays_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	. "github.com/smartystreets/goconvey/convey"
     8  
     9  	"github.com/MontFerret/ferret/pkg/runtime/core"
    10  	"github.com/MontFerret/ferret/pkg/runtime/values"
    11  	"github.com/MontFerret/ferret/pkg/stdlib/arrays"
    12  )
    13  
    14  func TestIntersection(t *testing.T) {
    15  	Convey("Should find intersections between 2 arrays", t, func() {
    16  		arr1 := values.NewArrayWith(
    17  			values.NewInt(1),
    18  			values.NewInt(2),
    19  			values.NewInt(3),
    20  			values.NewInt(4),
    21  			values.NewInt(5),
    22  			values.NewInt(6),
    23  		)
    24  
    25  		arr2 := values.NewArrayWith(
    26  			values.NewInt(4),
    27  			values.NewInt(5),
    28  			values.NewInt(6),
    29  			values.NewInt(7),
    30  			values.NewInt(8),
    31  			values.NewInt(9),
    32  		)
    33  
    34  		out, err := arrays.Intersection(context.Background(), arr1, arr2)
    35  
    36  		check := map[int]bool{
    37  			4: true,
    38  			5: true,
    39  			6: true,
    40  		}
    41  
    42  		So(err, ShouldBeNil)
    43  
    44  		arr := out.(*values.Array)
    45  
    46  		So(arr.Length(), ShouldEqual, 3)
    47  
    48  		arr.ForEach(func(value core.Value, idx int) bool {
    49  			_, exists := check[int(value.(values.Int))]
    50  
    51  			So(exists, ShouldBeTrue)
    52  
    53  			return true
    54  		})
    55  	})
    56  
    57  	Convey("Should find intersections between more than 2 arrays", t, func() {
    58  		arr1 := values.NewArrayWith(
    59  			values.NewInt(1),
    60  			values.NewInt(2),
    61  			values.NewInt(3),
    62  			values.NewInt(4),
    63  			values.NewInt(5),
    64  		)
    65  
    66  		arr2 := values.NewArrayWith(
    67  			values.NewInt(2),
    68  			values.NewInt(3),
    69  			values.NewInt(4),
    70  			values.NewInt(5),
    71  			values.NewInt(6),
    72  		)
    73  
    74  		arr3 := values.NewArrayWith(
    75  			values.NewInt(3),
    76  			values.NewInt(4),
    77  			values.NewInt(5),
    78  			values.NewInt(6),
    79  			values.NewInt(7),
    80  		)
    81  
    82  		out, err := arrays.Intersection(context.Background(), arr1, arr2, arr3)
    83  
    84  		check := map[int]bool{
    85  			3: true,
    86  			4: true,
    87  			5: true,
    88  		}
    89  
    90  		So(err, ShouldBeNil)
    91  
    92  		arr := out.(*values.Array)
    93  
    94  		So(arr.Length(), ShouldEqual, 3)
    95  
    96  		arr.ForEach(func(value core.Value, idx int) bool {
    97  			_, exists := check[int(value.(values.Int))]
    98  
    99  			So(exists, ShouldBeTrue)
   100  
   101  			return true
   102  		})
   103  	})
   104  }