github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/opt/colset_test.go (about)

     1  // Copyright 2019 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 opt
    12  
    13  import (
    14  	"testing"
    15  
    16  	"github.com/cockroachdb/cockroach/pkg/util"
    17  )
    18  
    19  func BenchmarkColSet(b *testing.B) {
    20  	// Verify that the wrapper doesn't add overhead (as was the case with earlier
    21  	// go versions which couldn't do mid-stack inlining).
    22  	const n = 50
    23  	b.Run("fastintset", func(b *testing.B) {
    24  		for i := 0; i < b.N; i++ {
    25  			var c util.FastIntSet
    26  			for j := 0; j < n; j++ {
    27  				c.Add(j)
    28  			}
    29  		}
    30  	})
    31  	b.Run("colset", func(b *testing.B) {
    32  		for i := 0; i < b.N; i++ {
    33  			var c ColSet
    34  			for j := 0; j < n; j++ {
    35  				c.Add(ColumnID(j))
    36  			}
    37  		}
    38  	})
    39  }
    40  
    41  func TestTranslateColSet(t *testing.T) {
    42  	test := func(t *testing.T, colSetIn ColSet, from ColList, to ColList, expected ColSet) {
    43  		t.Helper()
    44  
    45  		actual := TranslateColSet(colSetIn, from, to)
    46  		if !actual.Equals(expected) {
    47  			t.Fatalf("\nexpected: %s\nactual  : %s", expected, actual)
    48  		}
    49  	}
    50  
    51  	colSetIn, from, to := MakeColSet(1, 2, 3), ColList{1, 2, 3}, ColList{4, 5, 6}
    52  	test(t, colSetIn, from, to, MakeColSet(4, 5, 6))
    53  
    54  	colSetIn, from, to = MakeColSet(2, 3), ColList{1, 2, 3}, ColList{4, 5, 6}
    55  	test(t, colSetIn, from, to, MakeColSet(5, 6))
    56  
    57  	// colSetIn and colSetOut might not be the same length.
    58  	colSetIn, from, to = MakeColSet(1, 2), ColList{1, 1, 2}, ColList{4, 5, 6}
    59  	test(t, colSetIn, from, to, MakeColSet(4, 5, 6))
    60  
    61  	colSetIn, from, to = MakeColSet(1, 2, 3), ColList{1, 2, 3}, ColList{4, 5, 4}
    62  	test(t, colSetIn, from, to, MakeColSet(4, 5))
    63  
    64  	colSetIn, from, to = MakeColSet(2), ColList{1, 2, 2}, ColList{4, 5, 6}
    65  	test(t, colSetIn, from, to, MakeColSet(5, 6))
    66  }