Skip to content
Snippets Groups Projects
uidlist_test.go 5.27 KiB
Newer Older
  • Learn to ignore specific revisions
  • Jay Chiu's avatar
    Jay Chiu committed
    /*
     * Copyright 2016 Dgraph Labs, Inc.
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     * 		http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    
    package algo
    
    import (
    	"testing"
    
    
    Jay Chiu's avatar
    Jay Chiu committed
    	"github.com/stretchr/testify/require"
    
    Jay Chiu's avatar
    Jay Chiu committed
    
    	"github.com/dgraph-io/dgraph/task"
    )
    
    
    func newList(data []uint64) *task.List {
    	return &task.List{Uids: data}
    
    Jay Chiu's avatar
    Jay Chiu committed
    }
    
    func TestMergeSorted1(t *testing.T) {
    
    	input := []*task.List{
    		newList([]uint64{55}),
    
    Jay Chiu's avatar
    Jay Chiu committed
    	}
    
    	require.Equal(t, MergeSorted(input).Uids, []uint64{55})
    
    Jay Chiu's avatar
    Jay Chiu committed
    }
    
    func TestMergeSorted2(t *testing.T) {
    
    	input := []*task.List{
    		newList([]uint64{1, 3, 6, 8, 10}),
    		newList([]uint64{2, 4, 5, 7, 15}),
    
    Jay Chiu's avatar
    Jay Chiu committed
    	}
    
    	require.Equal(t, MergeSorted(input).Uids,
    
    Jay Chiu's avatar
    Jay Chiu committed
    		[]uint64{1, 2, 3, 4, 5, 6, 7, 8, 10, 15})
    
    Jay Chiu's avatar
    Jay Chiu committed
    }
    
    func TestMergeSorted3(t *testing.T) {
    
    	input := []*task.List{
    		newList([]uint64{1, 3, 6, 8, 10}),
    		newList([]uint64{}),
    
    Jay Chiu's avatar
    Jay Chiu committed
    	}
    
    	require.Equal(t, MergeSorted(input).Uids, []uint64{1, 3, 6, 8, 10})
    
    Jay Chiu's avatar
    Jay Chiu committed
    }
    
    func TestMergeSorted4(t *testing.T) {
    
    	input := []*task.List{
    		newList([]uint64{}),
    		newList([]uint64{1, 3, 6, 8, 10}),
    
    Jay Chiu's avatar
    Jay Chiu committed
    	}
    
    	require.Equal(t, MergeSorted(input).Uids, []uint64{1, 3, 6, 8, 10})
    
    Jay Chiu's avatar
    Jay Chiu committed
    }
    
    func TestMergeSorted5(t *testing.T) {
    
    	input := []*task.List{
    		newList([]uint64{}),
    		newList([]uint64{}),
    
    Jay Chiu's avatar
    Jay Chiu committed
    	}
    
    	require.Empty(t, MergeSorted(input).Uids)
    
    Jay Chiu's avatar
    Jay Chiu committed
    }
    
    func TestMergeSorted6(t *testing.T) {
    
    	input := []*task.List{
    		newList([]uint64{11, 13, 16, 18, 20}),
    		newList([]uint64{12, 14, 15, 15, 16, 16, 17, 25}),
    		newList([]uint64{1, 2}),
    
    Jay Chiu's avatar
    Jay Chiu committed
    	}
    
    	require.Equal(t, MergeSorted(input).Uids,
    
    Jay Chiu's avatar
    Jay Chiu committed
    		[]uint64{1, 2, 11, 12, 13, 14, 15, 16, 17, 18, 20, 25})
    
    Jay Chiu's avatar
    Jay Chiu committed
    }
    
    func TestMergeSorted7(t *testing.T) {
    
    	input := []*task.List{
    		newList([]uint64{5, 6, 7}),
    		newList([]uint64{3, 4}),
    		newList([]uint64{1, 2}),
    		newList([]uint64{}),
    
    Jay Chiu's avatar
    Jay Chiu committed
    	}
    
    	require.Equal(t, MergeSorted(input).Uids, []uint64{1, 2, 3, 4, 5, 6, 7})
    
    Jay Chiu's avatar
    Jay Chiu committed
    }
    
    func TestMergeSorted8(t *testing.T) {
    
    	input := []*task.List{}
    
    	require.Empty(t, MergeSorted(input).Uids)
    
    Jay Chiu's avatar
    Jay Chiu committed
    }
    
    func TestMergeSorted9(t *testing.T) {
    
    	input := []*task.List{
    		newList([]uint64{1, 1, 1}),
    
    Jay Chiu's avatar
    Jay Chiu committed
    	}
    
    	require.Equal(t, MergeSorted(input).Uids, []uint64{1})
    
    Jay Chiu's avatar
    Jay Chiu committed
    }
    
    func TestMergeSorted10(t *testing.T) {
    
    	input := []*task.List{
    		newList([]uint64{1, 2, 3, 3, 6}),
    		newList([]uint64{4, 8, 9}),
    
    Jay Chiu's avatar
    Jay Chiu committed
    	}
    
    	require.Equal(t, MergeSorted(input).Uids, []uint64{1, 2, 3, 4, 6, 8, 9})
    
    Jay Chiu's avatar
    Jay Chiu committed
    }
    
    func TestIntersectSorted1(t *testing.T) {
    
    	input := []*task.List{
    		newList([]uint64{1, 2, 3}),
    		newList([]uint64{2, 3, 4, 5}),
    
    Jay Chiu's avatar
    Jay Chiu committed
    	}
    
    	require.Equal(t, IntersectSorted(input).Uids, []uint64{2, 3})
    
    Jay Chiu's avatar
    Jay Chiu committed
    }
    
    func TestIntersectSorted2(t *testing.T) {
    
    	input := []*task.List{
    		newList([]uint64{1, 2, 3}),
    
    Jay Chiu's avatar
    Jay Chiu committed
    	}
    
    	require.Equal(t, IntersectSorted(input).Uids, []uint64{1, 2, 3})
    
    Jay Chiu's avatar
    Jay Chiu committed
    }
    
    func TestIntersectSorted3(t *testing.T) {
    
    	input := []*task.List{}
    
    	require.Empty(t, IntersectSorted(input).Uids)
    
    Jay Chiu's avatar
    Jay Chiu committed
    }
    
    func TestIntersectSorted4(t *testing.T) {
    
    	input := []*task.List{
    		newList([]uint64{100, 101}),
    
    Jay Chiu's avatar
    Jay Chiu committed
    	}
    
    	require.Equal(t, IntersectSorted(input).Uids, []uint64{100, 101})
    
    Jay Chiu's avatar
    Jay Chiu committed
    }
    
    func TestIntersectSorted5(t *testing.T) {
    
    	input := []*task.List{
    		newList([]uint64{1, 2, 3}),
    		newList([]uint64{2, 3, 4, 5}),
    		newList([]uint64{4, 5, 6}),
    
    Jay Chiu's avatar
    Jay Chiu committed
    	}
    
    	require.Empty(t, IntersectSorted(input).Uids)
    
    func TestIntersectSorted6(t *testing.T) {
    	input := []*task.List{
    		newList([]uint64{10, 12, 13}),
    		newList([]uint64{2, 3, 4, 13}),
    		newList([]uint64{4, 5, 6}),
    	}
    	require.Empty(t, IntersectSorted(input).Uids)
    }
    
    
    Jay Chiu's avatar
    Jay Chiu committed
    func TestUIDListIntersect1(t *testing.T) {
    
    	u := newList([]uint64{1, 2, 3})
    	v := newList([]uint64{})
    
    	require.Empty(t, u.Uids)
    
    Jay Chiu's avatar
    Jay Chiu committed
    }
    
    func TestUIDListIntersect2(t *testing.T) {
    
    	u := newList([]uint64{1, 2, 3})
    	v := newList([]uint64{1, 2, 3, 4, 5})
    
    	require.Equal(t, u.Uids, []uint64{1, 2, 3})
    
    Jay Chiu's avatar
    Jay Chiu committed
    }
    
    func TestUIDListIntersect3(t *testing.T) {
    
    	u := newList([]uint64{1, 2, 3})
    	v := newList([]uint64{2})
    
    	require.Equal(t, u.Uids, []uint64{2})
    
    Jay Chiu's avatar
    Jay Chiu committed
    }
    
    func TestUIDListIntersect4(t *testing.T) {
    
    	u := newList([]uint64{1, 2, 3})
    	v := newList([]uint64{0, 5})
    
    	require.Empty(t, u.Uids)
    
    Jay Chiu's avatar
    Jay Chiu committed
    }
    
    func TestUIDListIntersect5(t *testing.T) {
    
    	u := newList([]uint64{1, 2, 3})
    	v := newList([]uint64{3, 5})
    
    	require.Equal(t, u.Uids, []uint64{3})
    
    func TestUIDListIntersectDupFirst(t *testing.T) {
    	u := newList([]uint64{1, 1, 2, 3})
    	v := newList([]uint64{1, 2})
    	IntersectWith(u, v)
    	require.Equal(t, []uint64{1, 2}, u.Uids)
    }
    
    func TestUIDListIntersectDupBoth(t *testing.T) {
    	u := newList([]uint64{1, 1, 2, 3, 5})
    	v := newList([]uint64{1, 1, 2, 4})
    	IntersectWith(u, v)
    	require.Equal(t, []uint64{1, 1, 2}, u.Uids)
    }
    
    func TestUIDListIntersectDupSecond(t *testing.T) {
    	u := newList([]uint64{1, 2, 3, 5})
    	v := newList([]uint64{1, 1, 2, 4})
    	IntersectWith(u, v)
    	require.Equal(t, []uint64{1, 2}, u.Uids)
    }
    
    
    Jay Chiu's avatar
    Jay Chiu committed
    func TestApplyFilterUint(t *testing.T) {
    
    	u := newList([]uint64{1, 2, 3, 4, 5})
    	ApplyFilter(u, func(a uint64, idx int) bool { return (a % 2) == 1 })
    	require.Equal(t, u.Uids, []uint64{1, 3, 5})
    
    Jay Chiu's avatar
    Jay Chiu committed
    }