github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/util/unique/unique_test.go (about)

     1  // Copyright 2020 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package unique
    12  
    13  import (
    14  	"fmt"
    15  	"reflect"
    16  	"strconv"
    17  	"testing"
    18  )
    19  
    20  func TestUniquifyByteSlices(t *testing.T) {
    21  	tests := []struct {
    22  		input    []string
    23  		expected []string
    24  	}{
    25  		{
    26  			input:    []string{"foo", "foo"},
    27  			expected: []string{"foo"},
    28  		},
    29  		{
    30  			input:    []string{},
    31  			expected: []string{},
    32  		},
    33  		{
    34  			input:    []string{"", ""},
    35  			expected: []string{""},
    36  		},
    37  		{
    38  			input:    []string{"foo"},
    39  			expected: []string{"foo"},
    40  		},
    41  		{
    42  			input:    []string{"foo", "bar", "foo"},
    43  			expected: []string{"bar", "foo"},
    44  		},
    45  		{
    46  			input:    []string{"foo", "bar"},
    47  			expected: []string{"bar", "foo"},
    48  		},
    49  		{
    50  			input:    []string{"bar", "bar", "foo"},
    51  			expected: []string{"bar", "foo"},
    52  		},
    53  	}
    54  	for i, tt := range tests {
    55  		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
    56  			input := make([][]byte, len(tt.input))
    57  			expected := make([][]byte, len(tt.expected))
    58  			for i := range tt.input {
    59  				input[i] = []byte(tt.input[i])
    60  			}
    61  			for i := range tt.expected {
    62  				expected[i] = []byte(tt.expected[i])
    63  			}
    64  			if got := UniquifyByteSlices(input); !reflect.DeepEqual(got, expected) {
    65  				t.Errorf("UniquifyByteSlices() = %v, expected %v", got, expected)
    66  			}
    67  		})
    68  	}
    69  }
    70  
    71  type uasTestCase = struct {
    72  	left          []int
    73  	right         []int
    74  	expectedLeft  []int
    75  	expectedRight []int
    76  }
    77  
    78  func TestUniquifyAcrossSlices(t *testing.T) {
    79  	tests := []uasTestCase{
    80  		{
    81  			left:          []int{0, 5, 7, 10},
    82  			right:         []int{1, 5, 7, 11},
    83  			expectedLeft:  []int{0, 10},
    84  			expectedRight: []int{1, 11},
    85  		},
    86  		{
    87  			left:          []int{0, 5, 7, 10},
    88  			right:         []int{},
    89  			expectedLeft:  []int{0, 5, 7, 10},
    90  			expectedRight: []int{},
    91  		},
    92  		{
    93  			left:          []int{},
    94  			right:         []int{},
    95  			expectedLeft:  []int{},
    96  			expectedRight: []int{},
    97  		},
    98  		{
    99  			left:          []int{3, 5, 7},
   100  			right:         []int{7},
   101  			expectedLeft:  []int{3, 5},
   102  			expectedRight: []int{},
   103  		},
   104  		{
   105  			left:          []int{3, 5, 7},
   106  			right:         []int{8},
   107  			expectedLeft:  []int{3, 5, 7},
   108  			expectedRight: []int{8},
   109  		},
   110  		{
   111  			left:          []int{1, 2, 3},
   112  			right:         []int{1, 2, 3},
   113  			expectedLeft:  []int{},
   114  			expectedRight: []int{},
   115  		},
   116  	}
   117  
   118  	origTests := tests
   119  	for _, test := range origTests {
   120  		// For each test case, add a flipped test case.
   121  		rightCopy := make([]int, len(test.right))
   122  		leftCopy := make([]int, len(test.left))
   123  		for i := range rightCopy {
   124  			rightCopy[i] = test.right[i]
   125  		}
   126  		for i := range leftCopy {
   127  			leftCopy[i] = test.left[i]
   128  		}
   129  		tests = append(tests, uasTestCase{
   130  			left:          rightCopy,
   131  			right:         leftCopy,
   132  			expectedLeft:  test.expectedRight,
   133  			expectedRight: test.expectedLeft,
   134  		})
   135  	}
   136  	for i, tt := range tests {
   137  		t.Run(strconv.Itoa(i), func(t *testing.T) {
   138  			leftLen, rightLen := UniquifyAcrossSlices(tt.left, tt.right,
   139  				func(l, r int) int {
   140  					if tt.left[l] < tt.right[r] {
   141  						return -1
   142  					} else if tt.left[l] == tt.right[r] {
   143  						return 0
   144  					}
   145  					return 1
   146  				},
   147  				func(i, j int) {
   148  					tt.left[i] = tt.left[j]
   149  				},
   150  				func(i, j int) {
   151  					tt.right[i] = tt.right[j]
   152  				},
   153  			)
   154  			left := tt.left[:leftLen]
   155  			right := tt.right[:rightLen]
   156  			if !reflect.DeepEqual(left, tt.expectedLeft) {
   157  				t.Errorf("expected %v, got %v", tt.expectedLeft, left)
   158  			}
   159  			if !reflect.DeepEqual(right, tt.expectedRight) {
   160  				t.Errorf("expected %v, got %v", tt.expectedRight, right)
   161  			}
   162  		})
   163  	}
   164  }